Comments (6)
There is a bug in the code and which can be fixed by the making minor edit. I am also putting the updated functions below. The error was due to the line, n = len(matches) in to_permutation function because len(matches) can eventually decrease as S becomes sparser.
def to_permutation_matrix(matches, n):
P = np.zeros((n, n))
P[list(zip(*(matches.items())))] = 1
return P
def birkhoff_von_neumann_decomposition(D):
m, n = D.shape
if m != n:
raise ValueError('Input matrix must be square ({} x {})'.format(m, n))
indices = list(itertools.product(range(m), range(n)))
coefficients = []
permutations = []
S = D.astype('float')
while not np.all(S == 0):
W = to_pattern_matrix(S)
X = to_bipartite_matrix(W)
G = from_numpy_matrix(X)
left_nodes = range(n)
M = maximum_matching(G, left_nodes)
M = {u: v % n for u, v in M.items() if u < n}
P = to_permutation_matrix(M, n)
q = min(S[i, j] for (i, j) in indices if P[i, j] == 1)
coefficients.append(q)
permutations.append(P)
S -= q * P
S[np.abs(S) < TOLERANCE] = 0.0
return list(zip(coefficients, permutations))
from birkhoff.
Theoretically if S is a square doubly stochastic matrix of size n, the number of matches (which represents the number of elements being permuted) should be n every time through the loop. Can you find a specific example of an input for which this fails?
There may be some precision issues (like when very small numbers get rounded to zero in the doubly stochastic matrix), which is what I suspect the problem is here. One thing we could try is scaling the values in the matrix up by a large integer to protect against this problem.
from birkhoff.
Closing due to missing a minimal working example demonstrating the issue.
from birkhoff.
Hi,
Thanks for providing the package.
I have an example of a case where it does not work for. First of all the traceback:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\birkhoff.py", line 223, in birkhoff_von_neumann_decomposition
P = to_permutation_matrix(M)
File "C:\ProgramData\Anaconda3\lib\site-packages\birkhoff.py", line 60, in to_permutation_matrix
P[list(zip(*(matches.items())))] = 1
IndexError: index 39 is out of bounds for axis 0 with size 39
And here the matrix I tried it for:
It's a 40x40 Matrix.
It would be awesome if you could reopen this issue and try to fix this.
I am using it with Python 3.
I also tried to trace down the problem and found out, that the maximum_matching in line 211 at one point only returns len(M) = 78 instead of 80 like it should be. Maybe the problem lies somewhere around there.
Your help would be much appreciated!
Cheers,
Laura
from birkhoff.
Thank you for providing an example and tracking down the issue! I'll take a look when I have some time.
from birkhoff.
This worked for me, thank you! @prateeky2806
from birkhoff.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from birkhoff.