def eval_genomes(genomes, config): # Play game and get results idx, genomes = zip(*genomes) MyNeatFlappy = FlappyBirdApp(genomes, config) MyNeatFlappy.play() results = MyNeatFlappy.crash_info # Calculate fitness and top score top_score = 0 for result, genomes in results: score = result['score'] distance = result['distance'] energy = result['energy'] fitness = score * 3000 + 0.2 * distance - energy * 1.5 if fitness == 0: genomes.fitness = -1 else: genomes.fitness = fitness if top_score < score: top_score = score #print score print('The top score was:', top_score)
def run_winner(n=1): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'flappy-config') genomes = pickle.load(open('winner.pkl', 'rb')) for i in range(0, n): # Play game and get results flappy_Bio = FlappyBirdApp([genomes], config) flappy_Bio.play()
def eval_genomes(genomes, config): idx, genomes = zip(*genomes) flappy_Bio = FlappyBirdApp(genomes, config) flappy_Bio.play() results = flappy_Bio.crash_info top_score = 0 for result, genomes in results: score = result['score'] distance = result['distance'] energy = result['energy'] fitness = score * 3000 + 0.2 * distance - energy * 1.5 genomes.fitness = -1 if fitness == 0 else fitness if top_score < score: top_score = score print('The top score was:', top_score)
def fitness(self, current_generation): # Play game and get results flappy_Bio = FlappyBirdApp(current_generation) flappy_Bio.play() results = flappy_Bio.crash_info # Calculate fitness self.fitness_values = [] self.network_index_dict = {} for index, result in enumerate(results): network = result['network'] self.network_index_dict[index] = network distance = result['distance'] energy = result['energy'] fitness = distance - energy * 0.5 fitness = -1 if fitness == 0 else fitness self.fitness_values.append((index, fitness))
def fitness(self): """ Fitness ------- Each species in the pool is iterated over. They are passed into the FlappyBird App, and the game is played for all organisms in that species simultaneously. Flappy generates "crash_info", from which we obtain the 1. organism, 2. its expended energy during play, and 3. its distance traveled during play Using the organism's energy and distance, a fitness measure is generated and assigned to the organism """ # Iterate through the species in the list for species in self.species: # ================ PLAY FLAPPYBIRD ==================== flappy = FlappyBirdApp(species, self.generation) flappy.play() # ===================================================== for bird_results in flappy.crash_info: # ====== Results per organism ======= organism = bird_results['network'] energy = bird_results['energy'] distance = bird_results['distance'] # =================================== # ======== INTERFACE WITH VIS ======== print(organism) # =================================== # Determine the organism's fitness # ============ FITNESS FUNCTION ================ fitness = distance - energy * 1.5 # ============================================== # Assign the fitness to the organism organism.fitness = fitness if fitness > 0 else -1.0 # =============== Pool's Max Fitness =============== # First, check if max fitness dict is empty. # If so, assign the first organism and its fitness pool_max_organism = self.max_fitness['organism'] if not pool_max_organism['fitness']: pool_max_organism['object'] = organism pool_max_organism['fitness'] = organism.fitness # Check if current organism has higher fitness than pool's all-time max elif organism.fitness > pool_max_organism['fitness']: pool_max_organism['object'] = organism pool_max_organism['fitness'] = organism.fitness # ================================================== # Set the species intra rank and reorder the species organism list from top rank to lowest rank (0 to x, x being the lowest) species.set_intra_species_rank()