def check_names_2(population, offspring_pool): pop_cluster_names = population.get_cluster_names() off_cluster_names = offspring_pool.get_cluster_names() if check_for_duplicates(off_cluster_names): print('Error in def check_names, in GA_Program.py') print('We have found duplicates in both the population') print('off_cluster_names: ' + str(off_cluster_names.sort())) print('Check this out') Lock_Remove() raise Exception('Program finishing without completing') check = any(cluster_name in off_cluster_names for cluster_name in pop_cluster_names) if check: print('Error in def check_names, in GA_Program.py') print( 'We have found the cluster with the same name in both the offspring and population' ) print('pop_cluster_names: ' + str(pop_cluster_names)) print('off_cluster_names: ' + str(off_cluster_names)) duplicate_clusters = [] for cluster_name in pop_cluster_names: if cluster_name in off_cluster_names: duplicate_clusters.append(cluster_name) print('The cluster names in the population and offspring: ' + str(duplicate_clusters)) print('Check this out') Lock_Remove() raise Exception('Program finishing without completing')
def check_names_1(population): pop_cluster_names = population.get_cluster_names() if check_for_duplicates(pop_cluster_names): print('Error in def check_names, in GA_Program.py') print('We have found duplicates in both the population') print('pop_cluster_names: ' + str(pop_cluster_names.sort())) print('Check this out') Lock_Remove() raise Exception('Program finishing without completing')
def get_CNA_similarity(cluster_1_CNA, cluster_2_CNA): ''' Get the similarity for the two clusters at a particular value of rCut. :param cluster_1_CNA: the CNA profile of cluster 1 at rCut :type cluster_1_CNA: asap3.analysis.localstructure.FullCNA :param cluster_2_CNA: the CNA profile of cluster 2 at rCut :type cluster_2_CNA: asap3.analysis.localstructure.FullCNA :param total_no_of_atoms: The total number of atoms in the cluster :type total_no_of_atoms: int ''' tc_1_at_one_rCut = Counter(cluster_1_CNA) tc_2_at_one_rCut = Counter(cluster_2_CNA) total_CNA_signatures_in_common = tc_1_at_one_rCut & tc_2_at_one_rCut Union_of_total_CNAs = tc_1_at_one_rCut | tc_2_at_one_rCut sum_all_total_CNA_signatures_in_common = sum( total_CNA_signatures_in_common.values()) sum_all_Union_of_total_CNAs = sum(Union_of_total_CNAs.values()) try: similarity = (float(sum_all_total_CNA_signatures_in_common) / float(sum_all_Union_of_total_CNAs)) * 100.0 except ZeroDivisionError as error: error_message = '\n' error_message += '--------------------------------------------------------' + '\n' error_message += 'Error in def get_CNA_similarity, in TC_SCM_Methods.py' + '\n' error_message += 'Recieved the following error' + '\n' error_message += str(error) + '\n' error_message += 'The general problem that causes this to occur is that one or more rCut values that are being assessed are to low.' + '\n' error_message += 'This means that the CNA/SCM finds no pairs of atoms full stop throughout a cluster, thus giving a zero division error.' + '\n' error_message += 'Check this out' + '\n' error_message += 'Note: if you are happy with this issue however, you can replace the "raise ZeroDivisionError(error_message) from error" line of code with "print(error_message)" to only give this message and not exit this program in def get_CNA_similarity, in TC_SCM_Methods.py' + '\n' #error_message += '--------------------------------------------------------'+'\n' #print(error_message) error_message += 'As this a issue, the GA will finish without completing.' + '\n' error_message += '--------------------------------------------------------' + '\n' Lock_Remove() raise ZeroDivisionError(error_message) from error return similarity
def Initialise_Population_with_Randomly_Generated_Clusters( population, cluster_makeup, surface, Minimisation_Function, cell_length, vacuum_to_add_length, r_ij, rounding_criteria, no_of_cpus, memory_operator, predation_operator, fitness_operator, previous_cluster_name): """ This method will place a number of randomly generated clusters into the population until it is at the desired size. :param population: The population :type population: Organisms.GA.Population :param cluster_makeup: The makeup of the cluster :type cluster_makeup: dict. :param surface: This is the surface that the cluster is placed on. None means there is no surface. :type surface: Organisms.GA.Surface :param Minimisation_Function: The minimisation function :type Minimisation_Function: __func__ :param cell_length: This is the length of the square unit cell the cluster will be created in. :type cell_length: float :param vacuum_to_add_length: The amount of vacuum to place around the cluster :type vacuum_to_add_length: float :param r_ij: The maximum distance that should be between atoms to be considered bonded. This value should be as large a possible, to reflected the longest bond possible between atoms in the cluster. :type r_ij: float :param rounding_criteria: The number of decimal places to round the energy of clusters to. :type rounding_criteria: int :param no_of_cpus: The number of cpus available to create clusters :type no_of_cpus: int :param predation_operator: This is the predation operator :type predation_operator: Organisms.GA.Predation_Operator :param fitness_operator: This is the fitness operator :type fitness_operator: Organisms.GA.Fitness_Operator :param memory_operator: The memory operator :type memory_operator: Organisms.GA.Memory_Operator :param previous_cluster_name: This is the name of the last cluster created in the genetic algorithm. :type previous_cluster_name: int returns The name of the most recently created cluster by this method in the population. rtype int """ ############################################################################################################################################## # get the dirs of the clusters that need to be populated. clusters_to_create = list( zip( range(population.size), range(previous_cluster_name + 1, previous_cluster_name + population.size + 1))) # This part is to prevent the population making new clusters that override the clusters that the user placed into the original population. current_cluster_names = population.get_cluster_names( ) # The current Dir tag of the cluster being inputted or created in the GA. Initially use to record all cluster Dir in run folder. Will be turned into an int once all original clusters in the population have been recorded into the GA program. for index in range(len(clusters_to_create) - 1, -1, -1): if clusters_to_create[index][1] in current_cluster_names: del clusters_to_create[index] ######################################################################################################## # Initalise the rest of the population by creating randomised clusters using the InitaliseCluster method # from InitaliseCluster.py. The name of the dir tags will be those in the clusters_to_create list. while True: # This will create the required number of clusters to complete the population tasks = get_tasks(population, clusters_to_create, cell_length, vacuum_to_add_length, cluster_makeup, surface, r_ij, rounding_criteria, Minimisation_Function, memory_operator) ''' with mp.Pool(processes=no_of_cpus) as pool: # pool = mp.Pool() results = pool.map_async(self.create_a_cluster, tasks) results.wait() made_clusters = results.get() ''' # If you want to do work in serial rather than parallel made_clusters = [] for task in tasks: try: made_cluster = create_a_cluster(task) made_clusters.append(made_cluster) except Exception as exception: print( 'Error in def Initialise_Population_with_Randomly_Generated_Clusters, in Initialise_Population.py' ) print( 'There was an issue with initialising the population with randomly generated clusters.' ) print('The issue was with the following cluster') print('\t --> ' + str(task)) print( 'Check this out, see the following exception for details') Lock_Remove() raise exception for made_cluster in made_clusters: index, Opt_Cluster = made_cluster Opt_Cluster.ever_in_population = True Opt_Cluster.excluded_because_violates_predation_operator = False Opt_Cluster.initial_population = True Opt_Cluster.removed_by_memory_operator = False population.add( index, Opt_Cluster ) # Place the initialised cluster into the population #except: # print('Weird error happening here? 2') # import pdb; pdb.set_trace() # exit() print("-----------------------------") # Assign fitnesses to clusters. fitness_operator.assign_initial_population_fitnesses() print( 'Checking that the clusters in the population satisfies the Diversity Scheme' ) # Check that the population does not violate the diversity scheme clusters_to_create, removed_clusters_report = Check_Population_against_predation_operator( population, predation_operator) if clusters_to_create == []: break return population[-1].name
def exit_handler(): print('Unlocking the genetic algorithm program before exitting program.') Lock_Remove()