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.Sun, 06 Oct 2019 20:58:40 +0200Reuse of already entered parametershttps://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/ I would like to know if there is a general procedure to reuse some already enter parameters. For instance In
> MixedIntegerLinearProgram
one can enter an inequation as
> p.add_constraint( a[1,1]*x[0] + a[1,2]*x[1] + c[1,3]*x[2] >= b [1])
and now I need a[1, 1] ..., a[1, 3] and b[1]
to write
>pol = Polyhedron(ieqs = [[-b [1], a[1, 1], a[1, 2]],...].
And when the result of a calculaton is verbose --- composed of strings and numbers ---, how to keep only the interesting part ?
Sun, 06 Oct 2019 19:03:35 +0200https://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/Comment by dsejas for <p>I would like to know if there is a general procedure to reuse some already enter parameters. For instance In </p>
<blockquote>
<p>MixedIntegerLinearProgram</p>
</blockquote>
<p>one can enter an inequation as </p>
<blockquote>
<p>p.add_constraint( a[1,1]<em>x[0] + a[1,2]</em>x[1] + c[1,3]*x[2] >= b [1])</p>
</blockquote>
<p>and now I need a[1, 1] ..., a[1, 3] and b[1]</p>
<p>to write </p>
<blockquote>
<p>pol = Polyhedron(ieqs = [[-b [1], a[1, 1], a[1, 2]],...].</p>
</blockquote>
<p>And when the result of a calculaton is verbose --- composed of strings and numbers ---, how to keep only the interesting part ?</p>
https://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/?comment=48209#post-id-48209Hello, @Cyrille! I think I have been able to answer the first part of your question (see below).
Concerning the last part of your question,
> And when the result of a calculation is verbose --- composed of strings and numbers ---, how to keep only the interesting part ?
I am afraid I didn't understand that part. Could you show a specific example or give a little more detail?Sun, 06 Oct 2019 20:58:40 +0200https://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/?comment=48209#post-id-48209Answer by dsejas for <p>I would like to know if there is a general procedure to reuse some already enter parameters. For instance In </p>
<blockquote>
<p>MixedIntegerLinearProgram</p>
</blockquote>
<p>one can enter an inequation as </p>
<blockquote>
<p>p.add_constraint( a[1,1]<em>x[0] + a[1,2]</em>x[1] + c[1,3]*x[2] >= b [1])</p>
</blockquote>
<p>and now I need a[1, 1] ..., a[1, 3] and b[1]</p>
<p>to write </p>
<blockquote>
<p>pol = Polyhedron(ieqs = [[-b [1], a[1, 1], a[1, 2]],...].</p>
</blockquote>
<p>And when the result of a calculaton is verbose --- composed of strings and numbers ---, how to keep only the interesting part ?</p>
https://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/?answer=48208#post-id-48208Hello, @Cyrille! I am not completely sure that I understood your question, but you could store constants in a matrix and a vector. For example, suppose you want to rewrite the following MILP:
LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(-2*x[1] - x[2] >= 1.5)
LP.add_constraint(-x[1] - x[2] >= 1)
LP.add_constraint(-x[1] - 2*x[2] >= 1)
LP.set_objective(5*x[1] - 4*x[2])
Then you could define a matrix A containing all the coefficients for the variables, and a vector containing all the constant terms. In this particular case,
A = matrix(3, 2, [-2, -1, -1, -1, -1, -2])
b = vector([-1.5, -1, -1])
Now, you can rewrite the problem as you indicate in your question:
LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(A[0,0]*x[1] + A[0,1]*x[2] >= b[0])
LP.add_constraint(A[1,0]*x[1] + A[1,1]*x[2] >= b[1])
LP.add_constraint(A[2,0]*x[1] + A[2,1]*x[2] >= b[2])
LP.set_objective(5*x[1] + 4*x[2])
And you even can reuse the numbers in the `Polyhedron` command. For example,
pol = Polyhedron(ieqs = [[-b[0], A[0,0], A[0,1]], [-b[1], A[1,0], A[1,1]], [-b[2], A[2,0], A[2,1]], [0, 1, 0], [0, 0, 1]])
pol.show()
will show you the polyhedron of this MILP in particular.
However, notice that this method has its drawbacks: For example, you should always write the inequalities as `>=` in order to make them correspond with the way `Polyhedron(ieqs=...)` draws. Another disadvantage is that the `Polyhedron` command doesn't take into account that your variables are nonnegative, so you should add those restrictions manually as I did in the last piece of code (note the `[0, 1, 0], [0, 0, 1]`).
If all you want is to draw the feasible polyhedron of a MILP, you can use the `polyhedron` method (yes, lowercase letters), like in the following example:
pol = LP.polyhedron()
pol.show()
This will work, no matter how you write your problem, or the inequalities, and you don't need to do any special for non-negative variables (this is already taken into account due to the command `x = LP.new_variable(nonnegative=True)`).Sun, 06 Oct 2019 20:56:28 +0200https://ask.sagemath.org/question/48206/reuse-of-already-entered-parameters/?answer=48208#post-id-48208