Loading solver…
Problem: assign every worker to one task so the total assignment cost is as small as possible; this is the classic Hungarian algorithm problem.
import {
SimpleLinearSumAssignment,
SimpleLinearSumAssignmentStatus,
} from 'or-tools-wasm/network-flow';
const costs = [
[90, 76, 75, 70],
[35, 85, 55, 65],
[125, 95, 90, 105],
[45, 110, 95, 115],
];
const leftNodes: number[] = [];
const rightNodes: number[] = [];
const arcCosts: number[] = [];
for (let worker = 0; worker < costs.length; ++worker) {
for (let task = 0; task < costs[worker].length; ++task) {
leftNodes.push(worker);
rightNodes.push(task);
arcCosts.push(costs[worker][task]);
}
}
const assignment = new SimpleLinearSumAssignment();
assignment.addArcsWithCost(leftNodes, rightNodes, arcCosts);
const result = await assignment.solve({ executor: 'worker' });
if (result.status === SimpleLinearSumAssignmentStatus.OPTIMAL) {
console.log(result.optimalCost);
for (let worker = 0; worker < assignment.numNodes(); ++worker) {
console.log(worker, result.rightMate(worker), result.assignmentCost(worker));
}
}