Passed
Push — master ( f8108f...3d944f )
by Simon
01:32
created

gradient_free_optimizers._objective_adapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ObjectiveAdapter.__call__() 0 2 1
A ObjectiveAdapter._call_objective() 0 13 2
A ObjectiveAdapter.__init__() 0 3 1
1
from ._result import Result
2
3
4
class ObjectiveAdapter:
5
    """Maps *pos* → (score, metrics, params)."""
6
7
    def __init__(self, conv, objective):
8
        self._conv = conv
9
        self._objective = objective  # user callable
10
11
    def _call_objective(self, pos):
12
        """Run the underlying objective and normalise outputs."""
13
        params = self._conv.value2para(self._conv.position2value(pos))
14
        out = self._objective(params)
15
16
        if isinstance(out, tuple):
17
            score, metrics = out
18
        else:
19
            score, metrics = float(out), {}
20
21
        result = Result(score, metrics)
22
23
        return result, params
24
25
    # keep one public entry point so subclasses can override cleanly
26
    def __call__(self, pos):
27
        return self._call_objective(pos)
28