Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python CMA-ES Algorithm to solve user-defined function and constraints

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.

like image 896
kilojoules Avatar asked May 18 '16 14:05

kilojoules


2 Answers

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.

like image 50
paulduf Avatar answered Oct 14 '22 03:10

paulduf


I see your struggle with the DEAP docs. Nevertheless, I have written my own Evolutionary Computing library, and lately I have been using DEAP for many proof-of-concepts and I think they did a good job with it.

Going on, Let's take a look at the complete example. If you read the docs you will be confortable looking at the code. The problem size is the number of variables, so in your case if I understand correctly you would have N = 2 (x and y).

And you need your custom fitness function instead of benchamrks.rastrigin:

toolbox.register("evaluate", myownfunction)

The constraints are not implemented, but are an easy task. In the fitness function you can invalidate the individuals that violate the constraints (for instance by assigning a very high fitness, if minimizing) and in few generations your population should be free of invalids.

This would be the simplest approach with DEAP, but the deap.cma.Strategy class can be extended in order to override/extend any method, for instance, the generate method so that all the individuals in the initial population are valid.

like image 3
rll Avatar answered Oct 14 '22 02:10

rll