Skip to content

Commit

Permalink
Fixed return solution when stopping by timeout. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
LebedevIlyaG committed Jul 27, 2023
1 parent 372f27f commit 54a0bc8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions iOpt/method/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions iOpt/method/mixed_integer_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand Down
1 change: 1 addition & 0 deletions iOpt/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions iOpt/solver_parametrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -51,3 +54,4 @@ def __init__(self,
self.start_point = start_point
self.number_of_parallel_points = number_of_parallel_points
self.timeout = timeout

0 comments on commit 54a0bc8

Please sign in to comment.