Ask Your Question

Mixed integer programming constraint definition

asked 2019-10-07 21:23:21 +0200

Cyrille gravatar image

updated 2023-05-19 21:59:08 +0200

FrédéricC gravatar image

Sorry to ask so much questions but I am in hurry. I would like to know why this procedure doesn't work



b= matrix(8,1,(0,0,0,-1,0,0,0,0))

sign=list('==' '==' '>=' '>=' '>='> '>=' '>=' '>=')

x = vector(var('x',n=4, latex_name='x'))


p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK")

x = p.new_variable(integer=True)

p.add_constraint(B[0] 'sign[0]' b[0])

for the last line I have tryed

p.add_constraint(B[0] sign[0] b[0])

p.add_constraint(B[0] == b[0])

without any success.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-10-08 01:44:59 +0200

dsejas gravatar image

updated 2019-10-08 01:50:10 +0200

Hello, @Cyrille! I can see what you are trying to do. Unfortunately, you are trying to make operations with different data 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]) # 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 >=.

edit flag offensive delete link more


dsejas one more time thanks

Cyrille gravatar imageCyrille ( 2019-10-08 05:04:02 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-10-07 21:23:21 +0200

Seen: 310 times

Last updated: Nov 15 '19