Beispiel #1
0
    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
Beispiel #2
0
 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')
Beispiel #3
0
 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 evolve(world: World, population: Population):

    new_generation = Population(population.population_size, world.gene_length,
                                False, [])
    elitism_number = population.population_size // 2

    #Elitism
    for _ in range(elitism_number):
        fittest = population.getFittest()
        new_generation.addIndividual(fittest)
        population.removeIndividual(fittest)

    #Crossover
    for _ in range(elitism_number, population.population_size):
        parent1 = selection(new_generation, world)
        parent2 = selection(new_generation, world)
        child = crossover(parent1, parent2)
        child.setFitness(
            fitnessFN(world, child.path, world.startX, world.startY,
                      world.foodCount))
        new_generation.addIndividual(child)

    #Mutation
    for i in range(elitism_number, population.population_size):
        mutation(new_generation.individuals[i], world.mutation_rate)

    return new_generation
Beispiel #5
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)
    def initialize():
        # initializes modules required for pygame
        pygame.init()

        # screen parameters
        icon = pygame.image.load(Game.ICON_PATH)
        pygame.display.set_icon(icon)
        pygame.display.set_caption(Game.TITLE)
        screen = pygame.display.set_mode(Game.get_dimensions())

        # Feature limits for neural network's normalization
        feature_limits = array([Game.HEIGHT, 100, Game.HEIGHT, Game.HEIGHT, Game.WIDTH])
        Game.feature_limits = reshape(feature_limits, (1,-1))
        
        # reference clock
        clock = pygame.time.Clock()
        
        # initializing game objects
        population = Population(pop_size=30, feature_limits=Game.feature_limits)    # population of birds
        pipes = []                                                                  # pipes on screen

        # background image
        background_image = pygame.image.load('res/background.png').convert()

        return screen, clock, population, pipes, background_image
Beispiel #7
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 #9
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 #10
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 #11
0
    def initialize():
        pygame.init()

        # screen parameters
        icon = pygame.image.load(Game.ICON_PATH)
        pygame.display.set_icon(icon)
        pygame.display.set_caption(Game.TITLE)
        screen = pygame.display.set_mode(Game.get_dimensions())

        # Feature limits
        feature_limits = array(
            [Game.HEIGHT, 100, Game.HEIGHT, Game.HEIGHT, Game.WIDTH])
        Game.feature_limits = reshape(feature_limits, (1, -1))

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

        # initializing game objects
        population = Population(pop_size=30,
                                feature_limits=Game.feature_limits)
        pipes = []

        return screen, clock, population, pipes
from attribute import Attribute
from dataset import Dataset
from genetic import Population

attributes = [Attribute("score", [x/10 for x in range(-50, 50)]),]

data = [[x/10] for x in range(-50, 50)] * 5

scores = Population(Dataset(attributes, data), lambda i: -i[0]**4 + i[0]**3 + 20*i[0]**2 + i[0]-2)

print("using -x^4+x^3+20x^2+x-2\n")
print("initial",scores)
for i in range(1000):
    scores.generation()
print("final",scores)
print("frequency", scores.frequency())
print("mode",scores.mode())
print("fittest",scores.fittest())
Beispiel #13
0
 def test_selection(self, *args):
     population = Population(self.buildings_template)
     population.selection()
     self.assertEqual(45, len(population.creatures))
Beispiel #14
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)
from genetic import Population

population_size = 100
mutation_rate = 0.01
target_word = "The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge."

p = Population(population_size, mutation_rate, target_word)
while p.get_best().fitness < 1.0:
    print(''.join(p.get_best().genotype))
    p.natural_selection()
print(''.join(p.get_best().genotype))
Beispiel #16
0
 def test_random_population(self):
     population = Population(self.buildings_template)
     self.assertEqual(45, len(population.creatures))
Beispiel #17
0
 def setupWorld(self, world):
     for goal in self.goals:
         population = Population(self.setupPopulation, self.selection,
                                 self.regulation, self.fitness, goal)
         world.populations.append(population)
from attribute import Attribute
from dataset import Dataset
from genetic import Population

def fitness(i):
    return 50 - i[0] ** 2 - i[1] ** 2

i = [x for x in range(-5, 6)]

attributes = [Attribute("x", i), Attribute("y", i)]

data = [[x, y] for x in i for y in i] * 5

final = Population(Dataset(attributes, data), fitness, 1000)

print(final.population)
final.summary()
Beispiel #19
0
import cupy as cp
from genetic import Population

# 生成初始种群,种群有 pop_size 个个体,每个个体有 problem_size 个基因位点
pop_size = 100
problem_size = 12
population = Population.generate_initial_population(pop_size, problem_size)

distance_mat = cp.random.randint(low=1,
                                 high=1000,
                                 size=(
                                     problem_size,
                                     problem_size,
                                 )).astype(cp.float32)

max_evolves_n = 100
for i in range(max_evolves_n):
    population.evolve(distance_mat)
    population.update_chance(distance_mat)
    print("At generation: %s\n Score: %s\n" % (
        str(i),
        population.get_maximum_score(),
    ))
