Skip to content

Commit 7678b5f

Browse files
UsovaMAMarina Usova
andauthored
Fix bug with original optimum using (#181)
* fix bug with original optimum using * add var for number of constraints and fix objective function value --------- Co-authored-by: Marina Usova <usova@itmm.unn.com>
1 parent d71cc85 commit 7678b5f

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

iOpt/output_system/listeners/static_painters.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ def __init__(self, file_name: str, path_for_saves="", mode='analysis', calc='obj
4040
self.search_dataSorted = []
4141
self.bestValueSorted = []
4242
self.number_of_parallel_points = 1
43+
self.number_of_constraints = 0
4344

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

5557
def on_method_stop(self, search_data: SearchData,
5658
solution: Solution, status: bool):
@@ -64,8 +66,8 @@ def on_method_stop(self, search_data: SearchData,
6466
solution.problem.lower_bound_of_float_variables,
6567
solution.problem.upper_bound_of_float_variables,
6668
self.file_name, self.path_for_saves, solution.problem.calculate,
67-
solution.best_trials[0].function_values[0].value,
68-
search_data, self.number_of_parallel_points)
69+
solution.best_trials[0].function_values[self.number_of_constraints].value,
70+
search_data, self.number_of_parallel_points, self.number_of_constraints)
6971
if self.mode == 'analysis':
7072
painter.paint_analisys(mrks=2)
7173
elif self.mode == 'bestcombination':
@@ -105,11 +107,15 @@ def __init__(self, file_name: str, path_for_saves="", indx=0, is_points_at_botto
105107
self.parameterInNDProblem = indx
106108
self.is_points_at_bottom = is_points_at_bottom
107109
self.mode = mode
110+
self.number_of_constraints = 0
111+
112+
def before_method_start(self, method: Method):
113+
self.number_of_constraints = method.task.problem.number_of_constraints
108114

109115
def on_method_stop(self, search_data: SearchData,
110116
solution: Solution, status: bool):
111117
painter = StaticPainter(search_data, solution, self.mode, self.is_points_at_bottom,
112-
self.parameterInNDProblem, self.path_for_saves, self.file_name)
118+
self.parameterInNDProblem, self.path_for_saves, self.file_name, self.number_of_constraints)
113119
painter.paint_objective_func()
114120
painter.paint_points()
115121
painter.paint_optimum()
@@ -149,11 +155,14 @@ def __init__(self, file_name: str, path_for_saves="", vars_indxs=[0, 1], mode='l
149155
self.parameters = vars_indxs
150156
self.mode = mode
151157
self.calc = calc
158+
self.number_of_constraints = 0
159+
def before_method_start(self, method: Method):
160+
self.number_of_constraints = method.task.problem.number_of_constraints
152161

153162
def on_method_stop(self, search_data: SearchData,
154163
solution: Solution, status: bool, ):
155164
painter = StaticPainterND(search_data, solution, self.parameters, self.mode, self.calc,
156-
self.file_name, self.path_for_saves)
165+
self.file_name, self.path_for_saves, self.number_of_constraints)
157166
painter.paint_objective_func()
158167
painter.paint_points()
159168
painter.paint_optimum()

iOpt/output_system/outputers/console_outputer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, problem: Problem, parameters: SolverParameters):
1313
self.__functions = OutputFunctions()
1414
self.iterNum = 1
1515
self.ndv = self.problem.number_of_discrete_variables
16+
self.number_of_constraints = self.problem.number_of_constraints
1617

