Algorithm
AnsweredHi everyone!
I have a question about coding in Python with the Gurobi package. I am coding an algorithm. In each iteration, there is a constraint (constraint_90 in the master problem) in which the right-handed side needs to be updated. This value is gained from the previous model (the subproblem), and all the previous constraints with related past right-handed sides must be kept as well in each iteration. It is similar to adding cuts to find the optimal solution. I have coded this, but I am not sure about it and want to check with you. You can find the algorithm and codes in the following. Please note that pi_n is obtained from the subproblem and used as an input to the master problem.
#constraint_90
constraints=[]
constraint = gamma >= pi_n
for ro in range(eta+1):
cut = model.addConstr(constraint)
constraints.append(cut)
for prev_constraint in constraints[:-1]:
model.addConstr(prev_constraint)
pi_n = solve_subproblem(mu0_0, mu1_0, mu2_0, f_star, C, G, o, f, M, p, m, b, a, d)
gamma_tilde, mu0_tilde, mu1_tilde, mu2_tilde = solve_masterproblem(pi_n, f_star, eta, C, G, o, f, M, p, m, b, a, d)
eta +=1
-
Hi Vida,
Note that the addConstr method already adds the given constraint to the model. This means that you add all constraints twice except for the last one. You first add constraints in the first \(\texttt{for}\)-loop
for ro in range(eta+1): cut = model.addConstr(constraint) constraints.append(cut)
and then you add all those constraints again except for the last one
for prev_constraint in constraints[:-1]: model.addConstr(prev_constraint)
Could you explain what exactly are you trying to achieve in those \(\texttt{for}\)-loops? You can always use the write method to write the model you construct to a human-readable LP file
model.write("myModel.lp")You can open the \(\texttt{myModel.lp}\) file in any standard text editor.
Best regards,
Jaromił0 -
Hi again,
Thank you for your time.
A small part of the loop is here:
eta = 0
while True:
pi_n = solve_subproblem(mu0_0, mu1_0, mu2_0, f_star, C, G, o, f, M, p, m, b, a, d)
gamma_tilde, mu0_tilde, mu1_tilde, mu2_tilde = solve_masterproblem(pi_n, f_star, eta, C, G, o, f, M, p, m, b, a, d)
eta +=1
.
.
.Also, the constraint_90 is added internally within the
solve_masterproblemfunction. In each iteration, new pi_n is obtained. What I want is to ensure that the master problem contains all the constraint_90 from past iterations along with the new one added in the current iteration.Regards,
Vida
0 -
Hi Vida,
Also, the constraint_90 is added internally within the
solve_masterproblemfunction. In each iteration, new pi_n is obtained. What I want is to ensure that the master problem contains all the constraint_90 from past iterations along with the new one added in the current iteration.As long as you do not rebuild the master problem from scratch or use the remove method, old constraints will not be removed from the master problem. To verify this, you can use the write method after each iteration
model.write("myModel.lp")You can open the \(\texttt{myModel.lp}\) file in any standard text editor.
I think this is the best way to make sure that your algorithm behaves as expected and your model looks what it should look like.
Best regards,
Jaromił0
Please sign in to leave a comment.
pi_n = solve_subproblem(mu0_0, mu1_0, mu2_0, f_star, C, G, o, f, M, p, m, b, a, d)
Comments
3 comments