I am struggling to create a simple example of a CMA-ES optimization algorithm in python. What is the most streamlined way to optimize the function x**2 + 2*y**2 -4*x*y - 0.5*y
, subject to constraints -2<x<2
and -1<2*(x**2)*y<1
, using the CMA-ES algorithm?
I looked into the DEAP library, but was not able to develop a cohesive attempt. I found their documentation less than intuitive. I also looked into the cma package, but it is not clear to me how I can implement constraints.
In the python cma
package you can specify bound constraints:
import cma
opts = cma.CMAOptions()
opts.set("bounds", [[-2, None], [2, None]])
cma.fmin(cost_function, x_start, sigma_start, opts)
For the second constraint, as it has been said before, it is not straightforward, but you can indeed assign high fitness values to out-of-domain candidate solutions. You would just have to tune cost_function
here. These values can be very high (higher than any function value in the feasible domain) or depend on the constraint violation value.
There are several methods to handle constraint with penalties. In you case (small dimension) you can try with the simplest one.