def calculate_score_vs_parameters(best_parameters, err_settings, fit_settings, simulator, exp_data, spinA, spinB, calc_settings): sys.stdout.write("Calculating chi2 in dependence of fitting parameters ...\n") score_vs_parameters = [] Ne = len(err_settings['variables']) Ns = err_settings['Ns'] for i in range(Ne): sys.stdout.write('\r') sys.stdout.write("Calculation step %d / %d" % (i+1, Ne)) sys.stdout.flush() # Create a generation generation = Generation(Ns) generation.first_generation(fit_settings['parameters']['bounds']) # Set all genes to the optimized values except the ones which will to be varied for name in const['variable_names']: index = fit_settings['parameters']['indices'][name] if not (index == -1) and not (name in err_settings['variables'][i]): for j in range(Ns): generation.chromosomes[j].genes[index] = best_parameters[name]['value'] # Score the generation generation.score_chromosomes(fit_settings, simulator, exp_data, spinA, spinB, calc_settings) # Sort chromosomes according to their score generation.sort_chromosomes() # Store the variables and the corresponding score values score_vs_variables = {} for name in err_settings['variables'][i]: score_vs_variables[name] = [] index = fit_settings['parameters']['indices'][name] for j in range(Ns): score_vs_variables[name].append(generation.chromosomes[j].genes[index]) score_vs_variables['score'] = [] for j in range(Ns): score_vs_variables['score'].append(generation.chromosomes[j].score) score_vs_parameters.append(score_vs_variables) sys.stdout.write('\n') return score_vs_parameters
def calculate_fit_and_noise_std(best_parameters, fit_settings, simulator, exp_data, spinA, spinB, calc_settings): # Create a generation with one chromosome and set all genes to the optimized values generation = Generation(1) generation.first_generation(fit_settings['parameters']['bounds']) for name in const['variable_names']: index = fit_settings['parameters']['indices'][name] if not (index == -1): generation.chromosomes[0].genes[index] = best_parameters[name][ 'value'] # Calculate the corresponding fit fit = get_fit(generation.chromosomes[0].genes, fit_settings, simulator, exp_data, spinA, spinB, calc_settings) # Calculate the standard deviation of noise noise_std = calculate_noise_std(fit, exp_data, fit_settings, calc_settings) return noise_std
def calculate_numerical_error(best_parameters, err_settings, fit_settings, simulator, exp_data, spinA, spinB, calc_settings): # Create a generation Ns = err_settings['Ns'] generation = Generation(Ns) generation.first_generation(fit_settings['parameters']['bounds']) # Set all genes to the optimized values for name in const['variable_names']: index = fit_settings['parameters']['indices'][name] if not (index == -1): for j in range(Ns): generation.chromosomes[j].genes[index] = best_parameters[name]['value'] # Score the generation generation.score_chromosomes(fit_settings, simulator, exp_data, spinA, spinB, calc_settings) # Sort chromosomes according to their score generation.sort_chromosomes() # Determine the variation of the score score_min = generation.chromosomes[0].score score_max = generation.chromosomes[Ns-1].score numerical_error = score_max - score_min return numerical_error
def run_optimization(self, fit_settings, simulator, exp_data, spinA, spinB, calc_settings): sys.stdout.write('Starting the fitting...\n') time_start = time.time() for i in range(self.num_generations): if (i == 0): # Create the first generation generation = Generation(self.size_generation) generation.first_generation( fit_settings['parameters']['bounds']) else: # Create the next generation generation.produce_offspring( fit_settings['parameters']['bounds'], self.prob_crossover, self.prob_mutation) # Score the generation generation.score_chromosomes(fit_settings, simulator, exp_data, spinA, spinB, calc_settings) # Sort chromosomes according to their score generation.sort_chromosomes() # Save the best score in each optimization step self.best_score[i] = generation.chromosomes[0].score # Display some graphics if self.display_graphics: # Calculate the best fit so far best_fit = get_fit(generation.chromosomes[0].genes, fit_settings, simulator, exp_data, spinA, spinB, calc_settings) if (i == 0): fig_fit, graph_fit = plot_fit(best_fit, exp_data, self.fitted_data, calc_settings) fig_score, axes_score = plot_score( self.best_score, bool(calc_settings['noise_std'])) elif ((i > 0) and (i < self.num_generations - 1)): update_fit_plot(fig_fit, graph_fit, best_fit) update_score_plot(axes_score, self.best_score, bool(calc_settings['noise_std'])) elif (i == self.num_generations - 1): close_fit_plot(fig_fit) close_score_plot(fig_score) sys.stdout.write('\r') sys.stdout.write("Optimization step %d / %d: chi2 = %f" % (i + 1, self.num_generations, self.best_score[i])) sys.stdout.flush() # Calculate the best fit if self.display_graphics: self.best_fit = best_fit else: self.best_fit = get_fit(generation.chromosomes[0].genes, fit_settings, simulator, exp_data, spinA, spinB, calc_settings) # Check that Chi2 is normalized by the variance of noise if calc_settings['noise_std'] == 0: noise_std = calculate_noise_std(self.best_fit, exp_data, fit_settings, calc_settings) calc_settings['noise_std'] = noise_std self.best_score = self.best_score / noise_std**2 # Store the best genes self.best_parameters = get_parameters( generation.chromosomes[0].genes, fit_settings['parameters']['indices'], fit_settings['parameters']['fixed']) time_finish = time.time() time_elapsed = str(datetime.timedelta(seconds=time_finish - time_start)) sys.stdout.write('\n') sys.stdout.write('The fitting is finished. Total duration: %s\n\n' % (time_elapsed))