Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- toolbox = base.Toolbox()
- # Attribute generator
- toolbox.register("indices",random.sample, range(IND_SIZE), IND_SIZE)
- #random ใส่ใน indices เลขระหว่าง 17 จำนวน 17ครั้ง
- # Structure initializers
- toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
- toolbox.register("population", tools.initRepeat, list, toolbox.individual)
- #initRepeat((container, func, n)
- # func = The function that will be called n times to fill the container.
- # n = loop n time
- #initIterate(container, generator)
- #container = The type to put in the data from func.
- #gen = function returning an iterable (list, tuple, ...), fill container
- def DisCity(individual):
- distance = distance_map[individual[-1]][individual[0]]
- for gene1, gene2 in zip(individual[0:-1], individual[1:]):
- distance += distance_map[gene1][gene2]
- return distance,
- #คำนวณระยะห่างระหว่างเเต่ละเมืองกัน
- toolbox.register("mate", tools.cxPartialyMatched) # ทำ croosover
- toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05) #
- toolbox.register("select", tools.selTournament, tournsize=3)
- toolbox.register("evaluate", DisCity)
- def main():
- random.seed(169)
- pop = toolbox.population(n=300)
- hof = tools.HallOfFame(1)
- stats = tools.Statistics(lambda ind: ind.fitness.values)
- stats.register("avg", numpy.mean)
- stats.register("std", numpy.std)
- stats.register("min", numpy.min)
- stats.register("max", numpy.max)
- algorithms.eaSimple(pop, toolbox, 0.7, 0.3,40, stats=stats,
- halloffame=hof)
- print(hof)
- return pop, stats, hof
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment