Skip to content Skip to sidebar Skip to footer

Scipy Minimize: How To Restrict X Only To 0 And 1?

I want to minimize a function with multiple parameters and constraints with Scipy.optimize.minimize: def f(x): return -1*(0.9*x[0] + 0.8*x[1] + 0.85*x[2])*(0.95*x[3] + 0.8*x[4]

Solution 1:

In theory you could add equality constraints:

x[i] * (x[i]-1) = 0

In practice that does not work very well as this adds a nasty non-convexity to the model. It looks you have a non-linear objective and linear constraints and binary variables, so that would indicate we need to look at a MINLP (Mixed Integer Non-linear Programming) solver. Such solvers are readily available (e.g. Bonmin, Couenne, Baron, Antigone).

However there is something we can do. We can expand your objective, and write

maximize 0.9*0.95*0.98*x[0]*x[3]*x[6] + 0.9*0.95*0.94*x[0]*x[3]*x[7] + ...

or

maximize c1*(x[0]*x[3]*x[6]) + c2*(x[0]*x[3]*x[7]) + ...

These products like x[0]*x[3]*x[6] where all the x[i] are 0-1 or binary variables can be linearized as follows:

maximize c1*y1 + c2*y2 + ....
y1 <= x[0]
y1 <= x[3]
y1 <= x[6]
y2 <= x[0]
y2 <= x[3]
y2 <= x[7]
...
y1,y2,... binary variables

If we want we can make y1,y2,... continuous variables between 0 and 1. They will be automatically zero or one. More details are here.

What we have now is a linear objective and linear constraints and binary variables x[i], y[j]. This can be solved with MIP (Mixed Integer Programming) solvers which are readily available. Very good ones are Cplex and Gurobi, but there are also public domain ones such as CBC and GLPK. Many of them have Python bindings.

Post a Comment for "Scipy Minimize: How To Restrict X Only To 0 And 1?"