@File : replacement.py
@Time : 2023/11/03 10:33:22
@Author : Alejandro Marrero
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2023, Alejandro Marrero
@Desc : None
elitist_replacement(current_population, offspring, hof=1)
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: |
-
current_population
( Sequence[IndType], )
–
Current population in the algorithm
-
offspring
( Sequence[IndType], )
–
-
hof
(int , default:
1
)
–
description. Defaults to 1.
|
Raises: |
-
ValueError
–
Raises if the sizes of the population are different
|
Source code in digneapy/operators/_replacement.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 | def elitist_replacement(
current_population: Sequence[IndType],
offspring: Sequence[IndType],
hof: int = 1,
) -> list[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:
current_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(current_population) != len(offspring):
msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in elitist_replacement"
raise ValueError(msg)
combined_population = sorted(
itertools.chain(current_population, offspring),
key=attrgetter("fitness"),
reverse=True,
)
top = combined_population[:hof]
return list(top + offspring[1:])
|
first_improve_replacement(current_population, offspring)
Returns a new population produced by a greedy operator.
Each individual in the current population is compared with its analogous in the offspring population
and the best survives
Parameters: |
-
current_population
( Sequence[IndType], )
–
Current population in the algorithm
-
offspring
( Sequence[IndType], )
–
|
Raises: |
-
ValueError
–
Raises if the sizes of the population are different
|
Returns: |
-
list[IndType]
–
list[IndType]: New population
|
Source code in digneapy/operators/_replacement.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 | def first_improve_replacement(
current_population: Sequence[IndType],
offspring: Sequence[IndType],
) -> list[IndType]:
"""Returns a new population produced by a greedy operator.
Each individual in the current population is compared with its analogous in the offspring population
and the best survives
Args:
current_population ( Sequence[IndType],): Current population in the algorithm
offspring ( Sequence[IndType],): Offspring population
Raises:
ValueError: Raises if the sizes of the population are different
Returns:
list[IndType]: New population
"""
if len(current_population) != len(offspring):
msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in first_improve_replacement"
raise ValueError(msg)
return [a if a > b else b for a, b in zip(current_population, offspring)]
|
generational_replacement(current_population, offspring)
Returns the offspring population as the new current population
Parameters: |
-
current_population
( Sequence[IndType], )
–
Current population in the algorithm
-
offspring
( Sequence[IndType], )
–
|
Raises: |
-
ValueError
–
Raises if the sizes of the population are different
|
Returns: |
-
list[IndType]
–
Sequence[IndType]: New population
|
Source code in digneapy/operators/_replacement.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 | def generational_replacement(
current_population: Sequence[IndType],
offspring: Sequence[IndType],
) -> list[IndType]:
"""Returns the offspring population as the new current population
Args:
current_population ( Sequence[IndType],): Current population in the algorithm
offspring ( Sequence[IndType],): Offspring population
Raises:
ValueError: Raises if the sizes of the population are different
Returns:
Sequence[IndType]: New population
"""
if len(current_population) != len(offspring):
msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in generational replacement"
raise ValueError(msg)
return offspring[:]
|