I have written a manual benders algorithm on python. I utilize CPLEX to solve both master and subproblems.
My problem is, the algorithm never ends. I debugged and found out that even though it adds a new constraint (I do not know how to see the added constraint, I can only see the number of constraints in the master problem increases), the upper and lower bounds never change, the solutions for the sub and master problems are always the same.
Do you see anything extraordinary with the code?
Is there a way that CPLEX allow me to see the added constraints while debugging the code?
Am I adding the optimality cuts in a wrong way?
Is there a more efficient way to solve this problem after splitting the master and subproblems? (I know CPLEX's automatic solver, as far as I know it only solves if you provide the original formulation.)
The code is as follows:
while abs(UB-LB)>8:
sub.solve()
sub_obj_value=sub_obj.solution_value
UB=sub_obj_value
for j in range(facility):
u_bar[j]=u[j].solution_value
master.add_constraint(q <=master.sum(-var[i,j]*u_bar[j]*x[i,j] for i in range(demand) for j in range(facility)), ctname='new constraint')
master.solve()
for i in range(demand):
for j in range(facility):
x_bar[i,j]=x[i,j].solution_value
master_obj_value=master_obj.solution_value
q_value=q.solution_value
LB=q_value
iter=iter+1
- x is the decision variable for the master problem.
- u is the decision variable for the subproblem.
- q is the variable for the optimality cut in the master problem.
- var is a parameter.
Btw, code is not quite efficient. If you have any suggestions on that too, I'd love to hear.
Edit:
Objective of the Subproblem:
sub_obj = sub.sum((var[i,j]*x_bar[i,j]*u[j] for i in range(demand) for j in range(facility)))
sub.set_objective("max", sub_obj)