saealib.PymooCrossover#

class saealib.PymooCrossover(operator, *, prob=None, n_parents=None, n_children=None)[ソース]#

ベースクラス: Crossover

Adapter wrapping a pymoo crossover operator as a saealib Crossover.

Lets researchers who already have a pymoo Crossover (e.g. SBX(), PCX()) reuse it unchanged inside saealib's GA.

パラメータ:
  • operator (pymoo.core.crossover.Crossover) -- An already-constructed pymoo crossover operator instance.

  • prob (float, optional) -- Individual-level crossover probability, gated by saealib's GA before crossover_batch()/crossover() is even called (on the gated subset in batch mode, per gated pair in sequential mode). Defaults to the wrapped operator's own prob.value (pymoo wraps it as a pymoo.core.variable.Real).

  • n_parents (int, optional) -- Defaults to operator.n_parents.

  • n_children (int, optional) -- Defaults to operator.n_offsprings.

メモ

crossover_batch() calls the wrapped operator's _do() (not .do(), which would apply pymoo's own prob gate a second time on top of saealib's). The inherited crossover() derives a single-parent- group call from that batch implementation.

A minimal internal pymoo.core.problem.Problem is synthesized to satisfy operators (e.g. SBX) that read problem.xl/problem.xu/problem.n_var; it is cached and rebuilt only when dim or bounds changes. Most pymoo crossovers unconditionally require finite xl/xu and raise their own (pymoo-internal) error when bounds=None is passed through; saealib's own GA always supplies bounds, so this only matters for direct calls.

rng is forwarded to the wrapped operator via pymoo's own random_state parameter, so results are reproducible under saealib's seeding.

Methods

__init__

contract

Return the wrapped crossover contract.

crossover

Execute crossover for one parent group.

crossover_batch

Execute crossover on a batch of parent pairs via a single pymoo call.

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

PymooCrossover.__init__(operator, *, prob=None, n_parents=None, n_children=None)[ソース]#
パラメータ:
  • operator (_PymooCrossoverLike)

  • prob (float | None)

  • n_parents (int | None)

  • n_children (int | None)

戻り値の型:

None

PymooCrossover.contract()[ソース]#

Return the wrapped crossover contract.

戻り値の型:

ComponentContract

PymooCrossover.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

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

Execute crossover on a batch of parent pairs via a single pymoo call.

This is the adapter's primary crossover implementation and calls the wrapped operator's _do() exactly once for the whole batch.

パラメータ:
  • parents_batch (np.ndarray) -- Batch of parent groups. shape = (n_pair, n_parents, dim)

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

  • rng (np.random.Generator, optional) -- Forwarded to the wrapped operator as random_state.

戻り値:

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

戻り値の型:

np.ndarray

メモ

For n_pair > 1, a loop of separate single-pair crossover_batch calls (equivalently, separate crossover() calls) does not reproduce the same per-row results as one batched call with the same seeded rng. Operators such as pymoo's SBX draw each random phase across the whole batch, whereas single-pair calls interleave phases per row.

PymooCrossover.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

PymooCrossover.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.

PymooCrossover.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