Skip to content

Commit 0b0fe10

Browse files
committed
Add Quadratic TestProblem to optimization fixtures
1 parent 70f1a1f commit 0b0fe10

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
@@ -13,6 +13,7 @@
1313
from CADETProcess.optimization import OptimizationProblem, OptimizationResults
1414

1515
__all__ = [
16+
'Quadratic',
1617
'Rosenbrock',
1718
'LinearConstraintsSooTestProblem',
1819
'LinearConstraintsSooTestProblem2',
@@ -90,6 +91,41 @@ def test_if_solved(self, results):
9091
def x0(self):
9192
raise NotImplementedError
9293

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

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

0 commit comments

Comments
 (0)