-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_routing_hadlock's_algo.py
378 lines (227 loc) · 9.47 KB
/
maze_routing_hadlock's_algo.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
#!/usr/bin/env python
# coding: utf-8
# # MAZE ROUTING - HADLOCK'S ALGORITM
#
# In[37]:
get_ipython().system('pip install pygame')
# In[38]:
import pygame
from pygame.locals import *
from queue import PriorityQueue
#pygame.init()
# Class node which corresponds to single rectangle in a grid, holds the node(pixel) information like its dimensions,color and its neighbors.
# In[39]:
class node:
def __init__(self, x, y, gap):
self.row = x
self.col = y
self.width= gap
self.color=(255, 255, 255)
self.neighbors=[]
def get_pos(self):
return self.row//self.width, self.col//self.width
def make_block(self):
self.color=pygame.Color('cyan')
def is_barrier(self):
return (self.color == pygame.Color('cyan') or self.color ==pygame.Color("yellow"))
def is_open(self):
return self.color == pygame.Color('white')
def draw(self):
pygame.draw.rect(WIN, self.color, (self.row, self.col, self.width, self.width))
def addtext(self, text_):
pygame.font.init()
font = pygame.font.Font('freesansbold.ttf', 10)
text = font.render(text_, True, (0,0,0), self.color)
WIN.blit(text, (self.row, self.col, self.width+self.row, self.col+self.width))
pygame.display.update()
# Class grid_: It forms connection between grid lines and nodes which helps in accessing all nodes
# In[40]:
class grid_:
def __init__(self, rows, width):
self.gap = width//rows
self.rows= rows
self.width = width
self.gap = self.width // self.rows
def form(self):
grid= []
for i in range(0,self.rows):
grid.append([])
for j in range(0, self.rows):
grid[i].append(node(i*self.gap,j*self.gap, self.gap))
return grid
# Some parameters that can be varied to change pygame screen settings
# In[41]:
WIDTH= 600 ## WIDTH of pygame GUI screen
rows_=40 ## Number of rows in grid
axis=3 ##Setting axis so that output appears at the centre of pygame screen
# Module create_block : Given coordinates and dimensions of block, colors rectangles inside that block so that they can be identified as obstacles or barrier.
# In[42]:
def create_block(x, y, w, h, gap, grid):
for i in list(range(x+axis, (x+w+axis))):
for j in list(range(y+axis, y+h+axis)):
grid[i][j].make_block()
# Module draw_grid : Draws grid lines on pygame screen
# In[43]:
def draw_grid(win, rows, width):
gap = width // rows
for i in range(rows):
pygame.draw.line(win, pygame.Color('GREY'), (0, i * gap), (width, i * gap))
for j in range(rows):
pygame.draw.line(win, pygame.Color('GREY'), (j * gap, 0), (j * gap, width))
# draw : Very important module, it displays all blocks, pins and path.
# In[44]:
def draw(win, grid, rows, width):
for row in range(0, rows):
for spot in range(0,rows):
grid[row][spot].draw()
draw_grid(win, rows, width)
pygame.display.update()
# create_pins : Adds identification color to pins (i.e, nodes that acts as pins )
# In[45]:
def create_pins(x1,y1,x2,y2,grid,i):
start=grid[x1+axis][y1+axis]
grid[x1+axis][y1+axis].color = pygame.Color('green')
end = grid[x2+axis][ y2+axis]
grid[x2+axis][y2+axis].color = pygame.Color('green')
return start, end
# update_neighbors : Updates each neighbor, giving information whether its neighbors are blocks or a node which is part of already existing path.
# In[46]:
def update_neighbors(row,col, grid):
neighbors = []
if (0 < row < len(grid[0])) and not grid[row + 1][col].is_barrier(): # DOWN
neighbors.append(grid[row + 1][col])
if row > 0 and not grid[row - 1][col].is_barrier(): #UP
neighbors.append(grid[row - 1][col])
if 0<col < len(grid[0]) and not grid[row][col + 1].is_barrier(): #RIGHT
neighbors.append(grid[row][col + 1])
if col > 0 and not grid[row][col - 1].is_barrier(): #LEFT
neighbors.append(grid[row][col - 1])
return neighbors
# ALGORITHM MODULE AND ITS HELPER MODULES
# In[47]:
def h(p1, p2):
x1, y1 = p1
x2, y2 = p2
return abs(x1 - x2) + abs(y1 - y2)
def reconstruct_path(came_from, current, draw):
color = pygame.Color("yellow")
wirelength=0
while current in came_from:
current = came_from[current]
current.color = color
wirelength+=1
draw()
return wirelength
def algorithm(draw, grid, start, end):
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
came_from = {};
g_score = {spot: float("inf") for row in grid for spot in row}
g_score[start] = 0
f_score = {spot: float("inf") for row in grid for spot in row}
f_score[start] = h(start.get_pos(), end.get_pos())
wirelength=0
open_set_hash = {start}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
x, y = current.get_pos()
neighbors=[]
current.neighbors = update_neighbors(x, y, grid)
open_set_hash.remove(current)
if current == end:
end.color = pygame.Color("yellow")
wirelen=reconstruct_path(came_from, end, draw)
return wirelen, came_from
for neighbor in current.neighbors:
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + h(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_hash:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
draw()
# return 0, 0
# In[48]:
WIN = pygame.display.set_mode((WIDTH, WIDTH), pygame.RESIZABLE)
pygame.display.set_caption("Hadlock's Routing Algorithm")
WIN.fill(pygame.Color('WHITE'))
def main(win):
rows=rows_;
gri = grid_(rows, WIDTH)
grid = gri.form()
gap = WIDTH//rows
draw(win, grid, rows, WIDTH)
#############################################################################
## Given test data inputs of blocks ##
## Can modify blocks or pins ##
#############################################################################
blocks = [[1, 0, 0, 7, 9],
[2, 11,8, 6, 8],
[3, 0, 23, 16, 6]]
#[4, 20,20,7,8]]
pins = [[1, 7, 0, 11, 12],
[2, 14, 8, 1, 9],
[3, 1, 23, 16, 24],
[4, 14, 16, 8, 29],
[5, 1, 29, 17, 12]]
#[6, 21,20, 27,27]
#############################################################################
######################### Creating Blocks on Pygame Screen ########################
for i in range(len(blocks)):
create_block(blocks[i][1], blocks[i][2], blocks[i][3], blocks[i][4], gap, grid)
draw(win, grid, rows, WIDTH)
######################### Analysing Pin Information ###############################
start =[None]*len(pins)
end = [None]*len(pins)
for i in range(len(pins)):
start[i], end[i] = create_pins(pins[i][1],pins[i][2], pins[i][3], pins[i][4], grid, i)
draw(win, grid, rows, WIDTH)
wirelen =[None]*len(pins)
wirelength =[None]*len(pins)
path={}
k={}
######################## Calling algorithm to form path ###########################
for i in range(len(pins)):
wirelength[i], path[i]=algorithm(lambda: draw(win, grid, rows, WIDTH), grid, start[i], end[i])
draw(win, grid, rows, WIDTH)
##################### NAMING BLOCK ID'S, PIN ID'S AND NET ID'S ##################################
for i in range(len(blocks)):
grid[blocks[i][1]+axis+(blocks[i][3]//2)][blocks[i][2]+axis+(blocks[i][4]//2)].addtext('B'+str(blocks[i][0])+' ('+str(blocks[i][3])+','+str( blocks[i][4])+')')
for i in range(len(pins)):
current=end[i]
while current in path[i]:
current=path[i][current]
current.addtext('N'+str(pins[i][0]))
for i in range(len(pins)):
start[i].color = pygame.Color('green')
end[i].color = pygame.Color('green')
start[i].draw()
end[i].draw()
start[i].addtext('P'+str(pins[i][0]))
end[i].addtext('P'+str(pins[i][0]))
print ('Pin Ids : Netlength' )
for i in range(len(pins)):
print( str(pins[i][0])+' : '+str(wirelength[i])+' units')
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.quit()
main(WIN)
# In[ ]:
#
# NOTE: Pygame display has (0,0) at top right corner.
#
# I have set axis=3 which shifts blocks to 3 units right and 3 units down. This is done to observe blocks at center of display.
#
#
# In[ ]:
# In[ ]: