Optimizer for ngspice
Using ngspice scripting language
by Friedrich Schmidt
downloaded from the original web page 
http://members.aon.at/fschmid7/examples_new.zip
sligthly modified by Holger Vogt, Sept. 9th, 2011

all files needs to be in the bin or in the script directory !


usage of optimizer:

1) start winspice and load test_opti.cir
3) type in wispice: > optimizer 2 40

optimizer 2 40 means : two variables to optimize , max nr. of iterations ( if not ftol breaks )

to optimize other circuits:

change only :  init and func -> copy from init.template and func.template

init: the initial simplex points ( one more than nr. of. parameters ), the ftol parameter to abort the optimization if the needed accuracy is reached -> if not max iterations are done

func: the definition of the cost function , the analysis ( one or more ) , the hard limits of the parameters, the definition of the parameters to optimize


example:

sample cirfile: test_opti.cir
=================================

* test opti
v1 1 0 dc 1v ; this is the first parameter to vary,optimize
v2 2 0 dc 1v ; this is the second parameter to vary,optimize
b1 3 0 v=v(1)*v(1)+v(2)*v(2) ; this is the costfunction

.control
save v(3) ; save the costvaluevector
.endc

.end

The costfunction is a twodimensional quadratic function with the minima at P(0,0)


sample initfile:
================
* init initial simplex points by user
.control
let ftol=0.000005
let tiny=1e-10
let p = vector(ndim*mpts)
=========== change this for other initpoints ============
let p[0][0]=10 ; this is the first simplex point
let p[0][1]=10 ; this is the first simplex point
let p[1][0]=10 ; this is the second simplex point
let p[1][1]=8  ; this is the second simplex point
let p[2][0]=8  ; this is the third simplex point
let p[2][1]=10 ; this is the third simplex point
............
.....let p[3][0]=xx ; add this for more than 2 variables
.....let p[3][1]=yy ; add this for more than 2 variables
.....let p[4][0]=zz ; add this for more than 2 variables
.....let p[4][1]=ww ; add this for more than 2 variables
............
==========================================================
cpt_psum
let ptry=p[0]
func
let y[0]=funcval
let ptry=p[1]
func
let y[1]=funcval
let ptry=p[2]
func
let y[2]=funcval
=======add this for additional variables to optimize =======
...........
.....let ptry=p[3]
.....func
.....let y[3]=funcval
.....let ptry=p[4]
.....func
.....let y[4]=funcval
...........
============================================================
.endc



sample funcfile:
================
* Here the costfunction, constrictions and the analysistype can by inserted by the user
.control
if ptry[0] lt -10  ; in this case two variables ptry[0] and ptry[1]
   let ptry[0]=-10
end
if ptry[0] gt 10
   let ptry[0]=10
end
if ptry[1] lt -10
   let ptry[1]=-10
end
if ptry[1] gt 10
	let ptry[1]=10
end
=======add this for additional variables to optimize =======
.........
...if ptry[2] lt aa
......let ptry[0]=aa
...end
...if ptry[2] gt bb
......let ptry[0]=bb
...end
...if ptry[3] lt cc
......let ptry[1]=cc
...end
...if ptry[3] gt dd
......let ptry[1]=dd
...end
.........
============================================================
* in this case two variables to optimize
alter v1 dc = ptry[0] -> defined in cirfile v1 1 0 dc 1
alter v2 dc = ptry[1] -> defined in cirfile v2 2 0 dc 1
=======add this for additional variables to optimize =======
......
...alter v3 dc = ptry[2]
...alter v4 dc = ptry[3]
......
============================================================
let cost=0
  ; print ptry[0] enable for debugging
  ; print ptry[1] enable for debugging
  ; set input = $< enable this if you want to pause the optimization
  ; here is the analysis - can be also transient or ac ......
op
let const.cost=v(3)*v(3)
==================================================================================
print v(3)-> equals the costfunction nodename in the cirfile "bcost 3 0 v=....."
==================================================================================
set saveplot=$curplot
setplot const
let funcval=cost
let funcvals[cnt]=funcval
.endc




The algorithm:
==============
The algorithm used is named flexible polyhedron ( downhill simplex from nelder and mead )
The algorithm can optimize 2 to n variables
HINT: You can also optimize for one variable and treat the second variable as dummy


