stk.ProgressPlotter

class stk.ProgressPlotter(property, y_label)[source]

Bases: object

Plots how a property changes during an EA run.

The produced plot will show the EA generations on the x axis and the min, mean and max values of an attribute on the y axis.

Examples

Plotting How Fitness Values Change Across Generations

import stk

# Initialize an EA somehow.
ea = stk.EvolutionaryAlgorithm(
    initial_population=[
        stk.MoleculeRecord(
            topology_graph=stk.polymer.Linear(
                building_blocks=[
                    stk.BuildingBlock(
                        smiles='BrCCBr',
                        functional_groups=stk.BromoFactory(),
                    ),
                ],
                repeating_unit='A',
                num_repeating_units=i,
            ),
        )
        for i in range(2, 22)
    ],
    fitness_calculator=stk.FitnessFunction(
        fitness_function=lambda record:
            record.get_molecule().get_num_atoms(),
    ),
    mutator=stk.RandomBuildingBlock(
        building_blocks=[
            stk.BuildingBlock('BrC[Si]CCBr', stk.BromoFactory()),
            stk.BuildingBlock('BrCCCCCCCBr', stk.BromoFactory()),
        ],
        is_replaceable=lambda building_block: True
    ),
    crosser=stk.GeneticRecombination(
        get_gene=lambda building_block: 0
    ),
    generation_selector=stk.Best(
        num_batches=22,
        duplicate_molecules=False,
    ),
    mutation_selector=stk.Roulette(
        num_batches=5,
        random_seed=10,
    ),
    crossover_selector=stk.Roulette(
        num_batches=5,
        batch_size=2,
        random_seed=10,
    ),
    num_processes=1,
)

fitness_values = []
for generation in ea.get_generations(10):
    fitness_values.append(
        [
            fitness_value.raw
            for fitness_value
            in generation.get_fitness_values().values()
        ]
    )

# Make the plotter which plots the fitness change across
# generations.
progress = stk.ProgressPlotter(
    property=fitness_values,
    y_label='Fitness'
)
progress.write('fitness_plot.png')

Plotting How a Molecular Property Changes Across Generations

As an example, plotting how the number of atoms changes across generations

import stk

# Initialize an EA somehow.
ea = stk.EvolutionaryAlgorithm(
    initial_population=[
        stk.MoleculeRecord(
            topology_graph=stk.polymer.Linear(
                building_blocks=[
                    stk.BuildingBlock(
                        smiles='BrCCBr',
                        functional_groups=stk.BromoFactory(),
                    ),
                ],
                repeating_unit='A',
                num_repeating_units=i,
            ),
        )
        for i in range(2, 22)
    ],
    fitness_calculator=stk.FitnessFunction(
        fitness_function=lambda record:
            record.get_molecule().get_num_atoms(),
    ),
    mutator=stk.RandomBuildingBlock(
        building_blocks=[
            stk.BuildingBlock('BrC[Si]CCBr', stk.BromoFactory()),
            stk.BuildingBlock('BrCCCCCCCBr', stk.BromoFactory()),
        ],
        is_replaceable=lambda building_block: True
    ),
    crosser=stk.GeneticRecombination(
        get_gene=lambda building_block: 0
    ),
    generation_selector=stk.Best(
        num_batches=22,
        duplicate_molecules=False,
    ),
    mutation_selector=stk.Roulette(
        num_batches=5,
        random_seed=10,
    ),
    crossover_selector=stk.Roulette(
        num_batches=5,
        batch_size=2,
        random_seed=10,
    ),
    num_processes=1,
)

num_atoms = []
for generation in ea.get_generations(10):
    num_atoms.append(
        [
            record.get_molecule().get_num_atoms()
            for record in generation.get_molecule_records()
        ]
    )

# Make the plotter which plots the number of atoms across
# generations.
progress = stk.ProgressPlotter(
    property=num_atoms,
    y_label='Number of Atoms'
)
progress.write('number_of_atoms_plot.png')

Excluding Molecules From the Plot

Sometimes, you want to ignore some molecules from the plot you make. For example, If the fitness calculation failed on a molecule, you not want to include in a plot of fitness.

import stk

# Initialize an EA somehow.
ea = stk.EvolutionaryAlgorithm(
    initial_population=[
        stk.MoleculeRecord(
            topology_graph=stk.polymer.Linear(
                building_blocks=[
                    stk.BuildingBlock(
                        smiles='BrCCBr',
                        functional_groups=stk.BromoFactory(),
                    ),
                ],
                repeating_unit='A',
                num_repeating_units=i,
            ),
        )
        for i in range(2, 22)
    ],
    fitness_calculator=stk.FitnessFunction(
        fitness_function=lambda record:
            record.get_molecule().get_num_atoms(),
    ),
    mutator=stk.RandomBuildingBlock(
        building_blocks=[
            stk.BuildingBlock('BrC[Si]CCBr', stk.BromoFactory()),
            stk.BuildingBlock('BrCCCCCCCBr', stk.BromoFactory()),
        ],
        is_replaceable=lambda building_block: True
    ),
    crosser=stk.GeneticRecombination(
        get_gene=lambda building_block: 0
    ),
    generation_selector=stk.Best(
        num_batches=22,
        duplicate_molecules=False,
    ),
    mutation_selector=stk.Roulette(
        num_batches=5,
        random_seed=10,
    ),
    crossover_selector=stk.Roulette(
        num_batches=5,
        batch_size=2,
        random_seed=10,
    ),
    num_processes=1,
)

fitness_values = []
for generation in ea.get_generations(10):
    fitness_values.append(
        [
            fitness_value.normalized
            for fitness_value
            in generation.get_fitness_values().values()
            if fitness_value.raw is not None
        ]
    )

# Make the plotter which plots the fitness change across
# generations.
progress = stk.ProgressPlotter(
    property=fitness_values,
    y_label='Fitness',
)
progress.write('fitness_plot.png')
Parameters:
  • property (list[list[float]]) – The generations of the EA, which are plotted.

  • y_label (str) – The y label for the produced graph.

Methods

get_plot_data

Get the plot data.

write

Write a progress plot to a file.

get_plot_data()[source]

Get the plot data.

Returns:

A data frame holding the plot data.

Return type:

DataFrame

write(path, dpi=500)[source]

Write a progress plot to a file.

Parameters:
  • path (Path | str) – The path into which the plot is written.

  • dpi (int) – The dpi of the image.

Returns:

The plotter is returned.

Return type:

ProgressPlotter