From 54a0bc8c15b29487fd523b4a9cb65edc5fe7b582 Mon Sep 17 00:00:00 2001 From: LebedevIlyaG Date: Thu, 27 Jul 2023 13:01:36 +0300 Subject: [PATCH] Fixed return solution when stopping by timeout. (#158) --- iOpt/method/method.py | 1 + iOpt/method/mixed_integer_method.py | 7 +++++-- iOpt/solver.py | 1 + iOpt/solver_parametrs.py | 8 ++++++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/iOpt/method/method.py b/iOpt/method/method.py index e8bc57f0..ec56b109 100644 --- a/iOpt/method/method.py +++ b/iOpt/method/method.py @@ -142,6 +142,7 @@ def first_iteration(self, calculator: Calculator = None) -> list[SearchDataItem] if calculator is None: for item in items: self.calculate_functionals(item) + self.update_optimum(item) else: calculator.calculate_functionals_for_items(items) diff --git a/iOpt/method/mixed_integer_method.py b/iOpt/method/mixed_integer_method.py index 528f7784..7fa79596 100644 --- a/iOpt/method/mixed_integer_method.py +++ b/iOpt/method/mixed_integer_method.py @@ -140,6 +140,7 @@ def first_iteration(self, calculator: Calculator = None) -> list[SearchDataItem] if calculator is None: for item in items: self.calculate_functionals(item) + self.update_optimum(item) else: calculator.calculate_functionals_for_items(items) @@ -246,7 +247,8 @@ def calculate_m(self, curr_point: SearchDataItem, left_point: SearchDataItem) -> else: other_point = None break - if other_point is not None and other_point.get_index() >= 0: + if other_point is not None and other_point.get_index() >= 0 \ + and other_point.get_discrete_value_index() == curr_point.get_discrete_value_index(): # print(index) m = abs(other_point.function_values[index].value - curr_point.get_z()) / \ self.calculate_delta(other_point, curr_point, self.dimension) @@ -262,7 +264,8 @@ def calculate_m(self, curr_point: SearchDataItem, left_point: SearchDataItem) -> other_point = None break - if other_point is not None and other_point.get_index() >= 0: + if other_point is not None and other_point.get_index() >= 0 \ + and other_point.get_discrete_value_index() == curr_point.get_discrete_value_index(): m = max(m, abs(curr_point.get_z() - other_point.function_values[index].value) / \ self.calculate_delta(curr_point, other_point, self.dimension)) diff --git a/iOpt/solver.py b/iOpt/solver.py index b1c23966..1bf4c483 100644 --- a/iOpt/solver.py +++ b/iOpt/solver.py @@ -60,6 +60,7 @@ def solve(self) -> Solution: solv_with_timeout = timeout(seconds=self.parameters.timeout * 60)(self.process.solve) try: solv_with_timeout() + sol = self.get_results() except Exception as exc: print(exc) sol = self.get_results() diff --git a/iOpt/solver_parametrs.py b/iOpt/solver_parametrs.py index d4fb5a0d..a62b09f8 100644 --- a/iOpt/solver_parametrs.py +++ b/iOpt/solver_parametrs.py @@ -15,7 +15,8 @@ def __init__(self, refine_solution: bool = False, start_point: Point = [], number_of_parallel_points: int = 1, - timeout: int = -1 + timeout: int = -1, + proportion_of_global_iterations: float = 0.95 ): r""" Конструктор класса SolverParameters @@ -34,12 +35,14 @@ def __init__(self, :param start_point: точка начального приближения к решению. :param number_of_parallel_points: число параллельно вычисляемых испытаний. :param timeout: ограничение на время вычислений в минутах. + :param proportion_of_global_iterations: доля глобальных итераций в поиске при использовании локальном метода """ self.eps = eps self.r = r self.iters_limit = iters_limit + self.proportion_of_global_iterations = proportion_of_global_iterations if refine_solution: - self.global_method_iteration_count = int(self.iters_limit * 0.95) + self.global_method_iteration_count = int(self.iters_limit * self.proportion_of_global_iterations) self.local_method_iteration_count = self.iters_limit - self.global_method_iteration_count else: self.global_method_iteration_count = self.iters_limit @@ -51,3 +54,4 @@ def __init__(self, self.start_point = start_point self.number_of_parallel_points = number_of_parallel_points self.timeout = timeout +