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, 21 May 2012 10:29:33 +0200p.show() at MixedIntegerLinearProgram resultshttps://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/I would appreciate it if someone could explain me how the p.show() method works (p=MixedIntegerLinearProgram)...
More precisely :
lets say we have the following Linear Program:
p=MixedIntegerLinearProgram()
w=p.new_variable()
p.add_constraint(-w[0]+w[2]+2*w[3]==4)
p.add_constraint(w[0]+w[1]+w[3]==3)
p.set_objective(w[0]- w[1]) #<---- Look here
the result from p.show() is
Maximization:
x_0 -x_3 #<----and here
Constraints:
4.0 <= -x_0 +x_1 +2.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
why SAGE shows a different objective function?
Sat, 19 May 2012 17:00:54 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/Answer by kcrisman for <p>I would appreciate it if someone could explain me how the p.show() method works (p=MixedIntegerLinearProgram)...
More precisely :
lets say we have the following Linear Program:</p>
<pre><code>p=MixedIntegerLinearProgram()
w=p.new_variable()
p.add_constraint(-w[0]+w[2]+2*w[3]==4)
p.add_constraint(w[0]+w[1]+w[3]==3)
p.set_objective(w[0]- w[1]) #<---- Look here
</code></pre>
<p>the result from p.show() is</p>
<pre><code>Maximization:
x_0 -x_3 #<----and here
Constraints:
4.0 <= -x_0 +x_1 +2.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>why SAGE shows a different objective function?</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13592#post-id-13592Apparently Sage renames the variables in the order you actually provided them! In this example it's easier to see because I've given a different coefficient to `w[2]` and `w[3]`.
sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable()
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4) # - for the first, 2 for the second, 3 for the third
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
sage: p.set_objective(w[0]- w[1]) # so if w[1] is the last variable to arrive, it will be x_3
sage: p.show()
Maximization:
x_0 -x_3 # and indeed it is
Constraints:
4.0 <= -x_0 +2.0 x_1 +3.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
Now maybe that's a bug, or at least needs to be documented better.Sat, 19 May 2012 23:43:07 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13592#post-id-13592Comment by koukourikos for <p>Apparently Sage renames the variables in the order you actually provided them! In this example it's easier to see because I've given a different coefficient to <code>w[2]</code> and <code>w[3]</code>.</p>
<pre><code>sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable()
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4) # - for the first, 2 for the second, 3 for the third
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
sage: p.set_objective(w[0]- w[1]) # so if w[1] is the last variable to arrive, it will be x_3
sage: p.show()
Maximization:
x_0 -x_3 # and indeed it is
Constraints:
4.0 <= -x_0 +2.0 x_1 +3.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>Now maybe that's a bug, or at least needs to be documented better.</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19763#post-id-19763yes you are right, it has to do with the order. I don't think that this is a bug but I agree that an explanation in the documentation about how the indexing works would be useful.Sun, 20 May 2012 07:47:27 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19763#post-id-19763Answer by Nathann for <p>I would appreciate it if someone could explain me how the p.show() method works (p=MixedIntegerLinearProgram)...
More precisely :
lets say we have the following Linear Program:</p>
<pre><code>p=MixedIntegerLinearProgram()
w=p.new_variable()
p.add_constraint(-w[0]+w[2]+2*w[3]==4)
p.add_constraint(w[0]+w[1]+w[3]==3)
p.set_objective(w[0]- w[1]) #<---- Look here
</code></pre>
<p>the result from p.show() is</p>
<pre><code>Maximization:
x_0 -x_3 #<----and here
Constraints:
4.0 <= -x_0 +x_1 +2.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>why SAGE shows a different objective function?</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13593#post-id-13593Helloooo everybody !!
Indeed, they are numbered this way just because of the order in which they are created. And there is no specific reason why they are called `x`. For the code you give as an example, I would not know from the inside of objects p or w that the value is stored in a variable which is named 'w'. Hence I do not know how to make this appear in p.show(). Actually, there is in Sage a way to "name" variables :
sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable(name="w")
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4)
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
sage: p.set_objective(w[0]- w[1])
sage: p.show()
Maximization:
w[0] -w[1]
Constraints:
R0: 4.0 <= -w[0] +2.0 w[2] +3.0 w[3] <= 4.0
R1: 3.0 <= w[0] +w[3] +w[1] <= 3.0
Variables:
w[0] is a continuous variable (min=0.0, max=+oo)
w[2] is a continuous variable (min=0.0, max=+oo)
w[3] is a continuous variable (min=0.0, max=+oo)
w[1] is a continuous variable (min=0.0, max=+oo)
But there you had to explicitely say that the variables had to be named by "w". Well. Now, this changes nothing to their numbering, and I have no idea how to fix that. In the part of the code that displays the formulas, the keys you used to access the variables have been forgotten a loooong time ago. I agree that the result of p.show() would be muc easier to read though...Sun, 20 May 2012 04:18:59 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13593#post-id-13593Comment by kcrisman for <p>Helloooo everybody !!</p>
<p>Indeed, they are numbered this way just because of the order in which they are created. And there is no specific reason why they are called <code>x</code>. For the code you give as an example, I would not know from the inside of objects p or w that the value is stored in a variable which is named 'w'. Hence I do not know how to make this appear in p.show(). Actually, there is in Sage a way to "name" variables :</p>
<pre><code>sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable(name="w")
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4)
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
sage: p.set_objective(w[0]- w[1])
sage: p.show()
Maximization:
w[0] -w[1]
Constraints:
R0: 4.0 <= -w[0] +2.0 w[2] +3.0 w[3] <= 4.0
R1: 3.0 <= w[0] +w[3] +w[1] <= 3.0
Variables:
w[0] is a continuous variable (min=0.0, max=+oo)
w[2] is a continuous variable (min=0.0, max=+oo)
w[3] is a continuous variable (min=0.0, max=+oo)
w[1] is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>But there you had to explicitely say that the variables had to be named by "w". Well. Now, this changes nothing to their numbering, and I have no idea how to fix that. In the part of the code that displays the formulas, the keys you used to access the variables have been forgotten a loooong time ago. I agree that the result of p.show() would be muc easier to read though...</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19757#post-id-19757@Nathann - what do you think about the suggestion in koukourikos' comments in the other two answers to make this default somehow? I don't know if it is syntactically possible to "grab" the name or not.Mon, 21 May 2012 10:29:33 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19757#post-id-19757Comment by koukourikos for <p>Helloooo everybody !!</p>
<p>Indeed, they are numbered this way just because of the order in which they are created. And there is no specific reason why they are called <code>x</code>. For the code you give as an example, I would not know from the inside of objects p or w that the value is stored in a variable which is named 'w'. Hence I do not know how to make this appear in p.show(). Actually, there is in Sage a way to "name" variables :</p>
<pre><code>sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable(name="w")
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4)
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
sage: p.set_objective(w[0]- w[1])
sage: p.show()
Maximization:
w[0] -w[1]
Constraints:
R0: 4.0 <= -w[0] +2.0 w[2] +3.0 w[3] <= 4.0
R1: 3.0 <= w[0] +w[3] +w[1] <= 3.0
Variables:
w[0] is a continuous variable (min=0.0, max=+oo)
w[2] is a continuous variable (min=0.0, max=+oo)
w[3] is a continuous variable (min=0.0, max=+oo)
w[1] is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>But there you had to explicitely say that the variables had to be named by "w". Well. Now, this changes nothing to their numbering, and I have no idea how to fix that. In the part of the code that displays the formulas, the keys you used to access the variables have been forgotten a loooong time ago. I agree that the result of p.show() would be muc easier to read though...</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19764#post-id-19764Thank you very much! your trick with the naming made the results to have more sense and the indexing is also correct! Sun, 20 May 2012 07:43:34 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19764#post-id-19764Answer by Volker Braun for <p>I would appreciate it if someone could explain me how the p.show() method works (p=MixedIntegerLinearProgram)...
More precisely :
lets say we have the following Linear Program:</p>
<pre><code>p=MixedIntegerLinearProgram()
w=p.new_variable()
p.add_constraint(-w[0]+w[2]+2*w[3]==4)
p.add_constraint(w[0]+w[1]+w[3]==3)
p.set_objective(w[0]- w[1]) #<---- Look here
</code></pre>
<p>the result from p.show() is</p>
<pre><code>Maximization:
x_0 -x_3 #<----and here
Constraints:
4.0 <= -x_0 +x_1 +2.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)
</code></pre>
<p>why SAGE shows a different objective function?</p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13591#post-id-13591The variables are such that `w[3] == x_1`. From the looks of it, a new `x_i` is generated the first time you access `w[j]`. Sat, 19 May 2012 23:38:50 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?answer=13591#post-id-13591Comment by kcrisman for <p>The variables are such that <code>w[3] == x_1</code>. From the looks of it, a new <code>x_i</code> is generated the first time you access <code>w[j]</code>. </p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19765#post-id-19765Which leads to the question of why they are called `x`, and the numbers. Sorry for not reloading before you posted - amazing how this often seems to happen.Sat, 19 May 2012 23:43:36 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19765#post-id-19765Comment by koukourikos for <p>The variables are such that <code>w[3] == x_1</code>. From the looks of it, a new <code>x_i</code> is generated the first time you access <code>w[j]</code>. </p>
https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19762#post-id-19762I think Nathann's solution solves both issues. Maybe the "name" option should be used by default.Sun, 20 May 2012 07:49:23 +0200https://ask.sagemath.org/question/8984/pshow-at-mixedintegerlinearprogram-results/?comment=19762#post-id-19762