# MixedIntegerProgramming with more than 2 indexes

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 close merge delete

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

( 2024-03-16 09:46:53 +0200 )edit

Sort by ยป oldest newest most voted

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

more