-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp.py
265 lines (187 loc) · 10.6 KB
/
tsp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import logging
import numpy as np
from operations.mutation import SingleTravelerMut
from operations.crossover import SingleTravelerX
from operations.initialization import Initialization
from operations.selection import SelectIndividuals, STSPKElitism
from operations.fitness import DistanceFitnessCalculator, MTSPFitnessCalculator
from typing import Tuple, List
from scipy.spatial.distance import cdist
from scipy.stats import norm
logger = logging.getLogger(__name__)
# >> setting up logging >>>>>
stream_handler = logging.StreamHandler()
logger.setLevel(logging.INFO)
logger.addHandler(stream_handler)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class SingleTSP:
"""
This class implements single traveling salesmans problem
"""
def __init__(self, n_gen: int) -> None:
self.n_gen = n_gen
self.statistics = {
"mean_fitness": [],
"std_fitness": [],
"best_overall": [],
"best_individual": [],
"best_fitness": [],
}
def sort_fitness(self, individuals: np.ndarray, fitness: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""
Sort individuals based on fitness
"""
indiv_fit = np.concatenate((individuals, fitness[:, np.newaxis]), axis=1)
indiv_fit = np.array(sorted(indiv_fit, key=lambda x: x[-1]))
individuals = indiv_fit[:, :-1].astype(int)
# remove o eixo adicional introduzido por np.newaxis
fitness = indiv_fit[:, -1:].reshape(-1)
return individuals, fitness
def save_statistics(self, individuals: np.ndarray, fitness: np.ndarray) -> None:
min_fitness_idx = fitness.argmin()
self.statistics["best_individual"].append(individuals[min_fitness_idx].copy())
self.statistics["best_fitness"].append(fitness[min_fitness_idx])
self.statistics["mean_fitness"].append(fitness.mean())
self.statistics["std_fitness"].append(fitness.std())
def evolve(self,
pop_initializer: Initialization,
crossover_op: SingleTravelerX,
mutation_op: SingleTravelerMut,
selection_op: SelectIndividuals,
fitness_calculator: DistanceFitnessCalculator,
k_elitism: STSPKElitism):
pop_size = pop_initializer.pop_size
population = pop_initializer.random()
fitness = np.apply_along_axis(fitness_calculator.distance_fitness, 1, population)
#population, fitness = self.sort_fitness(population, fitness)
self.save_statistics(population, fitness)
for gen_idx in range(self.n_gen):
best_overall = self.statistics["best_fitness"][-1]
logger.info(f"Generation {gen_idx}, Best overall solution: {best_overall}")
new_population = []
# new population creation
for _ in range(0, pop_size // 2):
parent_1, parent_2 = selection_op.apply(population, fitness)
child_1, child_2 = crossover_op.apply(parent_1, parent_2)
mchild_1 = mutation_op.apply(child_1)
mchild_2 = mutation_op.apply(child_2)
new_population += [mchild_1, mchild_2]
# computing new fitness
new_population = np.array(new_population, dtype=int)
new_fitness = np.apply_along_axis(fitness_calculator.distance_fitness, 1, new_population)
# sorting by fitness
#new_population, new_fitness = self.sort_fitness(new_population, new_fitness)
population, fitness = k_elitism.apply(population, fitness, new_population, new_fitness)
self.save_statistics(population, fitness)
class MTSP:
def __init__(self, n_gen: int, traveler_breaks: List[int], combine_multiple_x: bool=False, combine_multiple_mut: bool = False):
"""
Genetic algorithm for the Multi Traveling Salesmen problem.
Parameters:
-----------
n_gen: int the total number of gneerations
traveler_breaks: List[int] tell wich segment belongs to which traveler
combine_multiple_x: bool Tell the algorithm whether or not to probabilistcally choose among the multiple
crossover operations
combine_multiple_mut: bool Tell the algorithm whether or not to probabilistcally choose among the multiple
mutations operations
"""
self.n_gen = n_gen
self.traveler_breaks = traveler_breaks
self.combine_multiple_x = combine_multiple_x
self.combine_multiple_mut = combine_multiple_mut
self.statistics = {
"mean_fitness": [],
"std_fitness": [],
"best_overall": [],
"best_individual": [],
"best_fitness": [],
}
def save_statistics(self, individuals: np.ndarray, fitness: np.ndarray) -> None:
min_fitness_idx = fitness.argmin()
self.statistics["best_individual"].append(individuals[min_fitness_idx].copy().tolist())
self.statistics["best_fitness"].append(fitness[min_fitness_idx])
self.statistics["mean_fitness"].append(fitness.mean())
self.statistics["std_fitness"].append(fitness.std())
def _initialize_operation_counts(self, x_operation_options: List[str], mut_operation_options: List[str]):
x_conts = [0] * len(x_operation_options)
mut_counts = [0]*len(mut_operation_options)
self._x_op_counts = dict(zip(x_operation_options, x_conts))
self._mut_op_counts = dict(zip(mut_operation_options, mut_counts))
def _choose_from_options(self, operation_options: List[str], operation_probs: np.ndarray) -> str:
#rand_prob = np.random.rand()
rand_prob = np.random.uniform(low=0., high=1.)
chosen_operation = None
for operation_option, operation_prob in zip(operation_options, operation_probs):
if operation_prob >= rand_prob:
chosen_operation = operation_option
break
# checks if the variable chosen_operation is still None
chosen_operation = np.random.choice(operation_options) if not chosen_operation else chosen_operation
return chosen_operation
def _crossover_operations_combinator(self, crossover_op: SingleTravelerX, parent_1:np.ndarray, parent_2: np.ndarray ) -> Tuple[np.ndarray, np.ndarray]:
if self.combine_multiple_x:
chosen_operation = self._choose_from_options(crossover_op.crossover_options, self._crossover_op_probs)
self._x_op_counts[chosen_operation] += 1
crossover_op.crossover_type = chosen_operation
return crossover_op.apply(parent_1, parent_2)
self._x_op_counts[crossover_op.crossover_type] += 1
return crossover_op.apply(parent_1, parent_2)
def _mutation_operations_combinator(self, mutation_op: SingleTravelerMut, child: np.ndarray) -> np.ndarray:
if self.combine_multiple_mut:
chosen_operation = self._choose_from_options(mutation_op.mutation_optioins, self._mutation_op_probs)
self._mut_op_counts[chosen_operation] += 1
mutation_op.mutation_type = chosen_operation
return mutation_op.apply(child)
self._mut_op_counts[mutation_op.mutation_type] += 1
return mutation_op.apply(child)
def _scale_op_indices(self, op_indices: np.ndarray) -> np.ndarray:
"""
Rescales the operation indices so each of the elements are between 0 and 1
"""
return (op_indices - op_indices.min()) / (op_indices.max() - op_indices.min())
def _compute_per_operation_prob(self, population: np.ndarray, op_x_indices: List[int], op_mut_indices: List[int]) -> None:
if self.combine_multiple_x or self.combine_multiple_mut:
hamming_d_matrix = cdist(population, population, metric="hamming")
up_diag = hamming_d_matrix[np.triu_indices(hamming_d_matrix.shape[0])]
hamming_d_mean = up_diag.mean()
hamming_d_std = up_diag.std()
scaled_indices_x = self._scale_op_indices(np.array(op_x_indices))
scaled_indices_mut = self._scale_op_indices(np.array(op_mut_indices))
self._crossover_op_probs = norm.pdf(scaled_indices_x, loc=hamming_d_mean, scale=hamming_d_std)
self._mutation_op_probs = norm.pdf(scaled_indices_mut, loc=hamming_d_mean, scale=hamming_d_std)
def evolve(self,
pop_initializer: Initialization,
crossover_op: SingleTravelerX,
mutation_op: SingleTravelerMut,
selection_op: SelectIndividuals,
fitness_calculator: MTSPFitnessCalculator,
survivor_selection: STSPKElitism):
pop_size = pop_initializer.pop_size
self._initialize_operation_counts(crossover_op.crossover_options, mutation_op.mutation_optioins)
population = pop_initializer.random()
fitness = np.apply_along_axis(fitness_calculator.distance_fitness, 1, population, self.traveler_breaks)
self.save_statistics(population, fitness)
#population, fitness = self.sort_fitness(population, fitness)
for gen_idx in range(self.n_gen):
self._compute_per_operation_prob(
population,
list(range(len(crossover_op.crossover_options))),
list(range(len(mutation_op.mutation_optioins))))
best_overall = self.statistics["best_fitness"][-1]
logger.info(f"Generation {gen_idx}, Best overall solution: {best_overall}")
new_population = []
# new population creation
for _ in range(0, pop_size // 2):
parent_1, parent_2 = selection_op.apply(population, fitness)
child_1, child_2 = self._crossover_operations_combinator(crossover_op, parent_1, parent_2)
mchild_1 = self._mutation_operations_combinator(mutation_op, child_1)
mchild_2 = self._mutation_operations_combinator(mutation_op, child_2)
new_population += [mchild_1, mchild_2]
# computing new fitness
new_population = np.array(new_population, dtype=int)
new_fitness = np.apply_along_axis(fitness_calculator.distance_fitness, 1, new_population, self.traveler_breaks)
# sorting by fitness
#new_population, new_fitness = self.sort_fitness(new_population, new_fitness)
population, fitness = survivor_selection.apply(population, fitness, new_population, new_fitness)
self.save_statistics(population, fitness)