-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_250.py
More file actions
36 lines (26 loc) · 995 Bytes
/
Copy pathproblem_250.py
File metadata and controls
36 lines (26 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from population import Population
import matplotlib.pyplot as plt
import util
def read_cities_from_file(filename):
cities = []
with open(filename, 'r') as file:
for line in file:
x, y = map(float, line.strip().split(';'))
cities.append((x, y))
return cities
cities = read_cities_from_file("data/defi250.csv")
names = util.gen_names(len(cities))
population_size = len(cities) * 5
tournament_size = population_size // 10
mutation_rate = 1
france = Population(population_size, tournament_size, mutation_rate)
for i, (x, y) in enumerate(cities):
france.add_city(names[i], [x, y])
print("Starting animation...")
anim = france.animate_evolution(generations=5000, interval=5, method='hx extended', mutation='2-opt alt', duration=120)
if anim:
plt.show()
# Show the final result
best = max(france.individuals, key=lambda individual: individual.fitness)
print(f"\nFinal result: Distance = {best.total_distance:.3f}")
france.plot_result(best)