Algorithms¶
deap_er.algorithms
¶
PolicyActionGuard(max_promotes_per_gen=1, max_tune_gen=5, min_exam_size=1, promote_cooldown=0, n_evals=None, nevals_used=0, generation=0, promotes_this_gen=0, last_promote_gen=None)
dataclass
¶
Hard caps for :func:~deap_er.algorithms.apply_policy_action.
Tracks per-generation promote counts, promote cooldown,
inner tune generation limits, minimum exam size, and an optional
evaluation budget. Call :meth:begin_generation at the start of
each outer generation so promote limits reset.
Attributes:
| Name | Type | Description |
|---|---|---|
max_promotes_per_gen |
int
|
Maximum |
max_tune_gen |
int
|
Maximum inner |
min_exam_size |
int
|
Minimum catalog size required for
|
promote_cooldown |
int
|
Generations that must pass after a promote before another promote is allowed. |
n_evals |
int | None
|
Optional evaluation budget. When set, actions whose estimated cost would exceed the remaining budget are rejected. |
nevals_used |
int
|
Evaluations already charged to this guard. |
generation |
int
|
Current outer generation index. |
promotes_this_gen |
int
|
Promotes applied in the current generation. |
last_promote_gen |
int | None
|
Generation index of the last promote, or
|
__post_init__()
¶
Reject invalid cap configuration at construction.
Source code in deap_er/private/operators/policy_action_guard.py
begin_generation(generation=None)
¶
Reset per-generation counters and optionally bump generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generation
|
int | None
|
When given, replaces :attr: |
None
|
Source code in deap_er/private/operators/policy_action_guard.py
allows(action, /, **kwargs)
¶
Return whether action may run under the current caps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
Policy action token. |
required |
**kwargs
|
Any
|
Arguments that would be forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in deap_er/private/operators/policy_action_guard.py
note_applied(action, /, *, evals=0)
¶
Record a successfully applied action.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
Policy action token that ran. |
required |
evals
|
int
|
Evaluations to charge against :attr: |
0
|
Source code in deap_er/private/operators/policy_action_guard.py
PolicyActionResult(applied, rejected, value=None)
dataclass
¶
Outcome of :func:apply_policy_action.
Attributes:
| Name | Type | Description |
|---|---|---|
applied |
bool
|
True when an underlying callable ran. |
rejected |
bool
|
True when the token is unknown, required kwargs were
missing, or a |
value |
Any
|
Return value from the underlying callable when
|
ea_generate_update(toolbox, generations, hof=None, stats=None, verbose=False, logger=None, log_time=False, fronts=None)
¶
Evolve a strategy that generates and updates a population.
Requires generate, update, and evaluate on toolbox.
An empty generate batch stops the loop and returns the last
evaluated population. update is not called with [].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the generate, update, and evaluate operators. |
required |
generations
|
int
|
Number of generations to run. |
required |
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Source code in deap_er/private/algorithms/ea_generate_update.py
ea_generate_update_restarts(toolbox, restart_strategy, hof=None, stats=None, verbose=False, logger=None, log_time=False, log_restarts=True, fronts=None)
¶
Evolve with IPOP or BIPOP CMA restarts until the budget is spent.
Requires generate, update, and evaluate on toolbox.
The toolbox operators should be bound to restart_strategy.generate
and restart_strategy.update. An empty generate batch stops
the loop and returns the last evaluated population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with generate, update, and evaluate operators. |
required |
restart_strategy
|
RestartStrategy
|
Restart wrapper that tracks stagnation and relaunches the inner CMA strategy. |
required |
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
log_restarts
|
bool
|
If True, log |
True
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Source code in deap_er/private/algorithms/ea_generate_update_restarts.py
ea_map_elites(toolbox, archive, descriptor_fn, initial, generations, batch_size, cx_prob, mut_prob, stats=None, verbose=False, logger=None, log_time=False, n_evals=None)
¶
Run MAP-Elites with var_or variation on archive elites.
Generation zero evaluates initial and seeds the archive. Later
generations sample parents from archive, vary them with
var_or, evaluate the offspring, and try to improve cells.
When n_evals is set, the generation that meets or exceeds that
count is the last one recorded. Generations remain the default stop.
Requires clone, mate, mutate, and evaluate on
toolbox. archive stores single-objective fitness only.
Accepts :class:~deap_er.records.GridArchive,
:class:~deap_er.records.CvtArchive, or
:class:~deap_er.records.UnstructuredArchive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evolution operators. |
required |
archive
|
MapElitesArchive
|
MAP-Elites archive updated in place. |
required |
descriptor_fn
|
Callable[[Individual], Sequence[float]]
|
Maps an evaluated individual to a behavior descriptor. |
required |
initial
|
list[Individual]
|
Individuals evaluated and archived before generation one. |
required |
generations
|
int
|
Number of variation generations after the initial seeding generation. |
required |
batch_size
|
int
|
Offspring produced each variation generation. When
|
required |
cx_prob
|
float
|
Probability of crossover in |
required |
mut_prob
|
float
|
Probability of mutation in |
required |
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics compiled from the
offspring each generation. An empty seed or offspring list
skips that compile so reducers such as |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
n_evals
|
int | None
|
Optional evaluation budget. The generation that
meets or exceeds this count is finished, then the loop
stops. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[MapElitesArchive, Logbook]
|
The archive and the logbook. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a variation generation runs while the archive and
|
Source code in deap_er/private/algorithms/ea_map_elites.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 | |
ea_mu_comma_lambda(toolbox, population, generations, offsprings, survivors, cx_prob, mut_prob, hof=None, stats=None, verbose=False, logger=None, log_time=False, fronts=None, n_evals=None)
¶
Evolve a population with mu-comma-lambda selection.
Requires mate, mutate, select, and evaluate on
toolbox. Survivors are selected from the offspring only.
When n_evals is set, the generation that meets or exceeds
that count is the last one recorded. Generations remain the
default stop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evolution operators. |
required |
population
|
list[Individual]
|
Individuals to evolve. Replaced in place. |
required |
generations
|
int
|
Number of generations to run. |
required |
offsprings
|
int
|
Number of offspring to produce each generation. |
required |
survivors
|
int
|
Number of individuals to keep after selection. |
required |
cx_prob
|
float
|
Probability of mating two individuals. |
required |
mut_prob
|
float
|
Probability of mutating an individual. |
required |
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
n_evals
|
int | None
|
Optional evaluation budget. The generation that
meets or exceeds this count is finished, then the loop
stops. |
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deap_er/private/algorithms/ea_mu_comma_lambda.py
24 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 68 69 70 71 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 | |
ea_mu_plus_lambda(toolbox, population, generations, offsprings, survivors, cx_prob, mut_prob, hof=None, stats=None, verbose=False, logger=None, log_time=False, fronts=None, n_evals=None)
¶
Evolve a population with mu-plus-lambda selection.
Requires mate, mutate, select, and evaluate on
toolbox. Survivors are selected from the union of parents
and offspring. When n_evals is set, the generation that
meets or exceeds that count is the last one recorded.
Generations remain the default stop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evolution operators. |
required |
population
|
list[Individual]
|
Individuals to evolve. Replaced in place. |
required |
generations
|
int
|
Number of generations to run. |
required |
offsprings
|
int
|
Number of offspring to produce each generation. |
required |
survivors
|
int
|
Number of individuals to keep after selection. |
required |
cx_prob
|
float
|
Probability of mating two individuals. |
required |
mut_prob
|
float
|
Probability of mutating an individual. |
required |
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
n_evals
|
int | None
|
Optional evaluation budget. The generation that
meets or exceeds this count is finished, then the loop
stops. |
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deap_er/private/algorithms/ea_mu_plus_lambda.py
24 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 68 69 70 71 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 | |
ea_policy(toolbox, population, decide, generations, cx_prob, mut_prob, *, exams=None, cases=None, n_cases=None, guard=None, elite_count=8, action_kwargs=None, observe_kwargs=None, hof=None, stats=None, verbose=False, logger=None, log_time=False, fronts=None, n_evals=None)
¶
Evolve a population with one policy step each generation.
Same survivor rule as ea_simple: evaluate invalids, then each
generation observe → decide → apply_policy_action, select,
vary, and evaluate. Fitness stays on the toolbox. When cases
or a pool of exams is available, selection is lexicase on that
subset; otherwise toolbox.select is used.
Requires mate, mutate, and evaluate (or
evaluate_batch) on toolbox. select is required only
when no case subset is in play. Policy-action evaluations count
toward n_evals and the generation nevals. When a policy
step meets or exceeds that budget, the generation is recorded
without variation so the population is not replaced with
unevaluated offspring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evolution operators. |
required |
population
|
list[Individual]
|
Individuals to evolve. Replaced in place. |
required |
decide
|
Callable[[PolicyObservation], str]
|
Maps one |
required |
generations
|
int
|
Number of generations to run. |
required |
cx_prob
|
float
|
Probability of mating two individuals. |
required |
mut_prob
|
float
|
Probability of mutating an individual. |
required |
exams
|
CaseExamPool | None
|
Optional exam pool. Train / held-out scores become
observations. |
None
|
cases
|
Sequence[int] | None
|
Initial lexicase case indices. Defaults to the first
train exam when |
None
|
n_cases
|
int | None
|
Catalog size for |
None
|
guard
|
PolicyActionGuard | None
|
Optional action guard. |
None
|
elite_count
|
int
|
Elites used to build the observation. |
8
|
action_kwargs
|
dict[str, Any] | None
|
Extra kwargs forwarded to
|
None
|
observe_kwargs
|
dict[str, Any] | None
|
Extra kwargs forwarded to |
None
|
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
n_evals
|
int | None
|
Optional evaluation budget. Policy-action
evaluations count toward the total. When a policy
step meets or exceeds this count, that generation is
recorded without variation and the loop stops.
|
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deap_er/private/algorithms/ea_policy.py
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 68 69 70 71 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 | |
ea_simple(toolbox, population, generations, cx_prob, mut_prob, hof=None, stats=None, verbose=False, logger=None, log_time=False, fronts=None, n_evals=None)
¶
Evolve a population with crossover and mutation on every generation.
Requires mate, mutate, select, and evaluate on
toolbox. Survivors are the offspring of the current generation.
When n_evals is set, the generation that meets or exceeds that
count is the last one recorded. Generations remain the default stop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evolution operators. |
required |
population
|
list[Individual]
|
Individuals to evolve. Replaced in place. |
required |
generations
|
int
|
Number of generations to run. |
required |
cx_prob
|
float
|
Probability of mating two individuals. |
required |
mut_prob
|
float
|
Probability of mutating an individual. |
required |
hof
|
EvoRecords | None
|
Optional HallOfFame or ParetoFront to update. |
None
|
stats
|
EvoStats | None
|
Optional Statistics or MultiStatistics to compile. |
None
|
verbose
|
bool
|
If True, print the logbook stream each generation. |
False
|
logger
|
Logger | None
|
If given with |
None
|
log_time
|
bool
|
If True, record per-generation |
False
|
fronts
|
list[Any] | None
|
Optional list that receives a ParetoFront snapshot of each generation's population. |
None
|
n_evals
|
int | None
|
Optional evaluation budget. The generation that
meets or exceeds this count is finished, then the loop
stops. |
None
|
Returns:
| Type | Description |
|---|---|
EvoAlgoResult
|
The final population and the logbook. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deap_er/private/algorithms/ea_simple.py
evaluate_invalid(toolbox, individuals)
¶
Evaluate the individuals whose fitness is invalid.
This is the helper ea_* drivers and apply_policy_action
already use. When the toolbox has an evaluate_batch operator,
the whole batch of invalid individuals is handed to it in one
call and map is not used. Otherwise each individual goes
through map and evaluate as usual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the evaluate and map operators. |
required |
individuals
|
Sequence[Any]
|
Individuals to scan for invalid fitness. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of individuals that were evaluated. |
Source code in deap_er/private/algorithms/loop.py
apply_policy_action(action, /, **kwargs)
¶
Map a discrete policy action token onto existing toolbox callables.
Push emits action names, not trees. This helper is schema plus thin dispatch only: fitness assignment and rescore ownership stay on the caller. Skip tokens are intentional no-ops. Unknown tokens, missing required kwargs, and guard cap violations are rejected without raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
One of :data: |
required |
**kwargs
|
Any
|
Arguments forwarded to the underlying callable for
the chosen action. See that function's docstring. Optional
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
PolicyActionResult
|
class: |
PolicyActionResult
|
ran, was skipped, or was rejected. |
Source code in deap_er/private/algorithms/policy_action.py
estimate_policy_action_evals(action, /, **kwargs)
¶
Estimate how many evaluations an action would spend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
Policy action token. |
required |
**kwargs
|
Any
|
Arguments that would be forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
A conservative evaluation count used for budget checks. |
Source code in deap_er/private/operators/policy_action_guard.py
guard_policy_action(action, guard, /, **kwargs)
¶
Return whether action is allowed under guard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
str
|
Policy action token. |
required |
guard
|
PolicyActionGuard
|
Guard state and caps. |
required |
**kwargs
|
Any
|
Arguments that would be forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in deap_er/private/operators/policy_action_guard.py
step_islands(demes, migrate=None, *, eval_keys=None)
¶
Run one generation on unlike demes, then optionally migrate.
Each deme is a (toolbox, population) pair. The toolbox must
provide vary, select, and evaluate (or
evaluate_batch). The step is evaluate invalids, vary,
evaluate the offspring, then replace the population with
select(offspring, len(population)). Populations are modified
in place. A MAP-Elites island is a custom vary / select
pair that closes over an archive; this function does not call
ea_map_elites and does not merge archives.
migrate, if given, receives the list of populations after
every deme has stepped. Use mig_ring for a ring; this function
does not pick a topology.
Migrants keep their fitness when eval_keys is omitted or every
key is equal. Distinct keys mean the destination's cases or matrix
differ: immigrant fitness is cleared, including clones created by
a replacement migration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
demes
|
Sequence[tuple[Toolbox, list[Individual]]]
|
|
required |
migrate
|
Callable[[list[list[Individual]]], None] | None
|
Optional callable |
None
|
eval_keys
|
Sequence[Hashable] | None
|
Per-deme identity of the evaluation data. Length
must match |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a toolbox is missing |
Source code in deap_er/private/algorithms/step_islands.py
var_and(toolbox, population, cx_prob, mut_prob)
¶
Clone a population, then apply crossover and mutation independently.
Each of cx_prob and mut_prob must be in [0, 1]. The
result is a new list; fitnesses of varied individuals are cleared.
Requires clone, mate, and mutate on toolbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the variation operators. |
required |
population
|
list[Individual]
|
Individuals to vary. |
required |
cx_prob
|
float
|
Probability of mating each consecutive pair. |
required |
mut_prob
|
float
|
Probability of mutating each individual. |
required |
Returns:
| Type | Description |
|---|---|
list[Individual]
|
A new list of varied individuals. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either probability is outside |
Source code in deap_er/private/algorithms/variation.py
var_or(toolbox, population, offsprings, cx_prob, mut_prob)
¶
Build offspring by applying crossover or mutation or copy.
Each of cx_prob and mut_prob must be in [0, 1], and
their sum must also be in [0, 1]. The remaining probability
copies an unmodified parent. The result is a new list; fitnesses
of varied individuals are cleared. A crossover draw from a
one-individual pool clones that parent twice and mates the clones.
Requires clone, mate, and mutate on toolbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toolbox
|
Toolbox
|
Toolbox with the variation operators. |
required |
population
|
list[Individual]
|
Individuals to sample from. |
required |
offsprings
|
int
|
Number of individuals to produce. |
required |
cx_prob
|
float
|
Probability of producing a child by crossover. |
required |
mut_prob
|
float
|
Probability of producing a child by mutation. |
required |
Returns:
| Type | Description |
|---|---|
list[Individual]
|
A new list of offspring. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either probability is outside |