saealib.PymooMutation#

class saealib.PymooMutation(operator, *, prob=1.0)[source]#

Bases: Mutation

Adapter wrapping a pymoo mutation operator as a saealib Mutation.

Lets researchers who already have a pymoo Mutation (e.g. PM()) reuse it unchanged inside saealib’s GA.

Parameters:
  • operator (pymoo.core.mutation.Mutation) – An already-constructed pymoo mutation operator instance.

  • prob (float, optional) – Individual-level mutation probability. Unlike PymooCrossover, this class applies the gate itself inside mutate_batch(). Defaults to 1.0.

Notes

prob_var (pymoo’s per-variable mutation probability) is deliberately not mirrored from the wrapped operator: pymoo stores it as a pymoo.core.variable.Real or None, and the wrapped operator’s _do() resolves its own per-variable rate internally regardless of what saealib’s prob_var attribute says. self.prob_var stays None here so that any saealib code reading it (e.g. mixed-variable routing in GA) falls back to its own default rather than seeing a foreign, potentially non-float value.

mutate_batch() calls the wrapped operator’s _do() at most once, on the gated subset, and forwards rng via pymoo’s random_state parameter for reproducibility. The inherited mutate() derives a single-row call from that batch implementation. A minimal cached pymoo Problem shim is synthesized the same way.

Methods

__init__

contract

Return the mutation contract; mutate_range is required for every mutation.

mutate

Execute mutation for one candidate.

mutate_batch

Execute mutation on a batch of candidates via a single pymoo call.

post_mutation

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

post_mutation_batch

Run the post-mutation lifecycle hook for a batch.

with_post

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

Method Details

PymooMutation.__init__(operator, *, prob=1.0)[source]#
Parameters:
  • operator (_PymooMutationLike)

  • prob (float)

Return type:

None

PymooMutation.contract()#

Return the mutation contract; mutate_range is required for every mutation.

Return type:

ComponentContract

PymooMutation.mutate(p, mutate_range, rng=Generator(PCG64) at 0x7F7917914740)#

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 default variation_execution="batch": the batch path calls mutate_batch() directly and never calls mutate(). Such an override only takes effect under variation_execution="sequential". To customize behavior under both modes, override mutate_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 float64 because mutate_batch() casts its input internally; the result does not preserve the input’s dtype.

Return type:

np.ndarray

PymooMutation.mutate_batch(candidates_batch, mutate_range, rng=Generator(PCG64) at 0x7F7917916880)[source]#

Execute mutation on a batch of candidates via a single pymoo call.

Unlike Crossover.crossover_batch, this method owns its own prob gate: it draws one gate value per row (rng.random(n) < self.prob, mirroring mutate()’s per-call rng.random() >= self.prob check) and only mutates the gated rows; ungated rows are returned unchanged. The wrapped operator’s _do() is called at most once, on only the gated subset. If no row is gated (gate.sum() == 0), _do() is not called at all, avoiding a zero-length-batch call into the wrapped pymoo operator.

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) – Forwarded to the wrapped operator as random_state.

Returns:

Mutated individuals. shape = (n, dim)

Return type:

np.ndarray

Notes

For more than one gated row, a loop of separate single-row mutate_batch calls (equivalently, separate mutate() calls) does not reproduce the same per-row results as one batched call with the same seeded rng. Pymoo operators such as PM draw each random phase across the whole gated batch, whereas single-row calls interleave phases per row.

PymooMutation.post_mutation(offspring, mutate_range, rng, ctx=None)#

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

PymooMutation.post_mutation_batch(offspring_batch, mutate_range, rng, ctx=None)#

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’s post_mutation() hook. A subclass override of this method is responsible for composing that hook itself. If the override does not call self.post_mutation, a hook installed with with_post() will not run in GA batch mode. It still runs in GA sequential mode, which calls post_mutation() directly.

PymooMutation.with_post(fn)#

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

Parameters:

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

Returns:

Shallow copy with the hook registered.

Return type:

Mutation