Passed
Pull Request — master (#110)
by
unknown
01:30
created

SphereFunction._paramnames()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 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