-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
568 lines (437 loc) · 20.3 KB
/
main.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import copy
from datetime import datetime
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QTimer
from distribution_ui import Ui_Dialog
from emergence_magic import emerge_step, getSurrounding
from assign_loop_magic import assign_loop_step
from rand_loop_magic import rand_loop_step
from random_magic import random_step
from emergence_history import History
from functools import partial
import random
from random import randint
import sys
colors = ["red", "blue", "green", "yellow", "purple", "orange", "cyan", "magenta", "black"]
INIT_BLACK = 8
EMERGENCE_INDEX = 3
ASSIGN_LOOP_INDEX = 2
RAND_LOOP_INDEX = 1
RANDOM_INDEX = 0
solution_steps = {
RANDOM_INDEX: random_step, RAND_LOOP_INDEX: rand_loop_step,
ASSIGN_LOOP_INDEX: assign_loop_step, EMERGENCE_INDEX: emerge_step
}
BUTTON_INDEX = ROW_INDEX = HIST_INDEX = 0
COLOR_INDEX = COL_INDEX = LABL_INDEX = 1
ADVANCE_INTERVAL_MS = 1000
# This is the main class of the application where all functions relative to the buttons go
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.history = History()
# Make the random seed predicatble or truly random
#random.seed(time.time())
random.seed(0xBEEFCAFE)
self.numV = 2
self.ui.rows_spinBox.valueChanged.connect(self.create_new_map)
self.ui.cols_spinBox.valueChanged.connect(self.create_new_map)
self.ui.varNum_comboBox.currentIndexChanged.connect(self.change_num_vars)
self.ui.solution_comboBox.currentIndexChanged.connect(self.change_solution)
self.ui.randomOnOff_pushButton.clicked.connect(self.toggle_on_off)
self.ui.percentOff_spinBox.valueChanged.connect(self.randomize_on_off)
self.onOff = True
self.boxes : dict = {}
self.vals = []
'''
The self.boxes dictionary is formatted like so:
key -> tuple(row, column)
value -> list(button, color index)
The self.vals list is a 2d array that contains the color indices
'''
self.histogram_total = {
0: (self.ui.var1_progressBar, self.ui.varBar1_label),
1: (self.ui.var2_progressBar, self.ui.varBar2_label),
2: (self.ui.var3_progressBar, self.ui.varBar3_label),
3: (self.ui.var4_progressBar, self.ui.varBar4_label),
4: (self.ui.var5_progressBar, self.ui.varBar5_label),
5: (self.ui.var6_progressBar, self.ui.varBar6_label),
6: (self.ui.var7_progressBar, self.ui.varBar7_label),
7: (self.ui.var8_progressBar, self.ui.varBar8_label)
}
self.histogram_collisions = {
0: (self.ui.collisions1_progressBar, self.ui.collisionsBar1_label),
1: (self.ui.collisions2_progressBar, self.ui.collisionsBar2_label),
2: (self.ui.collisions3_progressBar, self.ui.collisionsBar3_label),
3: (self.ui.collisions4_progressBar, self.ui.collisionsBar4_label),
4: (self.ui.collisions5_progressBar, self.ui.collisionsBar5_label),
5: (self.ui.collisions6_progressBar, self.ui.collisionsBar6_label),
6: (self.ui.collisions7_progressBar, self.ui.collisionsBar7_label),
7: (self.ui.collisions8_progressBar, self.ui.collisionsBar8_label)
}
self.init_histograms()
self.analysis_tools_update()
self.ui.randomVars_pushButton.clicked.connect(self.randomize_vars)
self.ui.grid_slider.valueChanged.connect(self.changeGrid)
self.advanceTimer = QTimer()
self.advanceTimer.setInterval(ADVANCE_INTERVAL_MS)
self.advanceTimer.timeout.connect(self.autoStep)
self.ui.step_pushButton.clicked.connect(self.step_step)
self.ui.checkBox.clicked.connect(self.toggleAutoStep)
self.ui.next_pushButton.clicked.connect(self.step_forward)
self.ui.prev_pushButton.clicked.connect(self.step_backward)
self.ui.return_pushButton.clicked.connect(self.step_return)
self.clustered = False
self.ui.clusterOnOff_pushButton.clicked.connect(self.toggle_cluster)
self.ui.clusterPercent_comboBox.currentIndexChanged.connect(self.seed_clusters)
self.ui.clusterType_comboBox.currentIndexChanged.connect(self.seed_clusters)
self.fillBoxes(random_vars=True, new_map=True)
def clear_map(self):
for key in self.boxes:
self.boxes[key][BUTTON_INDEX].deleteLater()
self.boxes.clear()
self.vals.clear()
### Fill stuff
def fillBoxes(self, random_vars=False, new_map=False, new_vals=False):
rows = int(self.ui.rows_spinBox.value())
cols = int(self.ui.cols_spinBox.value())
if new_map:
fWidth, fHeight = 1000, 600 #self.ui.map_frame.geometry().height(), self.ui.map_frame.geometry().width()
bWidth, bHeight = int(fWidth / cols), int(fHeight / rows)
self.clear_map()
self.vals.clear()
bX, bY = 10, 10
for r in range(rows):
self.vals.append([])
for c in range(cols):
# Create a new button and place it in the frame
button = QtWidgets.QPushButton(self.ui.map_frame)
button.setGeometry(QtCore.QRect(bX, bY, bWidth - 2, bHeight - 2))
name = "button_%d_%d" % (r, c)
button.setObjectName(name)
# Create its dictionary values for self.boxes;
# Should be [row, col]: [button, color]
key_tuple = (r,c)
color_int = randint(0, self.numV-1)
color = colors[color_int]
self.vals[r].append(color_int)
button_tuple = [button, color_int]
# Add new key and value to dict; add button click action
self.boxes[key_tuple] = button_tuple
button.clicked.connect(partial(self.click_q, key_tuple))
bX += bWidth
bY += bHeight
bX = 10
if random_vars:
for key in self.boxes:
color_int = randint(0, self.numV-1)
self.boxes[key][COLOR_INDEX] = color_int
self.vals[key[ROW_INDEX]][key[COL_INDEX]] = color_int
if new_vals:
for i in range(len(self.vals)):
for j in range(len(self.vals[i])):
key = (i, j)
self.boxes[key][COLOR_INDEX] = self.vals[i][j]
for key in self.boxes:
color_int = self.boxes[key][1]
color = colors[color_int]
self.boxes[key][BUTTON_INDEX].setStyleSheet("background: " + color)
self.run_alaysis()
if random_vars or new_map:
self.history.clear()
self.history.push(self.vals)
self.step_updateButtons()
### Grid stuff - square or hexagonal
def changeGrid(self):
# Start by getting the general geometry of the boxes
# TODO THESE ARE MAGIC NUMBERS! FIX WHEN YOU CAN CHANGE MAP DIMENSIONS!
leftEdge = 10
separation = 10
if not self.ui.grid_slider.value():
# Here we want to transition to hexagonal
init_pos = int(leftEdge + (separation / 2))
else:
# This would be square grid
init_pos = int(leftEdge)
# Shift each other row right/left by ~five pixels
rows = int(self.ui.rows_spinBox.value())
cols = int(self.ui.cols_spinBox.value())
for i in range(1,rows,2):
pos = init_pos
for j in range(cols):
key = (i, j)
cur = self.boxes[key][BUTTON_INDEX].geometry().getRect()
self.boxes[key][BUTTON_INDEX].setGeometry(pos, cur[1], cur[2], cur[3])
pos += separation
'''for i in range(1,rows,2):
pos = init_pos
for j in range(10):
cur = self.boxes[i][j].geometry().getRect()
self.boxes[i][j].setGeometry(pos, cur[1], cur[2], cur[3])
pos += separation'''
self.randomize_vars()
def randomize_vars(self):
if self.ui.solution_comboBox.currentIndex() == ASSIGN_LOOP_INDEX:
color_int = 0
for key in self.boxes:
if self.boxes[key][COLOR_INDEX] != INIT_BLACK:
color = colors[color_int]
self.boxes[key][BUTTON_INDEX].setStyleSheet("background: " + color)
self.boxes[key][COLOR_INDEX] = color_int
self.vals[key[ROW_INDEX]][key[COL_INDEX]] = color_int
color_int = (color_int + 1) % self.numV
else:
self.fillBoxes(random_vars=True)
def toggle_cluster(self):
self.clustered = not self.clustered
if self.clustered:
self.ui.clusterOnOff_pushButton.setText('Disable Clustering')
self.ui.clusterType_comboBox.setEnabled(True)
self.ui.clusterPercent_comboBox.setEnabled(True)
self.seed_clusters()
else:
self.ui.clusterOnOff_pushButton.setText('Enable Clustering')
self.ui.clusterType_comboBox.setEnabled(False)
self.ui.clusterPercent_comboBox.setEnabled(False)
self.randomize_vars()
def seed_clusters(self):
# We want to ensure first off that we have a fully filled or empty map
cluster_blanks: bool = not self.ui.clusterType_comboBox.currentIndex()
if cluster_blanks:
self.randomize_vars()
else:
for key in self.boxes:
self.boxes[key][COLOR_INDEX] = INIT_BLACK
self.vals[key[ROW_INDEX]][key[COL_INDEX]] = INIT_BLACK
new_vals = copy.deepcopy(self.vals)
# Figure out how we are starting and what we are going to
initial_percent = float(float(self.ui.clusterPercent_comboBox.currentIndex() + 1) / 100.0)
final_percent = float(initial_percent * 10.0)
# Initial seeding process
total = 0.0
cluster_total = 0.0
flipped = []
for key in self.boxes:
total += 1.0
r, c = key[0], key[1]
if random.random() < initial_percent:
new_vals[r][c] = INIT_BLACK if cluster_blanks else randint(0, self.numV-1)
cluster_total += 1.0
flipped.append(key)
current_percent = (cluster_total / total)
rows = int(self.ui.rows_spinBox.value())
cols = int(self.ui.cols_spinBox.value())
grid = int(self.ui.grid_slider.value())
chance_increase = 0.4
while current_percent < final_percent:
for key in flipped:
chance_of_flipping = 0.1
r, c = key[0], key[1]
surrounding = getSurrounding(r, c, rows, cols, grid)
del surrounding[0]
for sur in surrounding:
if cluster_blanks == (new_vals[sur[0]][sur[1]] == INIT_BLACK):
chance_of_flipping += chance_increase
if random.random() < chance_of_flipping:
new_vals[r][c] = INIT_BLACK if cluster_blanks else randint(0, self.numV-1)
cluster_total += 1.0
chance_increase *= 0.75
current_percent = (cluster_total / total)
print(current_percent)
self.vals = copy.deepcopy(new_vals)
self.history.clear()
self.history.push(self.vals)
self.fillBoxes(new_vals=True)
def create_new_map(self):
self.fillBoxes(new_map=True)
def toggle_on_off(self):
if self.onOff:
self.ui.randomOnOff_pushButton.setText('Turn All On')
self.ui.percentOff_spinBox.setEnabled(True)
else:
self.ui.randomOnOff_pushButton.setText('Randomize On/Off')
self.ui.percentOff_spinBox.setEnabled(False)
self.onOff = not self.onOff
self.randomize_on_off()
def change_num_vars(self):
self.numV = int(self.ui.varNum_comboBox.currentText())
self.randomize_vars()
self.analysis_tools_update()
def randomize_on_off(self):
if not self.onOff:
for key in self.boxes:
# Chance is proportional to number of vars
color_int = INIT_BLACK \
if randint(0, 10) <= (self.ui.percentOff_spinBox.value() / 10) \
else randint(0, self.numV-1)
color = colors[color_int]
self.boxes[key][BUTTON_INDEX].setStyleSheet("background: " + color)
self.boxes[key][COLOR_INDEX] = color_int
self.vals[key[ROW_INDEX]][key[COL_INDEX]] = color_int
# Call this in case we need to keep some sort of order; lost work earlier, but we'll survive
if self.ui.solution_comboBox.currentIndex() == ASSIGN_LOOP_INDEX:
self.randomize_vars()
return
else:
for key in self.boxes:
color_int = 0
color = colors[color_int]
self.boxes[key][BUTTON_INDEX].setStyleSheet("background: " + color)
self.boxes[key][COLOR_INDEX] = color_int
self.vals[key[ROW_INDEX]][key[COL_INDEX]] = color_int
self.randomize_vars()
return
self.fillBoxes()
def change_solution(self):
if self.ui.solution_comboBox.currentIndex() == ASSIGN_LOOP_INDEX:
self.ui.randomVars_pushButton.setText('Initialize Vars')
else:
self.ui.randomVars_pushButton.setText('Randomize Vars')
if not self.onOff:
self.toggle_on_off() # Runs the process of creating the map already
else:
self.randomize_vars()
def click_q(self, num):
color_int = self.boxes[num][COLOR_INDEX]
color_int = randint(0, self.numV-1) \
if (color_int == INIT_BLACK) \
else INIT_BLACK
color = colors[color_int]
self.boxes[num][BUTTON_INDEX].setStyleSheet("background: " + color)
self.boxes[num][COLOR_INDEX] = color_int
self.vals[num[ROW_INDEX]][num[COL_INDEX]] = color_int
### MAGIC ###
###############################################################
def step_generate(self):
rows = int(self.ui.rows_spinBox.value())
cols = int(self.ui.cols_spinBox.value())
grid = int(self.ui.grid_slider.value())
step_index = self.ui.solution_comboBox.currentIndex()
step = solution_steps[step_index]
tmp = step(self.vals, rows, cols, self.numV, grid)
self.vals = copy.deepcopy(tmp)
self.history.push(self.vals)
self.fillBoxes(new_vals=True)
###############################################################
### STEP FUNCTIONS ###
###############################################################
# In all of these I either generate new data or review old data;
# this then gets displayed, and I update the buttons accordingly
### Manual steps
def step_updateButtons(self):
canMove = self.history.canStillMove()
self.ui.prev_pushButton.setEnabled(canMove[0])
self.ui.next_pushButton.setEnabled(canMove[1])
self.ui.step_pushButton.setEnabled(not canMove[1])
self.ui.return_pushButton.setEnabled(canMove[1])
def step_step(self):
self.step_generate()
self.step_updateButtons()
def step_alt(self, fn):
self.vals = copy.deepcopy(fn())
self.fillBoxes(new_vals=True)
self.step_updateButtons()
def step_forward(self):
self.step_alt(self.history.getNext)
def step_backward(self):
self.step_alt(self.history.getPrev)
def step_return(self):
self.step_alt(self.history.getReturn)
### Auto step
def autoStep(self):
self.step_generate()
def toggleAutoStep(self):
if self.ui.checkBox.isChecked():
self.advanceTimer.start()
self.ui.next_pushButton.setEnabled(False)
self.ui.prev_pushButton.setEnabled(False)
self.ui.step_pushButton.setEnabled(False)
else:
self.advanceTimer.stop()
self.ui.prev_pushButton.setEnabled(True)
self.ui.next_pushButton.setEnabled(False)
self.ui.step_pushButton.setEnabled(True)
###############################################################
### ANALYSIS FUNCTIONS ###
###############################################################
# In all of these I am manipulating the widgets and math used for
# analyzing how well-distributed everything is
def run_alaysis(self):
self.analyze_totals_histogram()
self.analyze_collisions_histogram()
def analysis_tools_update(self):
# Start with totals histogram
for i in range(8):
self.histogram_total[i][HIST_INDEX].setEnabled(i < self.numV)
self.histogram_collisions[i][HIST_INDEX].setEnabled(i < self.numV)
if i >= self.numV:
self.histogram_total[i][HIST_INDEX].setValue(0)
self.histogram_total[i][LABL_INDEX].setText('--%')
self.histogram_collisions[i][HIST_INDEX].setValue(0)
self.histogram_collisions[i][LABL_INDEX].setText('--%')
total_line_goal = int(103 - (100 / self.numV))
self.ui.totalGoal_line.setGeometry(20, total_line_goal, 431, 16)
self.ui.totalGoal_label.setText("Goal: " + str(int(100 / self.numV)) + "%")
def init_histograms(self):
for i in range(8):
color = colors[i]
self.histogram_total[i][HIST_INDEX].setStyleSheet(
"QProgressBar::chunk {background-color: " + color + ";}"
)
self.histogram_collisions[i][HIST_INDEX].setStyleSheet(
"QProgressBar::chunk {background-color: " + color + ";}"
)
## Totals histogram - update function
def analyze_totals_histogram(self):
each, total = self.analyze_totals()
for i in range(0, self.numV):
self.histogram_total[i][HIST_INDEX].setValue(int(each[i] / total * 100))
self.histogram_total[i][LABL_INDEX].setText(str(int(each[i] / total * 100)) + "%")
def analyze_collisions_histogram(self):
each, total = self.analyze_collisions()
for i in range(0, self.numV):
self.histogram_collisions[i][HIST_INDEX].setValue(int(each[i] / total[i] * 100))
self.histogram_collisions[i][LABL_INDEX].setText(str(int(each[i] / total[i] * 100)) + "%")
def analyze_collisions(self):
rows = int(self.ui.rows_spinBox.value())
cols = int(self.ui.cols_spinBox.value())
grid = int(self.ui.grid_slider.value())
each = [0 for i in range(8)]
total = [0 for i in range(8)]
for key in self.boxes:
# Skip a sqaure if it's black
if self.boxes[key][COLOR_INDEX] == INIT_BLACK:
continue
r, c = key[ROW_INDEX], key[COL_INDEX]
surrounding = getSurrounding(r, c, rows, cols, grid)
del surrounding[0]
# We now have a list of neighboring boxes by coord tuples
for pair in surrounding:
# Add to the total number of neighbors
total[self.boxes[key][COLOR_INDEX]] += 1
if self.boxes[pair][COLOR_INDEX] == self.boxes[key][COLOR_INDEX]:
each[self.boxes[key][COLOR_INDEX]] += 1
return each, total
def analyze_totals(self):
each = [0 for i in range(8)]
total = 0
for key in self.boxes:
color_int = self.boxes[key][COLOR_INDEX]
if color_int == INIT_BLACK:
continue
else:
total += 1
each[color_int] += 1
return each, total
###############################################################
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
ret = app.exec_()
#application.check_signOut()
sys.exit(ret)
if __name__ == "__main__":
main()