saealib.CrossoverTwoPoint#
- class saealib.CrossoverTwoPoint(prob)[ソース]#
ベースクラス:
CrossoverTwo-point crossover operator.
- パラメータ:
prob (float)
- prob#
Individual-level crossover probability.
- Type:
float
Methods
Initialize two-point crossover operator. |
|
Return the crossover contract. |
|
Execute crossover for one parent group. |
|
Execute two-point crossover on a batch of parent pairs at once. |
|
Post-crossover lifecycle hook; override to inject custom processing. |
|
Run the post-crossover lifecycle hook for a batch. |
|
Return a copy of this operator with |
Method Details
- CrossoverTwoPoint.__init__(prob)[ソース]#
Initialize two-point crossover operator.
- パラメータ:
prob (float) -- Individual-level crossover probability.
- CrossoverTwoPoint.contract()#
Return the crossover contract.
- 戻り値の型:
- CrossoverTwoPoint.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 defaultvariation_execution="batch": the batch path callscrossover_batch()directly and never callscrossover(). Such an override only takes effect undervariation_execution="sequential". To customize behavior under both modes, overridecrossover_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.
Nonemeans unbounded.rng (np.random.Generator, optional) -- Random number generator, by default np.random.default_rng()
- 戻り値:
Offspring individuals. shape = (n_offspring, dim)
- 戻り値の型:
np.ndarray
- CrossoverTwoPoint.crossover_batch(parents_batch, bounds=None, rng=Generator(PCG64) at 0x7FE0F47A9D20)[ソース]#
Execute two-point crossover on a batch of parent pairs at once.
Unlike every other
crossover_batchin 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'srange(dim + 1)population. Callingrng.choicewith a multi-rowsizeandreplace=Falsewould 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 reproducesreplace=Falsechoice exactly. So the two cut points are still drawn with a small per-row Python loop, preserving the single-pairrng.choicesemantics and RNG consumption order row by row. The vectorization benefit instead comes from reconstructing the offspring (thedim-sized part) for the whole batch at once via a broadcast mask.- パラメータ:
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()
- 戻り値:
Offspring individuals. shape = (n_pair, 2, dim)
- 戻り値の型:
np.ndarray
メモ
Because the cut-point loop consumes
rngin the same per-row order as separate single-paircrossover_batchcalls (equivalently, separatecrossover()calls), every row reproduces the corresponding result from that loop with an identically seededrng.
- CrossoverTwoPoint.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
- 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.- パラメータ:
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'spost_crossover()hook. A subclass override of this method is responsible for composing that hook itself. If the override does not callself.post_crossover, a hook installed withwith_post()will not run in GA batch mode. It still runs in GA sequential mode, which callspost_crossover()directly.