Guest User

Untitled

a guest
Nov 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. toolbox = base.Toolbox()
  2.  
  3. # Attribute generator
  4. toolbox.register("indices",random.sample, range(IND_SIZE), IND_SIZE)
  5. #random ใส่ใน indices เลขระหว่าง 17 จำนวน 17ครั้ง
  6.  
  7.  
  8. # Structure initializers
  9. toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
  10. toolbox.register("population", tools.initRepeat, list, toolbox.individual)
  11. #initRepeat((container, func, n)
  12. # func = The function that will be called n times to fill the container.
  13. # n = loop n time
  14. #initIterate(container, generator)
  15. #container = The type to put in the data from func.
  16. #gen = function returning an iterable (list, tuple, ...), fill container
  17. def DisCity(individual):
  18. distance = distance_map[individual[-1]][individual[0]]
  19. for gene1, gene2 in zip(individual[0:-1], individual[1:]):
  20. distance += distance_map[gene1][gene2]
  21. return distance,
  22. #คำนวณระยะห่างระหว่างเเต่ละเมืองกัน
  23.  
  24. toolbox.register("mate", tools.cxPartialyMatched) # ทำ croosover
  25. toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05) #
  26. toolbox.register("select", tools.selTournament, tournsize=3)
  27. toolbox.register("evaluate", DisCity)
  28.  
  29. def main():
  30. random.seed(169)
  31. pop = toolbox.population(n=300)
  32. hof = tools.HallOfFame(1)
  33. stats = tools.Statistics(lambda ind: ind.fitness.values)
  34. stats.register("avg", numpy.mean)
  35. stats.register("std", numpy.std)
  36. stats.register("min", numpy.min)
  37. stats.register("max", numpy.max)
  38.  
  39. algorithms.eaSimple(pop, toolbox, 0.7, 0.3,40, stats=stats,
  40. halloffame=hof)
  41.  
  42. print(hof)
  43. return pop, stats, hof
  44.  
  45.  
  46.  
  47. if __name__ == "__main__":
  48. main()
Add Comment
Please, Sign In to add comment