-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorithms_vin.py
93 lines (58 loc) · 2.21 KB
/
genetic_algorithms_vin.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import random
import copy
inf = float('inf')
def create_population(population_num, cities_list, initial_city):
cities = copy.copy(cities_list)
cities.remove(initial_city)
solutions = []
for new_solution in range(population_num):
solution = copy.copy(cities)
random.shuffle(solution)
solution.insert(0, initial_city)
solutions.append(solution)
return solutions
def calc_distances_paths(graph, initial_city, solutions):
distances = []
for solution in solutions:
distance = 0
if len(solution) != len(set(solution)):
distance = float('inf')
distances.append(distance)
continue
for city_index in range(len(solution)-1):
distance += graph[solution[city_index]][solution[city_index+1]]
distance += graph[solution[-1]][initial_city]
distances.append(distance)
return distances
def calc_fitness(distances):
fitnesess = np.exp2(-np.array(distances))*100
return fitnesess
def crossover_solutions(solutions, population_num):
crossovered_solutions = []
#Keep the best alive for the next gen
for solution in solutions:
crossovered_solutions.append(solution)
for new_solution in range(population_num-len(solutions)):
crossover_point = random.randint(0, len(solutions[0]))
parents = copy.deepcopy(random.sample(solutions, 2))
cromosomeA = parents[0][:crossover_point]
cromosomeB = parents[1][crossover_point:]
solution = cromosomeA + cromosomeB
crossovered_solutions.append(solution)
return crossovered_solutions
def should_mutate(mutation_prob):
return random.random() < mutation_prob
def mutate_solutions(solutions, mutation_prob):
mutated_solutions = copy.deepcopy(solutions)
for solution in mutated_solutions:
if(should_mutate(mutation_prob)):
#Swap two cities
mutation_pointA, mutation_pointB = random.sample(range(1,len(solution)), 2)
temp = copy.copy(solution[mutation_pointA])
solution[mutation_pointA] = solution[mutation_pointB]
solution[mutation_pointB] = temp
return mutated_solutions
def sort_solutions_by_distance(gen_solutions, distances):
sorted_lists = sorted(zip(gen_solutions, distances))
return sorted_lists