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

NiaPy.algorithms.modified.hba   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

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