Population¶
Population management for evolutionary algorithms.
Individual¶
Bases: Generic[G]
An individual in the population.
Combines a genome with its evaluation results.
Source code in src/rotalabs_redqueen/core/population.py
from_result(genome: G, result: FitnessResult, generation: int = 0) -> Individual[G]
classmethod
¶
Create individual from fitness result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome
|
G
|
The genome |
required |
result
|
FitnessResult
|
Fitness evaluation result |
required |
generation
|
int
|
Birth generation |
0
|
Returns:
| Type | Description |
|---|---|
Individual[G]
|
New individual |
Source code in src/rotalabs_redqueen/core/population.py
Population¶
Bases: Generic[G]
A population of individuals.
Manages individuals across generations with selection and replacement.
Source code in src/rotalabs_redqueen/core/population.py
72 73 74 75 76 77 78 79 80 81 82 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
__init__(individuals: list[Individual[G]] | None = None, config: PopulationConfig | None = None, generation: int = 0)
¶
Initialize population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
individuals
|
list[Individual[G]] | None
|
Initial individuals (or empty) |
None
|
config
|
PopulationConfig | None
|
Population configuration |
None
|
generation
|
int
|
Current generation number |
0
|
Source code in src/rotalabs_redqueen/core/population.py
random(genome_class: type[G], size: int, rng: np.random.Generator | None = None) -> Population[G]
classmethod
¶
Create a population of random individuals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome_class
|
type[G]
|
Genome class with random() method |
required |
size
|
int
|
Population size |
required |
rng
|
Generator | None
|
Random number generator |
None
|
Returns:
| Type | Description |
|---|---|
Population[G]
|
New population with random genomes (unevaluated) |
Source code in src/rotalabs_redqueen/core/population.py
add(individual: Individual[G]) -> None
¶
best() -> Individual[G] | None
¶
Get the best individual by fitness.
Returns:
| Type | Description |
|---|---|
Individual[G] | None
|
Best individual or None if empty |
Source code in src/rotalabs_redqueen/core/population.py
top_n(n: int) -> list[Individual[G]]
¶
Get the top N individuals by fitness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of individuals to return |
required |
Returns:
| Type | Description |
|---|---|
list[Individual[G]]
|
List of top N individuals, sorted by fitness (best first) |
Source code in src/rotalabs_redqueen/core/population.py
tournament_select(tournament_size: int = 3, rng: np.random.Generator | None = None) -> Individual[G]
¶
Select an individual via tournament selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tournament_size
|
int
|
Number of individuals in tournament |
3
|
rng
|
Generator | None
|
Random number generator |
None
|
Returns:
| Type | Description |
|---|---|
Individual[G]
|
Winner of tournament (best fitness) |
Source code in src/rotalabs_redqueen/core/population.py
select_parents(n: int, tournament_size: int = 3, rng: np.random.Generator | None = None) -> list[Individual[G]]
¶
Select N parents via tournament selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of parents to select |
required |
tournament_size
|
int
|
Tournament size |
3
|
rng
|
Generator | None
|
Random number generator |
None
|
Returns:
| Type | Description |
|---|---|
list[Individual[G]]
|
List of selected parents |
Source code in src/rotalabs_redqueen/core/population.py
average_fitness() -> float
¶
Get average fitness of population.
fitness_std() -> float
¶
Get standard deviation of fitness.
diversity() -> float
¶
Measure population diversity using average pairwise distance.
Returns:
| Type | Description |
|---|---|
float
|
Average behavior distance between individuals |