Code Monkey home page Code Monkey logo

pylapjv's Introduction

This is an outdated package

Consider using scipy.optimize.linear_sum_assignment instead.

This Python module is just a simple wrapper for the C++ code written by Jonker to implement the Jonker-Volgenant algorithm, LAPJV, for the linear assignment problem.

See the important notes below to properly use this algorithm. For a more tolerant, but slower, LAP algorithm see http://github.com/hrldcpr/hungarian

Note that this module depends on the numpy module. You must install numpy before you can compile this module. Numpy can be downloaded from http://numpy.scipy.org

If you have any problems with this module, you should contact me, not Dr. Jonker.

To build this module run:

> python setup.py build

Then you can either put the file build/lib-/LAPJV.so in the same directory as the code that will be using it, or you can install it so that all of your python programs can see it:

> python setup.py install

For the module's documentation, type at a Python prompt:

>>> help('LAPJV')

For a usage example, courtesy of Dr. N.D. van Foreest, see example.py

Additional Information

LAPJV comes from the paper:

R. Jonker and A. Volgenant, "A Shortest Augmenting Path Algorithm for Dense and Sparse Linear Assignment Problems," Computing, vol. 38, pp. 325-340, 1987.

According to that paper, it is notably faster than the Hungarian algorithm (a.k.a. Munkres' algorithm) and several other linear assignment algorithms.

The C++ source comes from: https://web.archive.org/web/*/http://www.magiclogic.com/assignment.html

The original C++ source zip can be obtained at: https://web.archive.org/web/*/http://www.magiclogic.com/assignment/lap_cpp.zip

This wrapper for the algorithm uses single precision (i.e. 32-bit) floating point arithmetic. To change it to double precision, integer, or anything else, simply change the corresponding types in lap.h

Nota Bene

The following matrices, and others, cause Jonker's code to loop forever. According to Dr. Volgenant, "The failure is a consequence of a tolerance not suited for the data ... You have to reduce the value of the tolerance such that it is smaller than the smallest difference between any pair of cost elements in the cost matrix."

There is no variable tolerance inherent to Jonker's code, so the solution is to scale all of the costs by some factor, which of course will not change the optimal assignment. For example, both of the matrices below no longer fail under single-precision if multiplied by 10. Under double-precision, however, it seems harder to fix.

Here are two offending matrices, for single and double precision:

Nota Melior

Tom Marthaler may have solved the preceding problem, although I've yet to try it, but it sounds good. His email says:

"If you don't care about the actual value difference between the optimal assignments, have you tried to map the values using a simple log function? Meaning, if you did a -log(x) on all of the values in the cost matrix, then the optimal assignment should be the same (I am not going to prove it though!) but the relative differences between the cost values should increase. Its what we do for our lapjv implementation, and I have yet to see the type of problems that you describe (however, our precision is only to 2-3 decimals)."

Thanks Tom!

pylapjv's People

Contributors

hrldcpr 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pylapjv's Issues

Simple example fails.

I'm running the minimalist example from here:

import numpy
import LAPJV
a = numpy.array([[1, 1, 1, 2], [3, 2, 4, 1], [4, 4, 2, 4], [2, 3, 3, 3]])
print LAPJV.lap(a)[1]

This fails with this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative dimensions are not allowed

I have converted the C code to Scala, and there it runs, but it produces a sub-optimal cost of 10.0 instead of the expected 6.0. Is this "normal", or should J-V also give optimal cost?

Port to Cython

This is less of an issue and more of an idea. I ported the C code to typed Cython code using numpy, and have gotten even better performance from it than I do from the C code.

Returning very large arrays

I'm seeing very large arrays being returned, with all the data after the initial "correct" data looking like old memory:

import numpy as np
import LAPJV
n = 8
cost = np.random.uniform(low=0, high=100000, size=(n, n))
%time min_cost,row_assigns,col_assigns,row_dual_vars,col_dual_vars = LAPJV.lap(cost)
print row_assigns.shape
print row_assigns[:(2*n)]

Output:

CPU times: user 12 µs, sys: 7 µs, total: 19 µs
Wall time: 16 µs
(988661682962169864,)
[          3           4           6           0           2           5
           1           7 -1556947360 -1487184053   227174464           1
   227235536           1  2045669697  1795533007]

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.