optuna

Форк
0
/
transformation_functions.py 
196 строк · 5.2 Кб
1
from __future__ import annotations
2

3
import abc
4
from typing import Callable
5
from typing import Union
6

7
import numpy as np
8

9

10
class BaseIdenticalTransformation(metaclass=abc.ABCMeta):
11
    @abc.abstractmethod
12
    def __call__(self, y: float) -> float:
13
        raise NotImplementedError
14

15

16
class BaseBiasTransformation(metaclass=abc.ABCMeta):
17
    @abc.abstractmethod
18
    def __call__(self, y: float) -> float:
19
        raise NotImplementedError
20

21

22
class BaseShiftTransformation(metaclass=abc.ABCMeta):
23
    @abc.abstractmethod
24
    def __call__(self, y: float) -> float:
25
        raise NotImplementedError
26

27

28
class BaseReductionTransformation(metaclass=abc.ABCMeta):
29
    @abc.abstractmethod
30
    def __call__(self, y: np.ndarray) -> float:
31
        raise NotImplementedError
32

33

34
BaseTransformations = Union[
35
    BaseIdenticalTransformation,
36
    BaseBiasTransformation,
37
    BaseShiftTransformation,
38
    BaseReductionTransformation,
39
]
40

41

42
class IdenticalTransformation(BaseIdenticalTransformation):
43
    def __init__(self) -> None:
44
        pass
45

46
    def __call__(self, y: float) -> float:
47
        return y
48

49

50
class PolynomialBiasTransformation(BaseBiasTransformation):
51
    def __init__(self, alpha: float) -> None:
52
        assert alpha > 0 and alpha != 1.0
53
        self._alpha = alpha
54

55
    def __call__(self, y: float) -> float:
56
        return np.power(y, self._alpha)
57

58

59
class FlatRegionBiasTransformation(BaseBiasTransformation):
60
    def __init__(self, a: float, b: float, c: float) -> None:
61
        assert 0 <= a <= 1
62
        assert 0 <= b <= 1
63
        assert 0 <= c <= 1
64
        assert b < c
65
        assert not (b == 0) or (a == 0 and c != 1)
66
        assert not (c == 1) or (a == 1 and b != 0)
67

68
        self._a = a
69
        self._b = b
70
        self._c = c
71

72
    def __call__(self, y: float) -> float:
73
        a = self._a
74
        b = self._b
75
        c = self._c
76
        return (
77
            a
78
            + min(0, np.floor(y - b)) * a * (b - y) / b
79
            - min(0, np.floor(c - y)) * (1.0 - a) * (y - c) / (1.0 - c)
80
        )
81

82

83
class ParameterDependentBiasTransformation(BaseReductionTransformation):
84
    def __init__(
85
        self,
86
        w: np.ndarray,
87
        input_converter: Callable[[np.ndarray], np.ndarray],
88
        a: float,
89
        b: float,
90
        c: float,
91
        i: int,
92
    ) -> None:
93
        assert 0 < a < 1
94
        assert 0 < b < c
95

96
        self._w = w
97
        self._input_converter = input_converter
98
        self._a = a
99
        self._b = b
100
        self._c = c
101
        self._i = i
102

103
    def __call__(self, y: np.ndarray) -> float:
104
        w = self._w
105
        a = self._a
106
        b = self._b
107
        c = self._c
108
        i = self._i
109

110
        u = (self._input_converter(y) * w).sum() / w.sum()
111
        v = a - (1.0 - 2 * u) * np.fabs(np.floor(0.5 - u) + a)
112
        return np.power(y[i], b + (c - b) * v)
113

114

115
class LinearShiftTransformation(BaseShiftTransformation):
116
    def __init__(self, a: float) -> None:
117
        assert 0 < a < 1
118

119
        self._a = a
120

121
    def __call__(self, y: float) -> float:
122
        return np.fabs(y - self._a) / np.fabs(np.floor(self._a - y) + self._a)
123

124

125
class DeceptiveShiftTransformation(BaseShiftTransformation):
126
    def __init__(self, a: float, b: float, c: float) -> None:
127
        assert 0 < a < 1
128
        assert 0 < b < 1
129
        assert 0 < c < 1
130
        assert a - b > 0
131
        assert a + b < 1
132

133
        self._a = a
134
        self._b = b
135
        self._c = c
136

137
    def __call__(self, y: float) -> float:
138
        a = self._a
139
        b = self._b
140
        c = self._c
141

142
        q1 = np.floor(y - a + b) * (1.0 - c + (a - b) / b)
143
        q2 = np.floor(a + b - y) * (1.0 - c + (1.0 - a - b) / b)
144
        return 1.0 + (np.fabs(y - a) - b) * (q1 / (a - b) + q2 / (1.0 - a - b) + 1.0 / b)
145

146

147
class MultiModalShiftTransformation(BaseShiftTransformation):
148
    def __init__(self, a: int, b: float, c: float) -> None:
149
        assert a > 0
150
        assert b >= 0
151
        assert (4 * a + 2) * np.pi >= 4 * b
152
        assert 0 < c < 1
153

154
        self._a = a
155
        self._b = b
156
        self._c = c
157

158
    def __call__(self, y: float) -> float:
159
        a = self._a
160
        b = self._b
161
        c = self._c
162

163
        q1 = np.fabs(y - c) / (2 * (np.floor(c - y) + c))
164
        q2 = (4 * a + 2) * np.pi * (0.5 - q1)
165
        return (1.0 + np.cos(q2) + 4 * b * (q1**2)) / (b + 2)
166

167

168
class WeightedSumReductionTransformation(BaseReductionTransformation):
169
    def __init__(self, w: np.ndarray, input_converter: Callable[[np.ndarray], np.ndarray]) -> None:
170
        assert all(w > 0)
171

172
        self._w = w
173
        self._input_converter = input_converter
174

175
    def __call__(self, y: np.ndarray) -> float:
176
        y = self._input_converter(y)
177
        return (y * self._w).sum() / self._w.sum()
178

179

180
class NonSeparableReductionTransformation(BaseReductionTransformation):
181
    def __init__(self, a: int, input_converter: Callable[[np.ndarray], np.ndarray]) -> None:
182
        assert a > 0
183

184
        self._a = a
185
        self._input_converter = input_converter
186

187
    def __call__(self, y: np.ndarray) -> float:
188
        a = float(self._a)
189
        y = self._input_converter(y)
190
        n = y.shape[0]
191
        indices = [(j + k + 1) % n for k in np.arange(n) for j in np.arange(n)]
192

193
        q = y.sum() + np.fabs(y[indices].reshape((n, n)) - y)[:, 0 : int(a) - 1].sum()
194
        r = n * np.ceil(a / 2) * (1.0 + 2 * a - 2 * np.ceil(a / 2)) / a
195

196
        return q / r
197

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.