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, 16 Jul 2013 05:36:46 +0200How to get slack values when using GLPK backend?https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/Hello,
When using sagemath to compute a linear programming problem(using GLPK backend), is there a way to get the slack values other than the full p.print_ranges()?
that is just send the slacks(surplus) to a variable or vector?
Thanks.Mon, 15 Jul 2013 09:17:24 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/Comment by niles for <p>Hello,
When using sagemath to compute a linear programming problem(using GLPK backend), is there a way to get the slack values other than the full p.print_ranges()?
that is just send the slacks(surplus) to a variable or vector?
Thanks.</p>
https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17282#post-id-17282It would help if you post a small working example of how you are using GLPK.Mon, 15 Jul 2013 12:50:34 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17282#post-id-17282Answer by tmonteil for <p>Hello,
When using sagemath to compute a linear programming problem(using GLPK backend), is there a way to get the slack values other than the full p.print_ranges()?
that is just send the slacks(surplus) to a variable or vector?
Thanks.</p>
https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?answer=15237#post-id-15237There seems not to be a method for that in Sage. Those values are not returned but printed by the method `.print_ranges()`, but you can still get the lines by doing:
sage: s = open(SAGE_TMP+"/ranges.tmp").readlines()
and then try to parse the list of strings `s`.
For example, if you want to extract the values of the "Slack Marginal" field, you can do:
sage: L = []
sage: interesting = False
sage: for line in s:
sage: try:
sage: if line[45:50] == 'Slack':
sage: interesting = True
sage: except IndexError:
sage: pass
sage: if line.startswith('GLPK'):
sage: interesting = False
sage: if interesting:
sage: try:
sage: L.append(RR(line[37:50]))
sage: except TypeError:
sage: pass
Note that i do not know the meaning of the isolated dots in this column (perhaps they mean `0` or something else), so you may have to adapt this.
Mon, 15 Jul 2013 14:31:14 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?answer=15237#post-id-15237Comment by tmonteil for <p>There seems not to be a method for that in Sage. Those values are not returned but printed by the method <code>.print_ranges()</code>, but you can still get the lines by doing:</p>
<pre><code>sage: s = open(SAGE_TMP+"/ranges.tmp").readlines()
</code></pre>
<p>and then try to parse the list of strings <code>s</code>.</p>
<p>For example, if you want to extract the values of the "Slack Marginal" field, you can do:</p>
<pre><code>sage: L = []
sage: interesting = False
sage: for line in s:
sage: try:
sage: if line[45:50] == 'Slack':
sage: interesting = True
sage: except IndexError:
sage: pass
sage: if line.startswith('GLPK'):
sage: interesting = False
sage: if interesting:
sage: try:
sage: L.append(RR(line[37:50]))
sage: except TypeError:
sage: pass
</code></pre>
<p>Note that i do not know the meaning of the isolated dots in this column (perhaps they mean <code>0</code> or something else), so you may have to adapt this.</p>
https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17280#post-id-17280Not really, the `.print_ranges()` method only prints the file `SAGE_TMP+"/ranges.tmp"`, and this file seems to be created by GLPK since it does not appear anywhere else in the Sage source code.Mon, 15 Jul 2013 16:53:35 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17280#post-id-17280Comment by niles for <p>There seems not to be a method for that in Sage. Those values are not returned but printed by the method <code>.print_ranges()</code>, but you can still get the lines by doing:</p>
<pre><code>sage: s = open(SAGE_TMP+"/ranges.tmp").readlines()
</code></pre>
<p>and then try to parse the list of strings <code>s</code>.</p>
<p>For example, if you want to extract the values of the "Slack Marginal" field, you can do:</p>
<pre><code>sage: L = []
sage: interesting = False
sage: for line in s:
sage: try:
sage: if line[45:50] == 'Slack':
sage: interesting = True
sage: except IndexError:
sage: pass
sage: if line.startswith('GLPK'):
sage: interesting = False
sage: if interesting:
sage: try:
sage: L.append(RR(line[37:50]))
sage: except TypeError:
sage: pass
</code></pre>
<p>Note that i do not know the meaning of the isolated dots in this column (perhaps they mean <code>0</code> or something else), so you may have to adapt this.</p>
https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17281#post-id-17281surely there are some clues for a better solution in the source code of `.print_ranges()`, aren't there?Mon, 15 Jul 2013 15:39:50 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17281#post-id-17281Comment by Shimi for <p>There seems not to be a method for that in Sage. Those values are not returned but printed by the method <code>.print_ranges()</code>, but you can still get the lines by doing:</p>
<pre><code>sage: s = open(SAGE_TMP+"/ranges.tmp").readlines()
</code></pre>
<p>and then try to parse the list of strings <code>s</code>.</p>
<p>For example, if you want to extract the values of the "Slack Marginal" field, you can do:</p>
<pre><code>sage: L = []
sage: interesting = False
sage: for line in s:
sage: try:
sage: if line[45:50] == 'Slack':
sage: interesting = True
sage: except IndexError:
sage: pass
sage: if line.startswith('GLPK'):
sage: interesting = False
sage: if interesting:
sage: try:
sage: L.append(RR(line[37:50]))
sage: except TypeError:
sage: pass
</code></pre>
<p>Note that i do not know the meaning of the isolated dots in this column (perhaps they mean <code>0</code> or something else), so you may have to adapt this.</p>
https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17276#post-id-17276Thanks for the reply. The problem wound up being much simpler to formulate and get the necessary values using lp_solve.Tue, 16 Jul 2013 05:36:46 +0200https://ask.sagemath.org/question/10352/how-to-get-slack-values-when-using-glpk-backend/?comment=17276#post-id-17276