stk.FitnessFunction

class stk.FitnessFunction(fitness_function, input_database=None, output_database=None)[source]

Bases: FitnessCalculator[T]

Takes a function and uses it as a fitness calculator.

Examples

Calculating Fitness Values

import stk

# First create a function to use as the fitness function. For
# example, assume that the fitness value is given by the number
# of atoms.
def get_num_atoms(record):
    return record.get_molecule().get_num_atoms()

# Then use it to create a fitness calculator.
fitness_calculator = stk.FitnessFunction(
    fitness_function=get_num_atoms,
)

# Use the fitness calculator to get fitness values.
record = stk.MoleculeRecord(
    topology_graph=stk.polymer.Linear(
        building_blocks=(stk.BuildingBlock('BrCCBr'), ),
        repeating_unit='A',
        num_repeating_units=1,
    ),
)
value1 = fitness_calculator.get_fitness_value(record)

Storing Fitness Values in a Database

Sometimes you want to store fitness values in a database, you can do this by providing the output_database parameter.

import stk
import pymongo

# Create a database which stores the fitness value of each
# molecule.
fitness_db = stk.ValueMongoDb(
    # This connects to a local database - so make sure you have
    # local MongoDB server running. You can also connect to
    # a remote MongoDB with MongoClient(), read to pymongo
    # docs to see how to do that.
    mongo_client=pymongo.MongoClient(),
    collection='fitness_values',
)

# Define a fitness function.
def get_num_atoms(record):
    return record.get_molecule().get_num_atoms()

# Create a fitness calculator.
fitness_calculator = stk.FitnessFunction(
    fitness_function=get_num_atoms,
    output_database=fitness_db,
)

# Calculate fitness values.
record = stk.MoleculeRecord(
    topology_graph=stk.polymer.Linear(
        building_blocks=(stk.BuildingBlock('BrCCBr'), ),
        repeating_unit='A',
        num_repeating_units=1,
    ),
)
value = fitness_calculator.get_fitness_value(record)

# You can retrieve the fitness values from the database.
value1 = fitness_db.get(stk.BuildingBlock('BrCCBr'))

Caching Fitness Values

Usually, if you calculate the fitness value of a molecule, you do not want to re-calculate it, because this may be expensive, and the fitness value is going to be the same anyway. By using the input_database parameter, together with the output_database parameter, you can make sure you store and retrieve calculated fitness values instead of repeating the same calculation multiple times.

The input_database is checked before a calculation happens, to see if the value already exists, while the output_database has the calculated fitness value deposited into it.

import stk
import pymongo

# You can use the same database for both the input_database
# and output_database parameters.
fitness_db = stk.ValueMongoDb(
    # This connects to a local database - so make sure you have
    # local MongoDB server running. You can also connect to
    # a remote MongoDB with MongoClient(), read to pymongo
    # docs to see how to do that.
    mongo_client=pymongo.MongoClient(),
    collection='fitness_values',
)

# Define a fitness function.
def get_num_atoms(record):
    return record.get_molecule().get_num_atoms()

# Create a fitness calculator.
fitness_calculator = stk.FitnessFunction(
    fitness_function=get_num_atoms,
    input_database=fitness_db,
    output_database=fitness_db,
)

# Assuming that a fitness value for this molecule was not
# deposited into the database in a previous session, this
# will calculate the fitness value.
record = stk.MoleculeRecord(
    topology_graph=stk.polymer.Linear(
        building_blocks=(stk.BuildingBlock('BrCCBr'), ),
        repeating_unit='A',
        num_repeating_units=1,
    ),
)
value1 = fitness_calculator.get_fitness_value(record)
# This will not re-calculate the fitness value, instead,
# value1 will be retrieved from the database.
value2 = fitness_calculator.get_fitness_value(record)
Parameters:
  • fitness_function (Callable[[T], Any]) – Takes a single parameter, the molecule whose fitness needs to be calculated, and returns its fitness value.

  • input_database (ValueDatabase | None) – A database to check before calling fitness_function. If a fitness value exists for a molecule in the database, the stored value is returned, instead of calling fitness_function.

  • output_database (ValueDatabase | None) – A database into which the calculate fitness value is placed.

Methods

get_fitness_value

Return the fitness value of a molecule.

get_fitness_value(record)[source]

Return the fitness value of a molecule.

Parameters:

record (T) – The molecule whose fitness value should be calculated.

Returns:

The fitness value.

Return type:

Any