def restart(): global pipes,birds,generation,savedBirds,pipeScore pipes = [Pipe()] pipeScore = 0 ''' #All this code has been superceded by the GA module birds = bestBirds[::] savedBirds.sort(key=Bird.get_score) l = len(savedBirds) if l > 10: for i in range(1,10): newBird = savedBirds[-i] #highest score for i in range(int(NUM_BIRDS/4.0)): mutant = Bird(newBird.brain) mutant.brain.mutate(0.1) birds.append(mutant) for bird in savedBirds: newBird = bird for i in range(int(NUM_BIRDS / 4.0)): mutant = Bird(newBird.brain) mutant.brain.mutate(0.1) birds.append(mutant) for i in range(int(NUM_BIRDS)): birds.append(Bird()) savedBirds = [] if len(birds) > NUM_BIRDS: birds = birds[:NUM_BIRDS]''' ga.nextGeneration(NUM_BIRDS, savedBirds,birds,bestBirds) savedBirds = [] generation += 1 time.sleep(1)
def train() : global players, bestPlayer, gen, pendingThread threads = [] print('Train') for i in range(0, len(players)) : save(players[i].brain, "score\\" + str(i) + ".json") players.clear() p = ThreadPool(MAX_POPULATION) p.map(launchGame, range(MAX_POPULATION)) maxScore = bestPlayer.score if bestPlayer != None else 0 for player in players : if player.score > maxScore : maxScore = player.score bestPlayer = player if bestPlayer != None : save(bestPlayer.brain, "best.json") if len(players) > 0 : gen += 1 players = ga.nextGeneration(players)
def evolve(): #use the global variables global num_of_gens global nets global POPSIZE print(num_of_gens) #if it is the first generation, create the first nets if num_of_gens == 0: nets.append([]) for i in range(POPSIZE): nets[0].append(nn.NeuralNetwork([[3], [32], [16], [1]])) #loop through all of the nets of the current generation for net in nets[num_of_gens]: net.score = evaluateNetwork(net) #print(str(nets[num_of_gens].index(net)) + ' / ' + str(POPSIZE)) #get ready to spawn the next generation nets.append([]) #make the probabilities for the networks to be picked nets[num_of_gens + 1].extend(ga.nextGeneration(nets[num_of_gens])) #increment the number of generations num_of_gens += 1
for cycle in range(cycles): if counter % 60 == 0: pipes.append(pipe.Pipe(screen)) counter += 1 for p in reversed(pipes): p.update() for b in birds: if p.hit(b): saved_birds.append(b) birds.remove(b) if len(birds) == 0: birds = ga.nextGeneration(screen, birds, POPULATION, saved_birds) saved_birds = [] pipes = [] current_score = 0 counter = 0 score_surface = myfont.render(f'Score: {current_score}', True, (255, 255, 255)) pipes.append(pipe.Pipe(screen)) for bird in birds: bird.think(pipes) bird.update() for p in pipes: if p.offscreen(): pipes.remove(p)
currentPipe.setX(currentPipe.getX() - 2) # only ever check hits and have bird worry about the closest pipe... BIG SPEED UP if pipe == closest_pipe: for bird in birds: if (currentPipe.hits(bird)): savedBirds.append( bird) # add bird for saving before we delete it birds.remove(bird) all_sprites.remove(bird) else: bird.think(pipesOnScreen) # reset game and create next generation of birds if not birds: birds = nextGeneration(totalBirds, savedBirds) savedBirds = [] all_sprites.add(birds) # add new birds to sprite group current_generation += 1 score = 0 pipesOnScreen = [ ] # remove pipes off of screen, like resetting game #update sprites all_sprites.update() """ begin drawing stuff... excluding pipes because it's stuck where it is """ if slider.hit: slider.move() slider.draw(screen)
# When all bots die if len(bots) == 0 and not show_best: # Array of all scores this generation all_scores = [bot.score for bot in saved_bots] avg_score = math.floor(sum(all_scores) / len(all_scores)) # Historical avg scores past_gen_scores.append(avg_score) # Store best ever score if max(all_scores) > best_score: best_score = max(all_scores) best_bot = saved_bots[all_scores.index(best_score)] # print(past_gen_scores) # Set up new generation bots = ga.nextGeneration(saved_bots, SCREEN_HEIGHT, DISPLAYSURF, WHITE) saved_bots.clear() pipes.clear() frame_no = 0 generation += 1 train_or_best = "TRAINING" # If show best is on, create a new bird with the best bird's brain and reset elif show_best and generation > 1: saved_bots = [bot for bot in saved_bots] + [bot for bot in bots] bots = [bird.Bird(SCREEN_HEIGHT, DISPLAYSURF, WHITE)] # Append all alive bots to saved bots bots[0].brain = best_bot.brain pipes.clear() frame_no = 0 generation = 0