easystarjs icon indicating copy to clipboard operation
easystarjs copied to clipboard

Parallel Paths

Open misterhat opened this issue 5 years ago • 1 comments

add .setParallelLimit(limit)

when many varying path lengths are used, distributes the path finding per iterationsPerCalculation on up to limit paths. when calculate() is called, switch to a solving a different path in the queue instead of solving them sequentially.

if the parallel limit is 1, easystar acts as it currently does by resolving each path in the order they were submitted. if it's set to 2, it will switch between two path finding instances every .calculate() call. if -1, switch the path finding instance every time .calculate() is called.

it still results in the same total amount of time to calculate the paths, but the load distribution could be useful.

e.g.


const EasyStar = require('easystarjs');               
                                                      
const easystar = new EasyStar.js();                   
const grid = require('./obstacles.json') ;            
grid[1162][938] = 0;                                  
easystar.setGrid(grid);                               
easystar.setAcceptableTiles([0]);
easystar.setIterationsPerCalculation(500);            
easystar.setParallelLimit(-1); // 1, 2, etc.

for (let i = 0; i < 5; i += 1) {                      
    console.time(`long-path-${i}`);                   
    easystar.findPath(1380, 1318, 368, 982, () => {   
        console.timeEnd(`long-path-${i}`);            
    });                                               
}                                                     

for (let i = 0; i < 50; i += 1) {                     
    console.time(`short-path-${i}`);
    easystar.findPath(1380, 1318, 1360, 1324, () => {
        console.timeEnd(`short-path-${i}`);
    });
}

const calculate = () => {                             
    easystar.calculate();                             
    setTimeout(calculate, 5);                         
};                                                    
calculate();

with setParallelLimit unset or set to 1, it resolves the long-paths first (at ~400ms each), then the short-paths after ~2 seconds. with setParallelLimit set to -1, it resolves the short-paths from 60-120ms and the long paths after ~2 seconds.

this is useful if you have small paths mixed with large paths, as the small paths can return much quicker even if they were added during the solving of a much larger and slower path.

misterhat avatar Jan 04 '20 18:01 misterhat

Ok I think this is what you asked for? I restored the original bin/* files from the master branch, changed the API + typescript bindings and added unit tests for serial and parallel pathfinding.

misterhat avatar Jan 06 '20 15:01 misterhat