Coverage for moptipy / algorithms / modules / selections / best.py: 100%
10 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-24 08:49 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-24 08:49 +0000
1"""
2Choose the `n` best elements from the source array.
4"Best" selection is the standard way to perform survival selection in a
5(mu+lambda) Evolutionary Algorithm (:class:`~moptipy.algorithms.so.ea.EA`).
6It basically sorts the input array based on fitness and then copies the first
7`n` elements to the output array. The algorithm is not random but
8deterministic, i.e., it will always select each of the `n` best records
9exactly once.
11This selection is also often called "mu+lambda," "mu,lambda," or "top-n"
12selection. It is similar to the selection scheme in Eshelman's CHC algorithm.
13A randomized version of this algorithm is called "truncation selection" in the
14paper of Blickle and Thiele.
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. Larry J.Eshelman. The CHC Adaptive Search Algorithm: How to Have Safe
25 Search When Engaging in Nontraditional Genetic Recombination. In Gregory
26 J. E. Rawlins, editor, Foundations of Genetic Algorithms, volume 1, 1991,
27 pages 265-283. San Francisco, CA, USA: Morgan Kaufmann.
28 https://doi.org/10.1016/B978-0-08-050684-5.50020-3
293. Frank Hoffmeister and Thomas Bäck. Genetic Algorithms and Evolution
30 Strategies: Similarities and Differences. In Hans-Paul Schwefel and
31 Reinhard Männer, *Proceedings of the International Conference on Parallel
32 Problem Solving from Nature (PPSN I),* October 1-3, 1990, Dortmund,
33 Germany, volume 496 of Lecture Notes in Computer Science, pages 455-469,
34 Berlin/Heidelberg, Germany: Springer. ISBN: 978-3-540-54148-6.
35 https://doi.org/10.1007/BFb0029787.
364. Uday Kumar Chakraborty and Kalyanmoy Deb and Mandira Chakraborty. Analysis
37 of Selection Algorithms: A Markov Chain Approach. *Evolutionary
38 Computation,* 4(2):133-167. Summer 1996. Cambridge, MA, USA: MIT Press.
39 doi:10.1162/evco.1996.4.2.133.
40 https://dl.acm.org/doi/pdf/10.1162/evco.1996.4.2.133
415. Tobias Blickle and Lothar Thiele. A Comparison of Selection Schemes used in
42 Genetic Algorithms. Second edition, December 1995. TIK-Report 11 from the
43 Eidgenössische Technische Hochschule (ETH) Zürich, Department of Electrical
44 Engineering, Computer Engineering and Networks Laboratory (TIK), Zürich,
45 Switzerland. ftp://ftp.tik.ee.ethz.ch/pub/publications/TIK-Report11.ps
46"""
48from typing import Any, Callable
50from numpy.random import Generator
52from moptipy.algorithms.modules.selection import FitnessRecord, Selection
55# start book
56class Best(Selection):
57 """The best selection: select each of the best `n` elements once."""
59 def select(self, source: list[FitnessRecord],
60 dest: Callable[[FitnessRecord], Any],
61 n: int, random: Generator) -> None: # pylint: disable=W0613
62 """
63 Perform deterministic best selection without replacement.
65 :param source: the list with the records to select from
66 :param dest: the destination collector to invoke for each selected
67 record
68 :param n: the number of records to select
69 :param random: the random number generator
70 """
71 source.sort() # sort by fitness, best solutions come first
72 for i in range(n): # select the n first=best solutions
73 dest(source[i]) # by sending them to dest
74# end book
76 def __str__(self):
77 """
78 Get the name of the best selection algorithm.
80 :return: the name of the best selection algorithm
81 """
82 return "best"