def build_other_solution(self, result_storage: ResultStorage) -> ResultStorage: bs = result_storage.get_best_solution() colors = bs.colors set_colors = sorted(set(colors)) nb_colors = len(set(set_colors)) new_color_dict = {set_colors[i]: i for i in range(nb_colors)} new_solution = ColoringSolution( problem=self.problem, colors=[new_color_dict[colors[i]] for i in range(len(colors))]) fit = self.aggreg_from_sol(new_solution) result_storage.add_solution(new_solution, fit) return result_storage
def build_other_solution(self, result_storage: ResultStorage) -> ResultStorage: new_solution = sgs_variant(solution=result_storage.get_best_solution(), problem=self.problem, predecessors_dict=self.immediate_predecessors) fit = self.aggreg_from_sol(new_solution) result_storage.add_solution(new_solution, fit) import random for s in random.sample(result_storage.list_solution_fits, min(len(result_storage.list_solution_fits), 50)): new_solution = sgs_variant(solution=s[0], problem=self.problem, predecessors_dict=self.immediate_predecessors) fit = self.aggreg_from_sol(new_solution) result_storage.add_solution(new_solution, fit) return result_storage
def adding_constraint_from_results_store( self, cp_solver: CPSolver, child_instance, result_storage: ResultStorage) -> Iterable[Any]: range_node = range(1, self.problem.number_of_nodes + 1) current_solution = result_storage.get_best_solution() subpart_color = set( random.sample( range_node, int(self.fraction_to_fix * self.problem.number_of_nodes))) dict_color = { i + 1: current_solution.colors[i] + 1 for i in range(self.problem.number_of_nodes) } current_nb_color = max(dict_color.values()) for i in range_node: if i in subpart_color and dict_color[i] < current_nb_color: child_instance.add_string("constraint color_graph[" + str(i) + "] == " + str(dict_color[i]) + ";\n") child_instance.add_string("constraint color_graph[" + str(i) + "] <= " + str(current_nb_color) + ";\n")
def adding_constraint_from_results_store(self, milp_solver: LP_Solver_MRSCPSP, result_storage: ResultStorage) -> Iterable[ Any]: current_solution: MS_RCPSPSolution = result_storage.get_best_solution() # st = milp_solver.start_solution # if self.problem.evaluate(st)["makespan"] < self.problem.evaluate(current_solution)["makespan"]: # current_solution = st start = [] for j in current_solution.schedule: start_time_j = current_solution.schedule[j]["start_time"] mode = current_solution.modes[j] start += [(milp_solver.start_times_task[j], start_time_j)] start += [(milp_solver.modes[j][mode], 1)] for m in milp_solver.modes[j]: start += [(milp_solver.modes[j][m], 1 if mode == m else 0)] milp_solver.model.start = start constraints_dict = {} constraints_dict["range_start_time"] = [] max_time = max([current_solution.schedule[x]["end_time"] for x in current_solution.schedule]) last_jobs = [x for x in current_solution.schedule if current_solution.schedule[x]["end_time"] >= max_time - 5] nb_jobs = self.problem.nb_tasks jobs_to_fix = set(random.sample(current_solution.schedule.keys(), int(self.fraction_to_fix * nb_jobs))) for lj in last_jobs: if lj in jobs_to_fix: jobs_to_fix.remove(lj) for job in jobs_to_fix: start_time_j = current_solution.schedule[job]["start_time"] min_st = max(start_time_j - self.minus_delta, 0) max_st = min(start_time_j + self.plus_delta, max_time) constraints_dict["range_start_time"].append(milp_solver.model.add_constr(milp_solver.start_times_task[job] <= max_st)) constraints_dict["range_start_time"].append(milp_solver.model.add_constr(milp_solver.start_times_task[job] >= min_st)) if milp_solver.lp_solver == MilpSolverName.GRB: milp_solver.model.solver.update() return constraints_dict