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

ObjectiveAdapter.__call__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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