stk.RandomCrosser

class stk.RandomCrosser(crossers, weights=None, random_seed=None)[source]

Bases: Generic[T]

Use some other crosser at random.

Examples

Use One of Several Crossers at Random

import stk

def get_functional_group_type(building_block):
    fg, = building_block.get_functional_groups(0)
    return type(fg)

def get_num_functional_groups(building_block):
   return building_block.get_num_functional_groups()

crosser = stk.RandomCrosser(
    crossers=(
        stk.GeneticRecombination(get_functional_group_type),
        stk.GeneticRecombination(get_num_functional_groups),
    ),
    random_seed=12,
)

record1 = stk.MoleculeRecord(
    topology_graph=stk.cage.FourPlusSix(
        building_blocks=(
            stk.BuildingBlock(
                smiles='O=CC(C=O)CC=O',
                functional_groups=[stk.AldehydeFactory()],
            ),
            stk.BuildingBlock(
                smiles='NCCN',
                functional_groups=[stk.PrimaryAminoFactory()],
            ),
        ),
    ),
)
record2 = stk.MoleculeRecord(
    topology_graph=stk.cage.FourPlusSix(
        building_blocks=(
            stk.BuildingBlock(
                smiles='O=CNC(C=O)CC=O',
                functional_groups=[stk.AldehydeFactory()],
            ),
            stk.BuildingBlock(
                smiles='NCNCN',
                functional_groups=[stk.PrimaryAminoFactory()],
            ),
        ),
    ),
)

# Use one of the component crossers at random.
crossover_records1 = tuple(crosser.cross((record1, record2)))
# A different crosser may get selected at random the second,
# third, etc, time.
crossover_records2 = tuple(crosser.cross((record1, record2)))
Parameters:
  • crossers (list[MoleculeCrosser[T]]) – A selection of crossers, each time cross() is called, one will be selected at random to perform crossover.

  • weights (list[float] | None) – For each crosser, the probability that it will be chosen whenever cross() is called. If None all crossers will have equal chance of being selected.

  • random_seed (int | Generator | None) – The random seed to use.

Methods

cross

Cross records.

cross(records)[source]

Cross records.

Parameters:
  • molecules (list[T]) – The molecules on which a crossover operation is performed.

  • records (Sequence[T]) –

Yields:

A record of a crossover operation.

Return type:

Iterator[CrossoverRecord[T]]