optuna

Форк
0
/
shape_functions.py 
99 строк · 3.0 Кб
1
import abc
2

3
import numpy as np
4

5

6
class BaseShapeFunction(object, metaclass=abc.ABCMeta):
7
    def __init__(self, n_objectives: int) -> None:
8
        self._n_objectives = n_objectives
9

10
    def __call__(self, m: int, x: np.ndarray) -> float:
11
        assert 1 <= m <= self.n_objectives
12
        assert x.shape == (self.n_objectives - 1,)
13
        return self._call(m, x)
14

15
    @abc.abstractmethod
16
    def _call(self, m: int, x: np.ndarray) -> float:
17
        raise NotImplementedError
18

19
    @property
20
    def n_objectives(self) -> int:
21
        return self._n_objectives
22

23

24
class LinearShapeFunction(BaseShapeFunction):
25
    def _call(self, m: int, x: np.ndarray) -> float:
26
        if m == 1:
27
            return x[:-1].prod()
28

29
        if m == self.n_objectives:
30
            return 1 - x[0]
31

32
        return x[: self.n_objectives - m].prod() * (1.0 - x[self.n_objectives - m])
33

34

35
class ConvexShapeFunction(BaseShapeFunction):
36
    def _call(self, m: int, x: np.ndarray) -> float:
37
        if m == 1:
38
            return (
39
                1
40
                - np.cos(
41
                    x * np.pi / 2,
42
                )
43
            )[:-1].prod()
44

45
        if m == self.n_objectives:
46
            return 1 - np.sin(x[0] * np.pi / 2.0)
47

48
        return (1.0 - np.cos(x * np.pi / 2.0))[: self.n_objectives - m].prod() * (
49
            1.0 - np.sin(x[self.n_objectives - m] * np.pi / 2.0)
50
        )
51

52

53
class ConcaveShapeFunction(BaseShapeFunction):
54
    def _call(self, m: int, x: np.ndarray) -> float:
55
        if m == 1:
56
            return np.sin(x * np.pi / 2.0)[:-1].prod()
57

58
        if m == self.n_objectives:
59
            return np.cos(x[0] * np.pi / 2.0)
60

61
        return np.sin(x * np.pi / 2.0)[: self.n_objectives - m].prod() * np.cos(
62
            x[self.n_objectives - m] * np.pi / 2.0
63
        )
64

65

66
class MixedConvexOrConcaveShapeFunction(BaseShapeFunction):
67
    def __init__(self, n_objectives: int, alpha: float, n_segments: int) -> None:
68
        super().__init__(n_objectives)
69
        self._alpha = alpha
70
        self._n_segments = n_segments
71

72
    def _call(self, m: int, x: np.ndarray) -> float:
73
        if m == self.n_objectives:
74
            two_A_pi = 2 * self._n_segments * np.pi
75
            return np.power(
76
                1 - x[0] - np.cos(two_A_pi * x[0] + np.pi / 2.0) / two_A_pi, self._alpha
77
            )
78

79
        raise ValueError("m should be the number of objectives")
80

81

82
class DisconnectedShapeFunction(BaseShapeFunction):
83
    def __init__(
84
        self, n_objectives: int, alpha: float, beta: float, n_disconnected_regions: int
85
    ) -> None:
86
        super().__init__(n_objectives)
87
        self._alpha = alpha
88
        self._beta = beta
89
        self._n_disconnected_regions = n_disconnected_regions
90

91
    def _call(self, m: int, x: np.ndarray) -> float:
92
        if m == self.n_objectives:
93
            return (
94
                1
95
                - np.power(x[0], self._alpha)
96
                * np.cos(self._n_disconnected_regions * np.power(x[0], self._beta) * np.pi) ** 2
97
            )
98

99
        raise ValueError("m should be the number of objectives")
100

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

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

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

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