Esempio n. 1
0
File: tsp.py Progetto: Seraphli/ga
def ga_p(city_list):
    cfg = GACfg(gene_len=25,
                gene_size=25,
                pop_size=1000,
                elite_size=50,
                tournament_num=50,
                tournament_size=100,
                mutation_rate=0.03,
                repeatable=False)
    ga = GA(fitness_func, cfg)
    ga.prepare_kwargs(city_list=city_list)
    ga.init_population()
    progress = []
    progress.append(1 / ga.cal_fitness()[0][1])
    for _ in range(100):
        early_stop = ga.evolve()
        progress.append(1 / ga.cal_fitness()[0][1])
        if early_stop:
            break

    print("Final distance: " + str(1 / ga.cal_fitness()[0][1]))
    plt.plot(progress)
    plt.ylabel('Distance')
    plt.xlabel('Generation')
    plt.show()