Skip to content

Commit

Permalink
Fix bug with original optimum using (#181)
Browse files Browse the repository at this point in the history
* fix bug with original optimum using

* add var for number of constraints and fix objective function value

---------

Co-authored-by: Marina Usova <[email protected]>
  • Loading branch information
UsovaMA and Marina Usova authored Feb 20, 2024
1 parent d71cc85 commit 7678b5f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
19 changes: 14 additions & 5 deletions iOpt/output_system/listeners/static_painters.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def __init__(self, file_name: str, path_for_saves="", mode='analysis', calc='obj
self.search_dataSorted = []
self.bestValueSorted = []
self.number_of_parallel_points = 1
self.number_of_constraints = 0

def before_method_start(self, method: Method):
self.number_of_constraints = method.task.problem.number_of_constraints
if method.task.problem.number_of_float_variables > 2 and self.calc == 'interpolation':
raise Exception(
"StaticDiscreteListener with calc 'interpolation' supported with dimension <= 2")
Expand All @@ -50,7 +52,7 @@ def before_method_start(self, method: Method):
def on_end_iteration(self, new_points, solution: Solution):
for newPoint in new_points:
self.search_dataSorted.append(newPoint)
self.bestValueSorted.append(solution.best_trials[0].function_values[0].value)
self.bestValueSorted.append(solution.best_trials[0].function_values[self.number_of_constraints].value)

def on_method_stop(self, search_data: SearchData,
solution: Solution, status: bool):
Expand All @@ -64,8 +66,8 @@ def on_method_stop(self, search_data: SearchData,
solution.problem.lower_bound_of_float_variables,
solution.problem.upper_bound_of_float_variables,
self.file_name, self.path_for_saves, solution.problem.calculate,
solution.best_trials[0].function_values[0].value,
search_data, self.number_of_parallel_points)
solution.best_trials[0].function_values[self.number_of_constraints].value,
search_data, self.number_of_parallel_points, self.number_of_constraints)
if self.mode == 'analysis':
painter.paint_analisys(mrks=2)
elif self.mode == 'bestcombination':
Expand Down Expand Up @@ -105,11 +107,15 @@ def __init__(self, file_name: str, path_for_saves="", indx=0, is_points_at_botto
self.parameterInNDProblem = indx
self.is_points_at_bottom = is_points_at_bottom
self.mode = mode
self.number_of_constraints = 0

def before_method_start(self, method: Method):
self.number_of_constraints = method.task.problem.number_of_constraints

def on_method_stop(self, search_data: SearchData,
solution: Solution, status: bool):
painter = StaticPainter(search_data, solution, self.mode, self.is_points_at_bottom,
self.parameterInNDProblem, self.path_for_saves, self.file_name)
self.parameterInNDProblem, self.path_for_saves, self.file_name, self.number_of_constraints)
painter.paint_objective_func()
painter.paint_points()
painter.paint_optimum()
Expand Down Expand Up @@ -149,11 +155,14 @@ def __init__(self, file_name: str, path_for_saves="", vars_indxs=[0, 1], mode='l
self.parameters = vars_indxs
self.mode = mode
self.calc = calc
self.number_of_constraints = 0
def before_method_start(self, method: Method):
self.number_of_constraints = method.task.problem.number_of_constraints

def on_method_stop(self, search_data: SearchData,
solution: Solution, status: bool, ):
painter = StaticPainterND(search_data, solution, self.parameters, self.mode, self.calc,
self.file_name, self.path_for_saves)
self.file_name, self.path_for_saves, self.number_of_constraints)
painter.paint_objective_func()
painter.paint_points()
painter.paint_optimum()
Expand Down
5 changes: 3 additions & 2 deletions iOpt/output_system/outputers/console_outputer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, problem: Problem, parameters: SolverParameters):
self.__functions = OutputFunctions()
self.iterNum = 1
self.ndv = self.problem.number_of_discrete_variables
self.number_of_constraints = self.problem.number_of_constraints

