-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
manager.py
189 lines (155 loc) · 7.31 KB
/
manager.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
import pygame
import random
from random import randint, sample
from point import Point
from utils import *
from genetic import Genetic
from antColony import *
from ant import *
offset = 100
width, height = 1920, 1080
populationSize = 300
n = 15
colony_size = 10
iterations = 300
pygame.font.init()
class Manager(object):
size = (width, height)
fps = 30
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
scaler = 1
max_radius = 15
Black = (0, 0, 0)
White = (255, 255, 255)
Yellow = (255, 255, 0)
Gray = (100, 100, 100)
Highlight = (255, 255, 0)
LineThickness = 4
showIndex = True
n_points = n
algorithms = ["Brute Force", "Lexicographic Order", "Genetic Algorithm", "Ant Colony ACS", "Ant Colony Elitist", "Ant Colony Max-Min"]
genetic = Genetic([sample(list(range(n)), n) for i in range(populationSize)], populationSize)
PossibleCombinations = Factorial(n_points)
# print("possible combinations : {}".format(Factorial(n_points)))
Order = [i for i in range(n_points)]
counter = 0
def __init__(self, Points = [Point(randint(offset, width-offset), randint(offset, height-offset)) for i in range(n_points)]):
self.Points = Points
self.recordDistance = SumDistance(self.Points)
self.OptimalRoutes = self.Points.copy()
self.currentList = self.Points.copy()
# --- Ant Colony ---
self.antColony = AntColony(variation="ACS", size=colony_size, max_iterations = iterations,
nodes=self.Points.copy(), alpha=1, beta=3, rho=0.1, pheromone=1, phe_deposit_weight=1)
def ResetGenetic(self):
self.genetic = Genetic([sample(list(range(n)), n) for i in range(populationSize)], populationSize)
def ChangeAntColonyVariation(self, name):
self.antColony.variation = name
def ResetAntColony(self, name="ACS"):
self.recordDistance = SumDistance(self.Points)
self.antColony = AntColony(variation=name, size=colony_size, max_iterations = iterations,
nodes=self.Points.copy(), alpha=1, beta=3, rho=0.1, pheromone=1, phe_deposit_weight=1)
def SetFps(self):
return self.clock.tick(self.fps)/1000.0
def UpdateCaption(self):
frameRate = int(self.clock.get_fps())
pygame.display.set_caption("Traveling Salesman Problem - Fps : {}".format(frameRate))
def Counter(self):
self.counter += 1
if self.counter > self.PossibleCombinations:
self.counter = self.PossibleCombinations
def BruteForce(self):
if self.counter != self.PossibleCombinations:
i1 = randint(0, self.n_points-1)
i2 = randint(0, self.n_points-1)
self.Points[i1], self.Points[i2] = self.Points[i2], self.Points[i1]
# self.Counter()
dist = SumDistance(self.Points)
if dist < self.recordDistance:
self.recordDistance = dist
self.OptimalRoutes = self.Points.copy()
#print("Shortest distance : {}" .format(self.recordDistance))
self.DrawLines()
def Lexicographic(self):
self.Order = LexicalOrder(self.Order)
nodes = []
for i in self.Order:
nodes.append(self.Points[i])
self.Counter()
dist = SumDistance(nodes)
if dist < self.recordDistance:
self.recordDistance = dist
self.OptimalRoutes = nodes.copy()
#print("Shortest distance : {}" .format(self.recordDistance))
self.DrawLines()
def GeneticAlgorithm(self):
self.genetic.CalculateFitness(self.Points)
self.genetic.NaturalSelection()
# self.Counter()
for i in range(self.n_points):
self.currentList[i] = self.Points[self.genetic.current[i]]
if self.genetic.record < self.recordDistance:
for i in range(self.n_points):
self.OptimalRoutes[i] = self.Points[self.genetic.fitest[i]]
self.recordDistance = self.genetic.record
# print(self.OptimalRoutes)
self.DrawLines(True)
def AntColonyOptimization(self, pause):
if pause == False:
self.counter += 1
if self.counter > self.antColony.max_iterations:
self.counter = self.antColony.max_iterations
if self.counter < self.antColony.max_iterations:
self.antColony.Simulate(self.counter)
self.antColony.Draw(self)
self.recordDistance = self.antColony.best_distance
def RandomPoints(self):
self.Points = [Point(randint(offset, width-offset), randint(offset, height-offset)) for i in range(self.n_points)]
self.ResetAntColony(self.antColony.variation)
self.recordDistance = SumDistance(self.Points)
self.OptimalRoutes = self.Points.copy()
self.currentList = self.Points.copy()
def Percentage(self, val):
percent = (self.counter/val) * 100
textColor = (255, 255, 255)
# textFont = pg.font.Font("freesansbold.ttf", size)
textFont = pygame.font.SysFont("Arial", 20)
textSurface = textFont.render(str(round(percent, 4)), False, textColor)
self.screen.blit(textSurface, (width//2, 50))
def ShowText(self, selectedIndex, started = True):
textColor = (255, 255, 255)
# textFont = pg.font.Font("freesansbold.ttf", size)
textFont = pygame.font.SysFont("Times", 20)
textFont2 = pygame.font.SysFont("Arial Black", 40)
textSurface1 = textFont.render("Best distance : " + str(round(self.recordDistance,2)), False, textColor)
textSurface2 = textFont.render(self.algorithms[selectedIndex], False, textColor)
textSurface3 = textFont2.render("... Press ' SPACE ' to start ..." ,False, textColor)
self.screen.blit(textSurface1, (100, 70))
self.screen.blit(textSurface2, (100, 35))
if started == False:
self.screen.blit(textSurface3, (width//2, height-200))
def DrawShortestPath(self):
if len(self.OptimalRoutes) > 0:
for n in range(self.n_points):
_i = (n+1)%self.n_points
pygame.draw.line(self.screen, self.Highlight,
(self.OptimalRoutes[n].x, self.OptimalRoutes[n].y),
(self.OptimalRoutes[_i].x, self.OptimalRoutes[_i].y),
self.LineThickness)
self.OptimalRoutes[n].Draw(self, self.showIndex, True, n)
def DrawPoints(self, selected_index = 0):
for point in self.Points:
point.radius = self.scaler
point.Draw(self)
def DrawLines(self, drawCurrent=False):
if drawCurrent == True:
for i, point in enumerate(self.currentList):
_i = (i+1)%len(self.currentList)
pygame.draw.line(self.screen, self.Gray, (point.x, point.y), (self.currentList[_i].x, self.currentList[_i].y), 1)
else:
for i, point in enumerate(self.Points):
_i = (i+1)%len(self.Points)
pygame.draw.line(self.screen, self.Gray, (point.x, point.y), (self.Points[_i].x, self.Points[_i].y), 1)
def Background(self):
self.screen.fill(self.Black)