@File : elitist.py
@Time : 2026/05/21 15:27:28
@Author : Alejandro Marrero (amarrerd@ull.edu.es)
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2026, Alejandro Marrero
@Desc : None
Elitist
Bases: Replacement
Source code in digneapy/operators/replacement/elitist.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 | class Elitist(Replacement):
def __init__(
self,
hall_of_fame: int = 1,
attr: str = "fitness",
seed: Optional[int | np.random.SeedSequence] = None,
):
super().__init__(seed)
self._hof = hall_of_fame
self._attr = attr
def __call__(
self,
population: Sequence[IndType],
offspring: Sequence[IndType],
) -> Sequence[IndType]:
"""Returns a new population constructed using the Elitist approach.
HoF number of individuals from the current + offspring populations are
kept in the new population. The remaining individuals are selected from
the offspring population.
Args:
population Sequence[IndType],: Current population in the algorithm
offspring Sequence[IndType],: Offspring population
hof (int, optional): _description_. Defaults to 1.
Raises:
ValueError: Raises if the sizes of the population are different
Returns:
list[IndType]:
"""
if len(population) != len(offspring):
msg = f"The size of the current population ({len(population)}) != size of the offspring ({len(offspring)}) in elitist_replacement"
raise ValueError(msg)
combined_population = sorted(
itertools.chain(population, offspring),
key=attrgetter(self._attr),
reverse=True,
)
top = combined_population[: self._hof]
return list(top + offspring[1:])
|
__call__(population, offspring)
Returns a new population constructed using the Elitist approach.
HoF number of individuals from the current + offspring populations are
kept in the new population. The remaining individuals are selected from
the offspring population.
| Parameters: |
-
population Sequence[IndType],
–
Current population in the algorithm
-
offspring Sequence[IndType],
–
-
hof
(int)
–
description. Defaults to 1.
|
| Raises: |
-
ValueError
–
Raises if the sizes of the population are different
|
Source code in digneapy/operators/replacement/elitist.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 | def __call__(
self,
population: Sequence[IndType],
offspring: Sequence[IndType],
) -> Sequence[IndType]:
"""Returns a new population constructed using the Elitist approach.
HoF number of individuals from the current + offspring populations are
kept in the new population. The remaining individuals are selected from
the offspring population.
Args:
population Sequence[IndType],: Current population in the algorithm
offspring Sequence[IndType],: Offspring population
hof (int, optional): _description_. Defaults to 1.
Raises:
ValueError: Raises if the sizes of the population are different
Returns:
list[IndType]:
"""
if len(population) != len(offspring):
msg = f"The size of the current population ({len(population)}) != size of the offspring ({len(offspring)}) in elitist_replacement"
raise ValueError(msg)
combined_population = sorted(
itertools.chain(population, offspring),
key=attrgetter(self._attr),
reverse=True,
)
top = combined_population[: self._hof]
return list(top + offspring[1:])
|