def learn_snake_nn(): # initialize the population population = [NeuralNetwork() for i in range(POPULATION_SIZE)] best_score = 0 best_nn = None for i in range(ITERATIONS): # calculate the fitness of generation population_fitness_tuples = ga.fitness(population) # perform selection parents, new_score, new_best_nn = ga.selection( population_fitness_tuples, best_score) if new_score > best_score: best_score = new_score best_nn = new_best_nn SnakeGame(best_nn, max_steps=1000000).play_game(gui=True) best_of_generation_tup = sorted(population_fitness_tuples, key=lambda x: x[1], reverse=True)[0] # play the game with the best of generation # if best_of_generation_tup[1] > 400: # SnakeGame(best_of_generation_tup[0]).play_game(gui=True) print( f"Iteration {i}: Best fitness for this generation: {best_of_generation_tup[1]}, Best so far: {best_score}" ) #perform crossover to get the new population population = ga.crossover(parents, best_nn) while True: SnakeGame(best_nn, max_steps=1000000).play_game(gui=True)
def create_next_generation(self): parameters_list = [ game.snake.brain.parameters for game in self.current_generation ] mating_pool = [ ga.tournament_selection(parameters_list, self.fitness_list) for _ in range(len(parameters_list)) ] nn_architecture = self.current_generation[0].snake.brain.architecture parent_chromosomes = [ ga.flatten_parameters(brain, nn_architecture) for brain in mating_pool ] crossed_chromosomes = [] for pair in range(0, len(parent_chromosomes), 2): for child in ga.crossover(parent_chromosomes[pair], parent_chromosomes[pair + 1], self.app.crossover_rate): child = ga.mutation(child, self.app.mutation_rate) crossed_chromosomes.append(child) # reshape parameters in the neural network architecture new_parameters = [] for chromosome in crossed_chromosomes: new_parameters.append( ga.reshape_parameters(chromosome, nn_architecture)) self.generation_number += 1 self.current_generation = [ Game(self.app.surface, parameters=parameters) for parameters in new_parameters ]
def ga_driver(population): new_generation = [] for i in range(20): for j in range(3): # Get 4 Random numbers index_1 = random.randint(0, 3) index_2 = random.randint(0, 3) index_3 = random.randint(0, 3) index_4 = random.randint(0, 3) #print("Index 1 = " + str(index_1)) # Perform Binary Tournament parent_1 = ga.binary_tournament(population[index_1], population[index_3]) parent_2 = ga.binary_tournament(population[index_4], population[index_2]) # Generate OffSprings offspring_1 = ga.crossover(parent_1, parent_2) offspring_2 = ga.crossover(parent_2, parent_1) new_generation.append(offspring_1) new_generation.append(offspring_2) population = new_generation ga.truncate(population) print("Current Generation Statistics : ") print("Generation = " + str((i + 1))) print("Elements : ", population) print("Highest Fitness Value = " + str(ga.fitness(ga.function, population[0])))
def generations( population, pop_size, fitness_func, prob_cross, prob_muta, select_parents, muta_method, cross_method, select_survivors, max_gener, sizes, max_size, bests_matrix, averages_matrix, current_generation, current_run): for gener in range(max_gener): mates = select_parents(population, pop_size, 3) offspring = crossover(mates, prob_cross, cross_method) offspring = mutation(offspring, prob_muta, muta_method) offspring = eval_pop(offspring, fitness_func, sizes, max_size) population = select_survivors(population, offspring) evaluate_generation( population, bests_matrix, averages_matrix, current_generation +gener , current_run) return population
def generations(population, pop_size, fitness_func, prob_cross, prob_muta, select_parents, muta_method, cross_method, select_survivors, max_gener, sizes, max_size, bests_matrix, averages_matrix, current_generation, current_run): for gener in range(max_gener): mates = select_parents(population, pop_size, 3) offspring = crossover(mates, prob_cross, cross_method) offspring = mutation(offspring, prob_muta, muta_method) offspring = eval_pop(offspring, fitness_func, sizes, max_size) population = select_survivors(population, offspring) evaluate_generation(population, bests_matrix, averages_matrix, current_generation + gener, current_run) return population
def GA(): startTime = time.time() #処理前の時刻 genetic_algorithm.generate() genetic_algorithm.evaluation(physical_weight=0.2) print('') print('第1世代') my_function.view_class() print('【目的関数】' + str(py_setting.best_score)) print( '-------------------------------------------------------------------------\n' ) for i in range(2, 1001): genetic_algorithm.crossover() genetic_algorithm.evaluation(physical_weight=0.2) generation = i print('第' + str(generation) + '世代') my_function.view_class() print('【目的関数】' + str(py_setting.best_score)) print( '-------------------------------------------------------------------------\n' ) if genetic_algorithm.escape_loop(50): break endTime = time.time() #処理後の時刻 elapsed_time = endTime - startTime print('【最終世代】' + '第' + str(generation) + '世代') print('【最終目的関数値】' + str(py_setting.best_score)) print('【経過時間】' + str(elapsed_time) + '(s)')
# ant_random_new = round(len(population) * random_new_rate) ant_random_new = 0 # Potential_Parent selection parent_rate = 0.10 ant_parents = max(round(parent_rate * config.pop_size), 2) potential_parents = population[0:ant_parents] ant_children = len(population) - ant_survivors - ant_random_new children = [] for i in range(ant_children): # parent = potential_parents[random.randrange(0, len(potential_parents))] # Parent selection parent1, parent2 = random.sample(potential_parents, k=2) child = crossover(parent1, parent2, config=config) mutate(child, config=config) children.append(child) population = survivers + children # replenish_population_with_new_random(pop_size, population) assert len(population) == config.pop_size non_evalutation_time = time.time() - after_evaluation print(non_evalutation_time) ### Summary best = max(population, key=lambda individual: individual.fitness) print("best Individual got score {}, and looked like: {}".format(
def test_crossover(self): p1 = "1234" p2 = "4321" c = crossover(p1, p2, 0.5) self.assertEqual(len(c), len(p1)) print c
import genetic_algorithm image_name = 'C:\\Users\\Dell\\Desktop\\My disk\\Sketches\\init7.jpg' final_name = 'C:\\Users\\Dell\\Desktop\\My disk\\Sketches\\final7.jpg' iterations_num = 30 genetic_algorithm.generate_init_pop(image_name) print('End of initial population\'s generation') for i in range(1, iterations_num + 1): genetic_algorithm.fitness_function() genetic_algorithm.selection_function() genetic_algorithm.crossover() genetic_algorithm.mutation() print('End of ' + str(i) + ' iteration') result = genetic_algorithm.termination() print('End of termination') result.save_image(final_name)
chromosome.fitness = pacman.evasion # 적합도는 유령 회피 성공 수 wr.writerow([n_gen, i, pacman.evasion, EndT, score]) # csv 파일에 게임 결과 기록 if pgf.keyPressed('esc'): n_gen = 0 break if pgf.keyPressed('esc'): break # 유전 알고리즘을 이용한 진화 시작 if best_chromosomes is not None: # 퇴화 방지를 위해 이전 세대 상위 chromosome 까지 포함 chromosomes.extend(best_chromosomes) chromosomes.sort(key=lambda x: x.fitness, reverse=True) # 적합도를 기준으로 내림차순 정렬 print('===== Generaton #%s\tBest Fitness %s =====' % (n_gen, chromosomes[0].fitness)) # 세대 끝, 최고 적합도 출력 best_chromosomes = deepcopy( chromosomes[:N_BEST]) # 상위 chromosome 4개 따로 저장 ga.crossover(N_CHILDREN, best_chromosomes) # crossover ga.mutation(N_POPULATION, N_BEST, N_CHILDREN, best_chromosomes, PROB_MUTATION, chromosomes) #mutation if pgf.keyPressed('esc'): break
# Survival selection ant_survivors = round(len(population) * config.survival_rate) survivers = population[0:ant_survivors] # Potential_Parent selection ant_parents = max(round(config.parent_rate * config.pop_size), 2) potential_parents = population[0:ant_parents] ant_children = len(population) - ant_survivors children = [] for i in range(ant_children): # parent = potential_parents[random.randrange(0, len(potential_parents))] # Parent selection parent1, parent2 = random.sample(potential_parents, k=2) child = crossover(parent1, parent2) mutate(child) children.append(child) population = survivers + children assert len(population) == config.pop_size ### Summary best = max(population, key=lambda individual: individual["fitness"]) print("best Individual got score {}, and looked like: {}".format( best["fitness"], best)) average_population_fitness = mean( [individual["fitness"] for individual in population]) print( "average population fitness: {}".format(average_population_fitness))
#Creating the initial population. new_population = numpy.random.uniform(low=0, high=10.0, size=pop_size) print(new_population) num_generations = 15 for generation in range(num_generations): print("Generation : ", generation) # Measing the fitness of each chromosome in the population. fitness = GA.cal_pop_fitness(equation_inputs, new_population) # Selecting the best parents in the population for mating. parents = GA.select_mating_pool(new_population, fitness, num_parents_mating) # Generating next generation using crossover. offspring_crossover = GA.crossover( parents, offspring_size=(pop_size[0] - parents.shape[0], num_weights)) # Adding some variations to the offsrping using mutation. offspring_mutation = GA.mutation(offspring_crossover) # Creating the new population based on the parents and offspring. new_population[0:parents.shape[0], :] = parents new_population[parents.shape[0]:, :] = offspring_mutation # The best result in the current iteration. print("Best result : ", numpy.max(numpy.sum(new_population * equation_inputs, axis=1))) # Getting the best solution after iterating finishing all generations. #At first, the fitness is calculated for each solution in the final generation. fitness = GA.cal_pop_fitness(equation_inputs, new_population)
def main(): nn_size = 3 generation_number = 1 clock = pygame.time.Clock() pygame.init() screen = pygame.display.set_mode((500, 500)) done = False # Create player players = [ Bird( 15, screen.get_width(), screen.get_height(), IntelligentBird(nn_size, screen.get_width(), screen.get_height()) ) for i in range(POPULATION_SIZE) ] dead_players = [] # Create pipes array and add first pipe pipes = [Pipe(screen.get_width(), screen.get_height())] score = 0 delta_time = time.time() time_bt_pipes = 2.7 # 1 second while not done: screen.fill((76, 188, 252)) # Player for p in players: p.draw(screen) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True for p in players: p.update() # Pipes if time.time() - delta_time >= time_bt_pipes: delta_time = time.time() pipes.append(Pipe(screen.get_width(), screen.get_height())) # TODO -> isto pode ser melhorado for pipe in pipes: pipe.draw(screen) # verify which players are already dead for p in players: if pipe.update(p): dead_players.append(p) p.brain.set_punctuation(score) players.remove(p) dead_players = list(set(dead_players)) # Check if pipe is off the screen or if it has passed the bird if pipe.x + pipe.width < 0: pipes.remove(pipe) elif pipe.passed is False and pipe.x + pipe.width < screen.get_width() / 6 - 15: score = score + 1 pipe.passed = True font = pygame.font.Font(None, 36) text = font.render(str(score), 1, (255, 255, 255)) text_pos = text.get_rect(centerx = screen.get_width() / 2) screen.blit(text, text_pos) text = font.render(str(generation_number), 1, (255, 255, 255)) text_gen = text.get_rect(centerx = screen.get_width() - 10) screen.blit(text, text_gen) # implementation of ml for player in players: horizontal_distance = pipes[0].x - player.position[0] vertical_distance = pipes[0].top - player.position[1] if player.brain.decision(horizontal_distance, vertical_distance, player.position[1]): player.handleKeys() if len(dead_players) == POPULATION_SIZE: new_brains = crossover( [dp.brain for dp in dead_players], 3, screen.get_width(), screen.get_height(), MUTATION_RATE ) players = [ Bird( 15, screen.get_width(), screen.get_height(), nb ) for nb in new_brains ] generation_number += 1 # TODO -> código repetido dead_players = [] pipes = [Pipe(screen.get_width(), screen.get_height())] score = 0 delta_time = time.time() pygame.display.update() clock.tick(60)
import variables from genetic_algorithm import (crossover, fitness, get_initial_generation, get_next_generation, get_total_distance, mutation) from utils import get_named_route, reached_stability from variables import GENERATION_MAX generation = get_initial_generation() print('#gen\tmin\tavg') for cnt in range(GENERATION_MAX): generation = fitness(generation) print( f'{cnt}\t{get_total_distance(generation[0])}\t{variables.avg_distance}' ) if reached_stability(generation): break new_individuals = crossover(generation) new_individuals.extend(mutation(generation)) if cnt != GENERATION_MAX - 1: generation = get_next_generation(generation, new_individuals) print('best route:') print(get_named_route(generation[0])) print('with total distance:', get_total_distance(generation[0]))
print("Generation : ", generation) # Measing the fitness of each chromosome in the population. # fitness = genetic_algorithm.pop_fitness(stock_values, new_population, T, lamda, alpha, epsilon, delta, # gamma) # Selecting the best parents in the population for mating. parents1 = genetic_algorithm.select_mating_pool( stock_values, fit_population, index, num_parents_mating, T, lamda, alpha, epsilon, delta, gamma) parents2 = genetic_algorithm.select_mating_pool_unfit( stock_values, unfit_population, num_parents_mating, T, current_portofolio, starting_time, epsilon, delta, gamma) # Generating next generation using crossover. offspring_crossover_fit, offspring_crossover_unfit = genetic_algorithm.crossover( parents1, parents2, index, stock_values, T, lamda, alpha, epsilon, delta, gamma, current_portofolio, starting_time, Kappa) # Adding some variations to the offspring using mutation. offspring_mutation_fit = genetic_algorithm.mutation( offspring_crossover_fit, sigma, mutation_chance, Kappa) offspring_mutation_unfit = genetic_algorithm.reverse_mutation( offspring_crossover_unfit, sigma, mutation_chance) if generation % 5 == 0 and generation > 0: sigma = max( 0, sigma * float((numpy.exp( genetic_algorithm.gaussian_mutation(0, 1))))) # Creating the new population based on the parents and offspring.