def write_all_models(test=False): """ Writes all the models to a file. :param: test: Determines if the function is called in test mode, which enables the deletion of the file at the end. :return: None """ # Open the output file name = 'acme_results_' + str(time.time()) + '.csv' outfile = open(name, 'w') # Make the header header = "Like" + ", " + "Genes" + "\n" outfile.write(header) # Write the entries for genes, like in LumpedCMFGenerator.models_so_far.items(): # Exclude the non active genes before writing it down genes_copy = genes.split() genes_copy = genome_arrange.find_active_genes( genes_copy, LumpedCMFGenerator.storages) genes_copy = ", ".join(genes_copy) line = str(like) + ", " + genes_copy + "\n" outfile.write(line) outfile.close() # Remove the file when only a test is run if test: os.remove(name)
def get_fitness(genes, data, begin_calibration, end_calibration, begin_validation, end_validation): """ Calculates the fitness of a given genotype. :param genes: genotype that is to be tested for its fitness :param data: the weather data in the form of a dict of lists :param begin_calibration: :param end_calibration: :param begin_validation: :param end_validation: :return: Fitness value """ # Check if the model to be generated is able to connect to an output genome_arrange.check_for_connection(genes, LumpedCMFGenerator.connections) # Find the effective structure of the current genes effective_structure = genome_arrange.find_active_genes( genes, LumpedCMFGenerator.storages) # Compare if the genes the function gets, have already been calculated # as a model for old_model in LumpedCMFGenerator.models_so_far.keys(): # Find the effective structure # Turn model in list version old_model_genes = old_model.split() # Find out if the model has already been calculated. If so, simply # return the fitness value of the old model if set(old_model_genes) == set(genes): return LumpedCMFGenerator.models_so_far[old_model] if set(old_model_genes) == set(effective_structure): return LumpedCMFGenerator.models_so_far[old_model] # If not call the template and run the model current_model = template.LumpedModelCMF(effective_structure, data, begin_calibration, end_calibration, begin_validation, end_validation) # Find out if the model should run parallel (for supercomputer) parallel = 'mpi' if 'OMPI_COMM_WORLD_SIZE' in os.environ else 'seq' # Connect the model to the dream algorithm. sampler = spotpy.algorithms.dream(current_model, parallel=parallel, dbformat="noData") # The template runs until the predefined convergence value of dream is # reached (or the maximal value for repetitions is reached). sampler.sample(10, convergence_limit=1.6) # Extract the best value from the model best_like = sampler.bestlike # Save the current model in the all models list model_key = " ".join(genes) LumpedCMFGenerator.models_so_far[model_key] = best_like # Return best fitness value of all runs return best_like
def find_effective_structure(): # Check if the model to be generated is able to connect to an output genome_arrange.check_for_connection(genes, LumpedCMFGenerator.connections) # Find the effective structure of the current genes effective_structure = genome_arrange.find_active_genes( genes, LumpedCMFGenerator.storages) return effective_structure
def display(candidate, start_time): """ Display the current candidate and his fitness. :param candidate: Model/genotype that is to be displayed :param start_time: Time when the current program started :return: None """ time_diff = datetime.datetime.now() - start_time # calculate how much % of the genes are active active_genes = genome_arrange.find_active_genes( candidate.genes, LumpedCMFGenerator.storages) activity = len(active_genes) / len(candidate.genes) * 100 print(("Genes: {}\t\nGene Activity: {} % \t\nFitness: {}\tStrategy: {}\t" "Time: {}".format(" ".join(map(str, candidate.genes)), activity, candidate.fitness, candidate.Strategy.name, time_diff)))
def test_find_active_genes(self): """ Tests if the Method is correctly finding the active genes and return them. :return: None """ genes = ["snow", "snow_meltrate", "canopy_closure", "second", "tr_second_out", "tr_first_second", "third", "tr_third_river"] active_genes = genome_arrange.find_active_genes( genes, generator.LumpedCMFGenerator.storages) right_solution = ["snow", "snow_meltrate", "second", "tr_second_out", "tr_first_second", "first"] print("\n test_active_genes") print("Start genes = " + str(genes)) print("Active genes = " + str(active_genes)) print("Right solution = " + str(right_solution)) self.assertTrue(len(active_genes) < len(genes) and len(active_genes) == len(right_solution) and set(active_genes) == set(right_solution))