Skip to content

Commit

Permalink
Improved drawing of Pareto set (#201)
Browse files Browse the repository at this point in the history
* Added Pareto set drawing

* Improved drawing of Pareto set
  • Loading branch information
LebedevIlyaG authored Dec 10, 2024
1 parent bc7abd9 commit 8540f9d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# Добавляем вывод результатов в консоль
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions iOpt/output_system/listeners/console_outputers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 20 additions & 0 deletions iOpt/output_system/outputers/console_outputer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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))
6 changes: 5 additions & 1 deletion iOpt/output_system/painters/plotters/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 8 additions & 3 deletions iOpt/output_system/painters/static_painters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 8540f9d

Please sign in to comment.