CosineMixture.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
# encoding=utf8
2
3
"""Implementations of Cosine mixture functions."""
4
5
from numpy import cos, pi
6
from NiaPy.benchmarks.benchmark import Benchmark
7
8
__all__ = ['CosineMixture']
9
10
class CosineMixture(Benchmark):
11
	r"""Implementations of Cosine mixture function.
12
13
	Date: 2018
14
15
	Author: Klemen Berkovič
16
17
	License: MIT
18
19
	Function:
20
	**Cosine Mixture Function**
21
22
		:math:`f(\textbf{x}) = - 0.1 \sum_{i = 1}^D \cos (5 \pi x_i) - \sum_{i = 1}^D x_i^2`
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 ∈ [-1, 1]`, for all :math:`i = 1, 2,..., D`.
27
28
		**Global maximu:**
29
		:math:`f(x^*) = -0.1 D`, at :math:`x^* = (0.0,...,0.0)`
30
31
	LaTeX formats:
32
		Inline:
33
				$f(\textbf{x}) = - 0.1 \sum_{i = 1}^D \cos (5 \pi x_i) - \sum_{i = 1}^D x_i^2$
34
35
		Equation:
36
				\begin{equation} f(\textbf{x}) = - 0.1 \sum_{i = 1}^D \cos (5 \pi x_i) - \sum_{i = 1}^D x_i^2 \end{equation}
37
38
		Domain:
39
				$-1 \leq x_i \leq 1$
40
41
	Reference:
42
	http://infinity77.net/global_optimization/test_functions_nd_C.html#go_benchmark.CosineMixture
43
	"""
44
	Name = ['CosineMixture']
45
46
	def __init__(self, Lower=-1.0, Upper=1.0):
47
		r"""Initialize of Cosine mixture 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(\textbf{x}) = - 0.1 \sum_{i = 1}^D \cos (5 \pi x_i) - \sum_{i = 1}^D 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 f(D, X):
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
			v1, v2 = 0.0, 0.0
84
			for i in range(D): v1, v2 = v1 + cos(5 * pi * X[i]), v2 + X[i] ** 2
85
			return -0.1 * v1 - v2
86
		return f
87
88
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
89