Completed
Push — master ( ff5cab...a135cc )
by
unknown
17s queued 13s
created

DifferentialEvolutionMTS.setParameters()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 8
dl 0
loc 12
rs 10
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
import logging
3
4
from numpy import argsort
5
6
from NiaPy.algorithms.algorithm import Individual
7
from NiaPy.algorithms.basic.de import MultiStrategyDifferentialEvolution, DynNpDifferentialEvolution, DifferentialEvolution
8
from NiaPy.algorithms.other.mts import MTS_LS1, MTS_LS1v1, MTS_LS2, MTS_LS3, MTS_LS3v1, MultipleTrajectorySearch
9
10
logging.basicConfig()
11
logger = logging.getLogger('NiaPy.algorithms.modified')
12
logger.setLevel('INFO')
13
14
__all__ = ['DifferentialEvolutionMTS', 'DifferentialEvolutionMTSv1', 'DynNpDifferentialEvolutionMTS', 'DynNpDifferentialEvolutionMTSv1', 'MultiStrategyDifferentialEvolutionMTS', 'MultiStrategyDifferentialEvolutionMTSv1', 'DynNpMultiStrategyDifferentialEvolutionMTS', 'DynNpMultiStrategyDifferentialEvolutionMTSv1']
15
16
class MtsIndividual(Individual):
17
	r"""Individual for MTS local searches.
18
19
	Attributes:
20
		SR (numpy.ndarray): Search range.
21
		grade (int): Grade of individual.
22
		enable (bool): If enabled.
23
		improved (bool): If improved.
24
25
	See Also:
26
		:class:`NiaPy.algorithms.algorithm.Individual`
27
	"""
28
	def __init__(self, SR=None, grade=0, enable=True, improved=False, task=None, **kwargs):
29
		r"""Initialize the individual.
30
31
		Args:
32
			SR (numpy.ndarray): Search range.
33
			grade (Optional[int]): Grade of individual.
34
			enable (Optional[bool]): If enabled individual.
35
			improved (Optional[bool]): If individual improved.
36
			**kwargs (Dict[str, Any]): Additional arguments.
37
38
		See Also:
39
			:func:`NiaPy.algorithms.algorithm.Individual.__init__`
40
		"""
41
		Individual.__init__(self, task=task, **kwargs)
42
		self.grade, self.enable, self.improved = grade, enable, improved
43
		if SR is None and task is not None: self.SR = task.bRange / 4
44
		else: self.SR = SR
45
46
class DifferentialEvolutionMTS(DifferentialEvolution, MultipleTrajectorySearch):
47
	r"""Implementation of Differential Evolution with MTS local searches.
48
49
	Algorithm:
50
		Differential Evolution withm MTS local searches
51
52
	Date:
53
		2018
54
55
	Author:
56
		Klemen Berkovič
57
58
	License:
59
		MIT
60
61
	Attributes:
62
		Name (List[str]): List of strings representing algorithm names.
63
		LSs (Iterable[Callable[[numpy.ndarray, float, numpy.ndarray, float, bool, numpy.ndarray, Task, Dict[str, Any]], Tuple[numpy.ndarray, float, numpy.ndarray, float, bool, int, numpy.ndarray]]]): Local searches to use.
64
		BONUS1 (int): Bonus for improving global best solution.
65
		BONUS2 (int): Bonus for improving solution.
66
		NoLsTests (int): Number of test runs on local search algorithms.
67
		NoLs (int): Number of local search algorithm runs.
68
		NoEnabled (int): Number of best solution for testing.
69
70
	See Also:
71
		* :class:`NiaPy.algorithms.basic.de.DifferentialEvolution`
72
		* :class:`NiaPy.algorithms.other.mts.MultipleTrajectorySearch`
73
	"""
74
	Name = ['DifferentialEvolutionMTS', 'DEMTS']
75
76
	@staticmethod
77
	def algorithmInfo():
78
		r"""Get basic information about the algorithm.
79
80
		Returns:
81
			str: Basic information.
82
83
		See Also:
84
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
85
		"""
