Completed
Push — master ( 1ffd79...ed2fa1 )
by Grega
21s queued 13s
created

HybridSelfAdaptiveBatAlgorithm.getParameters()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
# encoding=utf8
2
import logging
3
4
from NiaPy.algorithms.modified import SelfAdaptiveBatAlgorithm
5
from NiaPy.algorithms.basic.de import CrossBest1
6
7
logging.basicConfig()
8
logger = logging.getLogger('NiaPy.algorithms.modified')
9
logger.setLevel('INFO')
10
11
__all__ = ['HybridSelfAdaptiveBatAlgorithm']
12
13
class HybridSelfAdaptiveBatAlgorithm(SelfAdaptiveBatAlgorithm):
14
	r"""Implementation of Hybrid self adaptive bat algorithm.
15
16
	Algorithm:
17
		Hybrid self adaptive bat algorithm
18
19
	Date:
20
		April 2019
21
22
	Author:
23
		Klemen Berkovič
24
25
	License:
26
		MIT
27
28
	Reference paper:
29
		Fister, Iztok, Simon Fong, and Janez Brest. "A novel hybrid self-adaptive bat algorithm." The Scientific World Journal 2014 (2014).
30
31
	Reference URL:
32
		https://www.hindawi.com/journals/tswj/2014/709738/cta/
33
34
	Attributes:
35
		Name (List[str]): List of strings representing algorithm name.
36
		F (float): Scaling factor for local search.
37
		CR (float): Probability of crossover for local search.
38
		CrossMutt (Callable[[numpy.ndarray, int, numpy.ndarray, float, float, mtrand.RandomState, Dict[str, Any]): Local search method based of Differential evolution strategy.
39
40
	See Also:
41
		* :class:`NiaPy.algorithms.basic.BatAlgorithm`
42
	"""
43
	Name = ['HybridSelfAdaptiveBatAlgorithm', 'HSABA']
44
45
	@staticmethod
46
	def algorithmInfo():
47
		r"""Get basic information about the algorithm.
48
49
		Returns:
50
			str: Basic information.
51
		"""
52
		return r"""Fister, Iztok, Simon Fong, and Janez Brest. "A novel hybrid self-adaptive bat algorithm." The Scientific World Journal 2014 (2014)."""
53
54
	@staticmethod
55
	def typeParameters():
56
		r"""Get dictionary with functions for checking values of parameters.
57
58
		Returns:
59
			Dict[str, Callable]: Additional arguments.
60
61
		See Also:
62
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.typeParameters`
63
		"""
64
		d = SelfAdaptiveBatAlgorithm.typeParameters()
65
		d.update({
66
			'F': lambda x: isinstance(x, (int, float)) and x > 0,
67
			'CR': lambda x: isinstance(x, float) and 0 <= x <= 1
68
		})
69
		return d
70
71
	def setParameters(self, F=0.9, CR=0.85, CrossMutt=CrossBest1, **ukwargs):
72
		r"""Set core parameters of HybridBatAlgorithm algorithm.
73
74
		Arguments:
75
			F (Optional[float]): Scaling factor for local search.
76
			CR (Optional[float]): Probability of crossover for local search.
77
			CrossMutt (Optional[Callable[[numpy.ndarray, int, numpy.ndarray, float, float, mtrand.RandomState, Dict[str, Any], numpy.ndarray]]): Local search method based of Differential evolution strategy.
78
			ukwargs (Dict[str, Any]): Additional arguments.
79
80
		See Also:
81
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
82
		"""
83
		SelfAdaptiveBatAlgorithm.setParameters(self, **ukwargs)
84
		self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
85
86
	def getParameters(self):
87
		r"""Get parameters of the algorithm.
88
89
		Returns:
90
			Dict[str, Any]: Parameters of the algorithm.
91
92
		See Also:
93
			* :func:`NiaPy.algorithms.modified.AdaptiveBatAlgorithm.getParameters`
94
		"""
95
		d = SelfAdaptiveBatAlgorithm.getParameters(self)
96
		d.update({
97
			'F': self.F,
98
			'CR': self.CR
99
		})
100
		return d
101
102
	def localSearch(self, best, A, i, Sol, task, **kwargs):
103
		r"""Improve the best solution.
104
105
		Args:
106
			best (numpy.ndarray): Global best individual.
107
			task (Task): Optimization task.
108
			i (int): Index of current individual.
109
			Sol (numpy.ndarray): Current best population.
110
			**kwargs (Dict[str, Any]):
111
112
		Returns:
113
			numpy.ndarray: New solution based on global best individual.
114
		"""
115
		return task.repair(self.CrossMutt(Sol, i, best, self.F, self.CR, rnd=self.Rand), rnd=self.Rand)
116
117
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
118