Beispiel #20
0
def main():
	"""Run the evolutionary algorithm"""
	# Command line options
	formatter = IndentedHelpFormatter(indent_increment=4, max_help_position=40, width=80)
	parser = OptionParser(usage="%prog -f INFILE -o OUTDIR [other opts]", 
						  version="%prog 0.1", formatter=formatter)

	# File options
	group = OptionGroup(parser, "File and Directory Options")
	group.add_option("-f", "--file", dest = "filename", 
					  default = None,
					  help = "Input file to evolve to", metavar = "FILE")
	group.add_option("-o", "--outImg", dest = "imgOutputDir", 
					  default = "./out",
					  help = "Directory to save output images in", 
					  metavar = "DIR")
	group.add_option("-p", "--outObj", dest = "objOutputDir", 
					  default = "./obj",
					  help = "Directory to save serialized objects in", 
					  metavar = "DIR")
	group.add_option("-z", "--loadObjs", dest = "loadObjs", 
					  default = False,
					  help = "Load objects from a previous generation?", 
					  metavar = "True")
	parser.add_option_group(group)

	# Individual Options
	group = OptionGroup(parser, "Individual Options",
						"These options affect how individuals evolve")
	group.add_option("-i", "--iGenes", dest = "initGenes", 
					  default = 5,
					  help = "Initial number of genes", metavar = "NUM")
	group.add_option("-m", "--mGenes", dest = "maxGenes", 
					  default = 60,
					  help = "Maximum number of genes", metavar = "NUM")
	group.add_option("-x", "--iMutation", dest = "initMutate", 
					  default = 5,
					  help = "Number of mutations for initial generation", 
					  metavar = "NUM")
	parser.add_option_group(group)

	# Population Options
	group = OptionGroup(parser, "Population Options",
						"These options affect the population dynamics")
	group.add_option("-s", "--iSize", dest = "initSize", 
					  default = 5,
					  help = "Initial size of the population", metavar = "SIZE")
	group.add_option("-l", "--mSize", dest = "maxSize", 
					  default = 40,
					  help = "Maximum size of the population", metavar = "SIZE")
	parser.add_option_group(group)

	# Get args
	(options, args) = parser.parse_args()

	if not options.filename:
		print "Did not supply -f INPUT FILE. Check the help docs with --help."
		return

	print "DEBUG - NUMBER OF GENES SET STATIC!"
	genes = int(options.initGenes)

	# Begin evolution
	P = Population("TODO",
				   options.filename, 
				   options.imgOutputDir, 
				   options.objOutputDir, 
				   loadObjs   = bool(options.loadObjs),
				   initSize	  = int(options.initSize), 
				   maxSize	  = int(options.maxSize),
				   initGenes  = genes,
				   maxGenes	  = genes,
				   initMutate = int(options.initMutate))

	P.runEvolution()
Beispiel #21
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()
from genetic import Population

# population with 100 individuals, 4 traits each
p = Population(100, 4)
# initialize all traits to value 15
p.init_population(15)
# set targets for each trait
p.set_targets([10, 20, 15, 25])
# run 100 generations
p.run_generations(100)
# plot the history of the traits on a 2x2 grid
p.plot_history((2,2))
import sys
import ast
import json
import test
from genetic import Population

if __name__ == '__main__':
    argu1 = ast.literal_eval(sys.argv[1])
    #argu1 = sys.argv[1]
    #argu2 = ast.literal_eval(sys.argv[2])
    #print(argu1)
    path = '/common/users/sl1560/temp/'
    name = 'aStar+120+0.1+500.pkl'
    p = Population(**argu1)
    pp = test.loadMaze(path, name)
    for i in range(1):
        p.replaceInitialMazes(i, pp[i])
        print('changed', pp[i].score)
    finalchild, finalPopulation = p.iterate()
    p.save()
    #finalchild.visualize()
    #p = Population(**sys.argv[1])
    #arguments = sys.argv[1].replace("'", "\"")
    #p = Population(json.loads(arguments))
def run(args):

    startX = args.startx
    startY = args.starty
    foodCount = args.food_count

    world = World(args.world_size, args.startx, args.starty, args.food_count,
                  args.mut_rate)

    #Generating the Population
    population = Population(args.population_size, world.gene_length, True, [])

    start_time = time.time()
    elapsed_time = 0

    total_fitness = 0
    max_fitness = 0
    max_fitness_index = 0
    i = 0
    for individual in population.individuals:
        fit = fitnessFN(world, individual.path, startX, startY, foodCount)
        individual.setFitness(fit)
        total_fitness += fit
        if fit > max_fitness:
            max_fitness = fit
            max_fitness_index = i
        i += 1

    best_individual = copy.copy(population.individuals[max_fitness_index])

    avg_fitness = total_fitness / population.population_size

    total_generations = args.max_generation_number
    generation_counter = 0

    while generation_counter < total_generations and best_individual.fitness < world.foodCount:
        print("Generation : ", generation_counter)
        print("Max fitness: ", best_individual.fitness)
        print("Average fitness: ", avg_fitness)
        population = evolve(world, population)
        new_best_individual = population.getFittest()
        avg_fitness = population.calculateAverageFitness()

        if new_best_individual.fitness > best_individual.fitness:
            generation_counter, best_individual = 0, new_best_individual
        else:
            generation_counter += 1

    total_generations = 0

    end_time = time.time()
    elapsed_time = end_time - start_time
    print("\n-------------------------------\nElapsed Time: ", elapsed_time,
          " sec")
    print("Population Size: ", args.population_size, " World Size: ",
          args.world_size, "\nStart x, y:", args.startx, ",", args.starty,
          " Food Count: ", args.food_count, "\nMutation Rate: ", args.mut_rate)

    print("Result: ", best_individual.toString())

    matrix = traverse(best_individual.path, world)
    draw_matrix(matrix, 'title')
Beispiel #25
0
from attribute import Attribute
from dataset import Dataset
from genetic import Population

attributes = [Attribute("score", list(range(-5,6))),]

data = [
[5],
[4],
[3],
[2],
[1],
[0],
[-1],
[-2],
[-3],
[-4],
[-5],
]

data = data * 10

scores = Population(Dataset(attributes, data), lambda i: 5 - abs(i[0]), rseed=1)

print(scores)
for i in range(100):
    scores.generation()
print(scores)
print(scores.frequency())