Passed
Push — master ( 8465f7...8358a6 )
by Grega
01:08
created

NiaPy.algorithms.modified.hba.HybridBatAlgorithm.runIteration()   B

Complexity

Conditions 6

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nop 8
dl 0
loc 28
rs 8.6666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 algorithmInfo():
44
		r"""Get basic information about the algorithm.
45
46
		Returns:
47
			str: Basic information.
48
		"""
49
		return r"""Fister Jr., Iztok and Fister, Dusan and Yang, Xin-She. "A Hybrid Bat Algorithm". Elektrotehniski vestnik, 2013. 1-7."""
50
51
	@staticmethod
52
	def typeParameters():
53
		r"""Get dictionary with functions for checking values of parameters.
54
55
		Returns:
56
			Dict[str, Callable]:
57
				* F (Callable[[Union[int, float]], bool]): Scaling factor.
58
				* CR (Callable[[float], bool]): Crossover probability.
59
60
		See Also:
61
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.typeParameters`
62
		"""
63
		d = BatAlgorithm.typeParameters()
64
		d.update({
65
			'F': lambda x: isinstance(x, (int, float)) and x > 0,
66
			'CR': lambda x: isinstance(x, float) and 0 <= x <= 1
67
		})
68
		return d
69
70
	def setParameters(self, F=0.78, CR=0.35, CrossMutt=CrossBest1, **ukwargs):
71
		r"""Set core parameters of HybridBatAlgorithm algorithm.
72
73
		Arguments:
74
			F (Optional[float]): Scaling factor.
75
			CR (Optional[float]): Crossover.
76
77
		See Also:
78
			* :func:`NiaPy.algorithms.basic.BatAlgorithm.setParameters`
79
		"""
80
		BatAlgorithm.setParameters(self, **ukwargs)
81
		self.F, self.CR, self.CrossMutt = F, CR, CrossMutt
82
		if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
83
84
	def generateBest(self, best, task, i, Sol, **kwargs):
85
		r"""Generate new solution based on global best known solution.
86
87
		Args:
88
			best (numpy.ndarray): Global best individual.
89
			task (Task): Optimization task.
90
			i (int): Index of current individual.
91
			Sol (numpy.ndarray): Current best population.
92
			**kwargs (Dict[str, Any]):
93
94
		Returns:
95
			numpy.ndarray: New solution based on global best individual.
96
		"""
97
		return task.repair(self.CrossMutt(Sol, i, best, self.F, self.CR, rnd=self.Rand), rnd=self.Rand)
98
99
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
100