86
		return r"""TODO"""
87
88
	@staticmethod
89
	def typeParameters():
90
		r"""Get dictionary with functions for checking values of parameters.
91
92
		Returns:
93
			Dict[str, Callable]:
94
				* NoLsTests (Callable[[int], bool]): TODO
95
				* NoLs (Callable[[int], bool]): TODO
96
				* NoEnabled (Callable[[int], bool]): TODO
97
98
		See Also:
99
			:func:`NiaPy.algorithms.basic.de.DifferentialEvolution.typeParameters`
100
		"""
101
		d = DifferentialEvolution.typeParameters()
102
		d.update({
103
			'NoLsTests': lambda x: isinstance(x, int) and x >= 0,
104
			'NoLs': lambda x: isinstance(x, int) and x >= 0,
105
			'NoEnabled': lambda x: isinstance(x, int) and x > 0
106
		})
107
		return d
108
109
	def setParameters(self, NoLsTests=1, NoLs=2, NoEnabled=2, BONUS1=10, BONUS2=2, LSs=(MTS_LS1, MTS_LS2, MTS_LS3), **ukwargs):
110
		r"""Set the algorithm parameters.
111
112
		Arguments:
113
			SR (numpy.ndarray): Search range.
114
115
		See Also:
116
			:func:`NiaPy.algorithms.basic.de.DifferentialEvolution.setParameters`
117
		"""
118
		DifferentialEvolution.setParameters(self, itype=ukwargs.pop('itype', MtsIndividual), **ukwargs)
119
		self.LSs, self.NoLsTests, self.NoLs, self.NoEnabled = LSs, NoLsTests, NoLs, NoEnabled
120
		self.BONUS1, self.BONUS2 = BONUS1, BONUS2
121
122
	def getParameters(self):
123
		d = DifferentialEvolution.getParameters(self)
124
		# TODO add parameter values to dictionary
125
		return d
126
127
	def postSelection(self, X, task, xb, fxb, **kwargs):
128
		r"""Post selection operator.
129
130
		Args:
131
			X (numpy.ndarray): Current populaiton.
132
			task (Task): Optimization task.
133
			xb (numpy.ndarray): Global best individual.
134
			**kwargs (Dict[str, Any]): Additional arguments.
135
136
		Returns:
137
			Tuple[numpy.ndarray, numpy.ndarray, float]: New population.
138
		"""
139
		for x in X:
140
			if not x.enable: continue
141
			x.enable, x.grades = False, 0
142
			x.x, x.f, xb, fxb, k = self.GradingRun(x.x, x.f, xb, fxb, x.improved, x.SR, task)
143
			x.x, x.f, xb, fxb, x.improved, x.SR, x.grades = self.LsRun(k, x.x, x.f, xb, fxb, x.improved, x.SR, 0, task)
144
		for i in X[argsort([x.grade for x in X])[:self.NoEnabled]]: i.enable = True
145
		return X, xb, fxb
146
147
class DifferentialEvolutionMTSv1(DifferentialEvolutionMTS):
148
	r"""Implementation of Differential Evolution withm MTSv1 local searches.
149
150
	Algorithm:
151
		Differential Evolution withm MTSv1 local searches
152
153
	Date:
154
		2018
155
156
	Author:
157
		Klemen Berkovič
158
159
	License:
160
		MIT
161
162
	Attributes:
163
		Name (List[str]): List of strings representing algorithm name.
164
165
	See Also:
166
		:class:`NiaPy.algorithms.modified.DifferentialEvolutionMTS`
167
	"""
168
	Name = ['DifferentialEvolutionMTSv1', 'DEMTSv1']
169
170
	@staticmethod
171
	def algorithmInfo():
172
		r"""Get basic information about the algorithm.
173
174
		Returns:
175
			str: Basic information.
176
177
		See Also:
178
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
179
		"""
180
		return r"""TODO"""
181
182
	def setParameters(self, **ukwargs):
