Quickstart Optimization

An optimization process can be presented as following:

../_images/optiScheme.svg
  • Optimization algorithm: algorithmInterface. This is the algorithm that performs the optimization, and outputs a vector of variables between [0, 1[.
  • Maths to physics: interfaceMathsToPhysics. Transforms the output vector of the optimization algorithm to the variables of a InterfaceDevice. The usage of this block becomes meaningful for more complex optimization problem, such as optimizing a BLDC motor while keeping the outer diameter constant. In this case, a good implementation of the M2P block automatically scales the inner dimensions of the motor to comply with this constraint.
  • Characterization: interfaceCharacterization. Based on the attributes of the device, performs some computation. This block is nearly useless for simple optimization problems (when the objective function is easily computed) but becomes interesting for more complex problems, where many things need to be precalculated before obtaining the objective functions and constraints. This for example can hold an analytical or a FEM magnetic model. A sub-optimization could also be performed there.
  • Objective and constraints: interfaceObjCons. These classes correspond to either what has to be minimized, or which constraints <=0 has to be complied with.

Quick example: \(\min_{x, y \in [0, 2]} f(x) = \sqrt{ 1 + (y+3) \cdot x^2 }, g(x) = 4 + 2 \sqrt{y+3} \cdot \sqrt{1+(x-1)^2}\), under the constrained that \(x \leq 0.55\). This is a bi-objective problem and will lead to a pareto front.

To set up optimization project, begin with these imports

from optimeed.core import InterfaceDevice
from optimeed.optimize.optiAlgorithms import MultiObjective_GA as OptimizationAlgorithm
# from optimeed.optimize.optiAlgorithms import NLOpt_Algorithm as OptimizationAlgorithm
from optimeed.optimize import Optimizer, Real_OptimizationVariable, FastObjCons, InterfaceCharacterization
from optimeed.visualize import start_qt_mainloop
import time

Then define the device to optimize

class Device(InterfaceDevice):
    def __init__(self):
        self.x = 1
        self.y = 1

Define how the device will be characterized. In this example nothing happens (but a sleep). This step is not mandatory

class Characterization(InterfaceCharacterization):
    def compute(self, theDevice):
        time.sleep(0.005)

Once the classes are defined, we can start to instantiate them:

theDevice = Device()
theMathsToPhysics = MathsToPhysics()
theAlgo = OptimizationAlgorithm()
theCharacterization = Characterization()

The optimization algorithm supports multicore usage (as option, default to be one):

theAlgo.set_optionValue(theAlgo.NUMBER_OF_CORES, 4)

We then set the variables to be optimized:

optimizationVariables = list()
optimizationVariables.append(Real_OptimizationVariable('x', 0, 2))
optimizationVariables.append(Real_OptimizationVariable('y', 0, 2))

And the objectives and constraints:

listOfObjectives = [FastObjCons("( 1 + ({y}+3)*{x}**2 )**0.5"), FastObjCons("4 + 2*({y}+3)**0.5*(1+({x}-1)**2)**0.5 ")]
listOfConstraints = [FastObjCons("{x} - 0.55")]

Finally set the optimizer:

theOptimizer = Optimizer()
theOptimizer.set_optionValue(theOptimizer.KWARGS_OPTIHISTO, {"autosave": False})
PipeOptimization = theOptimizer.set_optimizer(theDevice, listOfObjectives, listOfConstraints, optimizationVariables,
                                              theOptimizationAlgorithm=theAlgo, theCharacterization=theCharacterization)
theOptimizer.set_max_opti_time(2)

The optimizer can then be run with:

result, convergence = theOptimizer.run_optimization()

Or, if visualisation is needed:

from optimeed.visualize.displayOptimization import OptimizationDisplayer
optiDisplayer = OptimizationDisplayer(PipeOptimization, listOfObjectives, theOptimizer)
_, _ = optiDisplayer.generate_optimizationGraphs()
resultsOpti, convergence = optiDisplayer.launch_optimization()

Finally:

print("Best individuals :")
for device in resultsOpti:
    print("x : {} \t y : {}". format(device.x, device.y))