Completed
Push — master ( cc7745...279fb5 )
by Grega
19s queued 17s
created

NiaPy.algorithms.modified.hsaba   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 87 %

Importance

Changes 0
Metric Value
eloc 25
dl 87
loc 100
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A HybridSelfAdaptiveBatAlgorithm.setParameters() 14 14 1
A HybridSelfAdaptiveBatAlgorithm.localSearch() 14 14 1
A HybridSelfAdaptiveBatAlgorithm.typeParameters() 16 16 3
A HybridSelfAdaptiveBatAlgorithm.algorithmInfo() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class HybridSelfAdaptiveBatAlgorithm(SelfAdaptiveBatAlgorithm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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=3, CR=0.5, 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 localSearch(self, best, A, i, Sol, task, **kwargs):
87
		r"""Improve the best solution.
88
89
		Args:
90
			best (numpy.ndarray): Global best individual.
91
			task (Task): Optimization task.
92
			i (int): Index of current individual.
93
			Sol (numpy.ndarray): Current best population.
94
			**kwargs (Dict[str, Any]):
95
96
		Returns:
97
			numpy.ndarray: New solution based on global best individual.
98
		"""
99
		return task.repair(self.CrossMutt(Sol, i, best, self.F, self.CR, rnd=self.Rand), rnd=self.Rand)
100
101
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
102