-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.py
207 lines (157 loc) · 5.76 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
import sys
import random
import json
import numpy
import matplotlib.pyplot as plt
from deap import algorithms
from deap import base as d_base
from deap import creator
from deap import tools
##
## Importing data from JSON file
##
if(len(sys.argv) == 5):
trip_len = int(sys.argv[2])
with open(sys.argv[1], "r") as jsondata:
jsondata = json.load(jsondata)
else:
trip_len = 7
with open("exercise1.json", "r") as jsondata:
jsondata = json.load(jsondata)
cities = jsondata["cities"]
cities_name = [i["name"] for i in cities]
cities_reward = [i["reward"] for i in cities]
city_base = next((index for (index,d) in enumerate(cities) if d["base"] == True))
ncities = len(cities)
connections = jsondata["connections"]
connections_from = [i["from"] for i in connections]
connections_to = [i["to"] for i in connections]
connections_cost = [i["cost"] for i in connections]
nconnections = len(connections);
# This matrix contains the fuel cost for travelling throught cities
cost_matrix = [[-1 for i in range(ncities)] for j in range(ncities)]
for i in range(ncities):
for j in range(nconnections):
if(connections_from[j] == cities_name[i]):
cost_matrix[i][cities_name.index(connections_to[j])] = connections_cost[j]
cost_matrix[cities_name.index(connections_to[j])][i] = connections_cost[j]
#%%
##
## Auxialiary functions for the GA
##
# Find a valid random flight to the city corresponding to 'pos'
def findFlight(individual, pos):
posibles = []
for j in range(ncities):
if (cost_matrix[individual[pos-1]][j] != -1):
posibles.append(j)
if(len(posibles) == 0):
return random.randint(0,ncities-1)
return random.choice(posibles)
# Set the first generator for the generation of individual
# First and last city known (Madrid)
# Rest of cities linked randomly
def routeInit():
route = []
route.append(city_base)
for i in range(1,trip_len-1):
route.append(findFlight(route,i))
route.append(city_base)
return route
# Feasible function is used for penalize indivuals with wrong structure
# Penalty will be equal to 20 plus a bonus taken from routeQ function (line 141)
def feasible(individual):
if ((individual[0]!=city_base) or (individual[-1]!=city_base)):
return False
for gene1, gene2 in zip(individual[0:-1], individual[1:]):
if (cost_matrix[gene1][gene2] == -1):
return False
return True
# RouteQ determines the amount of penalization given to the individual
# Penalization bigger if not begging and finalizing route in Madrid
def routeQ(individual):
q = 0
if (individual[0]!=city_base):
q += 20
if (individual[-1]!=city_base):
q += 20
for gene1, gene2 in zip(individual[0:-1], individual[1:]):
if (cost_matrix[gene1][gene2] == -1):
q += 4
return q
# Evaluation function
def evalTSP(individual):
cost = 0
incentive = 0;
for gene1, gene2 in zip(individual[0:-1], individual[1:]):
cost += cost_matrix[gene1][gene2]
for i in range(len(individual)):
if(i in individual):
incentive += cities_reward[i];
return (incentive - cost),
# Mutation in which random cities are randomly linked
def mutMakeConnection(individual, p):
for i in range(1,len(individual)-1):
if(random.random() < p):
individual[i] = findFlight(individual,i)
individual[i+1] = findFlight(individual,i+1)
individual[0] = city_base
individual[-1] = city_base
return individual,
# Plot the evolution of the algorithm when it's finished
# Reresents minimun and maximun of each generation
def evolucion(log):
gen = log.select("gen")
fit_mins = log.select("min")
fit_maxs = log.select("max")
fit_ave = log.select("avg")
fig, ax1 = plt.subplots()
ax1.plot(gen, fit_mins, "b")
ax1.plot(gen, fit_maxs, "r")
ax1.plot(gen, fit_ave, "--k")
ax1.set_xlabel("Generation")
ax1.set_ylabel("Fitness")
ax1.legend(["Min", "Max", "Avg"])
plt.grid(True)
plt.savefig("evolution.png")
plt.show()
#%%
##
## GA implementational functions for the eaSimple algorithm
##
creator.create("FitnessMax", d_base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = d_base.Toolbox()
toolbox.register("indices", routeInit)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoints) # crossover
toolbox.register("mutate", mutMakeConnection, p = 0.5) # mutation
toolbox.register("select", tools.selTournament, tournsize=3) # selection
toolbox.register("evaluate", evalTSP) # evaluation
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible,-20, routeQ)) # penalization
#%%
def main():
random.seed(0)
if(len(sys.argv)!=5):
NIND, NGEN = 200, 100
else:
NIND, NGEN = int(sys.argv[3]), int(sys.argv[4])
pop = toolbox.population(NIND)
CXPB, MUTPB = 0.5, 0.3
# Save best individual and register data
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)
logbook = tools.Logbook()
pop, logbook= algorithms.eaSimple(pop, toolbox, CXPB, MUTPB, NGEN, stats=stats,
halloffame=hof)
return pop, hof, logbook
if __name__ == "__main__":
pop, hof, log = main()
print("Best solution: ", hof)
print("Best solution fitness: ", hof[0].fitness.values)
evolucion(log)