Giter Club home page Giter Club logo

lap-jv's Introduction

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, 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:

  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:

   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 or as a javascript worker, as in the following example:

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.

lap-jv's People

Contributors

fil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lap-jv's Issues

Does not actually calculate the best assignment.

I was using this as a reference as I was working on a Rust implementation accessible in JS via FFI. However when comparing values it appears this implementation doesn't actually follow the paper and computes a sub-optimal assignment.

I randomly generated a 5000x5000 cost matrix, exported it to JSON, and then timed each implementation against solving the same matrix.

Pure Rust:
Calculated in 3.64231825s
Total Cost: 11,216.864157676053

Rust via super inefficient NodeJS FFI :
Calculated in 10.239015541911126s
Total Cost: 11,216.864157676053

This Script:
Calculated in 612.758542060852ms
Total cost: 30,898.483242653267

The cost is almost 3x as high and the runtime is almost 3 seconds faster than a native compiled language. I haven't dug super deep into the code, but it appears that this isn't a correct implementation of the algorithm.

License

Any chance you'd be willing to provide a license for this code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.