1718
def print_init_info(self):
1819
self.__functions.print_init(
@@ -57,7 +58,7 @@ def print_best_point_info(self, solution, iters):
5758
else:
5859
best_trial_point = solution.best_trials[0].point.float_variables
5960
best_trial_d_point = solution.best_trials[0].point.discrete_variables
60-
best_trial_value = solution.best_trials[0].function_values[self.problem.number_of_constraints].value
61+
best_trial_value = solution.best_trials[0].function_values[self.number_of_constraints].value
6162
self.__functions.print_best(
6263
solution.number_of_global_trials,
6364
solution.number_of_local_trials,
@@ -72,7 +73,7 @@ def print_best_point_info(self, solution, iters):
7273
def print_final_result_info(self, solution: Solution, status: bool):
7374
best_trial_point = solution.best_trials[0].point.float_variables
7475
best_trial_d_point = solution.best_trials[0].point.discrete_variables
75-
best_trial_value = solution.best_trials[0].function_values[self.problem.number_of_constraints].value
76+
best_trial_value = solution.best_trials[0].function_values[self.number_of_constraints].value
7677
self.__functions.print_result(
7778
status,
7879
solution.number_of_global_trials,

iOpt/output_system/painters/animate_painters.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ def __init__(self, is_points_at_bottom, parameter_in_nd_problem, path_for_saves,
1616
self.objFunc = None
1717
self.parameterInNDProblem = parameter_in_nd_problem
1818
self.section = []
19+
self.number_of_constraints = 0
1920

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

2324
def set_problem(self, problem: Problem):
2425
self.objFunc = problem.calculate
26+
self.number_of_constraints = self.problem.number_of_constraints
2527

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

4547
def paint_optimum(self, solution: Solution):
4648
optimum = solution.best_trials[0].point.float_variables[self.parameterInNDProblem]
47-
optimumVal = solution.best_trials[0].function_values[0].value
49+
optimumVal = solution.best_trials[0].function_values[self.number_of_constraints].value
4850

4951
value = optimumVal
5052

@@ -79,6 +81,7 @@ def __init__(self, parameters_in_nd_problem, path_for_saves, file_name):
7981
self.objFunc = None
8082
self.parametersInNDProblem = parameters_in_nd_problem
8183
self.section = []
84+
self.number_of_constraints = 0
8285

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

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

iOpt/output_system/painters/plotters/plotters.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,14 @@ def plot_by_grid(self, calculate, section, points_count=100):
303303
xv, yv = np.meshgrid(x1, x2)
304304
z = []
305305

306+
fv = section.copy()
307+
306308
for i in range(points_count):
307309
z_ = []
308310
for j in range(points_count):
309-
section[self.indexes[0]] = xv[i, j]
310-
section[self.indexes[1]] = yv[i, j]
311-
z_.append(calculate(section))
311+
fv[self.indexes[0]] = xv[i, j]
312+
fv[self.indexes[1]] = yv[i, j]
313+
z_.append(calculate(fv))
312314
z.append(z_)
313315

314316
self.ax.contour(x1, x2, z, linewidths=1, levels=25, cmap=plt.cm.viridis)

iOpt/output_system/painters/static_painters.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
class DiscretePainter(Painter):
1313
def __init__(self, search_data_sorted, bestsvalues, pcount, floatdim, optimumPoint, discreteValues,
1414
discrete_name, mode, calc, subparameters, lb, rb, file_name, path_for_saves, calculate,
15-
optimum_value, search_data, number_of_parallel_points):
15+
optimum_value, search_data, number_of_parallel_points, number_of_constraints):
1616
self.path_for_saves = path_for_saves
1717
self.file_name = file_name
1818
self.calc = calc
1919
self.calculate = calculate
2020
self.optimum = optimumPoint
2121
self.optimumVal = optimum_value
2222
self.number_of_parallel_points = number_of_parallel_points
23+
self.number_of_constraints = number_of_constraints
2324

2425
self.values = []
2526
self.points = []
@@ -130,7 +131,8 @@ def __init__(self, search_data: SearchData,
130131
is_points_at_bottom,
131132
parameter_in_nd_problem,
132133
path_for_saves,
133-
file_name
134+
file_name,
135+
number_of_constraints
134136
):
135137
self.path_for_saves = path_for_saves
136138
self.file_name = file_name
@@ -139,6 +141,7 @@ def __init__(self, search_data: SearchData,
139141
self.is_points_at_bottom = is_points_at_bottom
140142

141143
self.objFunc = solution.problem.calculate
144+
self.number_of_constraints = number_of_constraints
142145

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

154157
self.optimum = solution.best_trials[0].point.float_variables
155158
self.optimumC = solution.best_trials[0].point.float_variables[parameter_in_nd_problem]
156-
self.optimumValue = solution.best_trials[0].function_values[0].value
159+
self.optimumValue = solution.best_trials[0].function_values[self.number_of_constraints].value
157160

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

204207
class StaticPainterND(Painter):
205-
def __init__(self, search_data, solution, parameters, mode, calc, file_name, path_for_saves):
208+
def __init__(self, search_data, solution, parameters, mode, calc, file_name, path_for_saves, number_of_constraints):
206209
self.path_for_saves = path_for_saves
207210
self.file_name = file_name
208211

209212
self.objectFunctionPainterType = mode
210213
self.objectFunctionCalculatorType = calc
211214

212215
self.objFunc = solution.problem.calculate
216+
self.number_of_constraints = number_of_constraints
213217

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

225229
self.optimum = solution.best_trials[0].point.float_variables
226-
self.optimumValue = solution.best_trials[0].function_values[0].value
230+
self.optimum_section = [solution.best_trials[0].point.float_variables[parameters[0]],
231+
solution.best_trials[0].point.float_variables[parameters[1]]]
232+
233+
self.optimumValue = solution.best_trials[0].function_values[self.number_of_constraints].value
227234

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

260267
def paint_optimum(self, solution: Solution = None):
261-
self.plotter.plot_points([self.optimum], [self.optimumValue], 'red', 'o', 4)
268+
self.plotter.plot_points([self.optimum_section], [self.optimumValue], 'red', 'o', 4)
262269

263270
def save_image(self):
264271
if not os.path.isdir(self.path_for_saves):

0 commit comments

Comments
 (0)