def nextGen(self): cGen = len(self.generations) - 1 nGen = cGen + 1 scores = [] i = 0 for genome in self.generations[cGen].genomes: scores.append((i, genome.score)) i += 1 scores = np.sort(np.array(scores, dtype=[('index', int), ('score', int)]), order='score') elite = list(reversed([x[0] for x in scores[30:40]])) self.generations[cGen].elite = elite self.generations.append(generation.Generation(nGen)) for i in range(40): if i < 5: self.generations[nGen].genomes[i].cross( self.generations[cGen].genomes[elite[i]], self.generations[cGen].genomes[elite[i]]) else: mum = np.random.randint(10) dad = np.random.randint(10) self.generations[nGen].genomes[i].cross( self.generations[cGen].genomes[elite[mum]], self.generations[cGen].genomes[elite[dad]]) self.generations[nGen].genomes[i].mutate()
def create_geneology(self): # create GENERATIONS generations for i in tqdm(range(self.parameters.GENERATIONS)): # create an empty generation gen = generation.Generation(i) if i == 0: self.populate_first_generation(gen) else: # fill the generation (create children from parents in previous generations) self.populate_generation(gen) self.add_generation(gen)
import generation import numpy as np X = np.array([ [0, 0], [0, 1], [1, 0], [1, 1], ]) y = np.array([ [0], [1], [1], [0], ]) G = generation.Generation(X, y, pop_size=4) G.calc_fitness() for i in range(4): G.next_generation() G.calc_fitness() G.print_gen_info()
def __init__(self): self.generations = [generation.Generation(0)] self.generations[0].initialGeneration()
def __init__(self, root_dir): self.root = root_dir self.percept = perception.Perception(self.root) self.generate = generation.Generation(self.root)
def main(self): """ Instances generation 0, evolves the population, plots values.""" self.pop = generation.Generation(self.X, self.y, pop_size=self.size) self.evolve() self.print_best_net() self.plot_results()
def main(self): """ Instantiates generation 0, evolves the population, plots fitness values.""" self.pop = generation.Generation(self.data, self.exp, self.size) self.evolve() self.print_best_net() self.plot_results()
def main(): game = Game() snake = Snake(game) if len(sys.argv) == 1: print "Running in regular mode" else: if sys.argv[1] == "watch": args = {} for arg in sys.argv[2:]: split = arg.split('=') args[split[0]] = split[1] if "name" not in args: args["name"] = "." snake.brain.ihBias = pickle.load(open("trained_model/"+args["name"]+"/ihBias.pb", "rb")) snake.brain.hhBias = pickle.load(open("trained_model/"+args["name"]+"/hhBias.pb", "rb")) snake.brain.hoBias = pickle.load(open("trained_model/"+args["name"]+"/hoBias.pb", "rb")) snake.brain.ihWeights = pickle.load(open("trained_model/"+args["name"]+"/ihWeights.pb", "rb")) snake.brain.hhWeights = pickle.load(open("trained_model/"+args["name"]+"/hhWeights.pb", "rb")) snake.brain.hoWeights = pickle.load(open("trained_model/"+args["name"]+"/hoWeights.pb", "rb")) pygame.init() win = pygame.display.set_mode((720, 480)) pygame.display.set_caption("Snake Neural Network - Watching") clock = pygame.time.Clock() snake.brain.play(win, True) pygame.quit() return elif sys.argv[1] == "train": args = {} for arg in sys.argv[2:]: split = arg.split('=') args[split[0]] = split[1] if "name" not in args: args["name"] = datetime.datetime.now().strftime("%Y%m%d%H%M%S") if "pop" not in args: args["pop"] = 80 else: args["pop"] = int(args["pop"]) if "gen" not in args: args["gen"] = 1000 else: args["gen"] = max(int(args["gen"]), 4) if "display" not in args: args["display"] = 0 else: args["display"] = int(args["display"]) gen = generation.Generation(args["pop"], util.DEFAULT_INPUT_SIZE, util.DEFAULT_HIDDEN_SIZE , util.DEFAULT_OUTPUT_SIZE) for _ in range(args["gen"]): gen.train(args["name"], None) gen.saveBestGenomes() gen.mutate() print("Training completed") return pygame.init() win = pygame.display.set_mode((720, 480)) pygame.display.set_caption("Snake Neural Network - Regular") clock = pygame.time.Clock() while not game.over: clock.tick(20) movedirection = snake.direction for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: movedirection = 27 elif event.key == pygame.K_RIGHT: movedirection = 26 elif event.key == pygame.K_UP: movedirection = 24 elif event.key == pygame.K_DOWN: movedirection = 25 if game.board[util.positionOnceTowardsDirection(snake.cells[0], game, movedirection)] == 2: print("Snake's score is now {}, length {}".format(game.score+100, snake.length+3)) snake.move(movedirection) win.fill((0,0,0)) game.display(win) pygame.display.update() print("You ended with a score of {}".format(game.score)) pygame.quit() sys.exit()