Ask Your Question
0

Linear programming sum and double sum

asked 2019-11-14 00:24:32 -0500

Cyrille gravatar image

updated 2019-11-14 05:33:05 -0500

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

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-11-14 01:17:57 -0500

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))
show(p)
edit flag offensive delete link more

Comments

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 02:32:00 -0500 )edit
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.

nbruin gravatar imagenbruin ( 2019-11-14 14:09:46 -0500 )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 15:27:35 -0500 )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-14 23:42:06 -0500 )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 04:29:04 -0500 )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

Stats

Asked: 2019-11-14 00:24:32 -0500

Seen: 74 times

Last updated: Nov 14 '19