saealib.Mutation#
- class saealib.Mutation[source]#
Bases:
ABCBase class for mutation operators.
- prob#
Individual-level mutation probability. Rows whose gate draw is greater than or equal to
probare returned unchanged.- Type:
float
- prob_var#
Per-variable mutation probability.
Nonemeans the effective value is resolved at call time asmin(0.5, 1 / dim).- Type:
float or None
Methods
Return the mutation contract; mutate_range is required for every mutation. |
|
Execute mutation for one candidate. |
|
Execute mutation on a batch of candidates at once. |
|
Post-mutation lifecycle hook; override to inject custom processing. |
|
Run the post-mutation lifecycle hook for a batch. |
|
Return a copy of this operator with |
Method Details
- Mutation.__init__()#
- Mutation.contract()[source]#
Return the mutation contract; mutate_range is required for every mutation.
- Return type:
- Mutation.mutate(p, mutate_range, rng=Generator(PCG64) at 0x7F7917914740)[source]#
Execute mutation for one candidate.
This method is derived from
mutate_batch()by passing a single-row batch.Notes
Overriding only this method (not
mutate_batch()) has no effect under GA’s defaultvariation_execution="batch": the batch path callsmutate_batch()directly and never callsmutate(). Such an override only takes effect undervariation_execution="sequential". To customize behavior under both modes, overridemutate_batch().- Parameters:
p (np.ndarray) – Parent individual. shape = (dim,)
mutate_range (tuple) – Tuple of (lower_bound, upper_bound) for mutation.
rng (np.random.Generator, optional) – Random number generator, by default np.random.default_rng()
- Returns:
Mutated individual. shape = (dim,). The result is always
float64becausemutate_batch()casts its input internally; the result does not preserve the input’s dtype.- Return type:
np.ndarray
- abstractmethod Mutation.mutate_batch(candidates_batch, mutate_range, rng=Generator(PCG64) at 0x7F7917914AC0)[source]#
Execute mutation on a batch of candidates at once.
This is the required primitive that every concrete mutation class must implement directly. Each implementation self-gates
probinternally; see Notes for why this differs fromCrossover.crossover_batch.- Parameters:
candidates_batch (np.ndarray) – Batch of candidate individuals. shape = (n, dim)
mutate_range (tuple) – Tuple of (lower_bound, upper_bound) for mutation.
rng (np.random.Generator, optional) – Random number generator, by default np.random.default_rng()
- Returns:
Mutated individuals. shape = (n, dim)
- Return type:
np.ndarray
Notes
Unlike
Crossover.crossover_batch,probgating is NOT the caller’s responsibility here — it must be applied by the overriding implementation itself, per row. Implementations must draw one gate value per row and leave ungated rows unchanged, rather than expecting the caller to pre-filtercandidates_batch. This is a deliberate asymmetry withCrossover.crossover_batch.
- Mutation.post_mutation(offspring, mutate_range, rng, ctx=None)[source]#
Post-mutation lifecycle hook; override to inject custom processing.
- Parameters:
offspring (np.ndarray) – Individual after mutation. shape = (dim,)
mutate_range (tuple) – Tuple of (lower_bound, upper_bound) used for mutation.
rng (np.random.Generator) – Random number generator.
ctx (ExecutionContext or None, optional) – Current optimization context.
- Returns:
Processed individual. shape = (dim,)
- Return type:
np.ndarray
- Mutation.post_mutation_batch(offspring_batch, mutate_range, rng, ctx=None)[source]#
Run the post-mutation lifecycle hook for a batch.
The default implementation calls
post_mutation()once per individual, in order. Override this method to provide genuinely vectorized post-processing.- Parameters:
offspring_batch (np.ndarray) – Offspring produced by mutation. shape = (n, dim)
mutate_range (tuple) – Tuple of (lower_bound, upper_bound) used for mutation.
rng (np.random.Generator) – Random number generator.
ctx (ExecutionContext or None, optional) – Current optimization context.
- Returns:
Processed offspring. shape = (n, dim)
- Return type:
np.ndarray
Notes
with_post()reassigns only the instance’spost_mutation()hook. A subclass override of this method is responsible for composing that hook itself. If the override does not callself.post_mutation, a hook installed withwith_post()will not run in GA batch mode. It still runs in GA sequential mode, which callspost_mutation()directly.