183
		r"""Set core parameters of DifferentialEvolutionMTSv1 algorithm.
184
185
		Args:
186
			**ukwargs (Dict[str, Any]): Additional arguments.
187
188
		See Also:
189
			:func:`NiaPy.algorithms.modified.DifferentialEvolutionMTS.setParameters`
190
		"""
191
		DifferentialEvolutionMTS.setParameters(self, LSs=(MTS_LS1v1, MTS_LS2, MTS_LS3v1), **ukwargs)
192
193
class DynNpDifferentialEvolutionMTS(DifferentialEvolutionMTS, DynNpDifferentialEvolution):
194
	r"""Implementation of Differential Evolution withm MTS local searches dynamic and population size.
195
196
	Algorithm:
197
		Differential Evolution withm MTS local searches and dynamic population size
198
199
	Date:
200
		2018
201
202
	Author:
203
		Klemen Berkovič
204
205
	License:
206
		MIT
207
208
	Attributes:
209
		Name (List[str]): List of strings representing algorithm name
210
211
	See Also:
212
		* :class:`NiaPy.algorithms.modified.DifferentialEvolutionMTS`
213
		* :class:`NiaPy.algorithms.basic.de.DynNpDifferentialEvolution`
214
	"""
215
	Name = ['DynNpDifferentialEvolutionMTS', 'dynNpDEMTS']
216
217
	@staticmethod
218
	def algorithmInfo():
219
		r"""Get basic information about the algorithm.
220
221
		Returns:
222
			str: Basic information.
223
224
		See Also:
225
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
226
		"""
227
		return r"""TODO"""
228
229
	def setParameters(self, pmax=10, rp=3, **ukwargs):
230
		r"""Set core parameters or DynNpDifferentialEvolutionMTS algorithm.
231
232
		Args:
233
			pmax (Optional[int]):
234
			rp (Optional[float]):
235
			**ukwargs (Dict[str, Any]): Additional arguments.
236
237
		See Also:
238
			* :func:`NiaPy.algorithms.modified.hde.DifferentialEvolutionMTS.setParameters`
239
			* :func`NiaPy.algorithms.basic.de.DynNpDifferentialEvolution.setParameters`
240
		"""
241
		DynNpDifferentialEvolution.setParameters(self, pmax=pmax, rp=rp, **ukwargs)
242
		DifferentialEvolutionMTS.setParameters(self, **ukwargs)
243
244
	def postSelection(self, X, task, xb, fxb, **kwargs):
245
		nX, xb, fxb = DynNpDifferentialEvolution.postSelection(self, X, task, xb, fxb)
246
		nX, xb, fxb = DifferentialEvolutionMTS.postSelection(self, nX, task, xb, fxb)
247
		return nX, xb, fxb
248
249
class DynNpDifferentialEvolutionMTSv1(DynNpDifferentialEvolutionMTS):
250
	r"""Implementation of Differential Evolution withm MTSv1 local searches and dynamic population size.
251
252
	Algorithm:
253
		Differential Evolution with MTSv1 local searches and dynamic population size
254
255
	Date:
256
		2018
257
258
	Author:
259
		Klemen Berkovič
260
261
	License:
262
		MIT
263
264
	Attributes:
265
		Name (List[str]): List of strings representing algorithm name.
266
267
	See Also:
268
		:class:`NiaPy.algorithms.modified.hde.DifferentialEvolutionMTS`
269
	"""
270
	Name = ['DynNpDifferentialEvolutionMTSv1', 'dynNpDEMTSv1']
271
272
	@staticmethod
273
	def algorithmInfo():
274
		r"""Get basic information about the algorithm.
275
276
		Returns:
277
			str: Basic information.
278
279
		See Also:
280
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
281
		"""
282
		return r"""TODO"""
283
284
	def setParameters(self, **ukwargs):
285
		r"""Set core arguments of DynNpDifferentialEvolutionMTSv1 algorithm.
286
287
		Args:
288
			**ukwargs (Dict[str, Any]): Additional arguments.
289
290
		See Also:
291
			:func:`NiaPy.algorithms.modified.hde.DifferentialEvolutionMTS.setParameters`
292
		"""
