stk.ReplaceFitness
- class stk.ReplaceFitness(get_replacement, filter=<function ReplaceFitness.<lambda>>)[source]
Bases:
FitnessNormalizer
[T
]Replaces fitness values of a certain value with a new value.
Examples
Giving a Fitness Value to Failed Calculations
You want to replace fitness values which are
None
with half the minimum fitness value in the population. A fitness value may beNone
because the fitness calculation failed for some reason.import stk building_block = stk.BuildingBlock('BrCCBr', stk.BromoFactory()) record1 = stk.MoleculeRecord( topology_graph=stk.polymer.Linear( building_blocks=[building_block], repeating_unit='A', num_repeating_units=2, ), ) record2 = stk.MoleculeRecord( topology_graph=stk.polymer.Linear( building_blocks=[building_block], repeating_unit='A', num_repeating_units=2, ), ) fitness_values = { record1: 2, record2: None, } def get_minimum_fitness_value(fitness_values): return min( value for value in fitness_values.values() if value is not None ) replacer = stk.ReplaceFitness( # The replacement is half the minimum fitness value in the # population. get_replacement=lambda fitness_values: get_minimum_fitness_value(fitness_values) / 2, # Only replace fitness values which are None. filter=lambda fitness_values, record: fitness_values[record] is None, ) # Use the replacer. normalized_fitness_values = replacer.normalize(fitness_values) assert normalized_fitness_values[record1] == 2 assert normalized_fitness_values[record2] == 1
- Parameters:
get_replacement (Callable[[dict[T, Any]], Any]) – Calculates the replacement fitness value. The entire population passed to
normalize()
is passed to this parameter, regardless of what is passed to the filter parameter.filter (Callable[[dict[T, Any], T], bool]) – A function which returns
True
orFalse
. Only molecules which returnTrue
will have fitness values normalized. By default, all molecules will have fitness values normalized. The instance passed to the fitness_values argument ofnormalize()
is passed as the first argument, while the second argument will be passed everyMoleculeRecord
in it, one at a time.
Methods
Normalize some fitness values.