Skip to content

Commit 24d6c90

Browse files
committed
Fully Configurable EA Implementation with Selection and Fitness Assignment
1 parent eb275cd commit 24d6c90

File tree

14 files changed

+337
-49
lines changed

14 files changed

+337
-49
lines changed

moptipy/algorithms/modules/selection.py

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def select(self, source: List[FitnessRecord], dest: List[FitnessRecord],
3838
only the :attr:`~FitnessRecord.fitness` attribute of the records and
3939
the random numbers from `random` should be used as decision criteria.
4040
41+
Selection algorithms are modules of the fully-configurable
42+
Evolutionary Algorithm :class:`~moptipy.algorithms.so.full_ea.FullEA`.
43+
They can utilize fitness values computed by the fitness assignment
44+
processes (:class:`~moptipy.algorithms.so.fitness.Fitness`). Of
45+
course, they can also be applied in different contexts and are not
46+
bound to single-objective optimization.
47+
4148
:param source: the list with the records to select from
4249
:param dest: the destination to append the selected records to
4350
:param n: the number of records to select
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Choose the `n` best elements from the source array.
3+
4+
"Best" selection is the standard way to perform survival selection in a
5+
(mu+lambda) Evolutionary Algorithm (:class:`~moptipy.algorithms.so.ea.EA`).
6+
It 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
8+
deterministic, i.e., it will always select each of the `n` best records
9+
exactly once.
10+
11+
This selection is also often called "mu+lambda," "mu,lambda," or "top-n"
12+
selection. It is similar to the selection scheme in Eshelman's CHC algorithm.
13+
A randomized version of this algorithm is called "truncation selection" in the
14+
paper of Blickle and Thiele.
15+
16+
1. 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).
24+
2. 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: Morgan Kaufmann.
28+
https://doi.org/10.1016/B978-0-08-050684-5.50020-3
29+
3. 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.
36+
4. 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
41+
5. 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+
"""
47+
48+
from typing import List
49+
50+
from numpy.random import Generator
51+
52+
53+
from moptipy.algorithms.modules.selection import Selection, FitnessRecord
54+
55+
56+
class Best(Selection):
57+
"""The best selection: select each of the best `n` elements once."""
58+
59+
def select(self, source: List[FitnessRecord], dest: List[FitnessRecord],
60+
n: int, random: Generator) -> None: # pylint: disable=W0613
61+
"""
62+
Perform deterministic best selection without replacement.
63+
64+
:param source: the list with the records to select from
65+
:param dest: the destination to append the selected records to
66+
:param n: the number of records to select
67+
:param random: the random number generator
68+
"""
69+
source.sort()
70+
dest.extend(source[0:n])
71+
72+
def __str__(self):
73+
"""
74+
Get the name of the best selection algorithm.
75+
76+
:return: the name of the best selection algorithm
77+
"""
78+
return "trunc"

moptipy/algorithms/modules/selections/truncation.py

-38
This file was deleted.

moptipy/algorithms/so/ea.py

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
Estimation, Simulation, and Control - Wiley-Interscience Series in Discrete
4040
Mathematics and Optimization, volume 6. 2003. Chichester, West Sussex, UK:
4141
Wiley Interscience. ISBN: 0-471-33052-3. http://www.jhuapl.edu/ISSO/.
42+
3. Frank Hoffmeister and Thomas Bäck. Genetic Algorithms and Evolution
43+
Strategies: Similarities and Differences. In Hans-Paul Schwefel and
44+
Reinhard Männer, *Proceedings of the International Conference on Parallel
45+
Problem Solving from Nature (PPSN I),* October 1-3, 1990, Dortmund,
46+
Germany, volume 496 of Lecture Notes in Computer Science, pages 455-469,
47+
Berlin/Heidelberg, Germany: Springer. ISBN: 978-3-540-54148-6.
48+
https://doi.org/10.1007/BFb0029787.
4249
"""
4350
from math import isfinite
4451
from typing import Final, Union, Callable, List, cast, Optional
+8-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
"""Fitness assignment processes."""
1+
"""
2+
Fitness assignment processes.
3+
4+
Fitness assignment processes are modules of the fully-configurable
5+
Evolutionary Algorithm :class:`~moptipy.algorithms.so.full_ea.FullEA`.
6+
The transform objective values to scalar fitness values that are then used
7+
by :class:`~moptipy.algorithms.modules.selection.Selection` algorithms.
8+
"""
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Use the objective values directly as fitness."""
2+
3+
from typing import List
4+
5+
from numpy.random import Generator
6+
7+
from moptipy.algorithms.so.fitness import Fitness, FRecord
8+
9+
10+
class Direct(Fitness):
11+
"""
12+
Objective values are used as fitness directly.
13+
14+
This is the most primitive and basic fitness assignment strategy.
15+
It does not give preference to new solutions over old solutions if both
16+
have the same objective value. In this, it is different from
17+
:class:`~moptipy.algorithms.so.fitnesses.rank_and_iteration\
18+
.RankAndIteration`, which is the default fitness assignment strategy.
19+
It is therefore also not compatible to our basic (mu+lambda) Evolutionary
20+
Algorithm implementation (:class:`~moptipy.algorithms.so.ea.EA`).
21+
"""
22+
23+
def assign_fitness(self, p: List[FRecord], random: Generator) -> None:
24+
"""
25+
Assign the objective value as fitness.
26+
27+
:param p: the list of records
28+
:param random: ignored
29+
"""
30+
for rec in p:
31+
rec.fitness = rec.f
32+
33+
def __str__(self):
34+
"""
35+
Get the name of this direct fitness assignment.
36+
37+
:return: the name of this fitness assignment strategy
38+
:retval "direct": always
39+
"""
40+
return "direct"

moptipy/algorithms/so/fitnesses/ffa.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
into the fitness.
1313
This index is used to break ties, in which case newer solutions are preferred.
1414
This can make the EA with FFA compatible with the
15-
:class:`moptipy.algorithms.so.fea1plus1.FEA1plus1` if truncation selection
16-
(:class:`moptipy.algorithms.modules.selections.truncation.Truncation`) is used
15+
:class:`moptipy.algorithms.so.fea1plus1.FEA1plus1` if "best" selection
16+
(:class:`moptipy.algorithms.modules.selections.best.Best`) is used
1717
at mu=lambda=1.
1818
1919
1. Thomas Weise, Zhize Wu, Xinlu Li, and Yan Chen. Frequency Fitness

moptipy/algorithms/so/fitnesses/rank_and_iteration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class RankAndIteration(Fitness):
3838
3939
This way, better solutions receive better fitness and ties are broken such
4040
that younger solutions (with higher iteration index) are preferred.
41-
In combination with truncation selection
42-
(:class:`moptipy.algorithms.modules.selections.truncation.Truncation`),
41+
In combination with best selection
42+
(:class:`moptipy.algorithms.modules.selections.best.Best`),
4343
this replicates the behavior of our simple (mu+lambda) Evolutionary
4444
Algorithm (:class:`~moptipy.algorithms.so.ea.EA`).
4545
"""