293
		DynNpDifferentialEvolutionMTS.setParameters(self, LSs=(MTS_LS1v1, MTS_LS2, MTS_LS3v1), **ukwargs)
294
295
class MultiStrategyDifferentialEvolutionMTS(DifferentialEvolutionMTS, MultiStrategyDifferentialEvolution):
296
	r"""Implementation of Differential Evolution withm MTS local searches and multiple mutation strategys.
297
298
	Algorithm:
299
		Differential Evolution withm MTS local searches and multiple mutation strategys
300
301
	Date:
302
		2018
303
304
	Author:
305
		Klemen Berkovič
306
307
	License:
308
		MIT
309
310
	Attributes:
311
		Name (List[str]): List of strings representing algorithm name.
312
313
	See Also:
314
		* :class:`NiaPy.algorithms.modified.hde.DifferentialEvolutionMTS`
315
		* :class:`NiaPy.algorithms.basic.de.MultiStrategyDifferentialEvolution`
316
	"""
317
	Name = ['MultiStrategyDifferentialEvolutionMTS', 'MSDEMTS']
318
319
	@staticmethod
320
	def algorithmInfo():
321
		r"""Get basic information about the algorithm.
322
323
		Returns:
324
			str: Basic information.
325
326
		See Also:
327
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
328
		"""
329
		return r"""TODO"""
330
331
	def setParameters(self, **ukwargs):
332
		r"""TODO.
333
334
		Args:
335
			**ukwargs (Dict[str, Any]): Additional arguments.
336
337
		See Also:
338
			* :func:`NiaPy.algorithms.modified.DifferentialEvolutionMTS.setParameters`
339
			* :func:`NiaPy.algorithms.basic.MultiStrategyDifferentialEvolution.setParameters`
340
		"""
341
		DifferentialEvolutionMTS.setParameters(self, **ukwargs)
342
		MultiStrategyDifferentialEvolution.setParameters(self, itype=ukwargs.pop('itype', MtsIndividual), **ukwargs)
343
344
	def evolve(self, pop, xb, task, **kwargs):
345
		r"""Evolve population.
346
347
		Args:
348
			pop (numpy.ndarray[Individual]): Current population of individuals.
349
			xb (Individual): Global best individual.
350
			task (Task): Optimization task.
351
			**kwargs (Dict[str, Any]): Additional arguments.
352
353
		Returns:
354
			numpy.ndarray[Individual]: Evolved population.
355
		"""
356
		return MultiStrategyDifferentialEvolution.evolve(self, pop, xb, task, **kwargs)
357
358
class MultiStrategyDifferentialEvolutionMTSv1(MultiStrategyDifferentialEvolutionMTS):
359
	r"""Implementation of Differential Evolution with MTSv1 local searches and multiple mutation strategys.
360
361
	Algorithm:
362
		Differential Evolution withm MTSv1 local searches and multiple mutation strategys
363
364
	Date:
365
		2018
366
367
	Author:
368
		Klemen Berkovič
369
370
	License:
371
		MIT
372
373
	Attributes:
374
		Name (List[str]): List of stings representing algorithm name.
375
376
	See Also:
377
		* :class:`NiaPy.algorithms.modified.MultiStrategyDifferentialEvolutionMTS`
378
	"""
379
	Name = ['MultiStrategyDifferentialEvolutionMTSv1', 'MSDEMTSv1']
380
381
	@staticmethod
382
	def algorithmInfo():
383
		r"""Get basic information about the algorithm.
384
385
		Returns:
386
			str: Basic information.
387
388
		See Also:
389
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
390
		"""
391
		return r"""TODO"""
392
393
	def setParameters(self, **ukwargs):
394
		r"""Set core parameters of MultiStrategyDifferentialEvolutionMTSv1 algorithm.
395
396
		Args:
397
			**ukwargs (Dict[str, Any]): Additional arguments.
398
399
		See Also:
400
			* :func:`NiaPy.algorithms.modified.MultiStrategyDifferentialEvolutionMTS.setParameters`
401
		"""
