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

"print_matrix" always print integral value of cost matrix #34

Closed
etanot opened this issue Jul 16, 2020 · 3 comments · Fixed by #35
Closed

"print_matrix" always print integral value of cost matrix #34

etanot opened this issue Jul 16, 2020 · 3 comments · Fixed by #35

Comments

@etanot
Copy link
Contributor

etanot commented Jul 16, 2020

When I enter cost values in floating point form inside cost matrix, print_matrix function treats those values as integer and print only integral part of the values. I run this code:

from munkres import Munkres, print_matrix

matrix = [[5.1, 9.6, 1.7],
          [10.2, 3.5, 2.8],
          [8.3, 7.4, 4.9]]
m = Munkres()
indexes = m.compute(matrix)
print_matrix(matrix, msg='Lowest cost through this matrix:')
total = 0
for row, column in indexes:
    value = matrix[row][column]
    total += value
    print(f'({row}, {column}) -> {value}')
print(f'total cost: {total}')

Output is as follows:

Lowest cost through this matrix:
[   5,    9,    1]
[  10,    3,    2]
[   8,    7,    4]
(0, 0) -> 5.1
(1, 1) -> 3.5
(2, 2) -> 4.9
total cost: 13.5

See matrix printed by print_matrix function isn't same matrix as given in code that's it only has integral part with them, although total cost calculation is correct. Is it a expected behavior of print_matrix function? Because it's not always the case, cost matrix would have all value in integral form.

@etanot
Copy link
Contributor Author

etanot commented Jul 16, 2020

I think this can be fixed by replacing 'd' with 's' in line L#517, or even when can remove whole if/else loop and replace it with single statement formatted = ((format + 's') % val). If you're agree, I can make a pull request for this.

Edit:
Using latter solution is a bad idea because printed matrix looks ugly, value of DISALLOWED object is printed in object form (<munkres.DISALLOWED_OBJ object at 0x7f424cbdab00>), see below:

Instance

matrix = [[5.2, 9.3, DISALLOWED],
          [10.1, DISALLOWED, 2.1],
          [8.4, 7.5, 4.6]]

Result

Highest profit through this matrix:
[ 5.2,  9.3, <munkres.DISALLOWED_OBJ object at 0x7f424cbdab00>]
[10.1, <munkres.DISALLOWED_OBJ object at 0x7f424cbdab00>,  2.1]
[ 8.4,  7.5,  4.6]
(0, 0) -> 5.2
(1, 2) -> 2.1
(2, 1) -> 7.5
total profit=14.8

So sticking to former solution, i.e. replacing d with s.

@etanot
Copy link
Contributor Author

etanot commented Aug 11, 2020

@bmc, sorry for directly pinging you. Can you comment something on this issue? I would like to fix this issue.

@bmc
Copy link
Owner

bmc commented Aug 11, 2020

Sorry. I haven't used this package myself in years. I'm find with the fix. I'll merge any PR and upload again to PyPI.

etanot pushed a commit to etanot/munkres that referenced this issue Aug 13, 2020
When we enter float value inside cost matrix, "print_matrix" function
treats those values as integer, and only prints integral part of the
values.

For example, if cost matrix is:
matrix = [[5.1, 9.6, 1.7],
          [10.2, 3.5, 2.8],
          [8.3, 7.4, 4.9]]

Then output is as follows:
Lowest cost through this matrix:
[   5,    9,    1]
[  10,    3,    2]
[   8,    7,    4]
(0, 0) -> 5.1
(1, 1) -> 3.5
(2, 2) -> 4.9
total cost: 13.5

We can see printed cost matrix isn't same as original cost matrix. And
it's not always the case, cost matrix would have all values in integral
form, cost of the assignment could be a float value.

Resolves: bmc#34
@bmc bmc closed this as completed in #35 Sep 15, 2020
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2022
Version 1.1.4 (September, 2020)

- Switched from Nose to Pytest for testing. Patch courtesy @kmosiejczuk,
  [PR #32](bmc/munkres#32), with some additional
  cleanup by me.
- Fix to [Issue #34](bmc/munkres#34), in which
  `print_matrix` wasn't handling non-integral values. Patch courtesy @finn0,
  via [PR #35](bmc/munkres#35).
- Various changes from `http:` URLs to `https:` URLs, courtesy @finn0
  via [PR #36](bmc/munkres#36).

Version 1.1.3:

**Nonexistent**. Accidentally published before check-in. Deleted from
PyPI. Use version 1.1.4.

Version 1.1.2 (February, 2019)

- Removed `NoReturn` type annotations, to allow compatibility with Python 3.5
  releases prior to 3.5.4. Thanks to @jackwilsdon for catching that issue.

Version 1.1.1 (February, 2019)

- Version bump to get past a PyPI publishing issue. (Can't republish
  partially published 1.1.0.)

Version 1.1.0 (February, 2019)

- Only supports Python 3.5 or better, from this version forward (since Python
  2 is at end of life in 11 months).
- Added `typing` type hints.
- Updated docs to use `pdoc`, since `epydoc` is pretty much dead.
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

Successfully merging a pull request may close this issue.

2 participants