-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem.py
226 lines (194 loc) · 7.1 KB
/
problem.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
import logging
import math
import random
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import pickle
class RandomProblem:
def __init__(self, w_max, w_min, h_max, h_min, l, pin_density, obs_density):
self.pending_pins = None
if l == 0:
return
self.max_x = random.randint(w_min, w_max)
self.max_y = random.randint(h_min, h_max)
area = self.max_x * self.max_y / 10000
pin_num = int(area * pin_density)
obs_num = int(area / 9 * obs_density)
logging.info('RandomProblem: max_x: %d, max_y: %d, area: %d, pin_num: %d, obs_num: %d' % (
self.max_x, self.max_y, area, pin_num, obs_num))
assert pin_density + obs_density <= 0.5
self.layer_num = l
self.min_x = 0
self.min_y = 0
self.grid = 2
self.clearance = 1
self.line_width = 1
self.via_radius = 1
self.padstacks = None
self.generate_padstack()
self.pins = None
self.generate_pins(pin_num, obs_num)
self.nets = None
self.generate_nets()
def generate_nets(self):
self.nets = []
# divide pins into nets
index = 0
logging.info('RandomProblem: generate_nets: pin_num: %d' % len(self.pending_pins))
while True:
if len(self.pending_pins) < 2:
break
net = {
'index': index,
'pins': [],
'name': 'net' + str(index),
'net_id': index
}
pin_num = 2
for i in range(pin_num):
pin_id = random.choice(self.pending_pins)
net['pins'].append(pin_id)
self.pending_pins.remove(pin_id)
self.pins[pin_id]['net_id'] = net['index']
self.nets.append(net)
index += 1
def generate_pins(self, pin_num, obs_num=10):
self.pins = []
self.pending_pins = []
generate_num = 0
generate_pin_num = 0
generate_obs_num = 0
i = 0
while len(self.pins) < pin_num + obs_num:
generate_num += 1
if generate_pin_num < pin_num:
t = 'PIN'
else:
t = 'OBS'
pin = {
'id': str(i),
'shape': random.choice(['CIRCLE', 'RECT']) if t == 'PIN' else 'OBS_CIRCLE',
'x': random.randint(0, self.max_x),
'y': random.randint(0, self.max_y),
'index': str(i),
'type': t,
}
pin['x_int'] = round(pin['x'])
pin['y_int'] = round(pin['y'])
counter_flag = False
for _pin in self.pins:
min_distance = 100
if _pin['type'] == 'OBS': min_distance += 200
if t == 'OBS': min_distance += 200
if math.hypot(_pin['x_int'] - pin['x_int'], _pin['y_int'] - pin['y_int']) < min_distance:
counter_flag = True
break
if not counter_flag:
self.pins.append(pin)
if t == 'PIN':
self.pending_pins.append(i)
generate_pin_num += 1
else:
generate_obs_num += 1
i += 1
if generate_num > 1000:
logging.warning('generate pins failed in 1000 times')
break
def generate_padstack(self):
self.padstacks = {
'CIRCLE': {},
'RECT': {},
'OBS_CIRCLE': {},
}
for i in range(1, 1 + self.layer_num):
self.padstacks['CIRCLE'][F'{i}'] = {'shape': 'circle', 'detail': [80]}
self.padstacks['RECT'][F'{i}'] = {'shape': 'polygon', 'detail': [0, -28, 24, 28, 24, 28, -24, -28, -24]}
self.padstacks['OBS_CIRCLE'][F'{i}'] = {'shape': 'circle', 'detail': [80 * 3]}
def save(self, path):
with open(path, 'wb') as f:
pickle.dump(self, f)
@staticmethod
def load(path):
with open(path, 'rb') as f:
self = pickle.load(f)
return self
class Problem:
def __init__(self, resolver):
boundary = resolver.boundary
pins = resolver.pins
connections = resolver.connections
self.padstacks = resolver.padstacks
self.min_x = round(boundary['min_x'])
self.min_y = round(boundary['min_y'])
self.max_x = round(boundary['max_x'])
self.max_y = round(boundary['max_y'])
self.grid = resolver.grid
self.clearance = resolver.clearance
self.line_width = resolver.line_width
self.via_radius = 1
via_names = None
for data in resolver.structure_data:
if list(data.keys())[0] == 'via':
via_names = list(data.values())[0].split()
for via_name in via_names:
patstack = self.padstacks[via_name]
for k, v in patstack.items():
if v['detail'][0] < self.via_radius or self.via_radius == 1:
self.via_radius = v['detail'][0]
self.x_offset = 0
self.y_offset = 0
if self.min_x != 0:
self.x_offset = -self.min_x
self.min_x = 0
self.max_x = self.max_x + self.x_offset
if self.min_y != 0:
self.y_offset = -self.min_y
self.min_y = 0
self.max_y = self.max_y + self.y_offset
self.layer_num = len(boundary['layer'])
self.board = np.zeros((self.max_x + 1, self.max_y + 1, self.layer_num), dtype=int)
self.pins = list(pins.values())
for pin in self.pins:
pin['x_int'] += self.x_offset
pin['y_int'] += self.y_offset
self.pins_dict = {}
for i in range(len(self.pins)):
self.pins[i]['index'] = i
self.pins_dict[self.pins[i]['id']] = self.pins[i]
self.nets = []
i = 0
for k, v in connections.items():
_pins = v
_pins = [self.pins_dict[ii]['index'] for ii in _pins]
d = {
'name': k,
'pins': _pins,
'index': i
}
for pin in _pins:
self.pins[pin]['net_id'] = i
self.nets.append(d)
i += 1
pass
def get_location(self, x, y):
return tuple([(x + self.x_offset), (y + self.y_offset)])
def show(self, layer_id):
img = np.zeros(((self.max_x + 1), (self.max_y + 1), 3), dtype=np.uint8)
for pin in self.pins:
x, y = self.get_location(pin['x_int'], pin['y_int'])
print(pin['x_int'], pin['y_int'], x, y)
img[x - 5:x + 5, y - 5:y + 5] = (0, 0, 255)
# cv2.circle(img, (y, x), 10, (0, 0, 255), 10)
for net in self.nets:
pass
cv2.imwrite('img.jpg', img)
# cv2.imshow('img', img)
# plt.figure("Image") # 图像窗口名称
# plt.imshow(img)
# plt.axis('on') # 关掉坐标轴为 off
# plt.title('image') # 图像题目
#
# # 必须有这个,要不然无法显示
# plt.show()