1 | # encoding=utf8 |
||
2 | |||
3 | """Implementations of Schwefels functions.""" |
||
4 | |||
5 | from math import sin, cos, sqrt |
||
6 | from NiaPy.benchmarks.benchmark import Benchmark |
||
7 | |||
8 | __all__ = ['SchafferN2', 'SchafferN4', 'ExpandedSchaffer'] |
||
9 | |||
10 | View Code Duplication | class SchafferN2(Benchmark): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
11 | r"""Implementations of Schaffer N. 2 functions. |
||
12 | |||
13 | Date: 2018 |
||
14 | |||
15 | Author: Klemen Berkovič |
||
16 | |||
17 | License: MIT |
||
18 | |||
19 | Function: |
||
20 | **Schaffer N. 2 Function** |
||
21 | :math:`f(\textbf{x}) = 0.5 + \frac{ \sin^2 \left( x_1^2 - x_2^2 \right) - 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }` |
||
22 | |||
23 | **Input domain:** |
||
24 | The function can be defined on any input domain but it is usually |
||
25 | evaluated on the hypercube :math:`x_i ∈ [-100, 100]`, for all :math:`i = 1, 2,..., D`. |
||
26 | |||
27 | **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (420.968746,...,420.968746)` |
||
28 | |||
29 | LaTeX formats: |
||
30 | Inline: |
||
31 | $f(\textbf{x}) = 0.5 + \frac{ \sin^2 \left( x_1^2 - x_2^2 \right) - 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }$ |
||
32 | |||
33 | Equation: |
||
34 | \begin{equation} f(\textbf{x}) = 0.5 + \frac{ \sin^2 \left( x_1^2 - x_2^2 \right) - 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 } \end{equation} |
||
35 | |||
36 | Domain: |
||
37 | $-100 \leq x_i \leq 100$ |
||
38 | |||
39 | Reference: |
||
40 | http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf |
||
41 | """ |
||
42 | Name = ['SchafferN2'] |
||
43 | |||
44 | def __init__(self, Lower=-100.0, Upper=100.0): |
||
45 | r"""Initialize of SchafferN2 benchmark. |
||
46 | |||
47 | Args: |
||
48 | Lower (Optional[float]): Lower bound of problem. |
||
49 | Upper (Optional[float]): Upper bound of problem. |
||
50 | |||
51 | See Also: |
||
52 | :func:`NiaPy.benchmarks.Benchmark.__init__` |
||
53 | """ |
||
54 | Benchmark.__init__(self, Lower, Upper) |
||
55 | |||
56 | @staticmethod |
||
57 | def latex_code(): |
||
58 | r"""Return the latex code of the problem. |
||
59 | |||
60 | Returns: |
||
61 | str: Latex code |
||
62 | """ |
||
63 | return r'''$f(\textbf{x}) = 0.5 + \frac{ \sin^2 \left( x_1^2 - x_2^2 \right) - 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }$''' |
||
64 | |||
65 | def function(self): |
||
66 | r"""Return benchmark evaluation function. |
||
67 | |||
68 | Returns: |
||
69 | Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function |
||
70 | """ |
||
71 | def f(D, x): |
||
72 | r"""Fitness function. |
||
73 | |||
74 | Args: |
||
75 | D (int): Dimensionality of the problem |
||
76 | sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check. |
||
77 | |||
78 | Returns: |
||
79 | float: Fitness value for the solution. |
||
80 | """ |
||
81 | return 0.5 + (sin(x[0] ** 2 - x[1] ** 2) ** 2 - 0.5) / (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2 |
||
82 | return f |
||
83 | |||
84 | View Code Duplication | class SchafferN4(Benchmark): |
|
0 ignored issues
–
show
|
|||
85 | r"""Implementations of Schaffer N. 2 functions. |
||
86 | |||
87 | Date: 2018 |
||
88 | |||
89 | Author: Klemen Berkovič |
||
90 | |||
91 | License: MIT |
||
92 | |||
93 | Function: |
||
94 | **Schaffer N. 2 Function** |
||
95 | :math:`f(\textbf{x}) = 0.5 + \frac{ \cos^2 \left( \sin \left( x_1^2 - x_2^2 \right) \right)- 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }` |
||
96 | |||
97 | **Input domain:** |
||
98 | The function can be defined on any input domain but it is usually |
||
99 | evaluated on the hypercube :math:`x_i ∈ [-100, 100]`, for all :math:`i = 1, 2,..., D`. |
||
100 | |||
101 | **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (420.968746,...,420.968746)` |
||
102 | |||
103 | LaTeX formats: |
||
104 | Inline: |
||
105 | $f(\textbf{x}) = 0.5 + \frac{ \cos^2 \left( \sin \left( x_1^2 - x_2^2 \right) \right)- 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }$ |
||
106 | |||
107 | Equation: |
||
108 | \begin{equation} f(\textbf{x}) = 0.5 + \frac{ \cos^2 \left( \sin \left( x_1^2 - x_2^2 \right) \right)- 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 } \end{equation} |
||
109 | |||
110 | Domain: |
||
111 | $-100 \leq x_i \leq 100$ |
||
112 | |||
113 | Reference: |
||
114 | http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf |
||
115 | """ |
||
116 | Name = ['SchafferN4'] |
||
117 | |||
118 | def __init__(self, Lower=-100.0, Upper=100.0): |
||
119 | r"""Initialize of ScahfferN4 benchmark. |
||
120 | |||
121 | Args: |
||
122 | Lower (Optional[float]): Lower bound of problem. |
||
123 | Upper (Optional[float]): Upper bound of problem. |
||
124 | |||
125 | See Also: |
||
126 | :func:`NiaPy.benchmarks.Benchmark.__init__` |
||
127 | """ |
||
128 | Benchmark.__init__(self, Lower, Upper) |
||
129 | |||
130 | @staticmethod |
||
131 | def latex_code(): |
||
132 | r"""Return the latex code of the problem. |
||
133 | |||
134 | Returns: |
||
135 | str: Latex code |
||
136 | """ |
||
137 | return r'''$f(\textbf{x}) = 0.5 + \frac{ \cos^2 \left( \sin \left( x_1^2 - x_2^2 \right) \right)- 0.5 }{ \left( 1 + 0.001 \left( x_1^2 + x_2^2 \right) \right)^2 }$''' |
||
138 | |||
139 | def function(self): |
||
140 | r"""Return benchmark evaluation function. |
||
141 | |||
142 | Returns: |
||
143 | Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function |
||
144 | """ |
||
145 | def f(D, x): |
||
146 | r"""Fitness function. |
||
147 | |||
148 | Args: |
||
149 | D (int): Dimensionality of the problem |
||
150 | sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check. |
||
151 | |||
152 | Returns: |
||
153 | float: Fitness value for the solution. |
||
154 | """ |
||
155 | return 0.5 + (cos(sin(x[0] ** 2 - x[1] ** 2)) ** 2 - 0.5) / (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2 |
||
156 | return f |
||
157 | |||
158 | View Code Duplication | class ExpandedSchaffer(Benchmark): |
|
0 ignored issues
–
show
|
|||
159 | r"""Implementations of Expanded Schaffer functions. |
||
160 | |||
161 | Date: 2018 |
||
162 | |||
163 | Author: Klemen Berkovič |
||
164 | |||
165 | License: MIT |
||
166 | |||
167 | Function: |
||
168 | **Expanded Schaffer Function** |
||
169 | :math:`f(\textbf{x}) = g(x_D, x_1) + \sum_{i=2}^D g(x_{i - 1}, x_i) \\ g(x, y) = 0.5 + \frac{\sin \left(\sqrt{x^2 + y^2} \right)^2 - 0.5}{\left( 1 + 0.001 (x^2 + y^2) \right)}^2` |
||
170 | |||
171 | **Input domain:** |
||
172 | The function can be defined on any input domain but it is usually |
||
173 | evaluated on the hypercube :math:`x_i ∈ [-100, 100]`, for all :math:`i = 1, 2,..., D`. |
||
174 | |||
175 | **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (420.968746,...,420.968746)` |
||
176 | |||
177 | LaTeX formats: |
||
178 | Inline: |
||
179 | $f(\textbf{x}) = g(x_D, x_1) + \sum_{i=2}^D g(x_{i - 1}, x_i) \\ g(x, y) = 0.5 + \frac{\sin \left(\sqrt{x^2 + y^2} \right)^2 - 0.5}{\left( 1 + 0.001 (x^2 + y^2) \right)}^2$ |
||
180 | |||
181 | Equation: |
||
182 | \begin{equation} f(\textbf{x}) = g(x_D, x_1) + \sum_{i=2}^D g(x_{i - 1}, x_i) \\ g(x, y) = 0.5 + \frac{\sin \left(\sqrt{x^2 + y^2} \right)^2 - 0.5}{\left( 1 + 0.001 (x^2 + y^2) \right)}^2 \end{equation} |
||
183 | |||
184 | Domain: |
||
185 | $-100 \leq x_i \leq 100$ |
||
186 | |||
187 | Reference: |
||
188 | http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf |
||
189 | """ |
||
190 | Name = ['ExpandedSchaffer'] |
||
191 | |||
192 | def __init__(self, Lower=-100.0, Upper=100.0): |
||
193 | r"""Initialize of Expanded Scaffer benchmark. |
||
194 | |||
195 | Args: |
||
196 | Lower (Optional[float]): Lower bound of problem. |
||
197 | Upper (Optional[float]): Upper bound of problem. |
||
198 | |||
199 | See Also: |
||
200 | :func:`NiaPy.benchmarks.Benchmark.__init__` |
||
201 | """ |
||
202 | Benchmark.__init__(self, Lower, Upper) |
||
203 | |||
204 | @staticmethod |
||
205 | def latex_code(): |
||
206 | r"""Return the latex code of the problem. |
||
207 | |||
208 | Returns: |
||
209 | str: Latex code |
||
210 | """ |
||
211 | return r'''$f(\textbf{x}) = g(x_D, x_1) + \sum_{i=2}^D g(x_{i - 1}, x_i) \\ g(x, y) = 0.5 + \frac{\sin \left(\sqrt{x^2 + y^2} \right)^2 - 0.5}{\left( 1 + 0.001 (x^2 + y^2) \right)}^2$''' |
||
212 | |||
213 | def function(self): |
||
214 | r"""Return benchmark evaluation function. |
||
215 | |||
216 | Returns: |
||
217 | Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function |
||
218 | """ |
||
219 | def g(x, y): return 0.5 + (sin(sqrt(x ** 2 + y ** 2)) ** 2 - 0.5) / (1 + 0.001 * (x ** 2 + y ** 2)) ** 2 |
||
220 | def f(D, x): |
||
221 | r"""Fitness function. |
||
222 | |||
223 | Args: |
||
224 | D (int): Dimensionality of the problem |
||
225 | sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check. |
||
226 | |||
227 | Returns: |
||
228 | float: Fitness value for the solution. |
||
229 | """ |
||
230 | val = 0.0 |
||
231 | for i in range(1, D): val += g(x[i - 1], x[i]) |
||
232 | return g(x[D - 1], x[0]) + val |
||
233 | return f |
||
234 | |||
235 | # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3 |
||
236 |