Code Duplication    Length = 76-77 lines in 2 locations

NiaPy/benchmarks/schaffer.py 1 location

@@ 158-233 (lines=76) @@
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
class ExpandedSchaffer(Benchmark):
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

NiaPy/benchmarks/schwefel.py 1 location

@@ 333-409 (lines=77) @@
330
			return 418.9829 * D - val
331
		return f
332
333
class ExpandedScaffer(Benchmark):
334
	r"""Implementations of High Conditioned Elliptic functions.
335
336
	Date: 2018
337
338
	Author: Klemen Berkovič
339
340
	License: MIT
341
342
	Function:
343
	**High Conditioned Elliptic Function**
344
345
		: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`
346
347
		**Input domain:**
348
		The function can be defined on any input domain but it is usually
349
		evaluated on the hypercube :math:`x_i ∈ [-100, 100]`, for all :math:`i = 1, 2,..., D`.
350
351
		**Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (420.968746,...,420.968746)`
352
353
	LaTeX formats:
354
		Inline:
355
				$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$
356
357
		Equation:
358
				\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}
359
360
		Domain:
361
				$-100 \leq x_i \leq 100$
362
363
	Reference:
364
		http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf
365
	"""
366
	Name = ['ExpandedScaffer']
367
368
	def __init__(self, Lower=-100.0, Upper=100.0):
369
		r"""Initialize of Expanded Scaffer benchmark.
370
371
		Args:
372
			Lower (Optional[float]): Lower bound of problem.
373
			Upper (Optional[float]): Upper bound of problem.
374
375
		See Also:
376
			:func:`NiaPy.benchmarks.Benchmark.__init__`
377
		"""
378
		Benchmark.__init__(self, Lower=Lower, Upper=Lower)
379
380
	@staticmethod
381
	def latex_code():
382
		r"""Return the latex code of the problem.
383
384
		Returns:
385
			str: Latex code
386
		"""
387
		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$'''
388
389
	def function(self):
390
		r"""Return benchmark evaluation function.
391
392
		Returns:
393
			Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
394
		"""
395
		def g(x, y): return 0.5 + (sin(sqrt(x ** 2 + y ** 2)) ** 2 - 0.5) / (1 + 0.001 * (x ** 2 + y ** 2)) ** 2
396
		def f(D, x):
397
			r"""Fitness function.
398
399
			Args:
400
				D (int): Dimensionality of the problem
401
				sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
402
403
			Returns:
404
				float: Fitness value for the solution.
405
			"""
406
			val = 0.0
407
			for i in range(1, D): val += g(x[i - 1], x[i])
408
			return g(x[D - 1], x[0]) + val
409
		return f
410
411
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
412