Skip to content

Commit 386fcf7

Browse files
committed
Add Quadratic TestProblem to optimization fixtures
1 parent 0794a23 commit 386fcf7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/optimization_problem_fixtures.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from CADETProcess.transform import NormLinearTransform, NormLogTransform
1515

1616
__all__ = [
17+
'Quadratic',
1718
'Rosenbrock',
1819
'LinearConstraintsSooTestProblem',
1920
'LinearConstraintsSooTestProblem2',
@@ -88,6 +89,41 @@ def test_if_solved(self, results):
8889
def x0(self):
8990
raise NotImplementedError
9091

92+
class Quadratic(TestProblem):
93+
"""A n-dimensional quadratic function."""
94+
def __init__(self, *args, n_var=1, **kwargs):
95+
super().__init__('quadratic', *args, **kwargs)
96+
97+
for i in range(n_var):
98+
self.add_variable(f'var_{i}', lb=-10, ub=10)
99+
100+
self.add_objective(self.nd_quadratic)
101+
102+
@staticmethod
103+
def nd_quadratic(x):
104+
return np.sum(x_i**2 for x_i in x)
105+
106+
@property
107+
def optimal_solution(self):
108+
x = np.repeat(0, self.n_variables).reshape(1, self.n_variables)
109+
f = 0
110+
111+
return x, f
112+
113+
@property
114+
def x0(self):
115+
return np.repeat(2, self.n_variables)
116+
117+
def test_if_solved(self, optimization_results: OptimizationResults,
118+
test_kwargs=default_test_kwargs):
119+
x_true, f_true = self.optimal_solution
120+
x = optimization_results.x
121+
f = optimization_results.f
122+
123+
test_kwargs["err_msg"] = error
124+
np.testing.assert_allclose(f, f_true, **test_kwargs)
125+
np.testing.assert_allclose(x, x_true, **test_kwargs)
126+
91127

92128
class Rosenbrock(TestProblem):
93129
def __init__(self, *args, n_var=2, **kwargs):

0 commit comments

Comments
 (0)