Code Duplication    Length = 65-73 lines in 2 locations

NiaPy/algorithms/basic/de.py 1 location

@@ 413-485 (lines=73) @@
410
			P.append(pop[i] if pop[i].f < e.f else e)
411
		return asarray(P)
412
413
class DynNpDifferentialEvolution(DifferentialEvolution):
414
	r"""Implementation of Dynamic poulation size Differential evolution algorithm.
415
416
	Algorithm:
417
		Dynamic poulation size Differential evolution algorithm
418
419
	Date:
420
		2018
421
422
	Author:
423
		Klemen Berkovič
424
425
	License:
426
		MIT
427
428
	Attributes:
429
		Name (List[str]): List of strings representing algorithm names.
430
		pmax (int): TODO
431
		rp (int): TODO
432
433
	See Also:
434
		* :class:`NiaPy.algorithms.basic.DifferentialEvolution`
435
	"""
436
	Name = ['DynNpDifferentialEvolution', 'dynNpDE']
437
438
	@staticmethod
439
	def typeParameters():
440
		r"""Get dictionary with functions for checking values of parameters.
441
442
		Returns:
443
			Dict[str, Callable]:
444
				* rp (Callable[[Union[float, int]], bool]): TODO
445
				* pmax (Callable[[int], bool]): TODO
446
447
		See Also:
448
			* :func:`NiaPy.algorithms.basic.DifferentialEvolution.typeParameters`
449
		"""
450
		r = DifferentialEvolution.typeParameters()
451
		r['rp'] = lambda x: isinstance(x, (float, int)) and x > 0
452
		r['pmax'] = lambda x: isinstance(x, int) and x > 0
453
		return r
454
455
	def setParameters(self, pmax=50, rp=3, **ukwargs):
456
		r"""Set the algorithm parameters.
457
458
		Arguments:
459
			pmax (Optional[int]): TODO
460
			rp (Optional[int]): TODO
461
462
		See Also:
463
			* :func:`NiaPy.algorithms.basic.DifferentialEvolution.setParameters`
464
		"""
465
		DifferentialEvolution.setParameters(self, **ukwargs)
466
		self.pmax, self.rp = pmax, rp
467
		if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
468
469
	def postSelection(self, pop, task, **kwargs):
470
		r"""Post selection operator.
471
472
		In this algorithm the post selection operator decrements the population at specific iterations/generations.
473
474
		Args:
475
			pop (numpy.ndarray[Individual]): Current population.
476
			task (Task): Optimization task.
477
			kwargs (Dict[str, Any]): Additional arguments.
478
479
		Returns:
480
			numpy.ndarray[Individual]: Changed current population.
481
		"""
482
		Gr = task.nFES // (self.pmax * len(pop)) + self.rp
483
		nNP = len(pop) // 2
484
		if task.Iters == Gr and len(pop) > 3: pop = objects2array([pop[i] if pop[i].f < pop[i + nNP].f else pop[i + nNP] for i in range(nNP)])
485
		return pop
486
487
def proportional(Lt_min, Lt_max, mu, x_f, avg, *args):
488
	r"""Proportional calculation of age of individual.

NiaPy/algorithms/modified/jde.py 1 location

@@ 211-275 (lines=65) @@
208
		self.mu = abs(self.LT_max - self.LT_min) / 2
209
		if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
210
211
class DynNpSelfAdaptiveDifferentialEvolutionAlgorithm(SelfAdaptiveDifferentialEvolution):
212
	r"""Implementation of Dynamic population size self-adaptive differential evolution algorithm.
213
214
	Algorithm:
215
		Dynamic population size self-adaptive differential evolution algorithm
216
217
	Date:
218
		2018
219
220
	Author:
221
		Jan Popič and Klemen Berkovič
222
223
	License:
224
		MIT
225
226
	Reference URL:
227
		https://link.springer.com/article/10.1007/s10489-007-0091-x
228
229
	Reference paper:
230
		Brest, Janez, and Mirjam Sepesy Maučec. Population size reduction for the differential evolution algorithm. Applied Intelligence 29.3 (2008): 228-247.
231
232
	Attributes:
233
		Name (List[str]): List of strings representing algorithm name.
234
		rp (int): Small non-negative number which is added to value of genp.
235
		pmax (int): Number of population reductions.
236
237
	See Also:
238
		* :class:`NiaPy.algorithms.modified.SelfAdaptiveDifferentialEvolution`
239
	"""
240
	Name = ['DynNpSelfAdaptiveDifferentialEvolutionAlgorithm', 'dynNPjDE']
241
242
	@staticmethod
243
	def typeParameters():
244
		d = SelfAdaptiveDifferentialEvolution.typeParameters()
245
		d['rp'] = lambda x: isinstance(x, (float, int)) and x > 0
246
		d['pmax'] = lambda x: isinstance(x, int) and x > 0
247
		return d
248
249
	def setParameters(self, rp=0, pmax=10, **ukwargs):
250
		r"""Set the parameters of an algorithm.
251
252
		Arguments:
253
			rp (Optional[int]): Small non-negative number which is added to value of genp (if it's not divisible).
254
			pmax (Optional[int]): Number of population reductions.
255
256
		See Also:
257
			* :func:`NiaPy.algorithms.modified.SelfAdaptiveDifferentialEvolution.setParameters`
258
		"""
259
		SelfAdaptiveDifferentialEvolution.setParameters(self, **ukwargs)
260
		self.rp, self.pmax = rp, pmax
261
		if ukwargs: logger.info('Unused arguments: %s' % (ukwargs))
262
263
	def postSelection(self, pop, task, **kwargs):
264
		r"""Post selection operator.
265
266
		Args:
267
			pop (numpy.ndarray[Individual]): Current population.
268
			task (Task): Optimization task.
269
270
		Returns:
271
			numpy.ndarray[Individual]: New population.
272
		"""
273
		Gr, nNP = task.nFES // (self.pmax * len(pop)) + self.rp, len(pop) // 2
274
		if task.Iters == Gr and len(pop) > 3: return objects2array([pop[i] if pop[i].f < pop[i + nNP].f else pop[i + nNP] for i in range(nNP)])
275
		return pop
276
277
class MultiStrategySelfAdaptiveDifferentialEvolution(SelfAdaptiveDifferentialEvolution):
278
	r"""Implementation of self-adaptive differential evolution algorithm with multiple mutation strategys.