Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @Cyrille! I can see what you are trying to do. Unfortunately, you are trying to make operations with different data types (see my comments to your original question).

Unfortunately, what you are trying to is not possible using a list, like the sign list that you defined. However, there is another way to do it: you can use the min and max arguments for the add_constraint method. Let me explain with some examples (on the left of the arrow you have the usual notation; on the right, you have the alternative form):

p.add_constraint(-1.0 <= 5*x[0] - 7*x[2] <= 1.0) --------> p.add_constraint(5*x[0] - 7*x[2], min=-1.0, max=1.0)
p.add_constraint(5*x[0] - 7*x[2] <= 3.4) --------> p.add_constraint(5*x[0] - 7*x[2], max=3.4)
p.add_constraint(-13.2 <= 5*x[0] - 7*x[2] ) --------> p.add_constraint(5*x[0] - 7*x[2], min=-13.2)
p.add_constraint(5*x[0] - 7*x[2] == 21.0) --------> p.add_constraint(5*x[0] - 7*x[2], min=21.0, max=21.0)

On the other hand, the instruction var('x',n=4, latex_name='x'), which I gave you in a previous of my answers, is not what you need to achieve this. Sorry, that is my fault. I didn't know what you were trying to do. Instead, you need to do the following:

x = p.new_variable(integer=True, indices=[0..7])

Considering all this, you can write a MILP like the one you present in your question with the following code:

A = matrix(8,4,[1,1,1,-14,0,1,2,-8,-1,1,1,0,0,0,0,-1,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0]) # the coefficients
bmin = [0,0,0,-1,0,0,0,0] # the lower bounds for the constraints
bmax = [0,0,oo,oo,oo,oo,oo,oo] # the upper bounds for the constraints
p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK") # we create the MILP
x = p.new_variable(integer=True, indices=[0..7]) # the new variable will be x[0] ... x[7}
B = A * x # the linear functions for the constraints
# Now we build the constraints using the bounds lists
for i in range(8):
    p.add_constraint(B[i], min=bmin[i], max=bmax[i])
p.show() # this is just to confirm we did it right

I am so sorry that I can't give you a way of doing this using the symbols ==, <= and >=.

Hello, @Cyrille! I can see what you are trying to do. Unfortunately, you are trying to make operations with different data types (see my comments to your original question).types.

Unfortunately, what you are trying to is not possible using a list, like the sign list that you defined. However, there is another way to do it: you can use the min and max arguments for the add_constraint method. Let me explain with some examples (on the left of the arrow you have the usual notation; on the right, you have the alternative form):

p.add_constraint(-1.0 <= 5*x[0] - 7*x[2] <= 1.0) --------> p.add_constraint(5*x[0] - 7*x[2], min=-1.0, max=1.0)
p.add_constraint(5*x[0] - 7*x[2] <= 3.4) --------> p.add_constraint(5*x[0] - 7*x[2], max=3.4)
p.add_constraint(-13.2 <= 5*x[0] - 7*x[2] ) --------> p.add_constraint(5*x[0] - 7*x[2], min=-13.2)
p.add_constraint(5*x[0] - 7*x[2] == 21.0) --------> p.add_constraint(5*x[0] - 7*x[2], min=21.0, max=21.0)

On the other hand, the instruction var('x',n=4, latex_name='x'), which I gave you in a previous of my answers, is not what you need to achieve this. Sorry, that is my fault. I didn't know what you were trying to do. Instead, you need to do the following:

x = p.new_variable(integer=True, indices=[0..7])

Considering all this, you can write a MILP like the one you present in your question with the following code:

A = matrix(8,4,[1,1,1,-14,0,1,2,-8,-1,1,1,0,0,0,0,-1,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0]) # the coefficients
bmin = [0,0,0,-1,0,0,0,0] # the lower bounds for the constraints
bmax = [0,0,oo,oo,oo,oo,oo,oo] # the upper bounds for the constraints
p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK") # we create the MILP
x = p.new_variable(integer=True, indices=[0..7]) # the new variable will be x[0] ... x[7}
B = A * x # the linear functions for the constraints
# Now we build the constraints using the bounds lists
for i in range(8):
    p.add_constraint(B[i], min=bmin[i], max=bmax[i])
p.show() # this is just to confirm we did it right

I am so sorry that I can't give you a way of doing this using the symbols ==, <= and >=.