saealib.Crossover#

class saealib.Crossover[ソース]#

ベースクラス: ABC

Base 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

__init__

contract

Return the crossover contract.

crossover

Execute crossover for one parent group.

crossover_batch

Execute crossover on a batch of parent pairs at once.

post_crossover

Post-crossover lifecycle hook; override to inject custom processing.

post_crossover_batch

Run the post-crossover lifecycle hook for a batch.

with_post

Return a copy of this operator with fn appended to the hook.

Method Details

Crossover.__init__()#
Crossover.contract()[ソース]#

Return the crossover contract.

戻り値の型:

ComponentContract

Crossover.crossover(parent, bounds=None, rng=Generator(PCG64) at 0x7FE0F4773140)[ソース]#

Execute crossover for one parent group.

This method is derived from crossover_batch() by passing a single-pair batch.

メモ

Overriding only this method (not crossover_batch()) has no effect under GA's default variation_execution="batch": the batch path calls crossover_batch() directly and never calls crossover(). Such an override only takes effect under variation_execution="sequential". To customize behavior under both modes, override crossover_batch().

パラメータ:
  • 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. None means unbounded.

  • rng (np.random.Generator, optional) -- Random number generator, by default np.random.default_rng()

戻り値:

Offspring individuals. shape = (n_offspring, dim)

戻り値の型:

np.ndarray

abstractmethod Crossover.crossover_batch(parents_batch, bounds=None, rng=Generator(PCG64) at 0x7FE0F4773760)[ソース]#

Execute crossover on a batch of parent pairs at once.

This is the required primitive that every concrete crossover class must implement directly.

パラメータ:
  • 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 single crossover(parent=parents_batch[k], ...) call would take as parent).

  • bounds (tuple of (np.ndarray, np.ndarray) or None) -- Lower and upper bounds for each variable. None means unbounded.

  • rng (np.random.Generator, optional) -- Random number generator, by default np.random.default_rng()

戻り値:

Offspring individuals. shape = (n_pair, n_children, dim)

戻り値の型:

np.ndarray

メモ

No prob gating is performed inside crossover_batch: the caller is responsible for having already filtered parents_batch down to only the pairs that should undergo crossover.

Crossover.post_crossover(offspring, parents, rng, ctx=None)[ソース]#

Post-crossover lifecycle hook; override to inject custom processing.

パラメータ:
  • 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.

戻り値:

Processed offspring. shape = (n_children, dim)

戻り値の型:

np.ndarray

Crossover.post_crossover_batch(offspring_batch, parents_batch, rng, ctx=None)[ソース]#

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.

パラメータ:
  • 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.

戻り値:

Processed offspring. shape = (n_pair, n_children, dim)

戻り値の型:

np.ndarray

メモ

with_post() reassigns only the instance's post_crossover() hook. A subclass override of this method is responsible for composing that hook itself. If the override does not call self.post_crossover, a hook installed with with_post() will not run in GA batch mode. It still runs in GA sequential mode, which calls post_crossover() directly.

Crossover.with_post(fn)[ソース]#

Return a copy of this operator with fn appended to the hook.

パラメータ:

fn (callable) -- fn(offspring, parents, rng, ctx) -> np.ndarray

戻り値:

Shallow copy with the hook registered.

戻り値の型:

Crossover