-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-bcu-pesos nas arestas.py
422 lines (323 loc) · 13.7 KB
/
3-bcu-pesos nas arestas.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
""" UCS - Uniform Cost Search algorithm with a representation using pygame """
# -----------------------------------------------------------------------------
import pygame
import sys
import random
import numpy as np
from tkinter import messagebox, Tk
dimension = (width, height) = 640, 480
pygame.init()
window = pygame.display.set_mode(dimension)
window_title = "Uniform Cost Search - UCS - Busca de Custo Uniforme"
clock = pygame.time.Clock()
# -----------------------------------------------------------------------------
# GLOBAL VARIABLES
# -----------------------------------------------------------------------------
# columns, rows = 64, 48
columns, rows = 32, 24
cell_width = width//columns
cell_height = height//rows
grid = []
start_cell = []
end_cell = []
to_visit = []
visited = []
path = []
add_weights = False
add_weight_to = None
# -----------------------------------------------------------------------------
# GLOBAL MODIFICATIONS ATRTIBUTES
# -----------------------------------------------------------------------------
# Defines whether a random wall will be generated or not
set_random_wall = False # True or False
# Defines whether the start_cell is at coordinates [0,0] or randomly generated coordinates
set_random_start_cell = False # True or False
# Defines if the end cell is at predefined or randomly generated coordinates
set_random_end_cell = False # True or False
# Defines whether weights are generated randomly or not
set_random_weights = True # True or False
# If set to True, no weight will be assigned to edges
set_weights_as_zero = True # True or False
# -----------------------------------------------------------------------------
# CLASSE
# -----------------------------------------------------------------------------
class Cell:
def __init__(self, x, y):
self.x = x
self.y = y
self.wall = False
self.previous = None
self.neighbors = []
self.right = None
self.down = None
self.left = None
self.up = None
self.right_weight = 0
self.down_weight = 0
self.left_weight = 0
self.up_weight = 0
self.distance = 0
if set_random_wall and random.randint(0,100) < 20:
self.wall = True
def show(self, window, color, shape='rect'):
# Font=pygame.font.SysFont('timesnewroman', 12)
# if self.right:
# rw = Font.render(str(self.right_weight), True, (0,0,0))
# window.blit(rw, (self.x*cell_width+cell_width//2, self.y*cell_height+cell_height//2))
# if self.down:
# dw = Font.render(str(self.down_weight), True, (0,0,0))
# window.blit(dw, (self.x*cell_width+cell_width//2, self.y*cell_height+cell_height//2))
if self.wall:
color = (0, 0, 0)
if shape == 'rect':
pygame.draw.rect(window, color, (self.x*cell_width, self.y*cell_height, cell_width-1, cell_height-1))
else:
pygame.draw.circle(window, color, (self.x*cell_width+cell_width//2, self.y*cell_height+cell_height//2), cell_width//3)
def get_right_neighbor(self, grid):
return grid[self.x+1][self.y] if self.x < columns - 1 else None
def get_down_neighbor(self, grid):
return grid[self.x][self.y+1] if self.y < rows - 1 else None
def get_left_neighbor(self, grid):
return grid[self.x-1][self.y] if self.x > 0 else None
def get_up_neighbor(self, grid):
return grid[self.x][self.y-1] if self.y > 0 else None
def add_neighbors(self, grid):
# Add neighbors at right, down, left, top respectively
self.right = self.get_right_neighbor(grid)
self.down = self.get_down_neighbor(grid)
self.left = self.get_left_neighbor(grid)
self.up = self.get_up_neighbor(grid)
def get_neighbors(self):
if self.up:
self.neighbors.append(self.up)
if self.left:
self.neighbors.append(self.left)
if self.down:
self.neighbors.append(self.down)
if self.right:
self.neighbors.append(self.right)
# -----------------------------------------------------------------------------
# FUNCTIONS
# -----------------------------------------------------------------------------
def get_random_weights(columns_, rows_):
return np.random.randint(1, 10, size=(columns_, rows_))
def get_static_weights(columns_, rows_):
np.random.seed(1)
return np.random.randint(1, 10, size=(columns_, rows_))
def wall_manager(position, set_wall):
# global grid
c = position[0] // cell_width
r = position[1] // cell_height
grid[c][r].wall = set_wall
def set_title(paused):
# global window_title
if paused:
pygame.display.set_caption("[PAUSED] " + window_title)
else:
pygame.display.set_caption(window_title)
def create_grid():
global grid
for c in range(columns):
arr = []
for r in range(rows):
arr.append(Cell(c, r))
grid.append(arr)
def add_neighbors_to_cell():
global grid
for c in range(columns):
for r in range(rows):
grid[c][r].add_neighbors(grid)
def define_start_and_end_cell():
global start_cell, end_cell
if set_random_start_cell:
start_cell = grid[random.randint(0, columns-1)][random.randint(0, rows-1)]
else:
start_cell = grid[0][0]
if set_random_end_cell:
end_cell = grid[random.randint(0, columns-1)][random.randint(0, rows-1)]
else:
end_cell = grid[columns - (columns//3) + (columns%5)][rows - (rows//8) + (rows%3)]
start_cell.wall = False
end_cell.wall = False
start_cell.distance = 0
to_visit.append(start_cell)
def get_weights():
global grid
weights_ = []
if set_random_weights:
weights_ = get_random_weights(columns_=columns, rows_=(2*rows))
else:
weights_ = get_static_weights(columns_=columns, rows_=(2*rows))
for c in range(columns):
for r in range(rows):
if not grid[c][r].right:
if grid[c][r].down:
grid[c][r].down_weight = weights_[c][(2*r)+1]
grid[c][r].down.up_weight = weights_[c][(2*r)+1]
elif not grid[c][r].down:
if grid[c][r].right:
grid[c][r].right_weight = weights_[c][2*r]
grid[c][r].right.left_weight = weights_[c][2*r]
else:
grid[c][r].right_weight = weights_[c][2*r]
grid[c][r].down_weight = weights_[c][(2*r)+1]
grid[c][r].right.left_weight = weights_[c][2*r]
grid[c][r].down.up_weight = weights_[c][(2*r)+1]
def add_weight_to_cell(position):
global add_weight_to, grid
c = position[0] // cell_width
r = position[1] // cell_height
if add_weight_to == pygame.K_RIGHT:
if grid[c][r].right:
grid[c][r].right_weight += 1
grid[c][r].right.left_weight += 1
elif add_weight_to == pygame.K_DOWN:
if grid[c][r].down:
grid[c][r].down_weight += 1
grid[c][r].down.up_weight += 1
elif add_weight_to == pygame.K_LEFT:
if grid[c][r].left:
grid[c][r].left_weight += 1
grid[c][r].left.right_weight += 1
elif add_weight_to == pygame.K_UP:
if grid[c][r].up:
grid[c][r].up_weight += 1
grid[c][r].up.down_weight += 1
def get_edge_weight(current_cell, cn):
if current_cell.right == cn:
return current_cell.right_weight
elif current_cell.down == cn:
return current_cell.down_weight
elif current_cell.left == cn:
return current_cell.left_weight
elif current_cell.up == cn:
return current_cell.up_weight
def close():
pygame.quit()
sys.exit()
def main():
background_color = (118, 54, 38)
start_color = (50, 255, 50)
to_visit_color = (144, 175, 197)
visited_color = (51, 107, 135)
path_color = (75, 135, 165)
path_circle_color = (42, 49, 50)
end_color = (255, 50, 50)
running = False
flag = False
noFlag = True
paused = False
global add_weights, add_weight_to, grid
set_title(paused)
create_grid()
add_neighbors_to_cell()
if not set_weights_as_zero:
get_weights()
define_start_and_end_cell()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
elif event.type == pygame.MOUSEBUTTONDOWN:
if add_weights and event.button == 1:
add_weight_to_cell(pygame.mouse.get_pos())
elif event.button in (1, 3):
wall_manager(pygame.mouse.get_pos(), event.button==1)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] or event.buttons[2]:
wall_manager(pygame.mouse.get_pos(), event.buttons[0])
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
close()
elif event.key == pygame.K_RETURN:
add_weights = False
running = True
elif event.key == pygame.K_SPACE:
paused = not paused
set_title(paused)
elif not running and event.key in (pygame.K_RIGHT, pygame.K_DOWN, pygame.K_LEFT, pygame.K_UP):
if event.key != add_weight_to:
add_weights = True
add_weight_to = event.key
else:
add_weights = False
add_weight_to = None
if running and not paused:
if len(to_visit) > 0:
current_cell = to_visit.pop(0)
if current_cell == end_cell:
temporary = current_cell
path_nodes = 0
while temporary.previous:
path.append(temporary.previous)
path_nodes += 1
# print(get_edge_weight(temporary, temporary.previous))
temporary = temporary.previous
if not flag:
# for t in to_visit:
# print(t.distance)
flag = True
Tk().wm_withdraw()
messagebox.showinfo("Solution Found", "Solution was found!\nPath length: " + str(path_nodes) + "\nTotal Path Distance: " + str(end_cell.distance))
elif flag:
continue
if flag == False:
visited.append(current_cell)
current_cell.get_neighbors()
for cn in current_cell.neighbors:
# print('r', cn.right_weight) if cn.right_weight > 0 else None
# print('l', cn.left_weight) if cn.left_weight > 0 else None
if cn.wall:
continue
if cn not in visited and cn not in to_visit:
cn.previous = current_cell
cn.distance = current_cell.distance + get_edge_weight(current_cell, cn)
# print('- ',get_edge_weight(current_cell, cn)) if get_edge_weight(current_cell, cn) > 0 else None
to_visit.append(cn)
elif cn in to_visit:
temp_cn_dist = current_cell.distance + get_edge_weight(current_cell, cn)
cn_in_visited = to_visit[to_visit.index(cn)]
if temp_cn_dist < cn_in_visited.distance:
cn.parent = current_cell
cn.distance = temp_cn_dist
# print(cn.distance)
to_visit.remove(cn_in_visited)
to_visit.append(cn)
# print(' - ', cn.distance) if cn.distance > 0 else None
to_visit.sort(key=lambda x: x.distance)
# print(to_visit[0].distance) if to_visit[0].distance > 0 else None
# for t in to_visit:
# print(t.distance) if t.distance > 0 else None
else:
if not flag and noFlag:
Tk().wm_withdraw()
messagebox.showinfo("No Solution Found", "There was no solution!")
noFlag = False
else:
continue
window.fill(background_color)
for c in range(columns):
for r in range(rows):
cell = grid[c][r]
cell.show(window, to_visit_color)
if cell in path:
cell.show(window, path_color)
cell.show(window, path_circle_color, 'circle')
elif cell in visited:
cell.show(window, visited_color)
if cell in to_visit and not flag:
cell.show(window, to_visit_color)
cell.show(window, visited_color, 'circle')
if cell == start_cell:
cell.show(window, start_color)
if cell == end_cell:
cell.show(window, end_color)
clock.tick(60)
pygame.display.update()
if __name__ == '__main__':
main()
# Code based on:
# https://sites.icmc.usp.br/sandra/G2_t2/Busca.html