-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPA1.py
300 lines (231 loc) · 11.7 KB
/
PA1.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from PyQt5 import QtWidgets as qtw, QtCore, QtGui
from mainUI import Ui_MainWindow
from PyQt5.QtCore import QThread, QTimer
import sys, os, glob, PyQt5, time
from algorithms.genetic_algorithm import RunGeneticAlgorithm
from algorithms.a_star import RunAStarAlgorithm
# If the resolution is high, then these lines will take care of the UI being displayed properly.
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
folder = 'states_images'
ga_output = []
a_star_output = []
class MainWindow(qtw.QMainWindow):
"""Class that handles all the backend of the UI."""
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.index = None
print(self.index)
self.image_names = []
# Populating combobox values.
self.ui.elitismComboBox.addItem('Y')
self.ui.elitismComboBox.addItem('N')
self.ui.crossoverTypeComboBox.addItem('SP')
self.ui.crossoverTypeComboBox.addItem('TP')
self.ui.parentSelectComboBox.addItem('RS')
self.ui.parentSelectComboBox.addItem('RWS')
self.ga_thread = None
self.a_star_thread = None
self.ui.autoTrace.clicked.connect(self.run_auto_trace)
self.ui.stopAutotrace.clicked.connect(self.stop_auto_trace)
self.ui.nextButton.clicked.connect(self.show_next_image)
self.ui.prevButton.clicked.connect(self.show_prev_image)
self.ui.runAStarButton.clicked.connect(self.run_a_star)
self.ui.stopastarButton.clicked.connect(self.stop_a_star_thread)
self.ui.runGaButton.clicked.connect(self.run_genetic_algorithm)
self.ui.stopgaButton.clicked.connect(self.stop_ga_thread)
self.ui.resetButton.clicked.connect(self.reset)
self.ui.nextButton.setDisabled(True)
self.ui.prevButton.setDisabled(True)
self.ui.autoTrace.setDisabled(True)
self.ui.stopAutotrace.setDisabled(True)
os.makedirs(folder, exist_ok=True) # Creating folder to store images if it does not exist.
def run_a_star(self):
"""Function to run the A* algorithm. It uses a thread to run the algorithm in the background."""
N_queens = self.ui.nQueenSpinBox.value()
self.a_star_thread = AStarAlgorithmThread(N_queens=N_queens)
self.ui.outputLabel.setText("A Star Algorithm Running...")
self.a_star_thread.start()
self.a_star_thread.finished.connect(self.show_a_star_results)
def stop_a_star_thread(self):
"""Function to stop the algorithm's thread and enable some buttons."""
self.ui.outputLabel.setText('Searching Stopped!')
self.a_star_thread.terminate()
self.ui.nextButton.setDisabled(False)
self.ui.prevButton.setDisabled(False)
self.ui.autoTrace.setDisabled(False)
self.ui.stopAutotrace.setDisabled(False)
win.close() # Closing window as stopping on large inputs can sometimes freeze the window.
def show_a_star_results(self):
"""Function to display the output of the algorithm."""
global a_star_output
if len(a_star_output) != 0:
output = f'Steps = {a_star_output[0]} | Time = {a_star_output[1] :.4} secs | Solution Found = {a_star_output[2]}'
self.ui.outputLabel.setText(output)
self.load_images_directory()
else:
print('Output List is Empty!')
def run_genetic_algorithm(self):
"""Function to run the Genetic algorithm. It uses a thread to run the algorithm in the background."""
N_queens = self.ui.nQueenSpinBox.value()
init_pop_size = self.ui.initpopSpinBox.value()
mutation_prob = self.ui.mutprobDoubleSpinBox.value()
max_gen = self.ui.maxgenSpinBox.value()
crossover_rate = self.ui.crossoverRateDoubleSpinBox.value()
elitism = self.ui.elitismComboBox.currentText()
crossover_method = self.ui.crossoverTypeComboBox.currentText()
parent_selection_algo= self.ui.parentSelectComboBox.currentText()
self.ga_thread = GeneticAlgorithmThread(
N_queens=N_queens,
init_pop_size=init_pop_size,
mutation_prob=mutation_prob,
max_gen=max_gen,
crossover_method=crossover_method,
crossover_rate=crossover_rate,
parent_selection_algo=parent_selection_algo,
elitism=elitism
)
self.ui.outputLabel.setText("Genetic Algorithm Running...")
self.ga_thread.start()
self.ga_thread.finished.connect(self.show_ga_results)
def stop_ga_thread(self):
"""Function to stop the algorithm's thread and enable some buttons."""
self.ui.outputLabel.setText('Searching Stopped!')
self.ga_thread.terminate()
self.ui.nextButton.setDisabled(False)
self.ui.prevButton.setDisabled(False)
self.ui.autoTrace.setDisabled(False)
self.ui.stopAutotrace.setDisabled(False)
win.close() # Closing window as stopping on large inputs can sometimes freeze the window.
def show_ga_results(self):
"""Function to display the output of the algorithm."""
global ga_output
if len(ga_output) != 0:
output = f'Steps = {ga_output[0]} | Time = {ga_output[1] :.4} secs | Solution Found = {ga_output[2]}'
self.ui.outputLabel.setText(output)
self.load_images_directory()
else:
print('Output List is Empty!')
def run_auto_trace(self):
"""Function to run auto trace feature."""
self.image_names.clear()
self.load_images_directory()
self.file_it = iter(self.image_names)
self._timer = QtCore.QTimer(self, interval=300)
self._timer.timeout.connect(self._on_timeout)
self._timer.start()
def _on_timeout(self):
"""Utility timeout function for auto tracing."""
try:
img = next(self.file_it)
self.ui.imagecountLabel.setText(img)
self.ui.imageLabel.setPixmap(QtGui.QPixmap(folder+'\\'+img))
except StopIteration:
self._timer.stop()
def stop_auto_trace(self):
"""Function to stop the auto tracer."""
self._timer.stop()
def load_images_directory(self):
"""Function that loads the image names from the 'states_images' directory."""
for filename in sorted(os.listdir(folder), key=lambda f: int(f[5:-4])):
self.image_names.append(filename)
print(self.image_names)
self.ui.nextButton.setDisabled(False)
self.ui.prevButton.setDisabled(False)
self.ui.autoTrace.setDisabled(False)
self.ui.stopAutotrace.setDisabled(False)
def show_next_image(self):
"""Function to show next image in line."""
if self.index is None:
self.index = 0
img = self.image_names[self.index]
self.ui.imageLabel.setPixmap(QtGui.QPixmap(folder+'\\'+img))
self.ui.imagecountLabel.setText(img)
self.index += 1
self.index = self.index % len(self.image_names)
def show_prev_image(self):
"""Function to show previous image in line."""
self.index -= 1
self.index = self.index % len(self.image_names) # Using the property og -ve modulo in python.
img = self.image_names[self.index]
self.ui.imageLabel.setPixmap(QtGui.QPixmap(folder+'\\'+img))
self.ui.imagecountLabel.setText(img)
def reset(self):
self.ui.outputLabel.setText("Steps/Time")
self._clear_states_images_folder()
self._display_initial_text()
self.index = None
self.image_names = []
self.ui.imagecountLabel.setText('Image Count')
self.ui.nextButton.setDisabled(True)
self.ui.prevButton.setDisabled(True)
self.ui.autoTrace.setDisabled(True)
self.ui.stopAutotrace.setDisabled(True)
def _clear_states_images_folder(self):
"""Function to clear image folder and reset the instructions on the main screen."""
files = glob.glob('states_images/**/*.png', recursive=True)
for f in files:
try:
os.remove(f)
except OSError as e:
print(f"Error: {f} : {e.strerror}")
self._display_initial_text()
def _display_initial_text(self):
"""Function to display initial instructions again."""
_translate = QtCore.QCoreApplication.translate
text = '''
<html><head/><body><p align="justify"><span style=" font-size:12pt; font-weight:600; text-decoration: underline;">Instructions</span>
<span style=" font-size:12pt; font-weight:600;">:</span>
</p><p align="justify">1. Choose to run A* or Genetic algorithm at a time.</p>
<p align="justify">2. When output appears on the top label, then click <Next> button </p>
<p align="justify">to view the images (for visual tracing) or use <Auto Trace>.</p>
<p align="justify">NOTE: Check if the folder <states_images> is created or not.</p>
<p align="justify">3. Click on <RESET> button to clear <states_images> folder.</p>
<p align="justify">Then run any algorithm again.</p>
<p align="justify"><span style=" font-weight:600; text-decoration: underline;">Dictionary:</span></p>
<p align="justify">1. Crossover type - SP (Single point), TP (Two point)</p>
<p align="justify">2. Parent selection - RS (Rank selection), RWS (Roulette wheel selection)</p>
<p align="justify">3. Elitism - Y (Yes), N (No)</p><p align="justify">ENJOY THE APP!</p></body></html>
'''
self.ui.imageLabel.setText(_translate("MainWindow", text))
class AStarAlgorithmThread(QThread):
"""Class that runs the algorithm as a thread."""
def __init__(self, N_queens):
super().__init__()
self.N_queens = N_queens
def run(self):
global a_star_output
a_star_output = RunAStarAlgorithm.run_a_star(N_queens=self.N_queens)
class GeneticAlgorithmThread(QThread):
"""Class that runs the algorithm as a thread."""
def __init__(self, N_queens, init_pop_size, mutation_prob, max_gen, crossover_method, crossover_rate, parent_selection_algo, elitism):
super().__init__()
self.N_queens = N_queens
self.init_pop_size = init_pop_size
self.mutation_prob = mutation_prob
self.max_gen = max_gen
self.crossover_method = crossover_method
self.crossover_rate = crossover_rate
self.parent_selection_algo = parent_selection_algo
self.elitism = elitism
def run(self):
global ga_output
ga_output = RunGeneticAlgorithm.run_ga(
N_queens=self.N_queens,
init_pop_size=self.init_pop_size,
mutation_prob=self.mutation_prob,
max_gen=self.max_gen,
crossover_rate=self.crossover_rate,
crossover_method=self.crossover_method,
parent_selection_algo=self.parent_selection_algo,
elitism=self.elitism
)
if __name__ == '__main__':
app = qtw.QApplication([])
win = MainWindow()
win.show()
sys.exit(app.exec_())