pythonscipyscipy-optimizedifferential-evolution

Integer solution constraint for scipy.optimize.differential_evolution?


I am trying to use the scipy.optimize.differential_evolution optimizer in my program. But I need the solution to be of only integers...

Right now the solution (the attribute x of scipy.optimize.OptimizeResult) always looks something like this:

[3.56932195, 1.69611735, 8.67970522, 9.69922539, 6.43961603, 3.3292928 , 2.32741226]

But I need all the elements in the solution to be integers, e.g.:

[6, 6, 4, 7, 2, 5, 3]

Is there any way to enforce this? Or is there any other/better suited package I could use for this?


Solution

  • In scipy version 1.9 differential_evolution will gain an integrality keyword that will do exactly what you want, without problem modification. In the meantime you can simply round all the parameter values to integers, e.g. np.round, in your objective function. This approach works with and without constraints.