Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute: unexpected side effect on cost_matrix #12

Open
emanuele opened this issue May 27, 2015 · 1 comment
Open

compute: unexpected side effect on cost_matrix #12

emanuele opened this issue May 27, 2015 · 1 comment

Comments

@emanuele
Copy link

The current Munkres.compute(cost_matrix) alters cost_matrix, in contrast with what is written in the docstring.
Example:

import numpy as np
from munkres import Munkres
cost_matrix = np.random.rand(10, 10)
cost_matrix_copy = cost_matrix.copy()
m = Munkres()
print(cost_matrix == cost_matrix_copy)
result = m.compute(cost_matrix)
print(cost_matrix == cost_matrix_copy)
@addaleax
Copy link

The problem here is that the rows of the input matrix are cloned using new_row = row[:], which works just fine as long as the input is formatted as a plain list of lists of numbers (To be fair, this input format is also indicated by the docstring). However, for numpy arrays, this creates another numpy array which references the same underlying storage.

Changing new_row = row[:] to new_row = row.copy() in munkres.py should be fine, and I don’t really see any situation in which this might break something, since it also works fine for plain lists.
You might also want to look into #6, where someone already modified this module to use numpy internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants