-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspicega.py
341 lines (309 loc) · 15.4 KB
/
spicega.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
#!/usr/bin/env python3
#coding=utf-8
import sys
import os
import numpy, random, math
import traceback
import time, datetime
## Spice logger is disabled
import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging(config_file="logging.yml")
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit.Units import *
import matplotlib.pyplot as plt
try:
import networkx
except:
print("Networkx library not installed. No graph will be produced")
from PySpice.Spice.Library import SpiceLibrary
from PySpice.Plot.BodeDiagram import bode_diagram
from PySpice.Spice.Netlist import Circuit
import deap
from deap import base, tools, creator
import values
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
def mkschematic(ind, nnodes, nodelist, spice_library):
circuit = Circuit('generated circuit')
circuit.V('vcc', 'vcc', circuit.gnd, '5V')
circuit.V('vdd', 'vdd', circuit.gnd, '-5V')
circuit.Sinusoidal('input', 'vin', circuit.gnd, amplitude=5)
nodes = 0
for i in range(nnodes):
if ind[5 * i] == 1:
circuit.R(nodes,
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12R[ind[5 * i + 1]])
elif ind[5 * i] == 2:
circuit.C(nodes,
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12C[ind[5 * i + 1]])
elif ind[5 * i] == 3:
circuit.include(spice_library['2n2222a'])
circuit.BJT(nodes,
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
nodelist[ind[5 * i + 4]] if ind[5 * i + 4] != 0 else circuit.gnd,
'2n2222a')
elif ind[5 * i] == 4:
circuit.include(spice_library['2n2907'])
circuit.BJT(nodes,
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
nodelist[ind[5 * i + 4]] if ind[5 * i + 4] != 0 else circuit.gnd,
'2n2907')
elif ind[5 * i] == 6:
circuit.include(spice_library['1N4148'])
circuit.X('D{}'.format(nodes),
'1N4148',
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd)
elif ind[5 * i] == 5:
circuit.L(nodes,
nodelist[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
nodelist[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12I[ind[5 * i + 1]])
elif ind[5 * i] == 0:
continue
else:
continue
nodes += 1
print(circuit)
def mkattr(t, value=0, diverg=5):
if value == 0:
if t == 1:
return random.randint(0, len(values.E12R) - 1)
if t == 2:
return random.randint(0, len(values.E12C) - 1)
if t == 5:
return random.randint(0, len(values.E12I) - 1)
if t == 3 or t == 4 or t == 6:
return 150
return 0
if t == 1:
ndiverg = (diverg % value) + 1
diverg = diverg if (diverg + value) < len(values.E12R) else diverg - (diverg + value - len(values.E12R) + 1)
return value + random.randint(-ndiverg, diverg)
if t == 2:
ndiverg = (diverg % value) + 1
diverg = diverg if (diverg + value) < len(values.E12C) else diverg - (diverg + value - len(values.E12C) + 1)
return value + random.randint(-ndiverg, diverg)
if t == 5:
ndiverg = (diverg % value) + 1
diverg = diverg if (diverg + value) < len(values.E12I) else diverg - (diverg + value - len(values.E12I) + 1)
return value + random.randint(-ndiverg, diverg)
if t == 3 or t == 4 or t == 6:
return 150
return 0
def mkchromosome(toolbox):
t = toolbox.attr_type()
return [t, mkattr(t), toolbox.attr_node(), toolbox.attr_node(), toolbox.attr_node()]
def mkindividual(container, toolbox, n=1):
l = []
for _ in range(n):
l += mkchromosome(toolbox)
return container(l)
class SpiceGA:
def __init__(self, toolbox, elemlist, nodelist, spice_library, ngen=20, popsize=50, crossoverpb=.9, mutationpb=.15, n_nodes=8):
self.toolbox = toolbox
self.toolbox.register("attr_type", random.choice, elemlist) # type
self.toolbox.register("attr_node", random.choice, [i for i in nodelist.keys()]) # node key in NODELIST
self.toolbox.register("mutate", self.mutate)
self.toolbox.register("mate", self.mate)
self.toolbox.register("select", tools.selTournament)
self.toolbox.register("selectelits", tools.selBest)
self.spice_library = spice_library
self.toolbox.register("evaluate", self.generate_and_test, None)
self.history = deap.tools.History()
self.toolbox.decorate("mate", self.history.decorator)
self.toolbox.decorate("mutate", self.history.decorator)
self.toolbox.register("individual", mkindividual, creator.Individual, self.toolbox, n=n_nodes)
self.toolbox.register("population", tools.initRepeat, list, toolbox.individual)
self.NGEN = ngen
self.NODELIST = nodelist
self.POPSIZE = popsize
self.N_NODES = n_nodes
self.CXPB = crossoverpb
self.MUTPB = mutationpb
self.statistics = deap.tools.Statistics()
self.s = {'counter':1, 'pop':{}}
self.hof = deap.tools.HallOfFame(maxsize=5)
def mutate (self, i1):
x = 50
y = 60
for i in range(self.N_NODES):
if random.randint(0, 100) > 40:
k = i1[i * 5 + 0]
i1[i * 5 + 0] = self.toolbox.attr_type()
i1[i * 5 + 1] = mkattr(i1[i * 5])
if random.randint(0, 100) > 30:
i1[i * 5 + 2] = self.toolbox.attr_node()
if random.randint(0, 100) > 30:
i1[i * 5 + 3] = self.toolbox.attr_node()
if random.randint(0, 100) > 20:
i1[i * 5 + 1] = mkattr(i1[i * 5], value=i1[i * 5 + 1], diverg=10)
for i in range(self.N_NODES):
if random.randint(0, 100) > 60:
target = random.randint(0, self.N_NODES - 1)
for j in range(5):
holder = i1[i * 5 + j]
i1[target * 5 + j] = i1[i * 5 +j]
i1[i * 5 +j] = holder
return (i1,)
def mate(self, i1, i2):
holder = 0.
holder2 = 0
chooser = [i1, i2]
for i in range(self.N_NODES):
if random.randint(0, 100) > 60:
holder = random.choice(chooser)
i2[i * 5 + 0] = holder[i * 5 + 0]
i1[i * 5 + 0] = holder[i * 5 + 0]
i2[i * 5 + 1] = holder[i * 5 + 1]
i1[i * 5 + 1] = holder[i * 5 + 1]
if i2[i * 5] == i1[i * 5] and random.randint(0, 100) > 60:
i1[i * 5 + 1] = mkattr(i2[i * 5], value=math.floor(sum([i1[i * 5 + 1], i2[i * 5 + 1]]) / 2), diverg=int(abs(i1[i * 5 + 1] - i2[i * 5 + 1]) / 2))
i2[i * 5 + 1] = mkattr(i2[i * 5], value=math.floor(sum([i1[i * 5 + 1], i2[i * 5 + 1]]) / 2), diverg=int(abs(i1[i * 5 + 1] - i2[i * 5 + 1]) / 2))
i2[i * 5 + 2] = random.choice(chooser)[i * 5 + 2]
i1[i * 5 + 2] = random.choice(chooser)[i * 5 + 2]
i2[i * 5 + 3] = random.choice(chooser)[i * 5 + 3]
i1[i * 5 + 3] = random.choice(chooser)[i * 5 + 3]
i2[i * 5 + 4] = random.choice(chooser)[i * 5 + 4]
i1[i * 5 + 4] = random.choice(chooser)[i * 5 + 4]
return (i1, i2)
def generate_and_test(self, gui, ind):
circuit = Circuit('generated circuit')
sys.stdout.write(' {:.1%}%\b\r'.format(self.GENCOUNTER/ self.POPSIZE))
sys.stdout.flush()
circuit.V('vcc', 'vcc', circuit.gnd, '5V')
circuit.V('vdd', 'vdd', circuit.gnd, '-5V')
circuit.Sinusoidal('input', 'vin', circuit.gnd, amplitude=2)
nodes = 0
try:
for i in range(self.N_NODES):
if ind[5 * i] == 1:
circuit.R(nodes,
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12R[ind[5 * i + 1]])
elif ind[5 * i] == 2:
circuit.C(nodes,
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12C[ind[5 * i + 1]])
elif ind[5 * i] == 3:
circuit.include(self.spice_library['2n2222a'])
circuit.BJT(nodes,
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 4]] if ind[5 * i + 4] != 0 else circuit.gnd,
'2n2222a')
elif ind[5 * i] == 4:
circuit.include(self.spice_library['2n2907'])
circuit.BJT(nodes,
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 4]] if ind[5 * i + 4] != 0 else circuit.gnd,
'2n2907')
elif ind[5 * i] == 6:
circuit.include(self.spice_library['1N4148'])
circuit.X('D{}'.format(nodes),
'1N4148',
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd)
elif ind[5 * i] == 5:
circuit.L(nodes,
self.NODELIST[ind[5 * i + 2]] if ind[5 * i + 2] != 0 else circuit.gnd,
self.NODELIST[ind[5 * i + 3]] if ind[5 * i + 3] != 0 else circuit.gnd,
values.E12I[ind[5 * i + 1]])
elif ind[5 * i] == 0:
continue
else:
continue
nodes += 1
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.transient(start_time="2ms", step_time='1ms', end_time='40ms', max_time='40ms ')
except:
self.DEAD += 1
self.s['pop'][self.s['counter']] = [self.GEN, -1, self.GENCOUNTER]
self.s['counter'] += 1
self.GENCOUNTER += 1
return (-1.,)
result = 0
try:
j = 0.
for n, m in zip(analysis.nodes['vin'][1:-1], analysis.nodes['out'][1:-1]):
j += self.toolbox.evaluator(n, m)
result = (j / max([len (analysis.nodes['out'][1:-1]), len (analysis.nodes['out'][1:-1])])) * (1 + 0.01 * (self.N_NODES - nodes))
if result > 0 and gui != None:
gui.dc.update_data(result, analysis.nodes['out'][1:-1], analysis.nodes['vin'][1:-1])
self.s['pop'][self.s['counter']] = [self.GEN, result, self.GENCOUNTER]
self.GENCOUNTER += 1
self.s['counter'] += 1
return (result if result > 0 else 0,)
except:
self.s['pop'][self.s['counter']] = [self.GEN, -0.5, self.GENCOUNTER]
self.s['counter'] += 1
self.GENCOUNTER += 1
return (-0.5, )
def start(self):
f=open("gen_{}.csv".format(datetime.datetime.now().replace(microsecond=0)), 'w')
f.write("{},{},{},{},{},{},{}\n".format("# generation","max","moyen","ecart-type","# invalides", "# mutations","# croisements"))
self.GEN = 0
pop = self.toolbox.population(n=self.POPSIZE)
self.history.update(pop)
self.s['counter'] = 0
for gen in range(self.NGEN):
print("Generation {}".format(gen))
self.GENCOUNTER, self.DEAD, self.MUTD_COUNTER, self.CROS_COUNTER = 0, 0, 0, 0
fitnesses = map(self.toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
offspring = self.toolbox.select(pop, k=self.POPSIZE, tournsize= 5)# + self.toolbox.selectelits(pop, k=15)
offspring = list(map(self.toolbox.clone, offspring))
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < self.CXPB:
self.CROS_COUNTER += 1
self.toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < self.MUTPB:
self.MUTD_COUNTER += 1
self.toolbox.mutate(mutant)
del mutant.fitness.values
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
self.GEN += 1
self.GENCOUNTER = 0
fitnesses = map(self.toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
pop[:] = offspring
fits = [ind.fitness.values[0] for ind in pop]
length = len(pop)
self.hof.update(pop)
self.GEN += 1
self.GENCOUNTER = 0
sum2 = sum(x*x for x in fits)
print(" Min={}, Max={} avg={} std={} - deads={} mutations={} crossovers={}".format(min(fits), max(fits), sum(fits) / length, abs(sum2 / length - (sum(fits) / length)**2)**0.5, self.DEAD, self.MUTD_COUNTER, self.CROS_COUNTER))
f.write("{},{},{},{},{},{},{}\n".format(gen, max(fits), sum(fits)/ length, abs(sum2 / length - (sum(fits)/ length)**2)**0.5, self.DEAD, self.MUTD_COUNTER, self.CROS_COUNTER))
f.close()
def run(self):
random.seed()
print("starting generation")
print("population={}, max generations={}".format(self.POPSIZE, self.NGEN))
self.start()
try:
graph = networkx.DiGraph(self.history.genealogy_tree)
colors = [float(self.s['pop'][i][1]) for i in graph]
layers = {i:(self.s['pop'][i][2], -1 * int(self.s['pop'][i][0]),) for i in self.s['pop'].keys()}
networkx.draw(graph, pos=layers, node_color=colors)
plt.show()
except:
pass
for ind in self.hof:
mkschematic(ind, self.N_NODES, self.NODELIST, self.spice_library)
print("----------------------------")
print("generation ended, {} sims".format(self.s['counter']))