a Variable which is the upper bound of a sum
AnsweredHello,
I would like to know if it is possible to model the following expression in gurobi and if gurobi is able to solve it:
M is an integer variable that takes value 1, 2, 3 or 4.
This variable is actually presents in the upperbound of a summation:
1sum_{i from 1 to M } P_i
P_i are given in the problem
Regards,
Ibtissam
1

Hi,
I would model this using auxiliary variables /(y_i/):
M = m.addVar(vtype=GRB.INTEGER,lb=1,ub=4)
y_i = m.addVars(4, vtype=GRB.BINARY, name='y')
1sum(P_i*y_i) # The constraint you want
m.addConstrs(i*y_i <= M for i in range(4)) # This forces y_i to be 1 only if M reaches itIs this what you are looking for?
0 
Hello Richard,
Thank you for your response. Yes it does the trick. Although, I think that : the range should go to 5. So that y_4 can be taken into consideration :
m.addConstrs(i*y_i <= M for i in range(5))
Please, correct me if I am wrong.
Regards,
Ibtissam
0 
Yes, you are right, it actually should be
m.addConstrs(i*y_i <= M for i in range(1,5))
since y_0 does not exist.
0
Please sign in to leave a comment.
Comments
3 comments