def calculate_mutation_strength(run_id, generation, mutation_strength_bin):
"""Query mutation_strength for bin and adjust as necessary.
Args:
run_id (str): identification string for run.
generation (int): iteration in bin-mutate-simulate routine.
parent (sqlalchemy.orm.query.Query): parent-material corresponding to
the bin being queried.
Returns:
mutation_strength.strength (float): mutation strength to be used for
parents in the bin being queried. If the fraction of children from
previous generation which populate the SAME bin as their parent is LESS
THAN 10% then the mutation strength is REDUCED BY 5%. If the fraction
of these children populating the SAME bin as their parent is GREATER
THAN 50% then the mutation strength is INCREASED BY 5%.
"""
mutation_strength_key = [run_id, generation] + mutation_strength_bin
mutation_strength = session.query(MutationStrength).get(mutation_strength_key)
if mutation_strength:
print("Mutation strength already calculated for this bin and generation.")
else:
print("Calculating mutation strength...")
mutation_strength = MutationStrength.get_prior(*mutation_strength_key).clone()
if config['interactive_mode'] == 'on':
print('Prior mutation strength :\t{}'.format(mutation_strength.strength))
mutation_strength.generation = generation
try:
fraction_in_parent_bin = calculate_percent_children_in_bin(run_id, generation, mutation_strength_bin)
if config['interactive_mode'] != 'on':
if fraction_in_parent_bin < 0.1 and mutation_strength.strength - 0.05 > 0:
mutation_strength.strength -= 0.05
elif fraction_in_parent_bin > 0.5 and mutation_strength.strength + 0.05 < 1:
mutation_strength.strength += 0.05
elif config['interactive_mode'] == 'on':
print('\tFor adaptive mutation strength(s), DECREASE strength \n' +
'\tby 5% if the fraction of children in parent-bin is \n' +
'\tLESS THAN 0.1; INCREASE strength by 5% if the \n' +
'\tfraction of children in parent-bin is GREATER THAN \n' +
'\t0.5.')
mutation_strength.strength = set_variable(
'Please Enter new mutation strength :\t', 'mutation_strength.strength')
except ZeroDivisionError:
print("No prior generation materials in this bin with children.")
try:
session.add(mutation_strength)
session.commit()
except (FlushError, IntegrityError) as e:
print("Somebody beat us to saving a row with this generation. That's ok!")
session.rollback()
# it's ok b/c this calculation should always yield the exact same result!
sys.stdout.flush()
return mutation_strength.strength
评论列表
文章目录