def getbest(run): g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 0, substrate.GetMinCPPNOutputs(), False, NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.TANH, 0, params) pop = NEAT.Population(g, params, True, 1.0, run) for generation in range(1000): # Evaluate genomes genome_list = NEAT.GetGenomeList(pop) fitnesses = EvaluateGenomeList_Serial(genome_list, evaluate_xor, display=False) [ genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses) ] print('Gen: %d Best: %3.5f' % (generation, max(fitnesses))) # Print best fitness # print("---------------------------") # print("Generation: ", generation) # print("max ", max([x.GetLeader().GetFitness() for x in pop.Species])) # Visualize best network's Genome net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((500, 500, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 500, 500), net) cv2.imshow("CPPN", img) # Visualize best network's Pheotype net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildESHyperNEATPhenotype( net, substrate, params) img = np.zeros((500, 500, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 500, 500), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(1) if max(fitnesses) > 15.0: break # Epoch generations = generation pop.Epoch() return generations
def getbest(): g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params) pop = NEAT.Population(g, params, True, 1.0) generations = 0 for generation in range(1000): genome_list = NEAT.GetGenomeList(pop) fitness_list = NEAT.EvaluateGenomeList_Serial(genome_list, evaluate, display=False) NEAT.ZipFitness(genome_list, fitness_list) best = max([x.GetLeader().GetFitness() for x in pop.Species]) # print 'Best fitness:', best, 'Species:', len(pop.Species) # test net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net) cv2.imshow("nn_win", img) cv2.waitKey(1) pop.Epoch() # print "Generation:", generation generations = generation if best > 15.5: break return generations
def getbest(): g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 0, substrate.GetMinCPPNOutputs(), False, NEAT.ActivationFunction.SIGNED_GAUSS, NEAT.ActivationFunction.SIGNED_GAUSS, 0, params) pop = NEAT.Population(g, params, True, 1.0) for generation in range(1000): genome_list = NEAT.GetGenomeList(pop) # fitnesses = NEAT.EvaluateGenomeList_Parallel(genome_list, evaluate) fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list, evaluate, display=False) [ genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses) ] best = max([x.GetLeader().GetFitness() for x in pop.Species]) # print 'Best fitness:', best # test net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net) cv2.imshow("CPPN", img) net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildHyperNEATPhenotype(net, substrate) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(1) pop.Epoch() # print "Generation:", generation generations = generation if best > 15.5: break return generations
def evaluate_xor(genome): net = NEAT.NeuralNetwork() genome.BuildHyperNEATPhenotype(net, substrate) # nn=genome.BuildHyperNEATPhenotype(net, substrate) # error = 0 # depth = 5 # do stuff and return the fitness # net.Flush() net = NEAT.NeuralNetwork() genome.BuildPhenotype(net) img = np.zeros((400, 400, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 400, 400), net) cv2.imshow("CPPN", img) # Visualize best network's Pheotype net = NEAT.NeuralNetwork() genome.BuildESHyperNEATPhenotype(net, substrate, params) img = np.zeros((800, 800, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 800, 800), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(33) subprocess.call('./autostart.sh',shell=True) # print("b") # time.sleep(1) # print("c") main(MyDriver(net=net)) # print("d") with open("mydata.txt",'r') as f: fitt=f.read() # os.system('pkill torcs') subprocess.call('./autostop.sh',shell=True) # print("fitness ******************* ",fitt) return float(fitt)
def getbest(): g = NEAT.Genome(0, 7, 1, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, params) pop = NEAT.Population(g, params, True, 1.0) for generation in range(2000): genome_list = NEAT.GetGenomeList(pop) # fitnesses = NEAT.EvaluateGenomeList_Parallel(genome_list, evaluate) fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list, evaluate_xor, display=True) [ genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses) ] best = max([x.GetLeader().GetFitness() for x in pop.Species]) net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((500, 500, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 500, 500), net) cv2.imshow("CPPN", img) net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().Build_ES_Phenotype(net, substrate, params) img = np.zeros((500, 500, 3), dtype=np.uint8) img += 10 utilities.DrawPhenotype(img, (0, 0, 500, 500), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(1) generations = generation if best > 15.0: break pop.Epoch() return generations
def getbest(): g = NEAT.Genome(0, 3, 2, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 1, params) pop = NEAT.Population(g, params, True, 1.0) pool = mpc.Pool(processes=4) generations = 0 for generation in range(1000): genome_list = [] for s in pop.Species: for i in s.Individuals: genome_list.append(i) # for g in genome_list: # f = evaluate(g) # g.SetFitness(f) # Parallel processing fits = pool.map(evaluate, genome_list) for f, g in zip(fits, genome_list): g.SetFitness(f) best = max([x.GetLeader().GetFitness() for x in pop.Species]) # print 'Best fitness:', best, 'Species:', len(pop.Species) # test net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net) cv2.imshow("nn_win", img) cv2.waitKey(1) pop.Epoch() # print "Generation:", generation generations = generation if best > 15.5: break return generations
def main(): pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption("NEAT volleyball") ### Physics stuff space = pm.Space() space.gravity = Vec2d(0.0, -500.0) # walls - the left-top-right walls body = pm.Body() walls= [pm.Segment(body, (50, 50), (50, 1550), 10) ,pm.Segment(body, (50, 1550), (560, 1550), 10) ,pm.Segment(body, (560, 1550), (560, 50), 10) ] floor = pm.Segment(body, (50, 50), (560, 50), 10) floor.friction = 1.0 floor.elasticity = 0.0 floor.collision_type = collision_type_floor for s in walls: s.friction = 0 s.elasticity = 0.99 s.collision_type = collision_type_wall space.add(walls) space.add(floor) params = NEAT.Parameters() params.PopulationSize = 250 params.DynamicCompatibility = True params.AllowClones = True params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeTreshold = 35 params.MinSpecies = 3 params.MaxSpecies = 10 params.RouletteWheelSelection = True params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.01 params.MutateAddLinkProb = 0.05 params.MutateRemLinkProb = 0.00 rng = NEAT.RNG() rng.TimeSeed() #rng.Seed(0) g = NEAT.Genome(0, 6, 0, 2, False, NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params) pop = NEAT.Population(g, params, True, 1.0) best_genome_ever = None fast_mode = False for generation in range(1000): print "Generation:", generation now = time.time() genome_list = [] for s in pop.Species: for i in s.Individuals: genome_list.append(i) print 'All individuals:', len(genome_list) for i, g in enumerate(genome_list): total_fitness = 0 for trial in range(20): f, fast_mode = evaluate(g, space, screen, fast_mode, rnd.randint(80, 400), rnd.randint(-200, 200), rnd.randint(80, 400)) total_fitness += f g.SetFitness(total_fitness / 20) print best = max([x.GetLeader().GetFitness() for x in pop.Species]) print 'Best fitness:', best, 'Species:', len(pop.Species) # Draw the best genome's phenotype net = NEAT.NeuralNetwork() best_genome_ever = pop.Species[0].GetLeader() best_genome_ever.BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net ) cv2.imshow("current best", img) cv2.waitKey(1) if best >= 10000: break # evolution is complete if an individual keeps the ball up for that many timesteps #if pop.GetStagnation() > 500: # break print "Evaluation took", time.time() - now, "seconds." print "Reproducing.." now = time.time() pop.Epoch() print "Reproduction took", time.time() - now, "seconds." # Show the best genome's performance forever pygame.display.set_caption("Best genome ever") while True: evaluate(best_genome_ever, space, screen, False, rnd.randint(80, 400), rnd.randint(-200, 200), rnd.randint(80, 400))
def evaluate(genome, space, screen, fast_mode, start_x, start_vx, bot_startx): # Setup the environment clock = pygame.time.Clock() # The agents - the brain and the ball net = NEAT.NeuralNetwork() genome.BuildPhenotype(net) agent = NN_agent(space, net, bot_startx) ball = Ball(space, start_x, start_vx) space.add_collision_handler(collision_type_nn, collision_type_floor, agent.touch_floor, None, None, agent.leave_floor) space.add_collision_handler(collision_type_ball, collision_type_floor, ball.touch_floor, None, None, ball.leave_floor) tstep = 0 avg_ball_height = 0 while tstep < max_timesteps: tstep += 1 for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == KEYDOWN and event.key == K_ESCAPE: exit() elif event.type == KEYDOWN and event.key == K_f: fast_mode = not fast_mode elif event.type == KEYDOWN and event.key == K_LEFT and not fast_mode: ball.body.velocity = (ball.body.velocity[0] - 200, ball.body.velocity[1]) elif event.type == KEYDOWN and event.key == K_RIGHT and not fast_mode: ball.body.velocity = (ball.body.velocity[0] + 200, ball.body.velocity[1]) elif event.type == KEYDOWN and event.key == K_UP and not fast_mode: ball.body.velocity = (ball.body.velocity[0], ball.body.velocity[1] + 200) ### Update physics dt = 1.0/50.0 space.step(dt) # The NN interacts with the world on each 20 timesteps if (tstep % 20) == 0: agent.interact(ball) avg_ball_height += ball.body.position[1] # stopping conditions if not ball.in_air: break #if abs(agent.body.velocity[0]) < 50: # never stop on one place! # break if not fast_mode: # draw the phenotype img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net ) cv2.imshow("current best", img) cv2.waitKey(1) ## Draw stuff screen.fill(THECOLORS["black"]) ### Draw stuff draw(screen, space) ### Flip screen pygame.display.flip() clock.tick(50) fitness = tstep #+ avg_ball_height/tstep if ball.body.position[1] < 0: fitness = 0 # remove objects from space space.remove(agent.shape, agent.body) space.remove(ball.shape, ball.body) # print 'Genome ID:', genome.GetID(), 'Fitness:', tstep # the fitness is the number of ticks passed return fitness, fast_mode
import matplotlib.pyplot as plt sim = NEAT(PopulationSize=300, verbose=True) sim.run() genome_list = mneat.GetGenomeList(sim.pop) # Visualize the best network's genome net = mneat.NeuralNetwork() current_genome = sim.pop.Species[0].GetLeader() sim.pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((500, 500, 3), dtype=np.uint8) img += 10 mneat.DrawPhenotype(img, (0, 0, 500, 500), net) plt.imshow(img) plt.show() # # Pick out the genome # current_genome = genome_list[0] # Make a substrate meshgrid resolution = 100 x = np.linspace(-1, 1, resolution) y = np.linspace(-1, 1, resolution) xv, yv = np.meshgrid(x, y) response = [] for i in range(len(xv)):
# fitnesses, elapsed = NEAT.EvaluateGenomeList_Parallel(genome_list, evaluate, 4) fitnesses, elapsed = NEAT.EvaluateGenomeList_Serial(genome_list, evaluate) [ genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses) ] print 'Best fitness:', max( [x.GetLeader().GetFitness() for x in pop.Species]) # test net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net) cv2.imshow("CPPN", img) net = NEAT.NeuralNetwork() pop.Species[0].GetLeader().BuildHyperNEATPhenotype(net, substrate) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(1) pop.Epoch() print "Generation:", generation cv2.waitKey(1)
def run(self): params = NEAT.Parameters() params.PopulationSize = self.args.population_size params.AllowClones = self.args.allow_clones params.MutateAddNeuronProb = self.args.add_neuron_probability params.MutateAddLinkProb = self.args.add_link_probability params.MutateRemLinkProb = self.args.remove_link_probability params.MutateRemSimpleNeuronProb = self.args.remove_simple_neuron_probability params.TimeConstantMutationMaxPower = 1.0 params.MutateNeuronTimeConstantsProb = 0.2 params.MutateNeuronBiasesProb = 0.03 params.MinNeuronTimeConstant = 1.0 params.MaxNeuronTimeConstant = 2.0 params.MinNeuronBias = -1.0 params.MaxNeuronBias = 1.0 params.Elitism = 0.1 # fraction of population num_inputs = BeerTrackerGenotype.num_input_nodes + 1 # always add one extra input, see http://multineat.com/docs.html num_hidden_nodes = 2 num_outputs = 2 genome = NEAT.Genome( 0, # ID num_inputs, num_hidden_nodes, num_outputs, self.args.fs_neat, NEAT.ActivationFunction.TANH, # OutputActType NEAT.ActivationFunction.UNSIGNED_SIGMOID, # HiddenActType 0, # SeedType params # Parameters ) pop = NEAT.Population( genome, params, True, # whether the population should be randomized 2.0, # how much the population should be randomized, self.args.seed ) for generation in range(1, self.args.num_generations + 1): print '--------------------------' generation_start_time = time.time() print('generation {}'.format(generation)) # retrieve a list of all genomes in the population genotypes = NEAT.GetGenomeList(pop) genotype_fitness_values = [] for genotype in genotypes: fitness = self.evaluate(genotype, generation) genotype_fitness_values.append( (fitness, genotype) ) genotype.SetFitness(fitness) genotype_fitness_values.sort() flat_fitness_list = [x[0] for x in genotype_fitness_values] max_fitness = flat_fitness_list[-1] min_fitness = flat_fitness_list[0] avg_fitness = statistics.mean(flat_fitness_list) fitness_std_dev = statistics.pstdev(flat_fitness_list) stats_item = { 'generation': generation, 'min_fitness': min_fitness, 'max_fitness': max_fitness, 'avg_fitness': avg_fitness, 'fitness_std_dev': fitness_std_dev, } self.log.append(stats_item) pprint.pprint(stats_item) if self.args.visualize and generation % self.args.visualize_every == 0: net = NEAT.NeuralNetwork() genotype_fitness_values[-1][1].BuildPhenotype(net) # build phenotype from best genotype img = np.zeros((500, 500, 3), dtype=np.uint8) NEAT.DrawPhenotype(img, (0, 0, 500, 500), net) cv2.imshow("NN", img) cv2.waitKey(1) nn = neat_net_wrapper.NeatNetWrapper(net) seed = generation bt = BeerTracker( nn=nn, seed=seed ) bt.gfx = self.beer_tracker_gfx bt.run() print bt.world.agent.num_small_misses, 'small miss(es)' print bt.world.agent.num_large_misses, 'large miss(es)' print bt.world.agent.num_partial_captures, 'partial capture(s)' print bt.world.agent.num_small_captures, 'small capture(s)' print bt.world.agent.num_large_captures, 'large capture(s)' net.Save('best_neat_net.txt') # advance to the next generation pop.Epoch() print("Generation execution time: %s seconds" % (time.time() - generation_start_time))