forked from Knackie/algorithmshacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greddy_algorithm.py
208 lines (159 loc) · 6.18 KB
/
greddy_algorithm.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
# case Travel Salesman Problem
import matplotlib.pyplot as plt
import numpy as np
import time
import random
import itertools
import math
import os
import threading
class plotCompareProblem():
#So we don't use the edges of the graph
pltMin = 2
pltMax = 29
def __init__(self, count=12, start=(0,0)):
# 0, 0 is the start
self.pltCount = count
self.start = start
self.xdata = [start[0]]
self.ydata = [start[1]]
self.id = 1
self.plt = plt
self.plt.ion()
self.figure, (self.ax, self.ax1) = self.plt.subplots(1, 2, figsize=(24, 12))
self.lines1, = self.ax1.plot([],[], 'o')
self.ax1.set_xlim(0, self.pltMax + 6)
self.ax1.set_ylim(0, self.pltMax + 6)
self.ax1.grid()
self.populate()
def setTitle(self,handle,title):
handle.title.set_text(title)
def setText(self, handle, text):
handle.text(1, (self.pltMax+3), text, fontsize=8)
def getHandle(self):
return self.ax, self.ax1
def updateSet(self):
self.lines1.set_data(self.xdata, self.ydata)
#self.plt.pause(0.001)
self.updateFigure()
def populate(self):
for x in range(0, self.pltCount):
self.xdata.append(random.randint(self.pltMin, self.pltMax))
self.ydata.append(random.randint(self.pltMin, self.pltMax))
self.updateSet()
def connectDots(self, pt1, pt2, handle, color):
x1, x2 = pt1[0], pt2[0]
y1, y2 = pt1[1], pt2[1]
lineId = self.id
line = handle.plot([x1,x2], [y1,y2], color, gid = lineId)
self.id += 1
self.updateFigure()
return lineId
def updateFigure(self):
self.figure.canvas.draw()
self.figure.canvas.flush_events()
def deleteConnection(self, gid):
for i in self.ax.lines:
if i.get_gid() == gid:
i.remove()
break
def removeOldLines(self, handle, toKeep = 1):
del handle.lines[toKeep:]
self.updateSet()
def removeNewestLine(self, handle):
del handle.lines[-1]
self.updateSet()
def getStart(self):
return self.start
def getData(self):
return self.xdata, self.ydata
def calculateDist(self, p1, p2):
return math.sqrt((math.pow((p2[0] - p1[0]), 2)) + math.pow((p2[1] - p1[1]), 2))
class TSPGreedy():
basicColor = "b--"
bestColor = "r-"
def __init__(self, canvas, handle):
self.problemCanvas = canvas
self.handle = handle
self.problemCanvas.setTitle(self.handle, "Greedy Algorithm")
self.problemCanvas.removeOldLines(self.handle)
self.totalDistance = 0
def performAlgo(self):
self.start = self.problemCanvas.getStart()
data = self.problemCanvas.getData()
self.XCord = data[0]
self.YCord = data[1]
self.data = []
for i in range(1, len(self.XCord)):
self.data.append((self.XCord[i], self.YCord[i]))
nextPoint = self.smallestDistance(self.start, self.data)
self.problemCanvas.connectDots(self.start, nextPoint, self.handle, self.bestColor)
self.data.remove(nextPoint)
path =[self.start, nextPoint]
while self.data != []:
prevPoint = nextPoint
nextPoint = self.smallestDistance(prevPoint, self.data)
path.append(nextPoint)
self.problemCanvas.connectDots(prevPoint, nextPoint, self.handle, self.bestColor)
self.data.remove(nextPoint)
conclusionStr = "Total Distance: " + str(self.totalDistance) + "\nPath: " + str(path)
self.problemCanvas.setText(self.handle, conclusionStr)
return path
def smallestDistance(self, pt, data):
minimum = 999
retPt = None
for i in data:
self.problemCanvas.connectDots(pt, i, self.handle, self.basicColor)
curMin = self.problemCanvas.calculateDist(pt, i)
if curMin < minimum:
minimum = curMin
retPt = i
self.problemCanvas.removeNewestLine(self.handle)
self.totalDistance += minimum
return retPt
class TSPGreedy():
basicColor = "b--"
bestColor = "r-"
def __init__(self, canvas, handle):
self.problemCanvas = canvas
self.handle = handle
self.problemCanvas.setTitle(self.handle, "Traveling Salesmen Problem with Greedy Algorithm")
self.problemCanvas.removeOldLines(self.handle)
self.totalDistance = 0
def performAlgo(self):
self.start = self.problemCanvas.getStart()
data = self.problemCanvas.getData()
self.XCord = data[0]
self.YCord = data[1]
self.data = []
for i in range(1, len(self.XCord)):
self.data.append((self.XCord[i], self.YCord[i]))
nextPoint = self.smallestDistance(self.start, self.data)
self.problemCanvas.connectDots(self.start, nextPoint, self.handle, self.bestColor)
self.data.remove(nextPoint)
path =[self.start, nextPoint]
while self.data != []:
prevPoint = nextPoint
nextPoint = self.smallestDistance(prevPoint, self.data)
path.append(nextPoint)
self.problemCanvas.connectDots(prevPoint, nextPoint, self.handle, self.bestColor)
self.data.remove(nextPoint)
conclusionStr = "Total Distance: " + str(self.totalDistance) + "\nPath: " + str(path)
self.problemCanvas.setText(self.handle, conclusionStr)
return path
def smallestDistance(self, pt, data):
minimum = 999
retPt = None
for i in data:
self.problemCanvas.connectDots(pt, i, self.handle, self.basicColor)
curMin = self.problemCanvas.calculateDist(pt, i)
if curMin < minimum:
minimum = curMin
retPt = i
self.problemCanvas.removeNewestLine(self.handle)
self.totalDistance += minimum
return retPt
problem = plotCompareProblem(12)
handle, handle1 = problem.getHandle()
T = TSPGreedy(problem, handle1)
T.performAlgo()