402
		MultiStrategyDifferentialEvolutionMTS.setParameters(self, LSs=(MTS_LS1v1, MTS_LS2, MTS_LS3v1), **ukwargs)
403
404
class DynNpMultiStrategyDifferentialEvolutionMTS(MultiStrategyDifferentialEvolutionMTS, DynNpDifferentialEvolutionMTS):
405
	r"""Implementation of Differential Evolution withm MTS local searches, multiple mutation strategys and dynamic population size.
406
407
	Algorithm:
408
		Differential Evolution withm MTS local searches, multiple mutation strategys and dynamic population size
409
410
	Date:
411
		2018
412
413
	Author:
414
		Klemen Berkovič
415
416
	License:
417
		MIT
418
419
	Attributes:
420
		Name (List[str]): List of strings representing algorithm name
421
422
	See Also:
423
		* :class:`NiaPy.algorithms.modified.MultiStrategyDifferentialEvolutionMTS`
424
		* :class:`NiaPy.algorithms.modified.DynNpDifferentialEvolutionMTS`
425
	"""
426
	Name = ['DynNpMultiStrategyDifferentialEvolutionMTS', 'dynNpMSDEMTS']
427
428
	@staticmethod
429
	def algorithmInfo():
430
		r"""Get basic information about the algorithm.
431
432
		Returns:
433
			str: Basic information.
434
435
		See Also:
436
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
437
		"""
438
		return r"""TODO"""
439
440
	def setParameters(self, **ukwargs):
441
		r"""Set core arguments of DynNpMultiStrategyDifferentialEvolutionMTS algorithm.
442
443
		Args:
444
			**ukwargs (Dict[str, Any]): Additional arguments.
445
446
		See Also:
447
			* :func:`NiaPy.algorithms.modified.MultiStrategyDifferentialEvolutionMTS.setParameters`
448
			* :func:`NiaPy.algorithms.modified.DynNpDifferentialEvolutionMTS.setParameters`
449
		"""
450
		DynNpDifferentialEvolutionMTS.setParameters(self, **ukwargs)
451
		MultiStrategyDifferentialEvolutionMTS.setParameters(self, **ukwargs)
452
453
class DynNpMultiStrategyDifferentialEvolutionMTSv1(DynNpMultiStrategyDifferentialEvolutionMTS):
454
	r"""Implementation of Differential Evolution withm MTSv1 local searches, multiple mutation strategys and dynamic population size.
455
456
	Algorithm:
457
		Differential Evolution withm MTSv1 local searches, multiple mutation strategys and dynamic population size
458
459
	Date:
460
		2018
461
462
	Author:
463
		Klemen Berkovič
464
465
	License:
466
		MIT
467
468
	Attributes:
469
		Name (List[str]): List of strings representing algorithm name.
470
471
	See Also:
472
		* :class:`NiaPy.algorithm.modified.DynNpMultiStrategyDifferentialEvolutionMTS`
473
	"""
474
	Name = ['DynNpMultiStrategyDifferentialEvolutionMTSv1', 'dynNpMSDEMTSv1']
475
476
	@staticmethod
477
	def algorithmInfo():
478
		r"""Get basic information about the algorithm.
479
480
		Returns:
481
			str: Basic information.
482
483
		See Also:
484
			* :func:`NiaPy.algorithms.Algorithm.algorithmInfo`
485
		"""
486
		return r"""TODO"""
487
488
	def setParameters(self, **kwargs):
489
		r"""Set core parameters of DynNpMultiStrategyDifferentialEvolutionMTSv1 algorithm.
490
491
		Args:
492
			**kwargs (Dict[str, Any]): Additional arguments.
493
494
		See Also:
495
			* :func:`NiaPy.algorithm.modified.DynNpMultiStrategyDifferentialEvolutionMTS.setParameters`
496
		"""
497
		DynNpMultiStrategyDifferentialEvolutionMTS.setParameters(self, LSs=(MTS_LS1v1, MTS_LS2, MTS_LS3v1), **kwargs)
498
499
# vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
500