I am working on an optimisation model using Pyomo (with the iPopt solver) and I am trying to set up a constraint that keeps specific variables of the model outside of a certain range (0,500], that is the variable can either be zero or greater than 500.
As I cannot specify multiple domains to the variables, I am adding a constraint for each variable using binary variables.
The constraint works like this :
model.variable <= 1 + (large_upper_bound-1)*model.binary_variable
model.variable >= 500*model.binary_variable
Here is my code :
model.x2 = Var(binary_vars, within=Binary)
for route in binary_vars:
model.cons.add(model.x[route[0], route[1], route[2], route[3]] <= (1 + (100000-1)*model.x2[route[0], route[1], route[2], route[3]]))
model.cons.add(model.x[route[0], route[1], route[2], route[3]] >= (500*model.x2[route[0], route[1], route[2], route[3]]))
model_result = SolverFactory('ipopt').solve(model, tee=True)
The code runs fine, but the solutions for the binary variables are not binary (not 0 or 1), it is either a zero or a random fraction :
dict_values([0.0, 0.0, 0.0, 0.0, 0.0, 0.69771460622592687,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.52210753137570483, 0.0, 0.0,
0.44349809775540615])
Meaning that the solution for the x variables do not stay out of the specified range.
Could someone help me understand why the binary variables go to decimals ?
How can I stop that and impose the model a set of integers for these binary variables such as [0,1] ?
And/Or if there is another way around this problem ?
Thanks All.