Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import itertools
  2. import numpy as np
  3.  
  4. def generate_solutions_randomly(M):
  5. n, m = M.shape
  6. row_totals = M.sum(axis = 1)
  7. col_totals = M.sum(axis = 0)
  8. perms = []
  9. while 1:
  10. np.random.shuffle(M)
  11. if np.equal(M.sum(axis = 1), row_totals).all() and \
  12. np.equal(M.sum(axis = 0), col_totals).all():
  13. yield M
  14.  
  15.  
  16. if __name__ == '__main__':
  17.  
  18. # Demo
  19. # Generate random binary 9 by 14 matrix
  20. M = ( np.random.rand(9,14) > .5) * 1
  21. print M
  22. print
  23.  
  24. # Find first 10 solutions using the generate_solutions iterator
  25. solutions = set()
  26. i = 0
  27. for sol in generate_solutions_randomly(M):
  28. solutions.add(str(sol))
  29. if len(solutions) != i:
  30. i = len(solutions)
  31. print sol
  32. print
  33.  
  34. if len(solutions) == 10:
  35. break
Add Comment
Please, Sign In to add comment