def solve_model(mdl): mdl.parameters.timelimit = 120 # Call to custom code to update parameters value custom_code.update_solver_params(mdl.parameters) # Update parameters value based on environment variables definition cplex_param_env_prefix = 'ma.cplex.' cplex_params = [name.qualified_name for name in mdl.parameters.generate_params()] for param in cplex_params: env_param = cplex_param_env_prefix + param param_value = get_environment().get_parameter(env_param) if param_value: # Updating parameter value print("Updated value for parameter %s = %s" % (param, param_value)) parameters = mdl.parameters for p in param.split('.')[1:]: parameters = parameters.__getattribute__(p) parameters.set(param_value) msol = mdl.solve(log_output=True) if not msol: print("!!! Solve of the model fails") if mdl.get_solve_status() == JobSolveStatus.INFEASIBLE_SOLUTION or mdl.get_solve_status() == JobSolveStatus.INFEASIBLE_OR_UNBOUNDED_SOLUTION: crefiner = ConflictRefiner() conflicts = crefiner.refine_conflict(model, log_output=True) export_conflicts(conflicts) print 'Solve status: ', mdl.get_solve_status() mdl.report() return msol
def solve_model(mdl): mdl.parameters.timelimit = 120 msol = mdl.solve(log_output=True) if not msol: print("!!! Solve of the model fails") if mdl.get_solve_status() == JobSolveStatus.INFEASIBLE_SOLUTION: crefiner = ConflictRefiner() conflicts = crefiner.refine_conflict(model, log_output=True) export_conflicts(conflicts) print 'Solve status: ', mdl.get_solve_status() mdl.report() return msol
def solve(self, refine_conflict: bool = False, **kwargs) -> docplex.mp.solution.SolveSolution: msol = self.mdl.solve(**kwargs) # log_output=True if msol is not None: print('Found a solution') self.mdl.report() self.mdl.print_solution() else: print('No solution') if refine_conflict: print('Conflict Refiner:') crefiner = ConflictRefiner() # Create an instance of the ConflictRefiner conflicts = crefiner.refine_conflict(self.mdl) # Run the conflict refiner # ConflictRefiner.display_conflicts(conflicts) #Display the results for c in conflicts: print( c.element) # Display conflict result in a little more compact format than ConflictRefiner.display_conflicts return msol
rx = Relaxer() rx.relax(mdl) print("number_of_relaxations= " + str(rx.number_of_relaxations)) rx.print_information() mdl.report() print(f"* status after relaxation is: {mdl.solve_details.status}") #print(mdl.solution) print() print("------ starting conflict refiner") print() cr = ConflictRefiner() conflicts = cr.refine_conflict(mdl) conflicts.display() # for conflict in conflicts: # st = conflict.status # ct = conflict.element # label = conflict.name # label_type = type(conflict.element) # if isinstance(conflict.element, VarLbConstraintWrapper) \ # or isinstance(conflict.element, VarUbConstraintWrapper): # ct = conflict.element.get_constraint() # # # Print conflict information in console # print("Conflict involving constraint: %s" % label) # print(" \tfor: %s" % ct) """
def run(self): objective = self.model.sum(self.PriceProv[j] for j in range(self.nr_vms)) self.model.minimize(objective) # self.model.prettyprint("out") self.model.export_as_lp(self.cplexLPPath) # self.model.export_as_mps("nou") vmPrice = [] vmType = [] a_mat = [] self.get_current_time() starttime = time.time() xx = self.model.solve() stoptime = time.time() print("CPLEX: ", self.model.get_solve_status(), self.model.get_statistics) if dc.status.JobSolveStatus.OPTIMAL_SOLUTION == self.model.get_solve_status( ): print(self.model.solve_details) print("CPLEX vmType") # Variables for offers description vmType = self._get_solution_vm_type() print("\nvmPrice") for index, var in self.PriceProv.items(): print(var.solution_value, end=" ") vmPrice.append(var.solution_value) print("\nVm Aquired") for index, var in self.vm.items(): print(var.solution_value, end=" ") print() l = [] col = 0 for index, var in self.a.items(): if col == self.problem.nrVM: a_mat.append(l) l = [] col = 0 col += 1 l.append(int(var.solution_value)) a_mat.append(l) print("\nAllocation matrix M x N", self.nrVM, self.nrComp) for l in a_mat: print(l) print(xx.get_objective_value()) else: print("Unsolve CPLEX") print(self.model.get_time_limit()) cr = ConflictRefiner() conflicts = cr.refine_conflict(self.model) print(self.model.get_solve_status()) for conflict in conflicts: st = conflict.status ct = conflict.element label = conflict.name label_type = type(conflict.element) if isinstance(conflict.element, VarLbConstraintWrapper)\ or isinstance(conflict.element, VarUbConstraintWrapper): ct = conflict.element.get_constraint() # Print conflict information in console print("Conflict involving constraint: %s" % label) print(" \tfor: %s" % ct) return xx.get_objective_value( ) / 1000, vmPrice, stoptime - starttime, a_mat, vmType