Aaron
Aaron

Reputation: 144

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?

Upvotes: 0

Views: 801

Answers (2)

Andrew Nelson
Andrew Nelson

Reputation: 595

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.

Upvotes: 2

ekrall
ekrall

Reputation: 192

You need to use a mixed integer programming solver.

Unless your constraint matrix is unimodular, you cannot ensure an integer solution without a MIP solver.

By saying that all of the solutions need to be integer, you are saying that your problem is non-convex. But you are instead trying to solve a convex problem.

you might want to check out tools such as GLPK and CBC, if your constraints/equations are linear. If your constraints are nonlinear you need to look into nonlinear integer programming solvers.

Upvotes: 0

Related Questions