stk.RandomMutator

class stk.RandomMutator(mutators, weights=None, random_seed=None)[source]

Bases: object

Use some other mutator at random.

Examples

Use One of Several Mutators at Random

import stk

random_seed = 12
mutator = stk.RandomMutator(
    mutators=(
        stk.RandomBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
        stk.SimilarBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCCCCCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
        stk.RandomBuildingBlock(
            building_blocks=(
                stk.BuildingBlock(
                    smiles='BrCCNCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
                stk.BuildingBlock(
                    smiles='BrCNCBr',
                    functional_groups=[stk.BromoFactory()],
                ),
            ),
            is_replaceable=lambda building_block: True,
            random_seed=random_seed,
        ),
    ),
)
building_block = stk.BuildingBlock(
    smiles='BrCNNCBr',
    functional_groups=[stk.BromoFactory()],
)
record = stk.MoleculeRecord(
    topology_graph=stk.polymer.Linear(
        building_blocks=(building_block, ),
        repeating_unit='A',
        num_repeating_units=2,
    ),
)
# Use one of the component mutators at random.
mutation_record1 = mutator.mutate(record)
# A different mutator may get selected at random the second,
# third, etc, time.
mutation_record2 = mutator.mutate(record)
Parameters:
  • mutators (list[MoleculeMutator[T]]) – A selection of mutators, each time mutate() is called, one will be selected at random to perform the mutation.

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

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

Methods

mutate

Return a mutant of record.

mutate(record)[source]

Return a mutant of record.

Parameters:

record (T) – The molecule to be mutated.

Returns:

A record of the mutation or None if record cannot be mutated.

Return type:

MutationRecord[T] | None