NiaPy.benchmarks.sumSquares   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A SumSquares.latex_code() 0 8 1
A SumSquares.function() 0 24 2
A SumSquares.__init__() 0 11 1
1
# encoding=utf8
2
"""Sum Squares benchmark."""
3
4
import math
5
from NiaPy.benchmarks.benchmark import Benchmark
6
7
__all__ = ['SumSquares']
8
9
class SumSquares(Benchmark):
10
    r"""Implementation of Sum Squares functions.
11
12
    Date: 2018
13
14
    Authors: Lucija Brezočnik
15
16
    License: MIT
17
18
    Function: **Sum Squares function**
19
20
        :math:`f(\mathbf{x}) = \sum_{i=1}^D i x_i^2`
21
22
        **Input domain:**
23
        The function can be defined on any input domain but it is usually
24
        evaluated on the hypercube :math:`x_i ∈ [-10, 10]`, for all :math:`i = 1, 2,..., D`.
25
26
        **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (0,...,0)`
27
28
    LaTeX formats:
29
        Inline:
30
                $f(\mathbf{x}) = \sum_{i=1}^D i x_i^2$
31
32
        Equation:
33
                \begin{equation}f(\mathbf{x}) = \sum_{i=1}^D i x_i^2 \end{equation}
34
35
        Domain:
36
                $0 \leq x_i \leq 10$
37
38
    Reference paper:
39
        Jamil, M., and Yang, X. S. (2013).
40
        A literature survey of benchmark functions for global optimisation problems.
41
        International Journal of Mathematical Modelling and Numerical Optimisation,
42
        4(2), 150-194.
43
    """
44
    Name = ['SumSquares']
45
46
    def __init__(self, Lower=-10.0, Upper=10.0):
47
        r"""Initialize of Sum Squares 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}) = \sum_{i=1}^D i x_i^2$'''
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 += i * math.pow(sol[i], 2)
87
88
            return val
89
90
        return evaluate
91