Skip to content

Commit

Permalink
Fixed a bug with running a local method on tasks with partially integ…
Browse files Browse the repository at this point in the history
…er parameters (#162)
  • Loading branch information
LebedevIlyaG authored Sep 13, 2023
1 parent d64046e commit 8a8fe44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion iOpt/method/local_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def evaluate_function(self, y: List[float]) -> float:
if self.max_calcs != -1 and self.calcs_count >= self.max_calcs:
function_value.value = sys.float_info.max
return function_value.value
for i in range(self.task.problem.dimension):
for i in range(self.task.problem.number_of_float_variables):
if (y[i] < self.task.problem.lower_bound_of_float_variables[i]) \
or (y[i] > self.task.problem.upper_bound_of_float_variables[i]):
function_value.value = sys.float_info.max
Expand Down
17 changes: 16 additions & 1 deletion test/test_solving_test_problems.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import unittest
import numpy as np
from iOpt.problem import Problem
from iOpt.trial import Point
from problems.GKLS import GKLS
from problems.rastrigin import Rastrigin
from problems.rastriginInt import RastriginInt
from problems.xsquared import XSquared
from problems.hill import Hill
from problems.shekel import Shekel
Expand All @@ -15,6 +17,7 @@
from iOpt.solver_parametrs import SolverParameters



class TestSolvingProblems(unittest.TestCase):
"""setUp method is overridden from the parent class Rastrigin"""

Expand All @@ -29,7 +32,7 @@ def checkIsSolved(self, problem: Problem, params: SolverParameters, number_of_gl
sol = solver.solve()

# Проверяем что найденный АГП минимумом соответствуйте априори известному, для этой задачи, с точностью eps
for j in range(problem.dimension):
for j in range(problem.number_of_float_variables):
fabsx = np.abs(problem.known_optimum[0].point.float_variables[j] -
sol.best_trials[0].point.float_variables[j])
fm = params.eps * (problem.upper_bound_of_float_variables[j] -
Expand All @@ -38,6 +41,7 @@ def checkIsSolved(self, problem: Problem, params: SolverParameters, number_of_gl

# Проверяем что на решение потребовалось правильное число итераций АГП
self.assertEqual(sol.number_of_global_trials, number_of_global_trials)
return sol

def test_Rastrigin_Solve(self):
r = 3.5
Expand All @@ -47,6 +51,17 @@ def test_Rastrigin_Solve(self):

self.checkIsSolved(problem, params, number_of_global_trials)

def test_RastriginInt_Solve(self):
r = 3.5
problem = RastriginInt(dimension=5, number_of_discrete_variables=3)
start_point: Point = Point(float_variables=[0.5, 0.5], discrete_variables=['A', 'B', 'A'])
params = SolverParameters(r=r, eps=self.epsVal, iters_limit=100000, start_point=start_point,
number_of_parallel_points=1, refine_solution=True)
number_of_global_trials = 5678

sol = self.checkIsSolved(problem, params, number_of_global_trials)
self.assertEqual(sol.number_of_local_trials, 70)

def test_XSquared_Solve(self):
r = 3.5
problem = XSquared(1)
Expand Down

0 comments on commit 8a8fe44

Please sign in to comment.