# 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.$$

## Problem Description¶

We want to solve the problem:

under the four constraints $$g_i(x)$$:

## 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 - 10.0)**2 + 5.0 * (d - 12.0)**2           \
+ d**4  + 3.0 * (d - 11.0)**2                 \
+ 10.0 * d**6 + 7.0 * d**2 + d**4.      \
- 4.0 * d * d - 10.0 * d - 8.0 * d;

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 * v + 3.0 * pow(v, 4) + v + 4.0 * v * v + 5.0 * v

def g2(k):
v = k["Parameters"]
k["Evaluation"] = -282.0 + 7.0 * v + 3.0 * v + 10.0 * v * v + v - v

def g3(k):
v = k["Parameters"]
k["Evaluation"] = -196.0 + 23.0 * v + v * v + 6.0 * v * v - 8.0 * v

def g4(k):
v = k["Parameters"]
k["Evaluation"] = 4.0 * v * v + v * v - 3.0 * v * v + 2.0 * v * v + 5.0 * v - 11.0 * v


## 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