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.Fri, 15 Nov 2019 16:32:13 +0100Linear programming sum and double sumhttps://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/ I wonder why those syntax are invalid
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum([v[i,1] for i in [0,3]) <= 1)
show(p)
or
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum(sum([v[i,j] for i in [0,3]),for j in [1, 5]) <= 1)
show(p)Thu, 14 Nov 2019 07:24:32 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/Answer by ortollj for <p>I wonder why those syntax are invalid</p>
<pre><code>p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum([v[i,1] for i in [0,3]) <= 1)
show(p)
</code></pre>
<p>or</p>
<pre><code>p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint(sum(sum([v[i,j] for i in [0,3]),for j in [1, 5]) <= 1)
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?answer=48723#post-id-48723for the first one you forgot a ]
second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)
i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)Thu, 14 Nov 2019 08:17:57 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?answer=48723#post-id-48723Comment by Cyrille for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48724#post-id-48724Thanks Ortollj, I have added the missing ] but I have a sum of only two variables and if I write
p.add_constraint(sum([i*v[i,1] for i in [0,3]) <= 1) I have not the expected result. I will look at the second answer later. After a look, it doesn't do what I expect a sum of 20 variables with two indexes.Thu, 14 Nov 2019 09:32:00 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48724#post-id-48724Comment by nbruin for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48734#post-id-48734It looks like the print names of variables will never have two indices, but the indices are stored on v. I think you can get your sum of 20 variables with
sum([v[i,j] for i in range(3) for j in range(1,6)])
Afterwards, you can call v.items() to get a list showing how indices relate to print names of the variables.Thu, 14 Nov 2019 21:09:46 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48734#post-id-48734Comment by Cyrille for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48735#post-id-48735Obviously, the lack of a simple procedure of a double sommation disqualify Sagemath to be taught to students in Integer or mixed linear programming because it is nearly impossible to write a readable program. I am profoundly deceived. I was persuaded that if would be very simple with Sagemath. I have began to use it for my lessons in linear programming but I think it's a mistake.Thu, 14 Nov 2019 22:27:35 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48735#post-id-48735Comment by nbruin for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48739#post-id-48739That's a strange response when someone just gives you a very simple one-liner to do a double summation, that basically reads like plain english.Fri, 15 Nov 2019 06:42:06 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48739#post-id-48739Comment by Cyrille for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48750#post-id-48750nbruin I was not saying that your answer was not nice. If it was for my research I will be happy. I was only saying that I do not see how to use it to teach to economic students who are not specially brillant and just begin to learn programmation. I was expecting that I could directly define doubly indexed variables and what ever be the way sagemath transmit it to the solver I could received the result directly in a transparent format. Nevertheless I will try to write a program and see if I can do something with it.Fri, 15 Nov 2019 11:29:04 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48750#post-id-48750Comment by Iguananaut for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48754#post-id-48754@Cyrille "I do not see how to use it to teach to economic students who are not specially brillant" wow I hope none of your students see this. If you are teaching them Sage then you are teaching them Python, a general purpose programming language that may (or may not) serve them in many different contexts. In some places Python is being taught to 10 year olds. I don't think it's that hard, and it's not clear what you are expecting here.Fri, 15 Nov 2019 14:14:36 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48754#post-id-48754Comment by Cyrille for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48757#post-id-48757You are right. My soon learn it at 11 and has been selected for a doctorat in physic with the head of the normal sup school in Paris because he was expert in python. But I see you do not cope with french normal students. Nevertheless, I must apologize to nbruin,. When I was reading his answer I have not a sheet of paper under the hand. I have realized that its only a question of variable redefinition. I will send a new question linked to his answer. And sorry for my reaction which was more because opf desapointment.Fri, 15 Nov 2019 16:20:31 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48757#post-id-48757Comment by Cyrille for <p>for the first one you forgot a ]</p>
<p>second I'm not sure this below is what you want to do(sorry if it does not, tell me and I will erase my answer)</p>
<pre><code>i = [ 0, 3]
j = [1, 5]
ijzip = zip(i, j)
p = MixedIntegerLinearProgram(solver='GLPK')
v = p.new_variable(nonnegative=True)
p.add_constraint((sum([sum([ v[i,j]]) for i,j in ijzip ]) <= 1))
show(p)
</code></pre>
https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48758#post-id-48758The good answer is in the comments. One more time I am so sorry to have baddly reacted.Fri, 15 Nov 2019 16:32:13 +0100https://ask.sagemath.org/question/48722/linear-programming-sum-and-double-sum/?comment=48758#post-id-48758