def mutate_and_relax(candidate, name, iteration, cnt_max, **kwargs): print_output("__%s__" % name) found = False cnt = 0 while found is False and cnt < cnt_max: candidate_backup = Structure(candidate) candidate.mutate(**kwargs) print_output("%s after mutation: " % name + str(candidate)) run_util.str_info(candidate) if not candidate.is_geometry_valid(): print_output(" The geometry of %s is invalid." % name) cnt += 1 # Rebuild the structure candidate = candidate_backup continue if candidate not in blacklist: name = "generation_%d_%s" % (iteration, name) run_util.optimize(candidate, energy_function, params, name) run_util.check_for_kill() candidate.send_to_blacklist(blacklist) print_output(str(candidate)+":, energy: "+str(float( candidate))+", is temporary added to the population") run_util.relax_info(candidate) found = True population.append(candidate) else: print_output("Geomerty of "+str(candidate)+" is fine, but already " "known.") cnt += 1 candidate = candidate_backup if cnt == cnt_max: raise Exception("The allowed number of trials for generating" " a unique child has been exceeded.")
def mutate_and_relax(candidate, name, iteration, cnt_max, **kwargs): print_output("__%s__" % name) found = False cnt = 0 while found is False and cnt < cnt_max: candidate_backup = Structure(candidate) candidate.mutate(**kwargs) print_output("%s after mutation: " % name + str(candidate)) run_util.str_info(candidate) if not candidate.is_geometry_valid(): print_output(" The geometry of %s is invalid." % name) cnt += 1 # Rebuild the structure candidate = candidate_backup continue if candidate not in blacklist: name = "generation_%d_%s" % (iteration, name) run_util.optimize(candidate, energy_function, params, name) run_util.check_for_kill() candidate.send_to_blacklist(blacklist) print_output( str(candidate) + ":, energy: " + str(float(candidate)) + ", is temporary added to the population") run_util.relax_info(candidate) found = True population.append(candidate) else: print_output("Geomerty of " + str(candidate) + " is fine, but already " "known.") cnt += 1 candidate = candidate_backup if cnt == cnt_max: raise Exception("The allowed number of trials for generating" " a unique child has been exceeded.")
cnt += 1 continue else: child1, child2 = Structure(parent1), Structure(parent2) print_output("No crossover was performed. Children are copies of " "parents: " + str(child1) + (": ") + str(child1) + (", ") + str(child2) + (": ") + str(child2)) # Delete inherited attributes. for child in child1, child2: attr_list = ["initial_sdf_string", "energy"] for attr in attr_list: delattr(child, attr) for dof in child.dof: delattr(dof, "initial_values") run_util.str_info(child1) run_util.str_info(child2) try: mutate_and_relax(child1, "child1", iteration, cnt_max, **linked_params) except Exception as exc: print_output(exc) sys.exit(0) try: mutate_and_relax(child2, "child2", iteration, cnt_max, **linked_params) except Exception as exc: print_output(exc) sys.exit(0) population.sort() print_output("Sorted population: " + ', '.join([str(v) for v in population]))
cnt += 1 continue else: child1, child2 = Structure(parent1), Structure(parent2) print_output("No crossover was performed. Children are copies of " "parents: " + str(child1) + (": ") + str(child1) + (", ") + str(child2) + (": ") + str(child2)) # Delete inherited attributes. for child in child1, child2: attr_list = ["initial_sdf_string", "energy"] for attr in attr_list: delattr(child, attr) for dof in child.dof: delattr(dof, "initial_values") run_util.str_info(child1) run_util.str_info(child2) try: mutate_and_relax(child1, "child1", iteration, cnt_max, **linked_params) except Exception as exc: print_output(exc) sys.exit(0) try: mutate_and_relax(child2, "child2", iteration, cnt_max, **linked_params) except Exception as exc: print_output(exc) sys.exit(0) population.sort() print_output("Sorted population: " + ', '.join([ str(v) for v in population]))