Guest User

Untitled

a guest
Mar 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import math, random
  2. n_people = 6
  3. edges = []
  4. for i in range(n_people): edges.extend((i,j) for j in range(n_people) if j != i)
  5. # Ugly hack: enforce conjectured shape of solution
  6. # (to not do this, remove next 3 non-comment lines and see
  7. # single change below; note also that this needs changing if n_people
  8. # changes)
  9. edges0 = [(1,0), None, (2,1), (1,2), (0,1), (2,0), (0,2), (3,0), (0,3), (4,0), (0,4), (5,0), (0,5)] # adjust if n_people changes
  10. edges1 = [e for e in edges if e not in edges0]
  11. edges = edges0[:1] + edges1 + edges0[2:]
  12. n_edges = len(edges)
  13. def score(edges):
  14. money = n_people*[1]
  15. for (i,j) in edges:
  16. money[i] += money[j]
  17. #return sum(money) # use this to optimize total money (older problem)
  18. return money[0] # use this to optimize your own money (newer problem)
  19. def mutate(edges):
  20. edges = edges[:]
  21. # The -11 version is for the "restricted" optimization
  22. #i,j = random.sample(range(n_edges),2)
  23. i,j = random.sample(range(1, n_edges-11),2)
  24. edges[i],edges[j] = edges[j],edges[i]
  25. return edges
  26. curr = score(edges)
  27. best = (curr, edges)
  28. print(best)
  29. temp = 8000
  30. while True:
  31. candidate = mutate(edges)
  32. cscore = score(candidate)
  33. if cscore > curr or random.random() < math.exp((cscore-curr)/temp):
  34. edges = candidate
  35. curr = cscore
  36. if curr > best[0]:
  37. best = (curr, edges)
  38. print("new best", curr, edges)
  39. temp = 0.999993 * temp
  40. if temp < 1:
  41. print("resetting", curr, edges)
  42. temp = 8000
Add Comment
Please, Sign In to add comment