Ask Your Question

# Mixed integer programming constraint definition

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

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))

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

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

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

B=A*x

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

x = p.new_variable(integer=True)

p.add_constraint(B 'sign' b)

for the last line I have tryed

p.add_constraint(B sign b)

p.add_constraint(B == b)

without any success.

edit retag close merge delete

## 1 Answer

Sort by » oldest newest most voted

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 - 7*x <= 1.0) --------> p.add_constraint(5*x - 7*x, min=-1.0, max=1.0)
p.add_constraint(5*x - 7*x <= 3.4) --------> p.add_constraint(5*x - 7*x, max=3.4)
p.add_constraint(-13.2 <= 5*x - 7*x ) --------> p.add_constraint(5*x - 7*x, min=-13.2)
p.add_constraint(5*x - 7*x == 21.0) --------> p.add_constraint(5*x - 7*x, 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 ... 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 >=.

more

## Your Answer

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

Add Answer

## Stats

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

Seen: 168 times

Last updated: Nov 15 '19