Source code for niapy.problems.levy
# encoding=utf8
"""Implementations of Levy function."""
import numpy as np
from niapy.problems.problem import Problem
__all__ = ['Levy']
[docs]class Levy(Problem):
r"""Implementations of Levy functions.
Date: 2018
Author: Klemen Berkovič
License: MIT
Function:
**Levy Function**
:math:`f(\textbf{x}) = \sin^2 (\pi w_1) + \sum_{i = 1}^{D - 1} (w_i - 1)^2 \left( 1 + 10 \sin^2 (\pi w_i + 1) \right) + (w_d - 1)^2 (1 + \sin^2 (2 \pi w_d)) \\ w_i = 1 + \frac{x_i - 1}{4}`
**Input domain:**
The function can be defined on any input domain but it is usually
evaluated on the hypercube :math:`x_i ∈ [-10, 10]`, for all :math:`i = 1, 2,..., D`.
**Global minimum:**
:math:`f(\textbf{x}^*) = 0` at :math:`\textbf{x}^* = (1, \cdots, 1)`
LaTeX formats:
Inline:
$f(\textbf{x}) = \sin^2 (\pi w_1) + \sum_{i = 1}^{D - 1} (w_i - 1)^2 \left( 1 + 10 \sin^2 (\pi w_i + 1) \right) + (w_d - 1)^2 (1 + \sin^2 (2 \pi w_d)) \\ w_i = 1 + \frac{x_i - 1}{4}$
Equation:
\begin{equation} f(\textbf{x}) = \sin^2 (\pi w_1) + \sum_{i = 1}^{D - 1} (w_i - 1)^2 \left( 1 + 10 \sin^2 (\pi w_i + 1) \right) + (w_d - 1)^2 (1 + \sin^2 (2 \pi w_d)) \\ w_i = 1 + \frac{x_i - 1}{4} \end{equation}
Domain:
$-10 \leq x_i \leq 10$
Reference:
https://www.sfu.ca/~ssurjano/levy.html
"""
[docs] def __init__(self, dimension=4, lower=-10.0, upper=10.0, *args, **kwargs):
r"""Initialize Levy problem..
Args:
dimension (Optional[int]): Dimension of the problem.
lower (Optional[Union[float, Iterable[float]]]): Lower bounds of the problem.
upper (Optional[Union[float, Iterable[float]]]): Upper bounds of the problem.
See Also:
:func:`niapy.problems.Problem.__init__`
"""
super().__init__(dimension, lower, upper, *args, **kwargs)
[docs] @staticmethod
def latex_code():
r"""Return the latex code of the problem.
Returns:
str: Latex code.
"""
return r'''$f(\textbf{x}) = \sin^2 (\pi w_1) + \sum_{i = 1}^{D - 1} (w_i - 1)^2 \left( 1 + 10 \sin^2 (\pi w_i + 1) \right) + (w_d - 1)^2 (1 + \sin^2 (2 \pi w_d)) \\ w_i = 1 + \frac{x_i - 1}{4}$'''
def _evaluate(self, x):
w = 1 + (x - 1) / 4
term1 = np.sin(np.pi * w[0]) ** 2
wi = w[:-1]
term2 = np.sum((wi - 1) ** 2 * (1 + 10 * np.sin(np.pi * wi + 1)))
term3 = (w[-1] - 1) ** 2 * (1 + np.sin(2 * np.pi * w[-1]) ** 2)
return term1 + term2 + term3