-
Notifications
You must be signed in to change notification settings - Fork 21
/
deap_tsp.py
91 lines (69 loc) · 2.76 KB
/
deap_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
import random
import functools
import math
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
INPUT = 'inputs/tsp_std.in'
# reads the input set of values of objects
def read_locations(filename):
locations = []
with open(filename) as f:
for l in f.readlines():
tokens = l.split(' ')
locations.append((float(tokens[0]), float(tokens[1])))
return locations
locations = read_locations(INPUT)
@functools.lru_cache(maxsize=None) # this enables caching of the values
def distance(loc1, loc2):
# based on https://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [loc1[1], loc1[0], loc2[1], loc2[0]])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
# Radius of earth in kilometers is 6371
km = 6371.01 * c
return km
# the fitness function
def fitness(ind, cities):
# quickly check that ind is a permutation
num_cities = len(cities)
assert len(ind) == num_cities
assert sum(ind) == num_cities*(num_cities - 1)//2
dist = 0
for a, b in zip(ind, ind[1:]):
dist += distance(cities[a], cities[b])
dist += distance(cities[ind[-1]], cities[ind[0]])
return dist,
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
IND_SIZE = len(locations)
toolbox.register("indices", random.sample, range(IND_SIZE), IND_SIZE)
toolbox.register("individual", tools.initIterate, creator.Individual,
toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalOneMax(individual):
return sum(individual),
toolbox.register("evaluate", fitness, cities=locations)
toolbox.register("mate", tools.cxOrdered)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=2)
def main():
pop = toolbox.population(n=100)
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)
pop, log = algorithms.eaMuPlusLambda(pop, toolbox, mu=100, lambda_=100, cxpb=0.8, mutpb=0.2, ngen=500, stats=stats, halloffame=hof, verbose=True)
return pop, log, hof
if __name__ == "__main__":
pop, log, hof = main()
print('Best solution fitness:', hof[0].fitness.values)