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

gfo2hyper()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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