Random Crosser

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

Bases: object

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

Methods

cross(records)

Cross records.

__init__(crossers, weights=None, random_seed=None)[source]

Initialize a RandomCrosser instance.

Parameters
  • crossers (tuple) – Holds instances which have cross() method. The cross() method must return an instance of CrossoverRecord.

  • weights (tuple of float, optional) – 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, optional) – The random seed to use.

cross(records)[source]

Cross records.

Parameters

records (iterable of MoleculeRecord) – The molecule records on which a crossover operation is performed.

Yields

CrossoverRecord – A record of a crossover operation.