saealib.Crossover#
- class saealib.Crossover[source]#
Bases:
ABCBase class for crossover operators.
- n_parents#
Number of parents required per crossover call. Default is 2. Subclasses may override this class attribute if they require a different number of parents.
- Type:
int
- n_children#
Number of offspring produced per crossover call. Default is 2. Subclasses may override this class attribute if they produce a different number of offspring.
- Type:
int
- prob#
Individual-level crossover probability.
- Type:
float
Methods
Return the crossover contract. |
|
Execute crossover for one parent group. |
|
Execute crossover on a batch of parent pairs at once. |
|
Post-crossover lifecycle hook; override to inject custom processing. |
|
Run the post-crossover lifecycle hook for a batch. |
|
Return a copy of this operator with |
Method Details
- Crossover.__init__()#
- Crossover.crossover(parent, bounds=None, rng=Generator(PCG64) at 0x7F79179411C0)[source]#
Execute crossover for one parent group.
This method is derived from
crossover_batch()by passing a single-pair batch.Notes
Overriding only this method (not
crossover_batch()) has no effect under GA’s defaultvariation_execution="batch": the batch path callscrossover_batch()directly and never callscrossover(). Such an override only takes effect undervariation_execution="sequential". To customize behavior under both modes, overridecrossover_batch().- Parameters:
parent (np.ndarray) – Parent individuals. shape = (n_parents, dim)
bounds (tuple of (np.ndarray, np.ndarray) or None) – Lower and upper bounds for each variable.
Nonemeans unbounded.rng (np.random.Generator, optional) – Random number generator, by default np.random.default_rng()
- Returns:
Offspring individuals. shape = (n_offspring, dim)
- Return type:
np.ndarray
- abstractmethod Crossover.crossover_batch(parents_batch, bounds=None, rng=Generator(PCG64) at 0x7F79179417E0)[source]#
Execute crossover on a batch of parent pairs at once.
This is the required primitive that every concrete crossover class must implement directly.
- Parameters:
parents_batch (np.ndarray) – Batch of parent groups. shape = (n_pair, n_parents, dim), i.e. a batch of what
crossover()normally receives one instance of at a time (parents_batch[k]is what a singlecrossover(parent=parents_batch[k], ...)call would take asparent).bounds (tuple of (np.ndarray, np.ndarray) or None) – Lower and upper bounds for each variable.
Nonemeans unbounded.rng (np.random.Generator, optional) – Random number generator, by default np.random.default_rng()
- Returns:
Offspring individuals. shape = (n_pair, n_children, dim)
- Return type:
np.ndarray
Notes
No
probgating is performed insidecrossover_batch: the caller is responsible for having already filteredparents_batchdown to only the pairs that should undergo crossover.
- Crossover.post_crossover(offspring, parents, rng, ctx=None)[source]#
Post-crossover lifecycle hook; override to inject custom processing.
- Parameters:
offspring (np.ndarray) – Offspring produced by crossover. shape = (n_children, dim)
parents (np.ndarray) – Parent individuals. shape = (n_parents, dim)
rng (np.random.Generator) – Random number generator.
ctx (ExecutionContext or None, optional) – Current optimization context.
- Returns:
Processed offspring. shape = (n_children, dim)
- Return type:
np.ndarray
- Crossover.post_crossover_batch(offspring_batch, parents_batch, rng, ctx=None)[source]#
Run the post-crossover lifecycle hook for a batch.
The default implementation calls
post_crossover()once per parent group, in order. Override this method to provide genuinely vectorized post-processing.- Parameters:
offspring_batch (np.ndarray) – Offspring produced by crossover. shape = (n_pair, n_children, dim)
parents_batch (np.ndarray) – Batch of parent groups. shape = (n_pair, n_parents, dim)
rng (np.random.Generator) – Random number generator.
ctx (ExecutionContext or None, optional) – Current optimization context.
- Returns:
Processed offspring. shape = (n_pair, n_children, dim)
- Return type:
np.ndarray
Notes
with_post()reassigns only the instance’spost_crossover()hook. A subclass override of this method is responsible for composing that hook itself. If the override does not callself.post_crossover, a hook installed withwith_post()will not run in GA batch mode. It still runs in GA sequential mode, which callspost_crossover()directly.