saealib.CrossoverSBX#

class saealib.CrossoverSBX(prob, eta, *, prob_var=0.5)[source]#

Bases: Crossover

Simulated Binary Crossover (SBX) operator.

When bounds are provided and finite, the bounded variant is used, which constrains offspring to [lb, ub]. When bounds is None or contains infinite values, the unbounded variant is used.

Parameters:
  • prob (float)

  • eta (float)

  • prob_var (float)

prob#

Individual-level crossover probability.

Type:

float

eta#

Distribution index. Larger values produce offspring closer to parents.

Type:

float

prob_var#

Per-variable crossover probability. Default is 0.5.

Type:

float

References

[11]: Deb, K., & Agrawal, R. B. (1995). Simulated binary crossover for continuous search space. Complex Systems, 9(2), 115-148.

Methods

__init__

Initialize SBX crossover operator.

contract

Return the bounded real-valued crossover contract.

crossover

Execute crossover for one parent group.

crossover_batch

Execute SBX 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

CrossoverSBX.__init__(prob, eta, *, prob_var=0.5)[source]#

Initialize SBX crossover operator.

Parameters:
  • prob (float) – Individual-level crossover probability.

  • eta (float) – Distribution index.

  • prob_var (float, optional) – Per-variable crossover probability, by default 0.5.

CrossoverSBX.contract()[source]#

Return the bounded real-valued crossover contract.

Return type:

ComponentContract

CrossoverSBX.crossover(parent, bounds=None, rng=Generator(PCG64) at 0x7F79179411C0)#

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 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().

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. None means 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

CrossoverSBX.crossover_batch(parents_batch, bounds=None, rng=Generator(PCG64) at 0x7F7917943AE0)[source]#

Execute SBX crossover on a batch of parent pairs at once.

This is the primary SBX implementation, vectorized over the leading n_pair axis.

Parameters:
  • parents_batch (np.ndarray) – Batch of parent pairs. shape = (n_pair, 2, dim)

  • bounds (tuple of (np.ndarray, np.ndarray) or None) – Lower and upper bounds. When provided and finite, the bounded SBX variant is applied.

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

Returns:

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

Return type:

np.ndarray

Notes

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. Drawing each random phase for the whole batch has a different draw order from interleaving those phases one pair at a time.

CrossoverSBX.post_crossover(offspring, parents, rng, ctx=None)#

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

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

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

CrossoverSBX.with_post(fn)#

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

Parameters:

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

Returns:

Shallow copy with the hook registered.

Return type:

Crossover