# Error with matrix definition of a linear program

According to the folloing tract https://trac.sagemath.org/ticket/16714

this command works

p = MixedIntegerLinearProgram()
x = p.new_variable()


so why the following code returns an error ?

A=[[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=[1,1,1,1,1,1]
show(matrix(A))
show(vector(U1))
p = MixedIntegerLinearProgram(binary = true)
x = p.new_variable()

p.show()

edit retag close merge delete

Sort by ยป oldest newest most voted

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])
show(A)
show(U1)
p = MixedIntegerLinearProgram()
x = p.new_variable(binary=true)

p.show()


I hope this helps!

more