def breed_loop(N, data, gdata, save_interval): ffts = [] min_errs = [] for n in range(N): # print("Generation:", n) errors, goal = gen.fit_all(data, gdata) new_data = np.zeros(np.shape(data)) for i in range(len(data)//2): if goal: print("Goal!") r = np.arange(len(errors)) i1, i2 = np.random.choice(r, 2, p=errors) cross1, cross2 = gen.crossover(data[i1], data[i2]) cross1, cross2 = gen.mutate(cross1, get_temp(n+1)), gen.mutate(cross2, get_temp(n+1)) # cross1, cross2 = gen.mutate(cross1, .5), gen.mutate(cross2, .5) new_data[i], new_data[i+(len(data)//2)] = cross1, cross2 if n % save_interval == 0 and i == 0: play_fft(cross1) data = new_data if n % 25 == 0: print(f'min error {np.amin(errors)}') min_errs.append(np.amin(errors)) return ffts, min_errs
def main(): pop_size = 1000 max_runs = 50 mutate_ratio = 0.2 # Which data set to use. mode = 1 random.seed() pop = [] # Create a random population of trees. for i in range(pop_size): pop.append(ExprTree(mode)) print "Population created." for tree in pop: print tree for i in range(max_runs): print "Beginning crossover " + str(i) + ":" # Crossover and mutate population. genetic.mutate(pop, mutate_ratio, mode) pop = genetic.crossover(pop, mode) print "Crossover " + str(i) + " complete."
def spawn_child(self, nb=1, train=False): if len(self.nn_bots) > 0: for i in range(nb): nb1 = random.randint(0, len(self.nn_bots) - 1) nb2 = random.randint(0, len(self.nn_bots) - 1) parent1 = self.nn_bots[nb1] # parent 1 parent2 = self.nn_bots[nb2] # parent 2 # create a child by breeding to random bots child_weights = croisement(parent1.model.weights, parent2.model.weights, 1)[0] mutate(child_weights, 1, 1) if self.current_nb_step % 2 == 0: x, y = self.g_free_xy(parent1.x, parent1.y, 4) else: x, y = self.g_free_xy(parent2.x, parent2.y, 4) child = NN_bot(self.map, x, y, self, train=train) child.model.weights = child_weights self.map.board[x, y, 0] = 1 self.bots.append(child) self.nn_bots.append(child) else: self.load_bots(nb, train)
def test_mutation(function1, function2): """Test mutation function""" population = [ [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0], [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0], [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0], [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0], ] expected_result = [ [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0], [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0], [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0], [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0] ] result = genetic.mutate(population) assert(result == expected_result)
def main(): map_size = 100 p_type = 10 # dont get when we will use it max_route_length = 150 population_size = 30 number_of_each_new_generation = 10 number_of_couples = int( (population_size - number_of_each_new_generation) / 2) probability = 0.05 the_map = initialize(p_type, map_size) population = create_starting_population(max_route_length, population_size, the_map) last_distance = 1000000000 for i in range(0, 100): scores = score_population(population, the_map) new_population = [] best = population[np.argmin(scores)] number_of_moves = len(best) distance = fitness(best, the_map) if distance != last_distance: print( 'Iteration %i: Best so far is %i steps for a distance of %f' % (i, number_of_moves, distance)) plot_best(the_map, best, i) for j in range(0, number_of_couples): new_route1, new_route2 = crossover( population[pick_breeder(scores)], population[pick_breeder(scores)]) new_population.extend([new_route1, new_route2]) # mutate the current members of new_population for j in range(0, len(new_population)): new_population[j] = mutate(new_population[j], probability, the_map) new_population.append(population[np.argmin(scores)]) while len(new_population) < population_size: new_population.append(create_new_member(max_route_length, the_map)) population = new_population[::] # copy a values
def mitose(self): if self.g_cd_repro() == 0: self.incr_cd_repro(20) self.incr_energy(-5) # loose of energy to make the child # energy that will be transfered to the child energy_to_child = 5 x = -1 y = -1 # TODO: faire une fonction pour rendre ca plus propre if self.y - 2 > 0: if self.map.cellLibre(self.x, self.y-1) == 0: x = self.x y = self.y - 1 elif self.y + 2 < self.map.height - 1: if self.map.cellLibre(self.x, self.y+1) == 0: x = self.x y = self.y + 1 elif self.x - 2 > 0: if self.map.cellLibre(self.x-1, self.y) == 0: x = self.x - 1 y = self.y elif self.x + 2 < self.map.height - 1: if self.map.cellLibre(self.x+1, self.y) == 0: x = self.x + 1 y = self.y else: # no place to put the child self.incr_energy(energy_to_child) self.incr_energy(-1) if x == -1: pass else: new_model = genetic.mutate(self.model.weights, 1, 1) new_bot = NN_bot(self.map, x, y, self.sim, new_model) new_bot.s_energy(energy_to_child) self.sim.add_bots([new_bot])
ff = ff_init # ff used in the loop set to ff_init ff_av = ff_av_init ff_av_points, ff_max_points, ff_total_points, iteration_points = [], [], [], [] # MAIN LOOP while ff_av < 1: # repeat until all individuals have fitness = 1 ff.sort(key=lambda tup: tup[0]) # sort ff by first digit in each tuple selected = selections.select_rank(ff) new_x = genetic.mate(x, selected, crossover_prob) genetic.save_elite(x, new_x) x = new_x mutated = genetic.mutate(x, mutation_prob) if mutated: mutation_count += 1 ff = genetic.ff(x) ff_av = genetic.ff_av(ff) ff_max = genetic.ff_max(ff) iterations += 1 ff_av_points.append(ff_av) ff_max_points.append(ff_max) ff_total_points.append(genetic.ff_total(ff)) iteration_points.append(iterations) max_crossover = population_init / 2 * iterations
# popcpy = g.mutate(pop) # print(popcpy) # popcpy = g.mutate(pop) # print(popcpy) # popcpy = g.mutate(pop) # print(popcpy) # print(type(popcpy)) generations = 0 # print(pop[1]) while generations <= 100: generations += 1 pop = g.mutate(pop) newpop = [0 for _ in range(100)] newpop[0] = pop[g.getBest(pop, fn.rast, -5.12, 5.12, 17)] for i in range(1, len(newpop)): newpop[i] = g.select(pop, fn.rast, -5.12, 5.12, 17) pop = newpop for i in range(100): print(fn.rast(fn.decode(pop[i], -5.12, 5.12, 17)), end=" ") print("\n")
def combo_scaling(iterations: int, plot: bool, show_matrix: bool, population_size: int, mutation_probability: float, initial_matrix: str, smoothness: float, eval_type: str, breed: bool, breed_disable: int, save: bool, shape_covariance: bool, normalize_by_cross_section: bool, normalize_by_bins: bool, pseudoinverse: bool, normalization_penalty: bool, custom_covariance: str) -> None: if initial_matrix == 'ones': original_matrix = np.ones((24, 24)) else: with open(initial_matrix, 'rb') as f: original_matrix = pickle.load(f).reshape((24, 24)) for i in range(24): for j in range(i + 1, 24): original_matrix[j][i] = 0 original_matrix = original_matrix.reshape(24 * 24) experiments = [('minerva_neutrino', 156), ('minerva_antyneutrino', 60), ('T2K_neutrino', 58), ('T2K_antyneutrino', 58)] experimental_data = [] for experiment, bins in experiments: data = utils.import_data_from_path(f"Data/{experiment}") experiment_info = utils.load_experimental_data(experiment) if "minerva" in experiment: events = utils.events_to_cross_section( data.events.reshape( (24 * 24, -1)), data.nof_events, data.cross_section, experiment_info[0].shape, *experiment_info[4:6]) if custom_covariance and experiment == 'minerva_neutrino': with open(custom_covariance, 'rb') as f: covariance_matrix = np.linalg.pinv(pickle.load(f)) if shape_covariance: if experiment == 'minerva_neutrino': import covariance_matrix covariance = covariance_matrix.covariance elif experiment == 'minerva_antyneutrino': from minerva_antyneutrino import minerva_antyneutrino_covariance covariance = minerva_antyneutrino_covariance if custom_covariance and experiment == 'minerva_neutrino': with open(custom_covariance, 'rb') as f: covariance = pickle.load(f) data = experiment_info[0].reshape(-1) shape_matrix, _, _ = utils.shape_matrix_extraction( data, covariance) covariance_matrix = shape_matrix if pseudoinverse: covariance_matrix = np.linalg.pinv(covariance_matrix) else: covariance_matrix = np.linalg.inv(covariance_matrix) experiment_info[2] = covariance_matrix else: events = data.events experimental_data.append( Experiment(experiment, events, experiment_info, bins)) combined_score = Combined_score(experimental_data, normalization_penalty) # tmp_matrix = np.ones((24 * 24)) score = combined_score(original_matrix, normalize_by_bins, normalize_by_cross_section) print(f"Score without reweighting:\n{score}") population = [original_matrix] population += [ mutate(original_matrix, mutation_probability, smoothness) for _ in range(1, population_size) ] if eval_type == 'chi2_cov': key = lambda m: np.sum(m[0][:, 0]) elif eval_type == 'chi2_std': key = lambda m: np.sum(m[0][:, 1]) elif eval_type == 'chi2_both': key = lambda m: np.sum(m[0]) smoothness = smoothness if 0 < smoothness < 1 else 0 start = time.time() try: for i_no in range(1, iterations + 1): chi2 = [ combined_score(population[i], normalize_by_bins, normalize_by_cross_section) for i in range(population_size) ] population_with_score = list(zip(chi2, population)) population_with_score.sort(key=key, reverse=False) print(i_no, population_with_score[0][0][:, 0], sum(population_with_score[0][0][:, 0])) if breed_disable: if i_no > breed_disable: breed = False if breed: population = new_population(population_with_score, mutation_probability, smoothness) else: population[0] = population_with_score[0][1] for i in range(1, population_size): population[i] = mutate(population_with_score[0][1], mutation_probability, smoothness) except KeyboardInterrupt: print("Manually stopped computation") elapsed = time.time() - start minutes = elapsed / 60 seconds = int(elapsed) % 60 print(f"Calculated in: {int(minutes)}m{seconds}s") if save: save_chi2 = population_with_score[0][0] config = { 'iterations': i_no, 'population_size': population_size, 'mutation_probability': mutation_probability, 'initial_matrix': initial_matrix, 'smoothness': smoothness if smoothness else 1, 'eval_type': eval_type, 'breed': breed, 'chi2_cov': save_chi2[:, 0], 'chi2_std': save_chi2[:, 1] } summed_chi2 = np.sum(save_chi2, axis=0) name = f'scale_cov{summed_chi2[0]:.2f}_std{summed_chi2[1]:.2f}.pkl' with open(os.path.join('Combined_scaling', name), 'wb') as file: pickle.dump(population_with_score[0][1].reshape(24, 24), file) pickle.dump(config, file) print(f"Scaling matrix saved to {name}")