Passed
Push — master ( 79bd2c...57729f )
by Simon
01:39
created

gradient_free_optimizers._objective_functions._sphere_function   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 24
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A SphereFunction.objective_function() 0 9 2
A SphereFunction.search_space() 0 3 1
A SphereFunction.__init__() 0 3 1
1
import numpy as np
2
3
from ._base_function import BaseFunction
4
5
6
class SphereFunction(BaseFunction):
7
    def __init__(self, n_dim, A=1):
8
        self.n_dim = n_dim
9
        self.A = A
10
11
    def objective_function(self, para):
12
        loss = 0
13
        for dim in range(self.n_dim):
14
            dim_str = "x" + str(dim)
15
            x = para[dim_str]
16
17
            loss += self.A * x * x
18
19
        return loss
20
21
    @property
22
    def search_space(self):
23
        return {"x" + str(idx): np.arange(-8, 8, 0.1) for idx in range(self.n_dim)}
24