Passed
Push — master ( cba5ed...7e35c2 )
by Simon
04:17
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 i, 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, objective_function, optimizer, nth_process):
20
        super().__init__()
21
22
        self.objective_function = objective_function
23
        self.optimizer = optimizer
24
        self.nth_process = nth_process
25
26
    def __call__(self, search_space, data_c):
27
        # wrapper for GFOs
28
        def _model(para):
29
            para = gfo2hyper(search_space, para)
30
            self.para_dict = para
31
            results = self.objective_function(self)
32
33
            if data_c:
34
                progress_dict = para
35
36
                if isinstance(results, tuple):
37
                    score = results[0]
38
                    results_dict = results[1]
39
                else:
40
                    score = results
41
                    results_dict = {}
42
43
                results_dict["score"] = score
44
45
                progress_dict.update(results_dict)
46
                progress_dict["score_best"] = self.optimizer.best_score
47
                progress_dict["nth_iter"] = self.optimizer.nth_iter
48
                progress_dict["nth_process"] = self.optimizer.nth_process
49
50
                data_c.append(progress_dict)
51
52
            # ltm save after iteration
53
            # self.ltm.ltm_obj_func_wrapper(results, para, nth_process)
54
55
            return results
56
57
        _model.__name__ = self.objective_function.__name__
58
        return _model
59