def branch_n_price(n, demands, capacity, distances, MasterProb): queue = PriorityQueue() MasterProb.RelaxOptimize() obj_val = MasterProb.relax_modelo.ObjVal queue.insert(obj_val, MasterProb) best_int_obj = 1e3 best_relax_obj = 1e3 nodes_explored = 0 best_model = None while not queue.isEmpty(): obj_val, MP_branch = queue.delete() nodes_explored += 1 MP_branch.RelaxOptimize() solution = MP_branch.getSolution() duals = MP_branch.getDuals() branch_cost = MP_branch.getCosts() branch_routes = MP_branch.modelo.getA().toarray() sol_is_int = all([float(round(s, 4)).is_integer() for s in solution]) # sol_is_int = all([False if i > 0.3 and np.abs(i - 1.0) > 0.3 else True for i in solution ]) if obj_val < best_int_obj and sol_is_int: print(f"Best Integer Obj: {obj_val}") print(f"Nodes explored: {nodes_explored}") best_int_obj = obj_val # print(f"best sol: {solution}") best_model = copy_model(branch_cost, branch_routes, MP_branch) if obj_val < best_relax_obj: print(f"Best Relaxed Obj: {obj_val}") print(f"Nodes explored: {nodes_explored}") best_relax_obj = obj_val # --- # --- # Column generation # --- # --- # new_MP = column_generation(n, demands, capacity, distances, duals, MP_branch) if new_MP != None: new_MP.RelaxOptimize() branch_cost = new_MP.getCosts() branch_routes = new_MP.modelo.getA().toarray() if new_MP.relax_modelo.ObjVal <= best_relax_obj: queue.insert(new_MP.relax_modelo.ObjVal, copy_model(branch_cost, branch_routes, new_MP)) else: # --- # If stopped col generation then branch if solution is not integer # --- # if not sol_is_int: # print("#--#--#--# Not integer solution ........Branching") queue = branch(branch_cost, branch_routes, n, demands, capacity, distances, duals, solution, MP_branch, queue, best_relax_obj) else: # print(f"best sol: {solution}") best_model = MP_branch return best_model