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, 18 Nov 2019 08:49:47 +0100Double indexed variables in linear programminghttps://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/The following linear programming code works perfectly.
nlg=4 #nombre de contraintes
mlg=6 #nombre de variables
Alg= matrix(nlg,mlg,[0,0,0,1,1,1,
1,0,0,1,0,0,
0,1,0,0,1,0,
1,1,3,0,0,0
]) # les coefficients
blgmin = [1,0,0,1] # les bornes inférieures pour les contraintes
blgmax = [1,0,0,3] # les bornes supérieures pour les contraintes (oo = infini)
show(LatexExpr('A = '),Alg)
show(LatexExpr('bmin = '),blgmin)
show(LatexExpr('bmax = '),blgmax)
lg = MixedIntegerLinearProgram(maximization=False, solver = "GLPK") # on crèe le programme
x = lg.new_variable(integer=True, indices=[0..mlg-1]) # les nouvelles variables sont x[0] ... x[5]
Blg = Alg * x # la fonction linéaire pour les contraintes
lg.set_objective(x[0])# fixe l’objectif
# On construit les contraintes avec leurs bornes
for i in range(nlg) :
lg.add_constraint(Blg[i], min=blgmin[i], max=blgmax[i])
for i in range(mlg-1) :
lg.set_binary(x[i])
lg.show()
lg.solve()
xx=lg.get_values(x)
show(xx)
But now, for an other problem, I would like to define
1) variables with other names
2) double indexed variables
Is this possible ?
Tue, 12 Nov 2019 06:41:44 +0100https://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/Answer by nbruin for <p>The following linear programming code works perfectly. </p>
<pre><code>nlg=4 #nombre de contraintes
mlg=6 #nombre de variables
Alg= matrix(nlg,mlg,[0,0,0,1,1,1,
1,0,0,1,0,0,
0,1,0,0,1,0,
1,1,3,0,0,0
]) # les coefficients
blgmin = [1,0,0,1] # les bornes inférieures pour les contraintes
blgmax = [1,0,0,3] # les bornes supérieures pour les contraintes (oo = infini)
show(LatexExpr('A = '),Alg)
show(LatexExpr('bmin = '),blgmin)
show(LatexExpr('bmax = '),blgmax)
lg = MixedIntegerLinearProgram(maximization=False, solver = "GLPK") # on crèe le programme
x = lg.new_variable(integer=True, indices=[0..mlg-1]) # les nouvelles variables sont x[0] ... x[5]
Blg = Alg * x # la fonction linéaire pour les contraintes
lg.set_objective(x[0])# fixe l’objectif
# On construit les contraintes avec leurs bornes
for i in range(nlg) :
lg.add_constraint(Blg[i], min=blgmin[i], max=blgmax[i])
for i in range(mlg-1) :
lg.set_binary(x[i])
lg.show()
lg.solve()
xx=lg.get_values(x)
show(xx)
</code></pre>
<p>But now, for an other problem, I would like to define </p>
<p>1) variables with other names </p>
<p>2) double indexed variables</p>
<p>Is this possible ?</p>
https://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/?answer=48766#post-id-48766The primary resource for these question is the documentation: http://doc.sagemath.org/html/en/reference/numerical/sage/numerical/mip.html
as is explained there, the *indexing* of variables (so, how you refer the the indices in your code) is very flexible, and you can just use multi-indices as you like: the interface will keep track of the indices you use and map them to an internal index. The printing of variables is a little more restricted: it looks like variables just always print in the form `<name>_<index>`.
You can use variables with other names, but presently this seems to be relatively limited:
sage: p = MixedIntegerLinearProgram(solver='GLPK')
sage: x = p.new_variable(real=True, nonnegative=True, name='X')
sage: y = p.new_variable(integer=True, nonnegative=True, name='Y')
sage: x[1],x[2],y[1],y[2]
(x_0, x_1, x_2, x_3)
As the documentation says:
* "name" -- string. Associates a name to the variable. This is
only useful when exporting the linear program to a file using
"write_mps" or "write_lp", and has no other effect.
so it looks like presently, the name argument is essentially a no-op. It suggests that presently, there is excellent support for *writing* linear programming problems with very rich variable names and indexing, but the printing seems to focus on the linear algebra representation of the problem, where your variables are just entries in a vector. It would seem this could be changed, but might require discussion if the benefits of that are worth the breaking of backwards compatibility that it would entail.
From a teaching point of view, there is an argument to be made that helping people understand that variables are just entries in a vector (and therefore are naturally labeled with x_0,x_1,x_2,...), but printing that is a little closer to the commands used to input the problem would also be nice; especialy given that x_0,x_1, ... are NEVER appropriate names in a sage session to refer to variables in a MIP.
So ... DEFINING the things you want seems to be fully supported, but having them PRINT in a way that reflects it seems not to be.Fri, 15 Nov 2019 22:55:11 +0100https://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/?answer=48766#post-id-48766Comment by David Coudert for <p>The primary resource for these question is the documentation: <a href="http://doc.sagemath.org/html/en/reference/numerical/sage/numerical/mip.html">http://doc.sagemath.org/html/en/refer...</a>
as is explained there, the <em>indexing</em> of variables (so, how you refer the the indices in your code) is very flexible, and you can just use multi-indices as you like: the interface will keep track of the indices you use and map them to an internal index. The printing of variables is a little more restricted: it looks like variables just always print in the form <code><name>_<index></code>.</p>
<p>You can use variables with other names, but presently this seems to be relatively limited:</p>
<pre><code>sage: p = MixedIntegerLinearProgram(solver='GLPK')
sage: x = p.new_variable(real=True, nonnegative=True, name='X')
sage: y = p.new_variable(integer=True, nonnegative=True, name='Y')
sage: x[1],x[2],y[1],y[2]
(x_0, x_1, x_2, x_3)
</code></pre>
<p>As the documentation says:</p>
<ul>
<li>"name" -- string. Associates a name to the variable. This is
only useful when exporting the linear program to a file using
"write_mps" or "write_lp", and has no other effect.</li>
</ul>
<p>so it looks like presently, the name argument is essentially a no-op. It suggests that presently, there is excellent support for <em>writing</em> linear programming problems with very rich variable names and indexing, but the printing seems to focus on the linear algebra representation of the problem, where your variables are just entries in a vector. It would seem this could be changed, but might require discussion if the benefits of that are worth the breaking of backwards compatibility that it would entail.</p>
<p>From a teaching point of view, there is an argument to be made that helping people understand that variables are just entries in a vector (and therefore are naturally labeled with x_0,x_1,x_2,...), but printing that is a little closer to the commands used to input the problem would also be nice; especialy given that x_0,x_1, ... are NEVER appropriate names in a sage session to refer to variables in a MIP.</p>
<p>So ... DEFINING the things you want seems to be fully supported, but having them PRINT in a way that reflects it seems not to be.</p>
https://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/?comment=48792#post-id-48792To complete your answer, we can currently get:
sage: p.add_constraint(x[0] + x[1] + y[1] - y[2] == 0)
sage: p.add_constraint(x[0] + y[1,2] <= 3)
sage: p.show()
Maximization:
Constraints:
0.0 <= X[1] + Y[1] - Y[2] + X[0] <= 0.0
X[0] + Y[(1, 2)] <= 3.0
Variables:
X[1] = x_0 is a continuous variable (min=0.0, max=+oo)
X[2] = x_1 is a continuous variable (min=0.0, max=+oo)
Y[1] = x_2 is an integer variable (min=0.0, max=+oo)
Y[2] = x_3 is an integer variable (min=0.0, max=+oo)
X[0] = x_4 is a continuous variable (min=0.0, max=+oo)Mon, 18 Nov 2019 08:49:47 +0100https://ask.sagemath.org/question/48702/double-indexed-variables-in-linear-programming/?comment=48792#post-id-48792