Beispiel #1
0
 def genetic_algorithm(self, population_size, generations, mutation):
     population = Population(self.estimate_all_ages,
                             n=population_size,
                             mutation=mutation,
                             params_size=self.params.size)
     for i in range(generations):
         population.evolve()
     with open(
             "ga_" + str(population_size) + "_" + str(generations) + "_" +
             str(mutation) + ".log", "w+") as file:
         print("#generation, best_fitness_of_generation, best_params",
               file=file)
         for i in range(generations):
             print(str(i + 1) + " " + str(population.best_fitnesses[i]) +
                   " " + get_nice_string(population.best_params[i]),
                   file=file)
Beispiel #2
0
class Game:
    def __init__(self):
        # layers used for neural network
        self.layers = (2, 8, 8, 8, 3)
        # initializing population for genetic algorithm
        self.population = Population(layers=self.layers)
        # initializing openai gym environment for cartpole game
        self.env = gym.make('MountainCar-v0')
        self.episode_threshold = 300

    def loop(self):
        """ loops infinitely for training """
        high_score = -999999  # used to store high score in training so far
        # learn infinitely
        while True:
            # iterating individuals in population
            for individual in self.population.individuals:
                X = self.env.reset()  # Feature vector
                done = False  # flag set by gym when game is over
                episode_length = 0

                while not done and episode_length < self.episode_threshold:
                    # self.env.render()
                    y = individual.nn.feed_forward(X)
                    X, reward, done, info = self.env.step(argmax(y))
                    individual.score = X[0]
                    episode_length += 1

                # save model when new high score is achieved
                if individual.score > high_score:
                    data = {
                        'score': individual.score,
                        'number_of_layers': individual.nn.number_of_layers,
                        'weights': individual.nn.weights,
                    }
                    # write to file
                    with open('model.pkl', 'wb') as f:
                        pickle.dump(data, f)
                    high_score = individual.score

            self.population.evolve()
def main():
    max_generations = 100
    max_not_improved = 10

    # Creating initial population
    pop = Population( size = 100,
            crossover = 0.4,
            mutation = 0.05,
            elitism = 0.05,
            imigration = 0.3,
            tournament_size = 10,
            debug = False)

    # Generations without improvements
    no_improvements = 0
    for i in range(max_generations):
        #print 'Generation %d' %(i)

        print 'Calculating fitness'
        pop.evaluate()
        pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
        else:
            no_improvements = 0

        if no_improvements == max_not_improved:
            break

        print 'Evolving'
        pop.evolve()
        print

    if no_improvements == max_not_improved:
        print '%d generations without improvement' % (max_not_improved)

    print 'Best solution:'
    pop.show_first()
Beispiel #4
0
def main():
    max_generations = 100
    max_not_improved = 10

    # Creating initial population
    pop = Population( size = 100,
            crossover = 0.4,
            mutation = 0.05,
            elitism = 0.05,
            imigration = 0.3,
            tournament_size = 10,
            debug = False)

    # Generations without improvements
    no_improvements = 0
    for i in range(max_generations):
        print 'Generation %d' % (i+1)

        print 'Calculating fitness'
        pop.evaluate()
        pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
        else:
            no_improvements = 0

        if no_improvements == max_not_improved:
            break

        print 'Evolving'
        pop.evolve()
        print

    if no_improvements == max_not_improved:
        print '%d generations without improvement' % (max_not_improved)

    print 'Best solution:'
    pop.show_first()
Beispiel #5
0
class Game:
    def __init__(self):
        # layers used for neural network
        self.layers = (4, 64, 64, 64, 1)
        # initializing population for genetic algorithm
        self.population = Population(layers=self.layers)
        # initializing openai gym environment for cartpole game
        self.env = gym.make('CartPole-v0')
    
    def loop(self):
        """ loops infinitely for training """
        high_score = 0                          # used to store high score in training so far
        # learn infinitely
        while True:
            # iterating individuals in population
            for individual in self.population.individuals:
                X = self.env.reset()            # Feature vector
                done = False                    # flag set by gym when game is over

                while not done:
                    # self.env.render()
                    y = 1 if individual.nn.feed_forward(X)[0] > 0.5 else 0
                    X, reward, done, info = self.env.step(y)
                    individual.score += reward
                
                # save model when new high score is achieved
                if individual.score > high_score:
                    data = {
                        'score' : individual.score,
                        'number_of_layers' : individual.nn.number_of_layers,
                        'weights' : individual.nn.weights,
                    }
                    # write to file
                    with open('model.pkl', 'wb') as f:
                        pickle.dump(data, f)
                    high_score = individual.score
            
            self.population.evolve()
