Ask Your Question
0

MixedIntegerProgramming with more than 2 indexes

asked 2024-03-15 15:45:34 +0100

Cyrille gravatar image

updated 2024-03-15 16:26:23 +0100

Some integer linear programming needs more indexes than two. But this works

p = MixedIntegerLinearProgram(maximization=False, solver="GLPK")
x = p.new_variable(integer=True, nonnegative=True)
p.set_objective(sum(sum(x[i,t] for t in range(10)) for i in range(5)));
p.add_constraint(sum(sum(x[i,t] for t in range(10)) for i in range(5))<=1);
p.show()

and this doesn't.

p = MixedIntegerLinearProgram(maximization=False, solver="GLPK")
x = p.new_variable(integer=True, nonnegative=True)
p.set_objective(sum(sum(sum(x[i,j,t] for t in range(10)) for i in range(5)))for j in range(5));
p.add_constraint(sum(sum(sum(x[i,j,t] for t in range(10)) for i in range(5))) for j in range(5));
p.show()

Is there a way out ?

I would ask for uniformity of notations. The code for sum() is not the same in Linear programming and outside. This is missleading.

edit retag flag offensive close merge delete

Comments

Closing parenthesis are not well placed. This is working

p = MixedIntegerLinearProgram(maximization=False, solver="GLPK")
x = p.new_variable(integer=True, nonnegative=True)
p.set_objective(sum(sum(sum(x[i,j,t] for t in range(10)) for i in range(5)) for j in range(5)));
p.add_constraint(sum(sum(sum(x[i,j,t] for t in range(10)) for i in range(5)) for j in range(5)) <= 1);
p.show()
David Coudert gravatar imageDavid Coudert ( 2024-03-16 09:46:53 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2024-03-15 17:58:01 +0100

Max Alekseyev gravatar image

updated 2024-03-15 21:53:38 +0100

A simple solution is to use tuples as indices, that is, x[(i,j,t)] instead of x[i,j,t]. They may have as many elements as you like.

As for the sum(), it is enough to call it once, no matter how many summation indices you have inside - e.g.:

sum( x[(i,j,t)] for t in range(10) for i in range(5) for j in range(5) )
edit flag offensive delete link more

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: 2024-03-15 15:45:34 +0100

Seen: 109 times

Last updated: Mar 15