Constrained Optimization: Searching the Global Maximum¶
In this tutorial we show how to solve a constrained optimization problem CEC 2006 Test Problem g09), defined as: Find \(x^\star = \mathop{\arg\min}\limits_{x} \,\,\, f(x) \,,\) under the constraints \(g_i(x) \leq 0.\)
The Objective Function¶
Create a folder named model. Inside, create a file with name model.py and paste the following code,
#!/usr/bin/env python
def g09( k ):
d = k["Parameters"]
res = (d[0] - 10.0)**2 + 5.0 * (d[1] - 12.0)**2 \
+ d[2]**4 + 3.0 * (d[3] - 11.0)**2 \
+ 10.0 * d[4]**6 + 7.0 * d[5]**2 + d[6]**4. \
- 4.0 * d[5] * d[6] - 10.0 * d[5] - 8.0 * d[6];
k["Evaluation"] = -res;
This computational model represents our objective function.
For the constraints, add the following code in the same file,
def g1(k):
v = k["Parameters"]
k["Evaluation"] = -127.0 + 2 * v[0] * v[0] + 3.0 * pow(v[1], 4) + v[2] + 4.0 * v[3] * v[3] + 5.0 * v[4]
def g2(k):
v = k["Parameters"]
k["Evaluation"] = -282.0 + 7.0 * v[0] + 3.0 * v[1] + 10.0 * v[2] * v[2] + v[3] - v[4]
def g3(k):
v = k["Parameters"]
k["Evaluation"] = -196.0 + 23.0 * v[0] + v[1] * v[1] + 6.0 * v[5] * v[5] - 8.0 * v[6]
def g4(k):
v = k["Parameters"]
k["Evaluation"] = 4.0 * v[0] * v[0] + v[1] * v[1] - 3.0 * v[0] * v[1] + 2.0 * v[2] * v[2] + 5.0 * v[5] - 11.0 * v[6]
Optimization with (C)CMA-ES¶
First, open a file and import the korali module
#!/usr/bin/env python3
import korali
Import the computational model,
import sys
sys.path.append('./model')
from model import *
from constraints import *
The Korali Object¶
Next we construct a korali.Experiment object,
e = korali.Experiment()
Add the objective function and the constraints in the Korali object,
e["Problem"]["Objective Function"] = g09
e["Problem"]["Constraints"] = [ g1, g2, g3, g4 ]
The Problem Type¶
Then, we set the type of the problem to Direct Evaluation
e["Problem"]["Type"] = "Evaluation/Direct/Basic"
e["Problem"]["Objective"] = "Maximize"
The Variables¶
We add 7 variables to the experiment and set their domain,
for i in range(7) :
e["Variables"][i]["Name"] = "X" + str(i)
e["Variables"][i]["Lower Bound"] = -10.0
e["Variables"][i]["Upper Bound"] = +10.0
The Solver¶
We choose the solver CMA-ES,
e["Solver"]["Type"] = "Optimizer/CMAES"
Then we set a few parameters for CCMA-ES,
e["Solver"]["Is Sigma Bounded"] = True
e["Solver"]["Population Size"] = 32
e["Solver"]["Viability Population Size"] = 4
e["Solver"]["Termination Criteria"]["Max Value"] = -680.630057374402 - 1e-4
e["Solver"]["Termination Criteria"]["Max Generations"] = 500
For a detailed description of CCMA-ES settings see CMAES.
We configure output settings,
e["File Output"]["Frequency"] = 50
e["Console Output"]["Frequency"] = 50
Finally, we need to create a Korali Engine object add a call to its run() routine, to start the engine.
k = korali.Engine()
k.run(e)
Running¶
We are now ready to run our example:./run-ccmaes.py
The results are saved in the folder _korali_result/.
Plotting¶
You can see the results of CMA-ES by running the command, python3 -m korali.plotter