Python CMA-ES Algorithm to solve user-defined function and 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.


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.