Coverage for moptipy / algorithms / modules / selections / tournament_with_repl.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-24 08:49 +0000

1""" 

2Tournament selection with replacement in the tournaments. 

3 

4For each slot in the destination, a tournament with 

5:attr:`~moptipy.algorithms.modules.selections.tournament_with_repl.\ 

6TournamentWithReplacement.size` 

7randomly chosen participating solutions is conducted. The solution with 

8the best fitness wins and is copied to the destination. The solutions are 

9drawn with replacement for each tournament, meaning that one solution may 

10enter a given tournament several times. A solution may be selected multiple 

11times. 

12 

13Tournament selection without replacement is implemented in 

14:mod:`moptipy.algorithms.modules.selections.tournament_without_repl`. 

15 

161. Peter J. B. Hancock. An Empirical Comparison of Selection Methods in 

17 Evolutionary Algorithms. In Terence Claus Fogarty, editor, *Selected Papers 

18 from the AISB Workshop on Evolutionary Computing (AISB EC'94),* April 

19 11-13, 1994, Leeds, UK, volume 865 of Lecture Notes in Computer Science, 

20 pages 80-94, Berlin/Heidelberg, Germany: Springer, ISBN: 978-3-540-58483-4. 

21 https://dx.doi.org/10.1007/3-540-58483-8_7. Conference organized by the 

22 Society for the Study of Artificial Intelligence and Simulation of 

23 Behaviour (AISB). 

242. Uday Kumar Chakraborty and Kalyanmoy Deb and Mandira Chakraborty. Analysis 

25 of Selection Algorithms: A Markov Chain Approach. *Evolutionary 

26 Computation,* 4(2):133-167. Summer 1996. Cambridge, MA, USA: MIT Press. 

27 doi:10.1162/evco.1996.4.2.133. 

28 https://dl.acm.org/doi/pdf/10.1162/evco.1996.4.2.133 

293. Tobias Blickle and Lothar Thiele. A Comparison of Selection Schemes used in 

30 Genetic Algorithms. Second edition, December 1995. TIK-Report 11 from the 

31 Eidgenössische Technische Hochschule (ETH) Zürich, Department of Electrical 

32 Engineering, Computer Engineering and Networks Laboratory (TIK), Zürich, 

33 Switzerland. ftp://ftp.tik.ee.ethz.ch/pub/publications/TIK-Report11.ps 

344. Kumara Sastry and David Edward Goldberg. Modeling Tournament Selection with 

35 Replacement using Apparent Added Noise. In Lee Spector, Erik D. Goodman, 

36 Annie Wu, William Benjamin Langdon, and Hans-Michael Voigt, eds., 

37 *Proceedings of the 3rd Annual Conference on Genetic and Evolutionary 

38 Computation (GECCO'01)*, July 7-11, 2001, San Francisco, CA, USA, page 781. 

39 San Francisco, CA, United States: Morgan Kaufmann Publishers Inc. 

40 ISBN: 978-1-55860-774-3. https://dl.acm.org/doi/pdf/10.5555/2955239.2955378 

41""" 

42 

43from math import inf 

44from typing import Any, Callable, Final 

45 

46from numpy.random import Generator 

47from pycommons.types import check_int_range 

48 

49from moptipy.algorithms.modules.selection import FitnessRecord, Selection 

50from moptipy.utils.logger import KeyValueLogSection 

51 

52 

53# start book 

54class TournamentWithReplacement(Selection): 

55 """Tournament selection with replacement in the tournaments.""" 

56 

57# end book 

58 def __init__(self, size: int = 2) -> None: 

59 """ 

60 Create the tournament selection with replacement method. 

61 

62 :param size: the size of the tournaments 

63 """ 

64 super().__init__() 

65 #: the tournament size 

66 self.size: Final[int] = check_int_range(size, "tournament size", 1) 

67 

68# start book 

69 def select(self, source: list[FitnessRecord], 

70 dest: Callable[[FitnessRecord], Any], 

71 n: int, random: Generator) -> None: 

72 """ 

73 Perform tournament with replacement. 

74 

75 :param source: the list with the records to select from 

76 :param dest: the destination collector to invoke for each selected 

77 record 

78 :param n: the number of records to select 

79 :param random: the random number generator 

80 """ 

81 size: Final[int] = self.size # the tournament size 

82 m: Final[int] = len(source) # number of elements to select from 

83 ri: Final[Callable] = random.integers # fast call to random.integers 

84 for _ in range(n): # conduct n tournaments 

85 best: FitnessRecord | None = None # best competitor 

86 best_fitness: int | float = inf # best fitness, initial infinite 

87 for __ in range(size): # perform tournament 

88 rec = source[ri(m)] # get contestant record from source 

89 rec_fitness = rec.fitness # get its fitness 

90 if rec_fitness <= best_fitness: # if better or equal... 

91 best = rec # ... rec becomes the new best record 

92 best_fitness = rec_fitness # and remember fitness 

93 dest(best) # at end of the tournament, send best to dest 

94 # end book 

95 

96 def __str__(self): 

97 """ 

98 Get the name of the tournament selection algorithm. 

99 

100 :return: the name of the tournament selection algorithm 

101 """ 

102 return f"tour{self.size}r" 

103 

104 def log_parameters_to(self, logger: KeyValueLogSection) -> None: 

105 """ 

106 Log the parameters of the algorithm to a logger. 

107 

108 :param logger: the logger for the parameters 

109 """ 

110 super().log_parameters_to(logger) 

111 logger.key_value("size", self.size)