gnimuc / hungarian.jl Goto Github PK
View Code? Open in Web Editor NEWThe Hungarian(Kuhn-Munkres) algorithm for Julia
License: The Unlicense
The Hungarian(Kuhn-Munkres) algorithm for Julia
License: The Unlicense
Here is an example:
M = kron(A,B)
using Hungarian
@time assignH, costH = hungarian(M)
# => 44.816792 seconds (13.19 M allocations: 8.777 GB, 2.24% gc time)
using Munkres
@time assignM = munkres(M)
# => 0.062191 seconds (20.06 k allocations: 25.378 MB, 3.27% gc time)
where A
and B
are tho30 data.
The optimal assignment is pretty easy to find, because M
is a symmetric matrix and the elements of its diagonal are all 0.
Here is an MWE that can reproduce the failure:
A = UInt8[ 49 107 64 23 232 139 21 72 124 125 197 226 45 99 106;
152 106 95 138 109 171 45 11 173 57 129 223 6 242 116;
191 197 43 224 105 229 85 225 163 118 99 207 195 194 14;
239 225 216 48 127 252 234 114 6 64 81 116 161 90 19;
209 122 84 187 200 150 229 21 115 154 203 252 221 238 131;
172 247 161 255 102 128 176 63 67 105 107 194 217 226 109;
5 49 90 126 45 211 168 198 211 200 42 88 117 224 172;
205 250 117 234 75 251 80 28 121 67 87 106 172 111 54;
157 99 233 70 80 196 237 3 77 137 32 143 100 117 149;
221 80 132 251 233 238 44 212 204 41 158 124 113 17 252;
171 9 138 170 15 190 149 15 190 58 252 248 21 210 223;
97 240 45 200 53 45 94 122 77 12 114 84 155 94 21;
6 209 214 15 58 59 60 232 40 210 93 63 80 86 95;
11 184 129 159 130 171 181 41 164 65 171 55 164 72 132;
30 225 231 144 209 203 30 202 195 221 70 38 220 48 203]
using Hungarian
using Munkres
n = 15
hh = Hungarian.munkres(A)
match_h = [findfirst(hh[i,:].==Hungarian.STAR) for i = 1:n]
match_m = munkres(A)
sum_h = sum(A[ii, match_h[ii]] for ii in 1:n) |> Int #-> 712
sum_m = sum(A[ii, match_m[ii]] for ii in 1:n) |> Int #-> 456
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
see FugroRoames/Munkres.jl#9 (comment)
I suspect that the way I used for computing the min cost here is wrong:
Lines 48 to 50 in c64cf98
We're using Hungarian.jl in the https://github.com/ZIB-IOL/FrankWolfe.jl package to solve LPs over the Birkhoff polytope.
It scales well in terms of memory footprint but badly in terms of time, mostly because of step3!
, where most of the time seems to be spent in this block:
@inbounds for j in colUncoveredIdx, i in rowUncoveredIdx
cost = costMat[i, j] + Δcol[j] + Δrow[i]
if cost <= h
if cost != h
h = cost
empty!(minLocations)
end
push!(minLocations, (i, j))
end
end
this would be non-trivial to parallelize because of the shared bound h
and array minLocations
but could be worth it.
I'm observing a weird behavior with the hungarian
function (tested only on Julia 1.8). If weights are integers the algorithm works fine
julia> weights_int = [1 missing; missing missing]
2×2 Matrix{Union{Missing, Int64}}:
1 missing
missing missing
julia> hungarian(weights_int)
([1, 0], 1)
If the weight matrix has float values instead the algorithm hangs
julia> weights_float = [1.0 missing; missing missing]
2×2 Matrix{Union{Missing, Float64}}:
1.0 missing
missing missing
julia> hungarian(weights_float)
Is this expected behavior? Can it be fixed? How?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.