-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestSolver.py
66 lines (56 loc) · 1.99 KB
/
TestSolver.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
import GeneticSolver as gs
import SimulatedAnnealingSolver as sas
import AntColonySolver as acs
import matplotlib.pyplot as plt
def plot_route(node_map, route):
x, y = zip(*[node_map[i] for i in route])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, c='b', marker=".")
plt.show()
#n = 20
#cities = generate_random_map(n)
cities = [[1., 0.], [0.951057, 0.309017], [0.809017, 0.587785], \
[0.587785, 0.809017], [0.309017, 0.951057], [0., 1.], \
[-0.309017, 0.951057], [-0.587785, 0.809017], [-0.809017, 0.587785], \
[-0.951057,0.309017], [-1., 0.], [-0.951057, -0.309017],\
[-0.809017, -0.587785], [-0.587785, -0.809017], \
[-0.309017, -0.951057], [0., -1.], [0.309017, -0.951057], \
[0.587785, -0.809017], [0.809017, -0.587785], [0.951057, -0.309017]]
population_count = 50
route_lengths, shortest_route = gs.find_shortest_route(cities, population_count, gs.roulette_parent_selector, gs.greedy_crossover)
print "Genetic Algorithm"
print "================="
print " - Roulette Selection"
print shortest_route
print route_lengths[-1]
#plt.plot(route_lengths)
#plt.show()
route_lengths, shortest_route = gs.find_shortest_route(cities, population_count, gs.tournament_parent_selector, gs.greedy_crossover, selector_tuning_params=10)
print ""
print " - Tournament Selection"
print shortest_route
print route_lengths[-1]
#plt.plot(route_lengths)
#plt.show()
ant_count = 20
evaporation_fraction = 0.3
route_lengths, shortest_route = acs.find_shortest_route(cities, ant_count, evaporation_fraction)
print ""
print "Ant Colony Optimisation"
print "======================="
print shortest_route
print route_lengths[-1]
#plt.plot(route_lengths)
#plt.show()
steps = 20000
tMax = 100.0
route_lengths, shortest_route = sas.find_shortest_route(cities,steps,tMax)
sas.find_shortest_route(cities, steps, tMax)
print ""
print "Simulated Annealing"
print "==================="
print shortest_route
print route_lengths[-1]
#plt.plot(route_lengths)
#plt.show()