ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 01 Mar 2021 13:14:24 +0100Error with matrix definition of a linear programhttps://ask.sagemath.org/question/55970/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()
p.add_constraint(A_matrix*x <= a_vector)
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.add_constraint(A*x <= U1)
p.show()
Mon, 01 Mar 2021 12:04:34 +0100https://ask.sagemath.org/question/55970/error-with-matrix-definition-of-a-linear-program/Answer by dsejas for <p>According to the folloing tract <code>https://trac.sagemath.org/ticket/16714</code></p>
<p>this command works</p>
<pre><code>p = MixedIntegerLinearProgram()
x = p.new_variable()
p.add_constraint(A_matrix*x <= a_vector)
</code></pre>
<p>so why the following code returns an error ?</p>
<pre><code>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.add_constraint(A*x <= U1)
p.show()
</code></pre>
https://ask.sagemath.org/question/55970/error-with-matrix-definition-of-a-linear-program/?answer=55971#post-id-55971Hello, @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.add_constraint(A*x <= U1)
p.show()
I hope this helps!Mon, 01 Mar 2021 13:14:24 +0100https://ask.sagemath.org/question/55970/error-with-matrix-definition-of-a-linear-program/?answer=55971#post-id-55971