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 solve(self, initial_variable: Solution, nb_iteration_max: int, pickle_result=False, pickle_name="tsp") -> ResultLS: objective = self.aggreg_from_dict_values( self.evaluator.evaluate(initial_variable)) cur_variable = initial_variable.copy() if self.store_solution: store = ResultStorage(list_solution_fits=[(initial_variable, objective)], best_solution=initial_variable.copy(), limit_store=True, nb_best_store=1000) else: store = ResultStorage(list_solution_fits=[(initial_variable, objective)], best_solution=initial_variable.copy(), limit_store=True, nb_best_store=1) cur_best_variable = initial_variable.copy() cur_objective = objective cur_best_objective = objective self.restart_handler.best_fitness = objective iteration = 0 while iteration < nb_iteration_max: accept = False local_improvement = False global_improvement = False if self.mode_mutation == ModeMutation.MUTATE: nv, move = self.mutator.mutate(cur_variable) objective = self.aggreg_from_solution(nv) elif self.mode_mutation == ModeMutation.MUTATE_AND_EVALUATE: nv, move, objective = self.mutator.mutate_and_compute_obj( cur_variable) objective = self.aggreg_from_dict_values(objective) if self.mode_optim == ModeOptim.MINIMIZATION and objective < cur_objective: accept = True local_improvement = True global_improvement = objective < cur_best_objective elif self.mode_optim == ModeOptim.MAXIMIZATION and objective > cur_objective: accept = True local_improvement = True global_improvement = objective > cur_best_objective if accept: cur_objective = objective cur_variable = nv else: cur_variable = move.backtrack_local_move(nv) if self.store_solution: store.add_solution(nv, objective) if global_improvement: print("iter ", iteration) print("new obj ", objective, " better than ", cur_best_objective) cur_best_objective = objective cur_best_variable = cur_variable.copy() if not self.store_solution: store.add_solution(cur_variable, objective) # Update the temperature self.restart_handler.update(nv, objective, global_improvement, local_improvement) # Update info in restart handler cur_variable, cur_objective = self.restart_handler.restart( cur_variable, cur_objective) # possibly restart somewhere iteration += 1 if pickle_result and iteration % 20000 == 0: pickle.dump(cur_best_variable, open(pickle_name + ".pk", "wb")) store.finalize() return store