Code Duplication    Length = 77-87 lines in 4 locations

NiaPy/benchmarks/schwefel.py 1 location

@@ 10-86 (lines=77) @@
7
8
__all__ = ['Schwefel', 'Schwefel221', 'Schwefel222', 'ModifiedSchwefel']
9
10
class Schwefel(Benchmark):
11
	r"""Implementation of Schewel function.
12
13
	Date: 2018
14
15
	Author: Lucija Brezočnik
16
17
	License: MIT
18
19
	Function: **Schwefel function**
20
21
		:math:`f(\textbf{x}) = 418.9829d - \sum_{i=1}^{D} x_i \sin(\sqrt{\lvert x_i \rvert})`
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 ∈ [-500, 500]`, 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}) = 418.9829d - \sum_{i=1}^{D} x_i \sin(\sqrt{\lvert x_i \rvert})$
32
33
		Equation:
34
				\begin{equation} f(\textbf{x}) = 418.9829d - \sum_{i=1}^{D} x_i
35
				\sin(\sqrt{\lvert x_i \rvert}) \end{equation}
36
37
		Domain:
38
				$-500 \leq x_i \leq 500$
39
40
	Reference:
41
		https://www.sfu.ca/~ssurjano/schwef.html
42
	"""
43
	Name = ['Schwefel']
44
45
	def __init__(self, Lower=-500.0, Upper=500.0):
46
		r"""Initialize of Schwefel 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}) = 418.9829d - \sum_{i=1}^{D} x_i \sin(\sqrt{\lvert x_i \rvert})$'''
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):
84
				val += (sol[i] * sin(sqrt(abs(sol[i]))))
85
			return 418.9829 * D - val
86
		return evaluate
87
88
class Schwefel221(Benchmark):
89
	r"""Schwefel 2.21 function implementation.

NiaPy/benchmarks/salomon.py 1 location

@@ 11-97 (lines=87) @@
8
__all__ = ['Salomon']
9
10
11
class Salomon(Benchmark):
12
    r"""Implementation of Salomon function.
13
14
    Date: 2018
15
16
    Author: Lucija Brezočnik
17
18
    License: MIT
19
20
    Function: **Salomon function**
21
22
        :math:`f(\mathbf{x}) = 1 - \cos\left(2\pi\sqrt{\sum_{i=1}^D x_i^2}
23
        \right)+ 0.1 \sqrt{\sum_{i=1}^D x_i^2}`
24
25
        **Input domain:**
26
        The function can be defined on any input domain but it is usually
27
        evaluated on the hypercube :math:`x_i ∈ [-100, 100]`, for all :math:`i = 1, 2,..., D`.
28
29
        **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = f(0, 0)`
30
31
    LaTeX formats:
32
        Inline:
33
                $f(\mathbf{x}) = 1 - \cos\left(2\pi\sqrt{\sum_{i=1}^D x_i^2}
34
                \right)+ 0.1 \sqrt{\sum_{i=1}^D x_i^2}$
35
36
        Equation:
37
                \begin{equation} f(\mathbf{x}) =
38
                1 - \cos\left(2\pi\sqrt{\sum_{i=1}^D x_i^2}
39
                \right)+ 0.1 \sqrt{\sum_{i=1}^D x_i^2} \end{equation}
40
41
        Domain:
42
                $-100 \leq x_i \leq 100$
43
44
    Reference paper:
45
        Jamil, M., and Yang, X. S. (2013).
46
        A literature survey of benchmark functions for global optimisation problems.
47
        International Journal of Mathematical Modelling and Numerical Optimisation,
48
        4(2), 150-194.
49
    """
50
    Name = ['Salomon']
51
52
    def __init__(self, Lower=-100.0, Upper=100.0):
53
        r"""Initialize of Salomon benchmark.
54
55
        Args:
56
            Lower (Optional[float]): Lower bound of problem.
57
            Upper (Optional[float]): Upper bound of problem.
58
59
        See Also:
60
            :func:`NiaPy.benchmarks.Benchmark.__init__`
61
        """
62
        Benchmark.__init__(self, Lower, Upper)
63
64
    @staticmethod
65
    def latex_code():
66
        r"""Return the latex code of the problem.
67
68
        Returns:
69
            str: Latex code
70
        """
71
        return r'''$f(\mathbf{x}) = 1 - \cos\left(2\pi\sqrt{\sum_{i=1}^D x_i^2}
72
                \right)+ 0.1 \sqrt{\sum_{i=1}^D x_i^2}$'''
73
74
    def function(self):
75
        r"""Return benchmark evaluation function.
76
77
        Returns:
78
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
79
        """
80
        def evaluate(D, sol):
81
            r"""Fitness function.
82
83
            Args:
84
                D (int): Dimensionality of the problem
85
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
86
87
            Returns:
88
                float: Fitness value for the solution.
89
            """
90
            val = 0.0
91
92
            for i in range(D):
93
                val += math.pow(sol[i], 2)
94
95
            return 1.0 - math.cos(2.0 * math.pi * math.sqrt(val)) + 0.1 * val
96
97
        return evaluate
98

NiaPy/benchmarks/styblinskiTang.py 1 location

