https://github.com/fil/lap-jv
Linear Assignment Problem — A Javascript implementation of R. Jonker and A. Volgenant’s algorithm (LAPJV)
https://github.com/fil/lap-jv
assignment lap-jv
Last synced: 4 months ago
JSON representation
Linear Assignment Problem — A Javascript implementation of R. Jonker and A. Volgenant’s algorithm (LAPJV)
- Host: GitHub
- URL: https://github.com/fil/lap-jv
- Owner: Fil
- Created: 2017-01-03T17:00:04.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T07:01:01.000Z (over 1 year ago)
- Last Synced: 2025-03-25T03:13:13.443Z (11 months ago)
- Topics: assignment, lap-jv
- Language: JavaScript
- Homepage:
- Size: 65.4 KB
- Stars: 32
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## LAP-JV
### Linear Assignment Problem — algorithm by R. Jonker and A. Volgenant
“A shortest augmenting path algorithm for dense and sparse linear assignment problems,” by R. Jonker and A. Volgenant, _Computing_ (1987) 38: 325. doi:10.1007/BF02278710
Ported to javascript by Philippe Rivière, from the C++ implementation found at https://github.com/yongyanghz/LAPJV-algorithm-c
Added an epsilon to avoid infinite loops caused by rounding errors.
## Usage
In the [Linear Assignment Problem](https://en.wikipedia.org/wiki/Assignment_problem), you have _n_ agents and _n_ tasks, and need to assign one task to each agent, at minimal cost.
First, compute the cost matrix: how expensive it is to assign agent _i_ (rows) to task _j_ (columns).
The LAP-JV algorithm will give an optimal solution:
```javascript
n = 3, costs = [[1,2,3], [4,2,1], [2,2,2]];
// ^ _ _ _ _ ^ _ ^ _
solution = lap(n, costs);
console.log(solution.col);
// [0, 2, 1]
console.log(solution.cost);
// 4
```
Here agent 0 is assigned to task 0, agent 1 to task 2, agent 2 to task 1, resulting in a total cost of `1 + 1 + 2 = 4`.
**Cost callback**
For performance and usability reasons, the `lap` function now accepts a cost callback `cost(i,j)` instead of a cost matrix:
```javascript
var pos = new Float32Array(1000).map(d => Math.random() * 1000);
lap(pos.length, (i,j) => (pos[i] - j) * (pos[i] - j));
```
##
The algorithm runs in `O(n^2)`. You can run it [directly](http://bl.ocks.org/Fil/6ead5eea43ec506d5550f095edc45e3f) or as a javascript worker, as in the following example:
[](https://observablehq.observablehq.cloud/pangea/varia/lap-jv)
In the example above, we assign _n_ points to a grid of _n_ positions. `costs[i][j]` is the square distance between point _i_'s original coordinates and position _j_'s coordinates. The algorithm minimizes the total cost, i.e. the sum of square displacements.
##
Comments and patches at [Fil/lap-jv](https://github.com/Fil/lap-jv).