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="generated_netlist.net"
- f = open(cirfile,"w")
- f.write("* Generated Netlist \n")
- f.write("V1 N001 0 10 \n")
- f.write("R1 N001 out 10k \n")
- f.write("R2 out 0 %f \n" %R2)
- f.write(".tran 1ns \n")
- f.write(".meas TRAN vsaida FIND V(out) at 1n \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)
- vout = float(match.group(1))
- return vout
- def simulate():
- print("Simulating")
- os.system('ltspice -b 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 = []
- 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("Final cost: %f \n" % res.cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement