# 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)

edit retag close merge delete

Sort by » oldest newest most voted

for 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)

more

Thanks 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.

1

It 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.

Obviously, 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.

That'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.

nbruin 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.