Beispiel #6
0
class Game:
    __instance__ = None

    def get_instance():
        if Game.__instance__ == None:
            Game()
        return Game.__instance__

    def __init__(self):
        self.frame_rate = 10000  # FPS
        self.frames = 0  # counts number of frames elapsed
        self.exit = False  # Flag to exit the game
        self.pause = False
        self.manual = False
        self.screen_on = True
        title = 'Snake'  # Window Title
        icon_filename = 'res/icon.png'

        # loads pygame modules
        pygame.init()

        # initializing game objects
        self.population = Population(layers=(12, 16, 16, 3))

        # screen params
        icon = pygame.image.load(icon_filename)
        pygame.display.set_icon(icon)
        pygame.display.set_caption(title)
        self.screen = pygame.display.set_mode(Ground().get_dimensions())

        # reference clock
        self.clock = pygame.time.Clock()

        Game.__instance__ = self

    def loop(self):
        while not self.exit:
            for individual in self.population.individuals:
                snake = individual.snake
                while not self.exit and not snake.game_over:
                    # capture user input
                    self.capture_input(snake)

                    if self.pause:
                        continue

                    # update objects for each frame
                    self.update_objects(snake)

                    # if game over skip
                    if snake.game_over:
                        distance_from_food = snake.delta_distance
                        snake.score -= distance_from_food
                        break

                    # get feature vector
                    X = snake.get_features()

                    # output of feed forward of neural network
                    y = ravel(individual.nn.feed_forward(X))
                    snake.respond(y)

                    if self.screen_on:
                        # draw objects on screen
                        self.draw_objects(snake)
                # print(individual.nn.weights)
            self.population.evolve()

    def update_objects(self, snake):
        snake.update()

    def capture_input(self, snake):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.exit = True
            elif event.type == pygame.KEYDOWN:
                if self.manual:
                    if event.key == pygame.K_UP:
                        snake.move(Direction.UP)
                    elif event.key == pygame.K_DOWN:
                        snake.move(Direction.DOWN)
                    elif event.key == pygame.K_LEFT:
                        snake.move(Direction.LEFT)
                    elif event.key == pygame.K_RIGHT:
                        snake.move(Direction.RIGHT)

                # keys to control frame rate
                # u: 10000
                # i: 100
                # o: 10
                # p: 1
                if event.key == pygame.K_u:
                    self.frame_rate = 10000
                if event.key == pygame.K_i:
                    self.frame_rate = 100
                if event.key == pygame.K_o:
                    self.frame_rate = 10
                if event.key == pygame.K_p:
                    self.frame_rate = 1

                # on pressing q current snake's game is over
                if event.key == pygame.K_q:
                    snake.game_over = True

                # s: draws screen
                # a: stops drawing on screen
                if event.key == pygame.K_a:
                    self.screen_on = False
                if event.key == pygame.K_s:
                    self.screen_on = True

                # k: toggles pause
                if event.key == pygame.K_k:
                    self.pause = not self.pause

    def draw_objects(self, snake):
        self.screen.fill(Color.BLACK)
        snake.draw(self.screen)
        pygame.display.update()
        self.clock.tick(self.frame_rate)
Beispiel #7
0
def main(filename=None, ports=[5000]):
    if not filename:
        print 'Using default parameters'
        params = {
            'max_generations': 200,
            'max_not_improved': 20,
            'size': 100,
            'crossover': 0.3,
            'mutation': 0.05,
            'elitism': 0.2,
            'imigration': 0.2,
            'tour_size': 8,
            'local': None
        }
    else:
        print 'Loading parameters from %s\n' % (filename)
        params = util.parse_json(filename)

    # Creating initial population
    pop = Population(size=params['size'],
                     crossover=params['crossover'],
                     mutation=params['mutation'],
                     elitism=params['elitism'],
                     imigration=params['imigration'],
                     tour_size=params['tour_size'],
                     local=params['local'])

    sockets = []
    for port in ports:
        sock = util.open_socket(int(port))
        sockets.append(sock)
    Chromo.sockets = sockets

    Chromo.cross_op = params['cross_op']

    # Generations without improvements
    no_improv = 0
    for i in range(params['max_generations']):
        print 'Generation %d' % (i + 1)

        print 'Calculating fitness'
        for sock in Chromo.sockets:
            util.send_msg(sock, 'NEWGEN\n')
        best = pop.evaluate()
        for sock in Chromo.sockets:
            util.send_msg(sock, 'ENDGEN\n')
        print 'main:current_best ', best
        #pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
            if no_improvements == params['max_not_improved']:
                print 'Reached limit for not improved generations'
                break
        else:
            no_improvements = 0

        print 'Evolving'
        pop.evolve()
        print

    print '\nBest solution:'
    #pop.show_first()
    print 'Fitness: ', best[0]
    print 'Genes: ', Chromo.to_str(best[1]), '\''

    # Closing connection
    for sock in Chromo.sockets:
        util.send_msg(sock, 'ENDGA\n')
        sock.close()