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.Tue, 08 Oct 2019 05:04:02 +0200Mixed integer programming constraint definitionhttps://ask.sagemath.org/question/48228/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[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.Mon, 07 Oct 2019 21:23:21 +0200https://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/Answer by dsejas for <p>Sorry to ask so much questions but I am in hurry. I would like to know why this procedure doesn't work</p>
<blockquote>
<p>A=</p>
<p>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))</p>
<p>b= matrix(8,1,(0,0,0,-1,0,0,0,0))</p>
<p>sign=list('==' '==' '>=' '>=' '>='> '>=' '>=' '>=')</p>
<p>x = vector(var('x',n=4, latex_name='x'))</p>
<p>B=A*x</p>
<p>p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK")</p>
<p>x = p.new_variable(integer=True)</p>
<p>p.add_constraint(B[0] 'sign[0]' b[0])</p>
</blockquote>
<p>for the last line I have tryed</p>
<blockquote>
<p>p.add_constraint(B[0] sign[0] b[0])</p>
<p>p.add_constraint(B[0] == b[0])</p>
</blockquote>
<p>without any success.</p>
https://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/?answer=48232#post-id-48232Hello, @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](https://ask.sagemath.org/question/48220/creating-a-vector-of-indexed-variables/), 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])
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 `>=`.Tue, 08 Oct 2019 01:44:59 +0200https://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/?answer=48232#post-id-48232Comment by Cyrille for <p>Hello, <a href="/users/26565/cyrille/">@Cyrille</a>! I can see what you are trying to do. Unfortunately, you are trying to make operations with different data types.</p>
<p>Unfortunately, what you are trying to is not possible using a list, like the <code>sign</code> list that you defined. However, there is another way to do it: you can use the <code>min</code> and <code>max</code> arguments for the <code>add_constraint</code> 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>
<pre><code>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)
</code></pre>
<p>On the other hand, the instruction <code>var('x',n=4, latex_name='x')</code>, which I gave you in a <a href="https://ask.sagemath.org/question/48220/creating-a-vector-of-indexed-variables/">previous of my answers</a>, 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:</p>
<pre><code>x = p.new_variable(integer=True, indices=[0..7])
</code></pre>
<p>Considering all this, you can write a MILP like the one you present in your question with the following code:</p>
<pre><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])
p.show() # this is just to confirm we did it right
</code></pre>
<p>I am so sorry that I can't give you a way of doing this using the symbols <code>==</code>, <code><=</code> and <code>>=</code>.</p>
https://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/?comment=48233#post-id-48233dsejas one more time thanksTue, 08 Oct 2019 05:04:02 +0200https://ask.sagemath.org/question/48228/mixed-integer-programming-constraint-definition/?comment=48233#post-id-48233