10.4.8. Plotting the circuit’s response during optimization¶
This demo shows how to plot the circuit’s response during optimization. Read chapter Optimizing a circuit to learn more about circuit optimization.
The problem definition in the main file runme.py in folder demo/evaluation/06-iteration-plotter/ is identical to the one in chapter Optimizing a circuit. Only the differences are given in this document.
First we import the the things we need.
from pyopus.evaluator.performance import PerformanceEvaluator, updateAnalysisCount
from pyopus.evaluator.aggregate import *
from pyopus.evaluator.auxfunc import listParamDesc
from pyopus.optimizer import optimizerClass
from pyopus.plotter.evalplotter import EvalPlotter
from pyopus.plotter.optplugin import IterationPlotter
from pyopus.plotter import interface as plt
In the main program we insert an iteration plotter plugin in the optimizer.
# Performance and cost evaluators
pe=PerformanceEvaluator(heads, analyses, measures, corners, variables=variables, debug=0)
# Input parameter order in vector x is defined by inOrder.
ce=Aggregator(pe, costDefinition, inOrder, debug=0)
# Vectors of initial, low, and high values of input parameters.
xlow=listParamDesc(costInput, inOrder, "lo")
xhi=listParamDesc(costInput, inOrder, "hi")
xinit=listParamDesc(costInput, inOrder, "init")
# Optimizer (Hooke-Jeeves). xlo and xhi must be numpy arrays.
opt=optimizerClass("HookeJeeves")(ce, xlo=xlow, xhi=xhi, maxiter=1+1*1000)
# Set initial point. Must be a numpy array; xinit is a Python list.
opt.reset(xinit)
# Install reporter plugin.
# Print cost. Print performance every time cost is decreased.
opt.installPlugin(ce.getReporter())
# Install stopper plugin.
# Stop when all requirements are satisfied (all cost contributions are 0).
opt.installPlugin(ce.getStopWhenAllSatisfied())
# Plotter
plotter=EvalPlotter(visualisation, pe)
# Create and install iteration plotter plugin
opt.installPlugin(IterationPlotter(plotter))
# Run
opt.run()
# Optimization result
xresult=opt.x
iterresult=opt.bestIter
# Final evaluation at xresult.
cf=ce(xresult)
plotter("Final result")
# Print results.
print("\n\nFinal cost: "+str(cf)+", found in iter "+str(iterresult)+", total "+str(opt.niter)+" iteration(s)")
print(ce.formatParameters())
print(ce.formatResults(nMeasureName=10, nCornerName=15))
print("Performance in corners")
print(pe.formatResults(outOrder, nMeasureName=10, nCornerName=15))
# Every call to pe results in the same analysis count.
# Therefore we can multiply the analysis count of a single pe call
# with the number of calls to get the actual analysis count.
acnt={}
updateAnalysisCount(acnt, pe.analysisCount, opt.niter+1)
print("Analysis count: "+str(acnt))
# Cleanup intemediate files
pe.finalize()
# Wait for plot windows to close
plt.join()