Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import ipdb # ipdb.set_trace()
- import numpy as np
- import matplotlib.pyplot as plt
- import re
- from my_optimization import *
- def generate_netlist(R2):
- print("Generating netlist")
- cirfile="circuit/generated_netlist.net"
- f = open(cirfile,"w")
- f.write("* Generated Netlist \n")
- f.write("V1 in1 0 PWL file=pwl_input_xor_v1.txt\n")
- f.write("V§W1 w2_l1n0 0 0\n")
- f.write("V2 vreflow 0 0.5\n")
- f.write("V3 vrefmid 0 1.2\n")
- f.write("V§W2 w1_l1n0 0 5\n")
- f.write("V4 in2 0 PWL file=pwl_input_xor_v2.txt\n")
- f.write("XX1 in1 w1_l1n0 w2_l1n0 out_l1n0 vreflow vrefmid in2 two_input_neuron\n")
- f.write("XX2 in1 w1_l1n1 w2_l1n1 out_l1n1 vreflow vrefmid in2 two_input_neuron\n")
- f.write("XX3 out_l1n0 w1_l2n0 w2_l2n0 out1 vreflow vrefmid out_l1n1 two_input_neuron\n")
- f.write("V§W3 w2_l1n1 0 5\n")
- f.write("V§W4 w1_l1n1 0 -5\n")
- f.write("V§W5 w2_l2n0 0 5\n")
- f.write("V§W6 w1_l2n0 0 -5\n")
- f.write("\n")
- f.write("* block symbol definitions\n")
- f.write(".subckt two_input_neuron in1 w1 w2 out vreflow vrefmid in2\n")
- f.write("XU1 N001 N005 wsum opamp Aol=100K GBW=10Meg\n")
- f.write("R1 N005 vreflow 100\n")
- f.write("R2 wsum N001 100\n")
- f.write("M1 N005 w1 in1 0 N_1u l=1u w=3u\n")
- f.write("M2 N001 w1n in1 0 N_1u l=1u w=3u\n")
- f.write("XU2 N002 N004 N003 opamp Aol=100K GBW=10Meg\n")
- f.write("R3 N002 vreflow 1k\n")
- f.write("R4 N003 N002 20k\n")
- f.write("R5 N004 wsum 1k\n")
- f.write("R6 N004 vrefmid 20k\n")
- f.write("D1 out vrefmid D\n")
- f.write("D2 vrefmid out D\n")
- f.write("R7 out N003 100\n")
- f.write("M3 N001 w2n in2 0 N_1u l=1u w=3u\n")
- f.write("M4 N005 w2 in2 0 N_1u l=1u w=3u\n")
- f.write("B1 W1n 0 V=-V(W1)\n")
- f.write("B2 W2n 0 V=-V(W2)\n")
- f.write(".lib opamp.sub\n")
- f.write(".include cmosedu_models.txt\n")
- f.write(".ends two_input_neuron\n")
- f.write(".model D D\n")
- #f.write(".lib C:\users\tweber\My Documents\LTspiceXVII\lib\cmp\standard.dio \n")
- f.write(".lib standard.dio \n")
- f.write(".model NMOS NMOS\n")
- f.write(".model PMOS PMOS\n")
- #f.write(".lib C:\users\tweber\My Documents\LTspiceXVII\libx1\cmp\standard.mos \n")
- f.write(".lib standard.mos \n")
- f.write(".include cmosedu_models.txt\n")
- f.write(".tran 40ms\n")
- f.write(".end\n")
- f.close()
- def meas_result():
- print("Reading log file")
- logfile="generated_netlist.log"
- with open(logfile) as f:
- lines = f.readlines()
- f.close()
- for l in lines:
- #print(l)
- if 'vsaida' in l:
- match =re.search(r"=(.*) at",l) # LTSPICE
- #match =re.search(r"= (.*)",l) # ngspice
- vout = float(match.group(1))
- return vout
- def simulate():
- print("Simulating")
- #os.system('ngspice -b circuit/generated_netlist.net > generated_netlist.log')
- os.system('ltspice -b circuit/generated_netlist.net')
- def cost_function(x):
- print("Evaluating cost function")
- #generate, simulate and read log
- generate_netlist(x)
- simulate()
- voltage = meas_result()
- #calculate cost
- cost = abs(5-voltage)
- return cost
- # OPTIMIZATION
- size_x = 1
- bound_i = (100,20000)
- bounds = []
- for i in range (0, size_x):
- bounds.append(bound_i)
- x0 = np.array([3000])
- maxiter = 100
- step = 1000
- args = []
- cost_list = []
- number_of_optimizations = 1
- res = my_hillclimbing(cost_function, x0, bounds, maxiter, step, args)
- print("Final x: [",)
- print("%f" % res.x[0],)
- for i in range(1,len(res.x)):
- print(", %f" % res.x[i],)
- print("]\n",)
- #print res.x
- print("Final cost: %f \n" % res.cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement