Ask Your Question

Linear programming sum and double sum

asked 2019-11-14 07:24:32 +0200

Cyrille gravatar image

updated 2023-05-19 21:56:58 +0200

FrédéricC gravatar image

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)


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)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2019-11-14 08:17:57 +0200

ortollj gravatar image

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))
edit flag offensive delete link 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.

Cyrille gravatar imageCyrille ( 2019-11-14 09:32:00 +0200 )edit

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.

nbruin gravatar imagenbruin ( 2019-11-14 21:09:46 +0200 )edit

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.

Cyrille gravatar imageCyrille ( 2019-11-14 22:27:35 +0200 )edit

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 gravatar imagenbruin ( 2019-11-15 06:42:06 +0200 )edit

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.

Cyrille gravatar imageCyrille ( 2019-11-15 11:29:04 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-11-14 07:24:32 +0200

Seen: 674 times

Last updated: Nov 14 '19