Loading solver…
Problem: split visits across several vehicles so every location is served and total travel distance is minimized.
import {
FirstSolutionStrategy,
RoutingIndexManager,
RoutingModel,
} from 'or-tools-wasm/routing';
const locations = [
{ x: 480, y: 310 },
...Array.from({ length: 60 }, () => ({
x: 46 + Math.random() * 868,
y: 70 + Math.random() * 496,
})),
];
const manager = new RoutingIndexManager(locations.length, 4, 0);
const routing = new RoutingModel(manager);
const transit = routing.registerTransitCallback((from, to) => {
const a = locations[manager.indexToNode(from)];
const b = locations[manager.indexToNode(to)];
return Math.round(Math.hypot(a.x - b.x, a.y - b.y));
});
routing.setArcCostEvaluatorOfAllVehicles(transit);
const demand = routing.registerUnaryTransitCallback((from) =>
manager.indexToNode(from) === 0 ? 0 : 1,
);
routing.addDimensionWithVehicleCapacity(
demand,
0,
Array(4).fill(Math.ceil((locations.length - 1) / 4)),
true,
'load',
);
const result = await routing.solve({
firstSolutionStrategy: FirstSolutionStrategy.PATH_CHEAPEST_ARC,
executor: 'worker',
});
console.log(result.status, result.assignment?.objectiveValue());