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

hyperactive.optimization_backend.gradient_free_optimizers._objective_function   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 5

1 Function

Rating   Name   Duplication   Size   Complexity  
A gfo2hyper() 0 7 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ObjectiveFunction.__call__() 0 16 2
A ObjectiveFunction.__init__() 0 7 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
from ._dictionary import DictClass
7
8
9
def gfo2hyper(search_space, para):
10
    values_dict = {}
11
    for _, key in enumerate(search_space.keys()):
12
        pos_ = int(para[key])
13
        values_dict[key] = search_space[key][pos_]
14
15
    return values_dict
16
17
18
class ObjectiveFunction(DictClass):
19
    def __init__(self, experiment):
20
        super().__init__()
21
22
        self.objective_function = experiment.objective_function
23
        self.catch = experiment._catch
24
25
        self.nth_iter = -1
26
27
    def __call__(self, search_space):
28
        # wrapper for GFOs
29
        def _model(para):
30
            self.nth_iter += 1
31
            para = gfo2hyper(search_space, para)
32
            self.para_dict = para
33
34
            try:
35
                results = self.objective_function(self)
36
            except tuple(self.catch.keys()) as e:
37
                results = self.catch[e.__class__]
38
39
            return results
40
41
        _model.__name__ = self.objective_function.__name__
42
        return _model
43