Ask Your Question

Error with matrix definition of a linear program

asked 2021-03-01 12:04:34 +0100

Cyrille gravatar image

According to the folloing tract

this command works

p = MixedIntegerLinearProgram()
x = p.new_variable()
p.add_constraint(A_matrix*x <= a_vector)

so why the following code returns an error ?

p = MixedIntegerLinearProgram(binary = true)
x = p.new_variable()
p.add_constraint(A*x <= U1)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-01 13:14:24 +0100

dsejas gravatar image

Hello, @Cyrille! The problem with your code is that the constructor MixedIntegerLinearProgram() doesn't have a binary keyword, which is hinted by the error message created by your program:

TypeError: __init__() got an unexpected keyword argument 'binary'

(This is referring to the __init__() method of MixedIntegerLinearProgram(), which has to do with object-oriented programming. This detail is not too relevant in this case, so let's ignore it.)

The new_variable() is the one that has this binary argument. Notice that this is in agreement to standard definitions of mixed integer linear programming (MILP), where it is the variables that are binary, integer, etc., with the program inheriting that nomenclature. Fro example, we say binary program to stand for the clearer but longer program with binary variables.

One other correction you will need to do is to define A and U1 as a matrix and vector, respectively, in order to be able to make the product and comparison in your constraint A*x <= U1. This is because product between a list of lists (your current A) and a variable from a MILP (your current x) is not define, nor is it the comparison with a list (your current U1.)

With those considerations, this should work as intended:

A = matrix([[1,0,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,1,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,1,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,1,0],[0,0,0,0,0,0,0,0,1,0,0,1]])
U1 = vector([1,1,1,1,1,1])
p = MixedIntegerLinearProgram()
x = p.new_variable(binary=true)
p.add_constraint(A*x <= U1)

I hope this helps!

edit flag offensive delete link more

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: 2021-03-01 12:04:34 +0100

Seen: 220 times

Last updated: Mar 01 '21