@@ 10-95 (lines=86) @@
7
8
__all__ = ['StyblinskiTang']
9
10
class StyblinskiTang(Benchmark):
11
    r"""Implementation of Styblinski-Tang functions.
12
13
    Date: 2018
14
15
    Authors: Lucija Brezočnik
16
17
    License: MIT
18
19
    Function: **Styblinski-Tang function**
20
21
        :math:`f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
22
        x_i^4 - 16x_i^2 + 5x_i \right)`
23
24
        **Input domain:**
25
        The function can be defined on any input domain but it is usually
26
        evaluated on the hypercube :math:`x_i ∈ [-5, 5]`, for all :math:`i = 1, 2,..., D`.
27
28
        **Global minimum:** :math:`f(x^*) = -78.332`, at :math:`x^* = (-2.903534,...,-2.903534)`
29
30
    LaTeX formats:
31
        Inline:
32
                $f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
33
                x_i^4 - 16x_i^2 + 5x_i \right) $
34
35
        Equation:
36
                \begin{equation}f(\mathbf{x}) =
37
                \frac{1}{2} \sum_{i=1}^D \left( x_i^4 - 16x_i^2 + 5x_i \right) \end{equation}
38
39
        Domain:
40
                $-5 \leq x_i \leq 5$
41
42
    Reference paper:
43
        Jamil, M., and Yang, X. S. (2013).
44
        A literature survey of benchmark functions for global optimisation problems.
45
        International Journal of Mathematical Modelling and Numerical Optimisation,
46
        4(2), 150-194.
47
    """
48
    Name = ['StyblinskiTang']
49
50
    def __init__(self, Lower=-5.0, Upper=5.0):
51
        r"""Initialize of Styblinski Tang benchmark.
52
53
        Args:
54
            Lower (Optional[float]): Lower bound of problem.
55
            Upper (Optional[float]): Upper bound of problem.
56
57
        See Also:
58
            :func:`NiaPy.benchmarks.Benchmark.__init__`
59
        """
60
        Benchmark.__init__(self, Lower, Upper)
61
62
    @staticmethod
63
    def latex_code():
64
        r"""Return the latex code of the problem.
65
66
        Returns:
67
            str: Latex code
68
        """
69
        return r'''$f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
70
                x_i^4 - 16x_i^2 + 5x_i \right) $'''
71
72
    def function(self):
73
        r"""Return benchmark evaluation function.
74
75
        Returns:
76
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
77
        """
78
        def evaluate(D, sol):
79
            r"""Fitness function.
80
81
            Args:
82
                D (int): Dimensionality of the problem
83
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
84
85
            Returns:
86
                float: Fitness value for the solution.
87
            """
88
            val = 0.0
89
90
            for i in range(D):
91
                val += (math.pow(sol[i], 4) - 16.0 * math.pow(sol[i], 2) + 5.0 * sol[i])
92
93
            return 0.5 * val
94
95
        return evaluate
96

NiaPy/benchmarks/rastrigin.py 1 location

@@ 11-90 (lines=80) @@
8
__all__ = ['Rastrigin']
9
10
11
class Rastrigin(Benchmark):
12
    r"""Implementation of Rastrigin benchmark function.
13
14
    Date: 2018
15
16
    Authors: Lucija Brezočnik and Iztok Fister Jr.
17
18
    License: MIT
19
20
    Function: **Rastrigin function**
21
22
        :math:`f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)`
23
24
        **Input domain:**
25
        The function can be defined on any input domain but it is usually
26
        evaluated on the hypercube :math:`x_i ∈ [-5.12, 5.12]`, for all :math:`i = 1, 2,..., D`.
27
28
        **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (0,...,0)`
29
30
    LaTeX formats:
31
        Inline:
32
                $f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)$
33
34
        Equation:
35
                \begin{equation} f(\mathbf{x}) =
36
                10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)
37
                \end{equation}
38
39
        Domain:
40
                $-5.12 \leq x_i \leq 5.12$
41
42
    Reference: https://www.sfu.ca/~ssurjano/rastr.html
43
    """
44
    Name = ['Rastrigin']
45
46
    def __init__(self, Lower=-5.12, Upper=5.12):
47
        r"""Initialize of Rastrigni benchmark.
48
49
        Args:
50
            Lower (Optional[float]): Lower bound of problem.
51
            Upper (Optional[float]): Upper bound of problem.
52
53
        See Also:
54
            :func:`NiaPy.benchmarks.Benchmark.__init__`
55
        """
56
        Benchmark.__init__(self, Lower, Upper)
57
58
    @staticmethod
59
    def latex_code():
60
        r"""Return the latex code of the problem.
61
62
        Returns:
63
            str: Latex code
64
        """
65
        return r'''$f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)$'''
66
67
    def function(self):
68
        r"""Return benchmark evaluation function.
69
70
        Returns:
71
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
72
        """
73
        def evaluate(D, sol):
74
            r"""Fitness function.
75
76
            Args:
77
                D (int): Dimensionality of the problem
78
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
79
80
            Returns:
81
                float: Fitness value for the solution.
82
            """
83
            val = 0.0
84
85
            for i in range(D):
86
                val += math.pow(sol[i], 2) - (10.0 * math.cos(2 * math.pi * sol[i]))
87
88
            return 10 * D + val
89
90
        return evaluate
91