moptipy/algorithms/so/full_ea.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
Estimation, Simulation, and Control - Wiley-Interscience Series in Discrete
1515
Mathematics and Optimization, volume 6. 2003. Chichester, West Sussex, UK:
1616
Wiley Interscience. ISBN: 0-471-33052-3. http://www.jhuapl.edu/ISSO/.
17+
3. Frank Hoffmeister and Thomas Bäck. Genetic Algorithms and Evolution
18+
Strategies: Similarities and Differences. In Hans-Paul Schwefel and
19+
Reinhard Männer, *Proceedings of the International Conference on Parallel
20+
Problem Solving from Nature (PPSN I),* October 1-3, 1990, Dortmund,
21+
Germany, volume 496 of Lecture Notes in Computer Science, pages 455-469,
22+
Berlin/Heidelberg, Germany: Springer. ISBN: 978-3-540-54148-6.
23+
https://doi.org/10.1007/BFb0029787.
1724
"""
1825
from typing import Final, Union, Callable, List, cast, Optional
1926

@@ -22,7 +29,7 @@
2229
from moptipy.algorithms.modules.selection import Selection, check_selection
2330
from moptipy.algorithms.modules.selections.random_without_replacement \
2431
import RandomWithoutReplacement
25-
from moptipy.algorithms.modules.selections.truncation import Truncation
32+
from moptipy.algorithms.modules.selections.best import Best
2633
from moptipy.algorithms.so.ea import EA, _float_0
2734
from moptipy.algorithms.so.fitness import check_fitness, Fitness, FRecord
2835
from moptipy.algorithms.so.fitnesses.rank_and_iteration import RankAndIteration
@@ -174,10 +181,10 @@ def __init__(self, op0: Op0,
174181
if fitness.__class__ is not RankAndIteration:
175182
name = f"{name}{PART_SEPARATOR}{fitness}"
176183
if survival is None:
177-
survival = Truncation()
184+
survival = Best()
178185
if mating is None:
179186
mating = RandomWithoutReplacement()
180-
if (survival.__class__ is not Truncation) \
187+
if (survival.__class__ is not Best) \
181188
or (mating.__class__ is not RandomWithoutReplacement):
182189
name = f"{name}{PART_SEPARATOR}{survival}{PART_SEPARATOR}{mating}"
183190

moptipy/tests/fitness.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def __init__(self, x, z):
2424
self.z: Final[int] = z
2525

2626

27-
def validate_fitness(fitness: Fitness,
28-
objective: Objective,
29-
space: Space,
27+
def validate_fitness(fitness: Fitness, objective: Objective, space: Space,
3028
op0: Op0) -> None:
3129
"""
3230
Validate a fitness assignment process on a given problem.

0 commit comments

Comments
 (0)