NiaPy.benchmarks.elliptic   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 89.53 %

Importance

Changes 0
Metric Value
eloc 16
dl 77
loc 86
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Elliptic.__init__() 11 11 1
A Elliptic.function() 20 20 2
A Elliptic.latex_code() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# encoding=utf8
2
3
"""Implementations of High Conditioned Elliptic functions."""
4
5
from NiaPy.benchmarks.benchmark import Benchmark
6
7
__all__ = ['Elliptic']
8
9 View Code Duplication
class Elliptic(Benchmark):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
	r"""Implementations of High Conditioned Elliptic functions.
11
12
	Date: 2018
13
14
	Author: Klemen Berkovič
15
16
	License: MIT
17
18
	Function:
19
	**High Conditioned Elliptic Function**
20
21
		:math:`f(\textbf{x}) = \sum_{i=1}^D \left( 10^6 \right)^{ \frac{i - 1}{D - 1} } x_i^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:**
28
		:math:`f(x^*) = 0`, at :math:`x^* = (420.968746,...,420.968746)`
29
30
	LaTeX formats:
31
		Inline:
32
				$f(\textbf{x}) = \sum_{i=1}^D \left( 10^6 \right)^{ \frac{i - 1}{D - 1} } x_i^2$
33
34
		Equation:
35
				\begin{equation} f(\textbf{x}) = \sum_{i=1}^D \left( 10^6 \right)^{ \frac{i - 1}{D - 1} } x_i^2 \end{equation}
36
37
		Domain:
38
				$-100 \leq x_i \leq 100$
39
40
	Reference:
41
	http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf
42
	"""
43
	Name = ['Elliptic']
44
45
	def __init__(self, Lower=-100.0, Upper=100.0):
46
		r"""Initialize of High Conditioned Elliptic benchmark.
47
48
		Args:
49
			Lower (Optional[float]): Lower bound of problem.
50
			Upper (Optional[float]): Upper bound of problem.
51
52
		See Also:
53
			:func:`NiaPy.benchmarks.Benchmark.__init__`
54
		"""
55
		Benchmark.__init__(self, Lower, Upper)
56
57
	@staticmethod
58
	def latex_code():
59
		r"""Return the latex code of the problem.
60
61
		Returns:
62
			str: Latex code
63
		"""
64
		return r'''$f(\textbf{x}) = \sum_{i=1}^D \left( 10^6 \right)^{ \frac{i - 1}{D - 1} } x_i^2$'''
65
66
	def function(self):
67
		r"""Return benchmark evaluation function.
68
69
		Returns:
70
			Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
71
		"""
72
		def evaluate(D, sol):
73
			r"""Fitness function.
74
75
			Args:
76
				D (int): Dimensionality of the problem
77
				sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
78
79
			Returns:
80
				float: Fitness value for the solution.
81
			"""
82
			val = 0.0
83
			for i in range(D): val += (10 ** 6) ** (i / (D - 1)) * sol[i]
84
			return val
85
		return evaluate
86
87
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
88