Coverage for moptipy / algorithms / so / fitnesses / direct.py: 100%
8 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"""
2Direct fitness assignment uses the objective values directly as fitness.
4Together with fitness proportionate selection (:class:`~moptipy.algorithms.\
5modules.selections.fitness_proportionate_sus.FitnessProportionateSUS`), it
6turns an EA (:class:`~moptipy.algorithms.so.general_ea.GeneralEA`) to a
7minimization variant of the traditional Genetic Algorithms.
91. John Henry Holland. *Adaptation in Natural and Artificial Systems: An
10 Introductory Analysis with Applications to Biology, Control, and Artificial
11 Intelligence.* Ann Arbor, MI, USA: University of Michigan Press. 1975.
12 ISBN: 0-472-08460-7
132. David Edward Goldberg. *Genetic Algorithms in Search, Optimization, and
14 Machine Learning.* Boston, MA, USA: Addison-Wesley Longman Publishing Co.,
15 Inc. 1989. ISBN: 0-201-15767-5
16"""
19from numpy.random import Generator
21from moptipy.algorithms.so.fitness import Fitness, FRecord
24# start book
25class Direct(Fitness):
26 """
27 Objective values are used as fitness directly.
29 This is the most primitive and basic fitness assignment strategy.
30 It does not give preference to new solutions over old solutions if both
31 have the same objective value. In this, it is different from
32 :class:`~moptipy.algorithms.so.fitnesses.rank_and_iteration\
33.RankAndIteration`, which is the default fitness assignment strategy.
34 It is therefore also not compatible to our basic (mu+lambda) Evolutionary
35 Algorithm implementation (:class:`~moptipy.algorithms.so.ea.EA`).
36 """
38 def assign_fitness(self, p: list[FRecord], random: Generator) -> None:
39 """
40 Assign the objective value as fitness.
42 :param p: the list of records
43 :param random: ignored
44 """
45 for rec in p: # iterate over all records in the population
46 rec.fitness = rec.f # store objective value f as fitness
47# end book
49 def __str__(self):
50 """
51 Get the name of the direct fitness assignment.
53 :return: the name of this fitness assignment strategy
54 :retval "direct": always
55 """
56 return "direct"