| Total Complexity | 3 |
| Total Lines | 32 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import numpy as np |
||
| 2 | |||
| 3 | from hyperactive.base import BaseExperiment |
||
| 4 | |||
| 5 | |||
| 6 | class SphereFunction(BaseExperiment): |
||
| 7 | """A simple Sphere function. |
||
| 8 | |||
| 9 | This is a common test function for optimization |
||
| 10 | algorithms. The function is defined as the sum of the squares of |
||
| 11 | its input parameters plus a constant. |
||
| 12 | |||
| 13 | Parameters |
||
| 14 | ---------- |
||
| 15 | const (float) |
||
| 16 | A constant offset added to the sum of squares. |
||
| 17 | n_dim : int, optional, default=2 |
||
| 18 | The number of dimensions for the Sphere function. The default is 2. |
||
| 19 | """ |
||
| 20 | |||
| 21 | def __init__(self, const, n_dim=2): |
||
| 22 | self.const = const |
||
| 23 | self.n_dim = n_dim |
||
| 24 | |||
| 25 | super().__init__() |
||
| 26 | |||
| 27 | def _paramnames(self): |
||
| 28 | return [f"x{i}" for i in range(self.n_dim)] |
||
| 29 | |||
| 30 | def _score(self, params): |
||
| 31 | return np.sum(np.array(params) ** 2) + self.const |
||
| 32 |