- class ReplaceFitness(get_replacement, filter=<function ReplaceFitness.<lambda>>)
Replaces fitness values of a certain value with a new value.
Giving a Fitness Value to Failed Calculations
You want to replace fitness values which are
Nonewith half the minimum fitness value in the population. A fitness value may be
Nonebecause the fitness calculation failed for some reason.
import stk building_block = stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ) population = ( stk.MoleculeRecord( topology_graph=stk.polymer.Linear( building_blocks=(building_block, ), repeating_unit='A', num_repeating_units=2, ), ).with_fitness_value( fitness_value=2, normalized=False, ), # This will have a fitness value of None. stk.MoleculeRecord( topology_graph=stk.polymer.Linear( building_blocks=(building_block, ), repeating_unit='A', num_repeating_units=2, ), ), ) def get_minimum_fitness_value(population): return min( record.get_fitness_value() for record in population if record.get_fitness_value() is not None ) replacer = stk.ReplaceFitness( # The replacement is half the minimum fitness value in the # population. get_replacement=lambda population: get_minimum_fitness_value(population) / 2, # Only replace fitness values which are None. filter=lambda population, record: record.get_fitness_value() is None, ) # Use the replacer. normalized_population = tuple(replacer.normalize(population)) normalized_record1, normalized_record2 = normalized_population assert normalized_record1.get_fitness_value() == 2 assert normalized_record2.get_fitness_value() == 1
Normalize the fitness values in population.
- __init__(get_replacement, filter=<function ReplaceFitness.<lambda>>)
callable) – Takes a single parameter, a
MoleculeRecordinstances, which needs to be normalized. The entire population passed to
normalize()is passed to this parameter, regardless of what is passed to the filter parameter. The
callablereturns the value which is to be given to the normalized records.
callable, optional) – Takes two parameters, first is a
MoleculeRecordinstances, and the second is a
False. Only molecules which return
Truewill have fitness values normalized. By default, all molecules will have fitness values normalized. The instance passed to the population argument of
normalize()is passed as the first argument, while the second argument will be passed every
MoleculeRecordin it, one at a time.