saealib.CrossoverTwoPoint#

class saealib.CrossoverTwoPoint(prob)[source]#

Bases: Crossover

Two-point crossover operator.

Parameters:

prob (float)

prob#

Individual-level crossover probability.

Type:

float

Methods

__init__

Initialize two-point crossover operator.

contract

Return the crossover contract.

crossover

Execute crossover for one parent group.

crossover_batch

Execute two-point 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

CrossoverTwoPoint.__init__(prob)[source]#

Initialize two-point crossover operator.

Parameters:

prob (float) – Individual-level crossover probability.

CrossoverTwoPoint.contract()#

Return the crossover contract.

Return type:

ComponentContract

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

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

Execute two-point crossover on a batch of parent pairs at once.

Unlike every other crossover_batch in this module, the cut-point sampling itself is not vectorized here: rng.choice(dim + 1, size=2, replace=False) draws two values without replacement from the same row’s range(dim + 1) population. Calling rng.choice with a multi-row size and replace=False would instead draw that many DISTINCT values across the WHOLE output (not independently per row), which is a different – and wrong – sampling semantics, and there is no algebraic substitute that reproduces replace=False choice exactly. So the two cut points are still drawn with a small per-row Python loop, preserving the single-pair rng.choice semantics and RNG consumption order row by row. The vectorization benefit instead comes from reconstructing the offspring (the dim-sized part) for the whole batch at once via a broadcast mask.

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

  • bounds (tuple of (np.ndarray, np.ndarray) or None) – Ignored.

  • 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

Because the cut-point loop consumes rng in the same per-row order as separate single-pair crossover_batch calls (equivalently, separate crossover() calls), every row reproduces the corresponding result from that loop with an identically seeded rng.

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

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

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