code_junkie

Create many constrained, random permutation of a list

Nov 10th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. import math, random
  2.  
  3.  
  4. def get_pool(items, y, z):
  5. slots = y*z
  6.  
  7. use_each_times = slots/len(items)
  8. exceptions = slots - use_each_times*len(items)
  9.  
  10.  
  11. if (use_each_times > y or
  12. exceptions > 0 and use_each_times+1 > y):
  13. raise Exception("Impossible.")
  14.  
  15.  
  16. pool = {}
  17. for n in items:
  18. pool[n] = use_each_times
  19.  
  20. for n in random.sample(items, exceptions):
  21. pool[n] += 1
  22.  
  23. return pool
  24.  
  25. def rebalance(ret, pool, z):
  26. max_item = None
  27. max_times = None
  28.  
  29. for item, times in pool.items():
  30. if times > max_times:
  31. max_item = item
  32. max_times = times
  33.  
  34.  
  35. next, times = max_item, max_times
  36.  
  37. candidates = []
  38. for i in range(len(ret)):
  39. item = ret[i]
  40.  
  41. if next not in item:
  42. candidates.append( (item, i) )
  43.  
  44.  
  45. swap, swap_index = random.choice(candidates)
  46.  
  47. swapi = []
  48. for i in range(len(swap)):
  49. if swap[i] not in pool:
  50. swapi.append( (swap[i], i) )
  51.  
  52.  
  53. which, i = random.choice(swapi)
  54.  
  55. pool[next] -= 1
  56. pool[swap[i]] = 1
  57. swap[i] = next
  58.  
  59. ret[swap_index] = swap
  60.  
  61. def plist(items, y, z):
  62. pool = get_pool(items, y, z)
  63.  
  64. ret = []
  65. while len(pool.keys()) > 0:
  66. while len(pool.keys()) < z:
  67. rebalance(ret, pool, z)
  68.  
  69. selections = random.sample(pool.keys(), z)
  70.  
  71. for i in selections:
  72. pool[i] -= 1
  73. if pool[i] == 0:
  74. del pool[i]
  75.  
  76. ret.append( selections )
  77.  
  78. return ret
  79.  
  80.  
  81. print plist([0,1,2,3], 6, 2)
  82.  
  83. import math, random
  84.  
  85.  
  86. def get_pool(items, y, z):
  87. slots = y*z
  88.  
  89. use_each_times = slots/len(items)
  90. exceptions = slots - use_each_times*len(items)
  91.  
  92.  
  93. if (use_each_times > y or
  94. exceptions > 0 and use_each_times+1 > y):
  95. raise Exception("Impossible.")
  96.  
  97.  
  98. pool = {}
  99. for n in items:
  100. pool[n] = use_each_times
  101.  
  102. for n in random.sample(items, exceptions):
  103. pool[n] += 1
  104.  
  105. return pool
  106.  
  107. def rebalance(ret, pool, z):
  108. max_item = None
  109. max_times = None
  110.  
  111. for item, times in pool.items():
  112. if times > max_times:
  113. max_item = item
  114. max_times = times
  115.  
  116.  
  117. next, times = max_item, max_times
  118.  
  119. candidates = []
  120. for i in range(len(ret)):
  121. item = ret[i]
  122.  
  123. if next not in item:
  124. candidates.append( (item, i) )
  125.  
  126.  
  127. swap, swap_index = random.choice(candidates)
  128.  
  129. swapi = []
  130. for i in range(len(swap)):
  131. if swap[i] not in pool:
  132. swapi.append( (swap[i], i) )
  133.  
  134.  
  135. which, i = random.choice(swapi)
  136.  
  137. pool[next] -= 1
  138. pool[swap[i]] = 1
  139. swap[i] = next
  140.  
  141. ret[swap_index] = swap
  142.  
  143. def plist(items, y, z):
  144. pool = get_pool(items, y, z)
  145.  
  146. ret = []
  147. while len(pool.keys()) > 0:
  148. while len(pool.keys()) < z:
  149. rebalance(ret, pool, z)
  150.  
  151. selections = random.sample(pool.keys(), z)
  152.  
  153. for i in selections:
  154. pool[i] -= 1
  155. if pool[i] == 0:
  156. del pool[i]
  157.  
  158. ret.append( selections )
  159.  
  160. return ret
  161.  
  162.  
  163. print plist([0,1,2,3], 6, 2)
  164.  
  165. # list is the elements to be permuted
  166. # y is the number of results desired
  167. # z is the number of elements per result
  168. # equalizer keeps track of who got used how many times
  169. def constrained_permutations list, y, z
  170. list.uniq! # Never trust the user. We want no repetitions.
  171. equalizer = {}
  172. list.each { |element| equalizer[element] = 0 }
  173.  
  174. results = []
  175. # Do this until we get as many results as desired
  176. while results.size < y
  177. pool = []
  178. puts pool
  179. least_used = equalizer.each_value.min
  180. # Find how used the least used element was
  181. while pool.size < z
  182. # Do this until we have enough elements in this resultset
  183. element = nil
  184. while element.nil?
  185. # If we run out of "least used elements", then we need to increment
  186. # our definition of "least used" by 1 and keep going.
  187. element = list.shuffle.find do |x|
  188. !pool.include?(x) && equalizer[x] == least_used
  189. end
  190. least_used += 1 if element.nil?
  191. end
  192. equalizer[element] += 1
  193. # This element has now been used one more time.
  194. pool << element
  195. end
  196. results << pool
  197. end
  198. return results
  199. end
  200.  
  201. constrained_permutations [0,1,2,3,4,5,6], 6, 2
  202. => [[4, 0], [1, 3], [2, 5], [6, 0], [2, 5], [3, 6]]
  203. constrained_permutations [0,1,2,3,4,5,6], 6, 2
  204. => [[4, 5], [6, 3], [0, 2], [1, 6], [5, 4], [3, 0]]
  205. enter code here
Add Comment
Please, Sign In to add comment