def print_init_info(self):
self.__functions.print_init(
Expand Down Expand Up @@ -57,7 +58,7 @@ def print_best_point_info(self, solution, iters):
else:
best_trial_point = solution.best_trials[0].point.float_variables
best_trial_d_point = solution.best_trials[0].point.discrete_variables
best_trial_value = solution.best_trials[0].function_values[self.problem.number_of_constraints].value
best_trial_value = solution.best_trials[0].function_values[self.number_of_constraints].value
self.__functions.print_best(
solution.number_of_global_trials,
solution.number_of_local_trials,
Expand All @@ -72,7 +73,7 @@ def print_best_point_info(self, solution, iters):
def print_final_result_info(self, solution: Solution, status: bool):
best_trial_point = solution.best_trials[0].point.float_variables
best_trial_d_point = solution.best_trials[0].point.discrete_variables
best_trial_value = solution.best_trials[0].function_values[self.problem.number_of_constraints].value
best_trial_value = solution.best_trials[0].function_values[self.number_of_constraints].value
self.__functions.print_result(
status,
solution.number_of_global_trials,
Expand Down
7 changes: 5 additions & 2 deletions iOpt/output_system/painters/animate_painters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def __init__(self, is_points_at_bottom, parameter_in_nd_problem, path_for_saves,
self.objFunc = None
self.parameterInNDProblem = parameter_in_nd_problem
self.section = []
self.number_of_constraints = 0

# настройки графика
self.plotter = AnimatePlotter2D(parameter_in_nd_problem, 0, 1)

def set_problem(self, problem: Problem):
self.objFunc = problem.calculate
self.number_of_constraints = self.problem.number_of_constraints

for i in range(problem.number_of_float_variables):
self.section.append(float(problem.upper_bound_of_float_variables[i]) - float(problem.lower_bound_of_float_variables[i]))
Expand All @@ -44,7 +46,7 @@ def paint_points(self, curr_points):

def paint_optimum(self, solution: Solution):
optimum = solution.best_trials[0].point.float_variables[self.parameterInNDProblem]
optimumVal = solution.best_trials[0].function_values[0].value
optimumVal = solution.best_trials[0].function_values[self.number_of_constraints].value

value = optimumVal

Expand Down Expand Up @@ -79,6 +81,7 @@ def __init__(self, parameters_in_nd_problem, path_for_saves, file_name):
self.objFunc = None
self.parametersInNDProblem = parameters_in_nd_problem
self.section = []
self.number_of_constraints = 0

# настройки графика
self.plotter = AnimatePlotter3D(parameters_in_nd_problem)
Expand All @@ -103,7 +106,7 @@ def paint_points(self, curr_points):
def paint_optimum(self, solution: Solution):
optimum = [solution.best_trials[0].point.float_variables[self.parametersInNDProblem[0]],
solution.best_trials[0].point.float_variables[self.parametersInNDProblem[1]]]
optimumVal = solution.best_trials[0].function_values[0].value
optimumVal = solution.best_trials[0].function_values[self.number_of_constraints].value

self.plotter.plot_points(optimum, [], 'red', 'o', 4)

Expand Down
8 changes: 5 additions & 3 deletions iOpt/output_system/painters/plotters/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ def plot_by_grid(self, calculate, section, points_count=100):
xv, yv = np.meshgrid(x1, x2)
z = []

fv = section.copy()

for i in range(points_count):
z_ = []
for j in range(points_count):
section[self.indexes[0]] = xv[i, j]
section[self.indexes[1]] = yv[i, j]
z_.append(calculate(section))
fv[self.indexes[0]] = xv[i, j]
fv[self.indexes[1]] = yv[i, j]
z_.append(calculate(fv))
z.append(z_)

self.ax.contour(x1, x2, z, linewidths=1, levels=25, cmap=plt.cm.viridis)
Expand Down
19 changes: 13 additions & 6 deletions iOpt/output_system/painters/static_painters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
class DiscretePainter(Painter):
def __init__(self, search_data_sorted, bestsvalues, pcount, floatdim, optimumPoint, discreteValues,
discrete_name, mode, calc, subparameters, lb, rb, file_name, path_for_saves, calculate,
optimum_value, search_data, number_of_parallel_points):
optimum_value, search_data, number_of_parallel_points, number_of_constraints):
self.path_for_saves = path_for_saves
self.file_name = file_name
self.calc = calc
self.calculate = calculate
self.optimum = optimumPoint
self.optimumVal = optimum_value
self.number_of_parallel_points = number_of_parallel_points
self.number_of_constraints = number_of_constraints

self.values = []
self.points = []
Expand Down Expand Up @@ -130,7 +131,8 @@ def __init__(self, search_data: SearchData,
is_points_at_bottom,
parameter_in_nd_problem,
path_for_saves,
file_name
file_name,
number_of_constraints
):
self.path_for_saves = path_for_saves
self.file_name = file_name
Expand All @@ -139,6 +141,7 @@ def __init__(self, search_data: SearchData,
self.is_points_at_bottom = is_points_at_bottom

self.objFunc = solution.problem.calculate
self.number_of_constraints = number_of_constraints

# формируем массив точек итераций для графика
self.points = []
Expand All @@ -153,7 +156,7 @@ def __init__(self, search_data: SearchData,

self.optimum = solution.best_trials[0].point.float_variables
self.optimumC = solution.best_trials[0].point.float_variables[parameter_in_nd_problem]
self.optimumValue = solution.best_trials[0].function_values[0].value
self.optimumValue = solution.best_trials[0].function_values[self.number_of_constraints].value

# настройки графика
self.plotter = Plotter2D(parameter_in_nd_problem,
Expand Down Expand Up @@ -202,14 +205,15 @@ def calculate_func(self, x):
return fv.value

class StaticPainterND(Painter):
def __init__(self, search_data, solution, parameters, mode, calc, file_name, path_for_saves):
def __init__(self, search_data, solution, parameters, mode, calc, file_name, path_for_saves, number_of_constraints):
self.path_for_saves = path_for_saves
self.file_name = file_name

self.objectFunctionPainterType = mode
self.objectFunctionCalculatorType = calc

self.objFunc = solution.problem.calculate
self.number_of_constraints = number_of_constraints

# формируем массив точек итераций для графика
self.points = []
Expand All @@ -223,7 +227,10 @@ def __init__(self, search_data, solution, parameters, mode, calc, file_name, pat
self.values = self.values[1:-1]

self.optimum = solution.best_trials[0].point.float_variables
self.optimumValue = solution.best_trials[0].function_values[0].value
self.optimum_section = [solution.best_trials[0].point.float_variables[parameters[0]],
solution.best_trials[0].point.float_variables[parameters[1]]]

self.optimumValue = solution.best_trials[0].function_values[self.number_of_constraints].value

self.leftBounds = [float(solution.problem.lower_bound_of_float_variables[parameters[0]]),
float(solution.problem.lower_bound_of_float_variables[parameters[1]])]
Expand Down Expand Up @@ -258,7 +265,7 @@ def paint_points(self, curr_point: SearchDataItem = None):
self.plotter.plot_points(self.points, self.values, 'blue', 'o', 4)

def paint_optimum(self, solution: Solution = None):
self.plotter.plot_points([self.optimum], [self.optimumValue], 'red', 'o', 4)
self.plotter.plot_points([self.optimum_section], [self.optimumValue], 'red', 'o', 4)

def save_image(self):
if not os.path.isdir(self.path_for_saves):
Expand Down

0 comments on commit 7678b5f

Please sign in to comment.