|
14 | 14 | from CADETProcess.transform import NormLinearTransform, NormLogTransform |
15 | 15 |
|
16 | 16 | __all__ = [ |
| 17 | + 'Quadratic', |
17 | 18 | 'Rosenbrock', |
18 | 19 | 'LinearConstraintsSooTestProblem', |
19 | 20 | 'LinearConstraintsSooTestProblem2', |
@@ -88,6 +89,41 @@ def test_if_solved(self, results): |
88 | 89 | def x0(self): |
89 | 90 | raise NotImplementedError |
90 | 91 |
|
| 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 | + |
91 | 127 |
|
92 | 128 | class Rosenbrock(TestProblem): |
93 | 129 | def __init__(self, *args, n_var=2, **kwargs): |
|
0 commit comments