From 01dd2ff89ab8ec53737362abd05c3da5e763749e Mon Sep 17 00:00:00 2001 From: LebedevIlyaG Date: Tue, 10 Dec 2024 09:23:33 +0300 Subject: [PATCH] Improved drawing of Pareto set --- ...Example_SVC_2D_MCO_Transformators_State.py | 6 +++--- .../listeners/console_outputers.py | 2 ++ .../outputers/console_outputer.py | 20 +++++++++++++++++++ .../painters/plotters/plotters.py | 6 +++++- .../output_system/painters/static_painters.py | 11 +++++++--- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/Machine_learning/SVC/_2D/Example_SVC_2D_MCO_Transformators_State.py b/examples/Machine_learning/SVC/_2D/Example_SVC_2D_MCO_Transformators_State.py index 60f15be..7af6877 100644 --- a/examples/Machine_learning/SVC/_2D/Example_SVC_2D_MCO_Transformators_State.py +++ b/examples/Machine_learning/SVC/_2D/Example_SVC_2D_MCO_Transformators_State.py @@ -34,7 +34,7 @@ def factory_dataset(): kernel_coefficient_bound = {'low': -3, 'up': 1} problem = MCO_SVC_2D_Transformators_State.MCO_SVC_2D_Transformators_State(X, Y, regularization_value_bound, kernel_coefficient_bound) - method_params = SolverParameters(r=np.double(2.0), iters_limit=500, number_of_parallel_points=1, + method_params = SolverParameters(r=np.double(2.0), iters_limit=500, number_of_parallel_points=10, evolvent_density=12, number_of_lambdas=5) solver = Solver(problem=problem, parameters=method_params) # Добавляем вывод результатов в консоль @@ -49,14 +49,14 @@ def factory_dataset(): # Выводим множество Парето (координаты - значения функций) var = [trial.point.float_variables for trial in sol.best_trials] - val = [[-trial.function_values[i].value for i in range(2)] for trial in sol.best_trials] + val = [[trial.function_values[i].value for i in range(2)] for trial in sol.best_trials] print("size pareto set: ", len(var)) for fvar, fval in zip(var, val): print(fvar, fval) # Строим график множества Парето z[0]-z[1] - fv1 = [-trial.function_values[0].value for trial in sol.best_trials] + fv1 = [trial.function_values[0].value for trial in sol.best_trials] fv2 = [-trial.function_values[1].value for trial in sol.best_trials] plt.plot(fv1, fv2, 'ro') plt.show() diff --git a/iOpt/output_system/listeners/console_outputers.py b/iOpt/output_system/listeners/console_outputers.py index 3addce5..4a56299 100644 --- a/iOpt/output_system/listeners/console_outputers.py +++ b/iOpt/output_system/listeners/console_outputers.py @@ -43,3 +43,5 @@ def on_end_iteration(self, curr_points: List[SearchDataItem], curr_solution: Sol def on_method_stop(self, search_data: SearchData, solution: Solution, status: bool): self.__outputer.print_final_result_info(solution, status) + if self.mode == 'full' and solution.best_trials.size > 1: + self.__outputer.print_pareto_set_info(solution) diff --git a/iOpt/output_system/outputers/console_outputer.py b/iOpt/output_system/outputers/console_outputer.py index 2611a68..a8020f0 100644 --- a/iOpt/output_system/outputers/console_outputer.py +++ b/iOpt/output_system/outputers/console_outputer.py @@ -85,6 +85,8 @@ def print_final_result_info(self, solution: Solution, status: bool): best_trial_value, self.ndv ) + def print_pareto_set_info(self, solution: Solution): + self.__functions.print_pareto_set(solution.best_trials) class OutputFunctions: @@ -186,3 +188,21 @@ def print_best(self, number_of_global_trials, number_of_local_trials, solution_a print("|{:>29} {:<{width}.8f}|".format("currant accuracy: ", solution_accuracy, width=size_max_one_output * dim)) print("." * (30 + size_max_one_output * dim + 2)) + + def print_pareto_set(self, best_trials): + size_max_one_output = 15 + dim = len(best_trials[0].point.float_variables) + criteria_count = len(best_trials[0].function_values) + + var = [trial.point.float_variables for trial in best_trials] + val = [[trial.function_values[i].value for i in range(len(trial.function_values))] for trial in best_trials] + + string_len = size_max_one_output * (dim + criteria_count) + 6 + print("| {:^{width}} |".format(f"Size pareto set: {len(var)}", width=string_len)) + print("-" * (string_len + 4)) + + for fvar, fval in zip(var, val): + print("| {:>{width_point}}".format(str(fvar), width_point=dim * size_max_one_output), end=' ') + print("{:<{width_criteria}} |".format(str(fval), width_criteria=criteria_count * size_max_one_output)) + + print("-" * (string_len + 4)) diff --git a/iOpt/output_system/painters/plotters/plotters.py b/iOpt/output_system/painters/plotters/plotters.py index 6e72e3e..0f7f361 100644 --- a/iOpt/output_system/painters/plotters/plotters.py +++ b/iOpt/output_system/painters/plotters/plotters.py @@ -441,5 +441,9 @@ class PlotterPareto(Plotter2D): def __init__(self): super().__init__(None, None, None) - def plot_pareto(self, first_criteria_values, second_criteria_values, clr='blue', mrkr='o', mrkrs=8): + def plot_pareto(self, first_criteria_values, second_criteria_values, + first_criteria_indx, second_criteria_indx, clr='blue', mrkr='o', mrkrs=8): self.plot_points(first_criteria_values, second_criteria_values, clr, mrkr, mrkrs) + self.ax.set_title('Pareto set', fontsize=8) + self.ax.set_xlabel(f'Values of criteria number: {first_criteria_indx}', fontsize=8) + self.ax.set_ylabel(f'Values of criteria number: {second_criteria_indx}', fontsize=8) diff --git a/iOpt/output_system/painters/static_painters.py b/iOpt/output_system/painters/static_painters.py index 8e11a92..1d16224 100644 --- a/iOpt/output_system/painters/static_painters.py +++ b/iOpt/output_system/painters/static_painters.py @@ -295,15 +295,20 @@ def __init__(self, self.path_for_saves = path_for_saves self.file_name = file_name + # numbers of criteria selected by user + self.first_criteria_indx = criteria_indxs[0] + self.second_criteria_indx = criteria_indxs[1] + # values of Pareto-efficient criteria with input indices - self.first_criteria_values = [trial.function_values[criteria_indxs[0]].value for trial in solution.best_trials] - self.second_criteria_values = [trial.function_values[criteria_indxs[1]].value for trial in solution.best_trials] + self.first_criteria_values = [trial.function_values[self.first_criteria_indx].value for trial in solution.best_trials] + self.second_criteria_values = [trial.function_values[self.second_criteria_indx].value for trial in solution.best_trials] # definition of plotter self.plotter = PlotterPareto() def paint_pareto(self): - self.plotter.plot_pareto(self.first_criteria_values, self.second_criteria_values) + self.plotter.plot_pareto(self.first_criteria_values, self.second_criteria_values, + self.first_criteria_indx, self.second_criteria_indx) def save_image(self): if not os.path.isdir(self.path_for_saves):