Passed
Pull Request — master (#202)
by
unknown
01:18
created

HybridBatAlgorithm.generateBest()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
# encoding=utf8
2
# pylint: disable=mixed-indentation, multiple-statements, logging-not-lazy, attribute-defined-outside-init, arguments-differ, bad-continuation, unused-argument
3
import logging
4
5
from NiaPy.algorithms.basic import BatAlgorithm
6
from NiaPy.algorithms.basic.de import CrossBest1
7
8
logging.basicConfig()
9
logger = logging.getLogger('NiaPy.algorithms.modified')
10
logger.setLevel('INFO')
11
12
__all__ = ['HybridBatAlgorithm']
13
14
class HybridBatAlgorithm(BatAlgorithm):
15
	r"""Implementation of Hybrid bat algorithm.
16
17
	Algorithm:
18
		Hybrid bat algorithm
19
20
	Date:
21
		2018
22
23
	Author:
24
		Grega Vrbancic and Klemen Berkovič
25
26
	License:
27
		MIT
28
29
	Reference paper:
30
		Fister Jr., Iztok and Fister, Dusan and Yang, Xin-She. "A Hybrid Bat Algorithm". Elektrotehniski vestnik, 2013. 1-7.
31
32
	Attributes:
33
		Name (List[str]): List of strings representing algorithm name.
34
		F (float): Scaling factor.
35
		CR (float): Crossover.
36
37
	See Also:
38
		* :class:`NiaPy.algorithms.basic.BatAlgorithm`
39
	"""
40
	Name = ['HybridBatAlgorithm', 'HBA']
41
42
	@staticmethod
43
	def typeParameters():
44
		r"""Get dictionary with functions for checking values of parameters.
45
46
		Returns:
47
			Dict[str, Callable]:
48
				* F (Callable[[Union[int, float]], bool]): Scaling factor.
49
				* CR (Callable[[float], bool]): Crossover probability.
50
		"""
51
		d = BatAlgorithm.typeParameters()
52
		d['F'] = lambda x: isinstance(x, (int, float)) and x > 0
53
		d['CR'] = lambda x: isinstance(x, float) and 0 <= x <= 1
54
		return d
55
56
	def setParameters(self, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
57
		r"""Set core parameters of HybridBatAlgorithm algorithm.
58
59
		Arguments:
60
			F (Optional[float]): Scaling factor.
61
			CR (Optional[float]): Crossover.
62
63
		See Also:
64
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
65
		"""
66
		BatAlgorithm.setParameters(self, **ukwargs)
67
		self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
68
		if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
69
70
	def generateBest(self, best, task, i, Sol, **kwargs):
71
		r"""Generate new solution based on global best known solution.
72
73
		Args:
74
			best (numpy.ndarray): Global best individual.
75
			task (Task): Optimization task.
76
			i (int): Index of current individual.
77
			Sol (numpy.ndarray): Current best population.
78
			**kwargs (Dict[str, Any]):
79
80
		Returns:
81
			numpy.ndarray: New solution based on global best individual.
82
		"""
83
		return task.repair(self.CrossMutt(Sol, i, best, self.F, self.CR, rnd=self.Rand), rnd=self.Rand)
84
85
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
86