コード例 #1
0
def run():
    local_dir = os.path.dirname(__file__)
    pop = population.Population(os.path.join(local_dir, 'nn_config'))
    pe = parallel.ParallelEvaluator(4, eval_fitness)
    pop.run(pe.evaluate, 1000)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Display the most fit genome.
    print('\nBest genome:')
    winner = pop.statistics.best_genome()
    print(winner)

    # Verify network output against a few randomly-generated sequences.
    winner_net = nn.create_recurrent_phenotype(winner)
    for n in range(4):
        print('\nRun {0} output:'.format(n))
        seq = [random.choice((0, 1)) for _ in range(N)]
        winner_net.reset()
        for s in seq:
            winner_net.activate([s, 0])

        for s in seq:
            output = winner_net.activate([0, 1])
            print("expected {0:1.5f} got {1:1.5f}".format(s, output[0]))

    # Visualize the winner network and plot/log statistics.
    visualize.draw_net(winner, view=True, filename="nn_winner.gv")
    visualize.draw_net(winner, view=True, filename="nn_winner-enabled.gv", show_disabled=False)
    visualize.draw_net(winner, view=True, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)
コード例 #2
0
    def getAction(self, gameState):
        legalActions = gameState.getLegalActions(0)
        if Directions.STOP in legalActions:
            legalActions.remove(Directions.STOP)

        inputs = []
        outputs = legalActions

        # Load the config file, which is assumed to live in
        # the same directory as this script.
        local_dir = os.path.dirname(__file__)
        config = Config(os.path.join(local_dir, 'nn_config'))

        pop = population.Population(config)
        pop.epoch(fitness_function, 1000)

        def eval_fitness(genomes):
            for g in genomes:
                net = nn.create_feed_forward_phenotype(g)
                g.fitness = gamestate.score

                output = net.serial_activate(intputs)

        "*** YOUR CODE HERE ***"
        #    util.raiseNotDefined()
        expectimax = self.expectimax(gameState, self.depth, 0)
        #    print "minimax({0},{1})" .format(expectimax[0], expectimax[1])
        #    print "GameState :\n{0}\n\n" .format(gameState)
        return expectimax[1]
コード例 #3
0
    def train_network(self, checkpoint):

        self.checkpoint = checkpoint
        # Simulation

        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'config.txt')
        pop = population.Population(config_path)

        # Load checkpoint
        if self.checkpoint != None:
            pop.load_checkpoint(self.checkpoint)

        #pe = parallel.ParallelEvaluator(args.numCores,worker_evaluate_genome)
        pop.run(self.eval_fitness, self.generations)

        pop.save_checkpoint("checkpoint")

        # Log statistics.
        statistics.save_stats(pop.statistics)
        statistics.save_species_count(pop.statistics)
        statistics.save_species_fitness(pop.statistics)

        print('Number of evaluations: {0}'.format(pop.total_evaluations))

        # Show output of the most fit genome against training data.
        winner = pop.statistics.best_genome()

        # Save best network
        import pickle
        with open('winner.pkl', 'wb') as output:
            pickle.dump(winner, output, 1)

        #visualize.draw_net("config.txt", winner, view=False, node_names=None, filename=self.game_name + "net")
        '''
コード例 #4
0
ファイル: grid_pacai.py プロジェクト: krishal/pac-ai
def main():

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'grid_pacai_config')

    pe = parallel.ParallelEvaluator(2, runPacman)
    pop = population.Population(config_path)

    pop.run(pe.evaluate, 400)

    winner = pop.statistics.best_genome()

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Visualize the winner network and plot/log statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True, filename="xor2-all.gv")
    visualize.draw_net(winner,
                       view=True,
                       filename="xor2-enabled.gv",
                       show_disabled=False)
    visualize.draw_net(winner,
                       view=True,
                       filename="xor2-enabled-pruned.gv",
                       show_disabled=False,
                       prune_unused=True)
コード例 #5
0
def main():
    global novSearch

    # myworld = World("Simulator", 1080, 280, 40)
    # myworld.readWorldConfigFile("testConfig.txt")

    myworld = World("Simulator", 2000, 400, 40)
    myworld.readWorldConfigFile("finalWorld.txt")

    myworld.getValidStand()
    myworld.getAirspace()

    # mario = Mario(myworld, "Mario", 0, 5)

    mario = Mario(myworld, "Mario", 0, 8)  # for final world

    # set up NEAT
    config.load("marNovelty_config")
    chromosome.node_gene_type = genome.NodeGene

    # use the noveltyFitness function to determine fitness scores
    population.Population.evaluate = noveltyFitness
    pop = population.Population()

    # set how many generations you want evolution to run
    generations = 10
    pop.epoch(generations, report=True)

    # After evolution completes...

    # Plot the best/average fitness across the evolution
    visualize.plot_stats(pop.stats)

    # Create a visualization of speciation that occurred
    visualize.plot_species(pop.species_log)

    # save the archive and growth data from novelty search
    novSearch.saveArchive("mario")
    novSearch.saveGrowth("mario")

    # Save the best coverage chormosomes found
    print "\nFound behaviors with the following coverage scores:"

    for i in range(len(bestChromos)):
        print bestChromos[i][1]
        f = open("bestChromo%d" % (i), "w")
        pickle.dump(bestChromos[i][0], f)
        f.close()

    # Save the most recently added chromosomes in the archive
    print "\nNovelty scores for 10 most recently added behaviors:"

    i = 0

    for saved in novSearch.archive[-10:]:
        print saved[1]
        f = open("novelty%d" % (i), "w")
        pickle.dump(saved[2], f)
        f.close()
        i += 1
コード例 #6
0
def run():
    # load settings file
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'dpole_config'))

    # change the number of inputs accordingly to the type
    # of experiment: markov (6) or non-markov (3)
    # you can also set the configs in dpole_config as long
    # as you have two config files for each type of experiment
    config.input_nodes = 3

    pop = population.Population(config)
    pop.epoch(evaluate_population, 200, report=1, save_best=0)

    winner = pop.most_fit_genomes[-1]

    print('Number of evaluations: {0:d}'.format(winner.ID))
    print('Winner fitness: {0:f}'.format(winner.fitness))

    # save the winner
    with open('winner_chromosome', 'w') as f:
        cPickle.dump(winner, f)

    # Plots the evolution of the best/average fitness
    visualize.plot_stats(pop, ylog=True)
    # Visualizes speciation
    visualize.plot_species(pop)
    # visualize the best topology
    visualize.draw_net(winner, view=True)
コード例 #7
0
def test_run():
    xor_inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
    xor_outputs = [0, 1, 1, 0]

    def eval_fitness(genomes):
        for g in genomes:
            net = nn.create_feed_forward_phenotype(g)

            error = 0.0
            for inputs, expected in zip(xor_inputs, xor_outputs):
                # Serial activation propagates the inputs through the entire network.
                output = net.serial_activate(inputs)
                error += (output[0] - expected)**2

            # When the output matches expected for all inputs, fitness will reach
            # its maximum value of 1.0.
            g.fitness = 1 - error

    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = population.Population(config)
    pop.run(eval_fitness, 10)

    winner = pop.statistics.best_genome()

    # Validate winner.
    for g in pop.statistics.most_fit_genomes:
        assert winner.fitness >= g.fitness

    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)
コード例 #8
0
    def reset(self):
        while True:
            # noinspection PyBroadException
            try:
                # re-create the population by configuration, which is the top-level object for a NEAT tasks.
                self._population = population.Population(self._config)
                break
            except Exception:
                print("re-create again!")

        if self._checkpoint_value >= 0:
            # create the check point reporter.
            self._checkpoint_reporter = checkpoint.Checkpointer(
                generation_interval=self._checkpoint_value,
                filename_prefix=self._output_path + "neat-checkpoint-")
            self._population.add_reporter(self._checkpoint_reporter)

        # create the stdout reporter.
        self._stdout_reporter = reporting.StdOutReporter(self._stdout)
        self._population.add_reporter(self._stdout_reporter)

        # create the statistics reporter.
        self._statistics_reporter = statistics.StatisticsReporter()
        self._population.add_reporter(self._statistics_reporter)

        # best genome after training.
        self._winner = None
        self._obtain_success = False
コード例 #9
0
ファイル: nn_evolve.py プロジェクト: tsarvey/neat-python
def run():
    local_dir = os.path.dirname(__file__)
    pop = population.Population(os.path.join(local_dir, 'nn_config'))
    pe = parallel.ParallelEvaluator(4, eval_fitness)
    pop.run(pe.evaluate, 1000)

    # Log statistics.
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Show output of the most fit genome against a random input.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = nn.create_recurrent_phenotype(winner)
    for n in range(4):
        print('\nRun {0} output:'.format(n))
        seq = [random.choice((0, 1)) for _ in range(N)]
        winner_net.reset()
        for s in seq:
            winner_net.activate([s, 0])

        for s in seq:
            output = winner_net.activate([0, 1])
            print("expected {0:1.5f} got {1:1.5f}".format(s, output[0]))
コード例 #10
0
def run():
    t0 = time.time()

    # Get the path to the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'xor2_config')

    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(4, fitness)
    pop = population.Population(config_path)
    pop.run(pe.evaluate, 400)

    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(
        ((time.time() - t0) / pop.generation)))

    print('Number of evaluations: {0:d}'.format(pop.total_evaluations))

    # Verify network output against training data.
    print('\nBest network output:')
    winner = pop.statistics.best_genome()
    net = nn.create_feed_forward_phenotype(winner)
    for i, inputs in enumerate(xor_inputs):
        output = net.serial_activate(inputs)  # serial activation
        print("{0:1.5f} \t {1:1.5f}".format(xor_outputs[i], output[0]))

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True)
コード例 #11
0
    def train_network(self, checkpoint):

        self.checkpoint = checkpoint

        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'config.txt')
        pop = population.Population(config_path)

        # Load checkpoint
        if self.checkpoint != None:
            pop.load_checkpoint(self.checkpoint)

        pop.run(self.eval_fitness, self.generations)

        pop.save_checkpoint("checkpoint")

        # Log statistics.
        statistics.save_stats(pop.statistics)
        statistics.save_species_count(pop.statistics)
        statistics.save_species_fitness(pop.statistics)

        print('Number of evaluations: {0}'.format(pop.total_evaluations))

        # Show output of the most fit genome against training data.
        winner = pop.statistics.best_genome()

        # Save best network
        import pickle
        with open('winner.pkl', 'wb') as output:
            pickle.dump(winner, output, 1)
コード例 #12
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Display the winning genome.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    dt = iz_params[-1]
    for inputData, outputData in zip(xor_inputs, xor_outputs):
        neuron_data = {}
        for i, n in net.neurons.items():
            neuron_data[i] = []

        # Reset the network, apply the XOR inputs, and run for the maximum allowed time.
        net.reset()
        net.set_inputs(inputData)
        t0 = None
        t1 = None
        v0 = None
        v1 = None
        num_steps = int(max_time / dt)
        for j in range(num_steps):
            t = dt * j
            output = net.advance()

            # Capture the time and neuron membrane potential for later use if desired.
            for i, n in net.neurons.items():
                neuron_data[i].append((t, n.v))

            # Remember time and value of the first output spikes from each neuron.
            if t0 is None and output[0] > 0:
                t0, v0 = neuron_data[net.outputs[0]][-2]

            if t1 is None and output[1] > 0:
                t1, v1 = neuron_data[net.outputs[1]][-2]

        response = compute_output(t0, t1)
        print(inputData, response)
コード例 #13
0
def main():
    # initalize game
    global FPSCLOCK, SCREEN, FONT
    pygame.init()
    FONT = pygame.font.SysFont("monospace", 15)
    FPSCLOCK = pygame.time.Clock()
    SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption('Flappy Bird Evolution')

    # get game element images
    day_time = ['day', 'night'][rnd.randint(0, 1)]
    IMAGES['background'] = pygame.image.load(path + 'background-' + day_time +
                                             '.png').convert()
    IMAGES['base'] = pygame.image.load(path + 'base.png').convert()
    IMAGES['message'] = pygame.image.load(path + 'message.png').convert_alpha()
    IMAGES['pipe_down'] = pygame.image.load(path +
                                            'pipe-red.png').convert_alpha()
    IMAGES['pipe_up'] = pygame.transform.rotate(IMAGES['pipe_down'], 180)
    IMAGES['numbers'] = [
        pygame.image.load(path + str(i) + '.png').convert_alpha()
        for i in range(10)
    ]
    for color in ['blue', 'red', 'yellow', 'black']:
        for state in range(3):
            IMAGES[color + '-' +
                   str(state)] = pygame.image.load(path + color + 'bird-' +
                                                   str(state) +
                                                   '.png').convert_alpha()

    # get hitmasks
    for i in range(3):
        HITMASKS['bird-' + str(i)] = get_mask(
            pygame.image.load(path + 'bluebird-' + str(i) +
                              '.png').convert_alpha())
    HITMASKS['pipe_up'] = get_mask(IMAGES['pipe_up'])
    HITMASKS['pipe_down'] = get_mask(IMAGES['pipe_down'])

    # show welcome screen
    SCREEN.blit(IMAGES['background'], (0, 0))
    SCREEN.blit(IMAGES['message'], (45, 45))
    SCREEN.blit(IMAGES['base'], (BASEX, BASEY))
    pygame.display.update()

    # hold until space is pushed
    hold = True
    while hold:
        for event in pygame.event.get():
            if event.type == KEYDOWN and event.key == K_SPACE:
                hold = False

    # start evolution
    pop = population.Population('flappy_config')
    pop.run(eval_fitness, 10000)
コード例 #14
0
def train_network(env):
    def evaluate_genome(g):
        net = nn.create_feed_forward_phenotype(g)
        return simulate_species(net,
                                env,
                                args.episodes,
                                args.max_steps,
                                render=args.render)

    def eval_fitness(genomes):
        for g in genomes:
            fitness = evaluate_genome(g)
            g.fitness = fitness

    # Simulation
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'gym_config')
    pop = population.Population(config_path)
    # Load checkpoint
    if args.checkpoint:
        pop.load_checkpoint(args.checkpoint)
    # Start simulation
    if args.render:
        pop.run(eval_fitness, args.generations)
    else:
        pe = parallel.ParallelEvaluator(args.numCores, worker_evaluate_genome)
        pop.run(pe.evaluate, args.generations)

    pop.save_checkpoint("checkpoint")

    # Log statistics.
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()

    # Save best network
    import pickle
    with open('winner.pkl', 'wb') as output:
        pickle.dump(winner, output, 1)

    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')

    raw_input("Press Enter to run the best genome...")
    winner_net = nn.create_feed_forward_phenotype(winner)
    for i in range(100):
        simulate_species(winner_net, env, 1, args.max_steps, render=True)
コード例 #15
0
def main():
    # set up NEAT
    config.load("NEAT_config")
    chromosome.node_gene_type = genome.NodeGene
    population.Population.evaluate = coverageFitness  #function name
    pop = population.Population()
    # set how many generations you want evolution to run
    generations = 30
    pop.epoch(generations, report=True, save_best=True)
    # Plots the best/average fitness across the evolution
    visualize.plot_stats(pop.stats)
    # Creates a visualization of speciation that occurred
    visualize.plot_species(pop.species_log)
コード例 #16
0
ファイル: evolveRed.py プロジェクト: dan-red/Capture_the_Flag
def main():

    population.Population.evaluate = eval_fitness
    pop = population.Population()
    generations = 23
    pop.epoch(generations, report=True, save_best=True, \
        checkpoint_interval=None, checkpoint_generation=11)
    stop = time.time()
    elapsed = stop - start
    print "Total time to evolve:", elapsed
    print "Time per generation:", elapsed / float(generations)
    visualize.plot_stats(pop.stats)
    visualize.plot_species(pop.species_log)
コード例 #17
0
    def __init__(self, env, config, threads: int = 4):
        print("\n------------------------------------------")
        print("               NEAT USING THE               ")
        print("            NEAT-PYTHON LIBRARY             ")
        print("------------------------------------------\n")

        self.env = env

        # Simulation
        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, config)
        pop = population.Population(config_path)  # Get the config file

        # Load checkpoint
        if args.checkpoint:
            pop.load_checkpoint(args.checkpoint)

        # Start simulation
        try:
            if threads == 1:
                pop.run(self.eval_fitness, args.generations)
            else:
                pe = parallel.ParallelEvaluator(threads, self.evaluate_genome)
                pop.run(pe.evaluate, args.generations)
        except KeyboardInterrupt:
            print("\nExited.")

        pop.save_checkpoint("checkpoint")  # Save the current checkpoint

        # Log statistics.
        statistics.save_stats(pop.statistics)
        statistics.save_species_count(pop.statistics)
        statistics.save_species_fitness(pop.statistics)

        print('Number of evaluations: {0}'.format(pop.total_evaluations))

        # Show output of the most fit genome against training data.
        winner = pop.statistics.best_genome()

        # Save best network
        name = input("Net name: ")
        import pickle
        with open(name + '.pkl', 'wb') as output:
            pickle.dump(winner, output, 1)
        print("Net saved as " + name + '.pkl')

        print('\nBest genome:\n{!s}'.format(winner))
        input("Press enter to run the best genome...")

        winner_net = nn.create_feed_forward_phenotype(winner)
        self.simulate_species(winner_net, env, 1, args.max_steps, render=True)
コード例 #18
0
    def __init__(self,
                 config,
                 fitter,
                 node_names,
                 max_generation=None,
                 checkpoint_value=-1,
                 stdout=False,
                 output_path=None):
        """
        Initialize the operator of NeuroEvolution.

        :param config: configures of NEAT.
        :param fitter: fitter of NEAT.
        :param node_names: node information for display the obtained network.
        :param max_generation: generations (iteration times).
        :param checkpoint_value: the point to save the current state.
        :param output_path: parent path for save file of displaying the genome and checkpoint.
        :param stdout: whether output the log.
        """
        # load configuration.
        self._config = config

        self._fitter = fitter
        self._node_names = node_names
        self._max_generation = max_generation
        self._output_path = output_path

        # create the population by configuration, which is the top-level object for a NEAT tasks.
        self._population = population.Population(self._config)

        self._checkpoint_value = checkpoint_value
        if self._checkpoint_value >= 0:
            # create the check point reporter.
            self._checkpoint_reporter = checkpoint.Checkpointer(
                generation_interval=checkpoint_value,
                filename_prefix=output_path + "neat-checkpoint-")
            self._population.add_reporter(self._checkpoint_reporter)

        # create the stdout reporter.
        self._stdout = stdout
        self._stdout_reporter = reporting.StdOutReporter(stdout)
        self._population.add_reporter(self._stdout_reporter)

        # create the statistics reporter.
        self._statistics_reporter = statistics.StatisticsReporter()
        self._population.add_reporter(self._statistics_reporter)

        # best genome after training.
        self._winner = None
        self._obtain_success = False
コード例 #19
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]
    chromo_file = None
    log_file = None
    if len(argv) == 0:
        pass
    elif len(argv) == 1:
        chromo_gen = argv[0]
        chromo_file = "blue_best_chromo_" + chromo_gen
    elif len(argv) == 2:
        chromo_gen = argv[0]
        chromo_file = "blue_best_chromo_" + chromo_gen
        log_file = argv[1]
    else:
        print "Usage: python blueEat.py [chromo_gen] [log_file]"
        return

    if chromo_file:
        if log_file:
            logFP = open(log_file, "w")
        else:
            logFP = None
        # Test an already evolved network
        fp = open(chromo_file, "r")
        chromo = pickle.load(fp)
        fp.close()
        print chromo
        result = eval_individual(chromo, int(chromo_gen), logFP)
        print "Fitness:", result
        if log_file:
            logFP.write("Fitness %f" % result)
            logFP.close()
    else:
        # Start the evolutionary process
        population.Population.evaluate = eval_fitness
        pop = population.Population()
        pop.epoch(23, report=True, save_best=True, checkpoint_interval=4, \
                  checkpoint_generation=None, name="blue")
        # changed from 4, None, 2

        # Plots the evolution of the best/average fitness (requires Biggles)
        visualize.plot_stats(pop.stats, name="blue")
        # Visualizes speciation
        visualize.plot_species(pop.species_log, name="blue")
        winner = pop.best
        # Visualize the winner network (requires PyDot)
        visualize.draw_net(winner, "blue")  # best chromosome
        print "NEAT evolution complete"
コード例 #20
0
def run():

    random.seed()
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'lab_config')

    pe = parallel.ParallelEvaluator(9, fitness)

    pop = population.Population(config_path)
    for i in range(100):
        pop.run(pe.evaluate, 10)
        winner = pop.statistics.best_genome()

        with open("ress/res_%d.net" % i, "wb") as f:
            pickle.dump(winner, f)
コード例 #21
0
def run():
    # 128x128 thumbnails, 1500x1500 rendered images, 1100x810 viewer, grayscale images.
    pb = PictureBreeder(128, 128, 1500, 1500, 1100, 810, 'gray', 4)
    cfg = config.Config('config')

    # Make sure the network has the expected number of outputs.
    if pb.scheme == 'color':
        cfg.output_nodes = 3
    else:
        cfg.output_nodes = 1

    cfg.pop_size = pb.num_cols * pb.num_rows
    pop = population.Population(cfg)
    while 1:
        pop.run(pb.eval_fitness, 1)
コード例 #22
0
ファイル: grid_pacai.py プロジェクト: krishal/pac-ai
def main():

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'grid_pacai_config')

    pe = parallel.ParallelEvaluator(2, runPacman)
    pop = population.Population(config_path)
    genJump = 10

    for numGen in range(0, 1000, genJump):
        pop.run(pe.evaluate, genJump)

        print('Number of evaluations: {0:d}'.format(pop.total_evaluations))

        with open('nn_pop' + str(numGen), 'wb') as f:
            pickle.dump(pop, f)
コード例 #23
0
    def run(self, cores, generations, pop_size):
        assert isinstance(cores, int)
        assert isinstance(generations, int)

        # print "Running experiment"
        # print "\tcores: %i" % cores
        # print "\tgenerations: %i" % generations
        # print "\tgenerations: %i" % generations
        self.neat_config.pop_size = pop_size
        pop = population.Population(self.neat_config)
        if cores > 1:
            pe = ParallelEvaluator(cores, self.evaluate_genome)
            pop.run(pe.evaluate, generations)
        else:
            pop.run(self.evaluate_genomes, generations)
        return pop
コード例 #24
0
ファイル: trainer.py プロジェクト: varunbajpai/go_dino
def main():
    local_dir = os.path.dirname(__file__)
    config = Config(neat.DefaultGenome, neat.DefaultReproduction,
                    neat.DefaultSpeciesSet, neat.DefaultStagnation,
                    os.path.join(local_dir, 'train_config.txt'))
    config.save_best = True
    config.checkpoint_time_interval = 3

    pop = population.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))
    pop.add_reporter(neat.StatisticsReporter())
    pop.add_reporter(neat.Checkpointer(2))
    winner = pop.run(eval_fitness, 100)
    with open('winner.pkl', 'wb') as f:
        pickle.dump(winner, f)
コード例 #25
0
def test_evolve():
    test_values = [random.random() for _ in range(10)]

    def evaluate_genome(genomes):
        for g in genomes:
            net = ctrnn.create_phenotype(g)

            fitness = 0.0
            for t in test_values:
                net.reset()
                output = net.serial_activate([t])

                expected = t ** 2

                error = output[0] - expected
                fitness -= error ** 2

            g.fitness = fitness

    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'ctrnn_config'))
    config.node_gene_type = ctrnn.CTNodeGene
    config.prob_mutate_time_constant = 0.1
    config.checkpoint_time_interval = 0.1
    config.checkpoint_gen_interval = 1

    pop = population.Population(config)
    pop.run(evaluate_genome, 10)

    # Save the winner.
    print('Number of evaluations: {0:d}'.format(pop.total_evaluations))
    winner = pop.statistics.best_genome()
    with open('winner_genome', 'wb') as f:
        pickle.dump(winner, f)

    repr(winner)
    str(winner)

    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)
コード例 #26
0
def train(checkpoint, config):
    print("Starting...")
    pop = population.Population(config)
    # HINT change checkpoints for new try or reloading
    try:
        pop.load_checkpoint(checkpoint)
    except:
        print("Checkpoint not found, starting from scratch: ", checkpoint)
    trainfunc = partial(eval_fitness_internalfight, num_runs=4, steplength=100, x=16, y=16)
    pop.run(trainfunc, 20)
    pop.save_checkpoint(checkpoint)

    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)

    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
コード例 #27
0
def run():
    cfg = config.Config('novelty_config')
    cfg.pop_size = 100
    cfg.max_fitness_threshold = 1e38

    ne = NoveltyEvaluator(4, 'gray')
    if ne.scheme == 'color':
        cfg.output_nodes = 3
    else:
        cfg.output_nodes = 1

    pop = population.Population(cfg)

    while 1:
        pop.run(ne.evaluate, 1)

        winner = pop.statistics.best_genome()
        if ne.scheme == 'gray':
            image = eval_gray_image(winner, full_scale * width,
                                    full_scale * height)
        elif ne.scheme == 'color':
            image = eval_color_image(winner, full_scale * width,
                                     full_scale * height)
        elif ne.scheme == 'mono':
            image = eval_mono_image(winner, full_scale * width,
                                    full_scale * height)
        else:
            raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme))

        im = np.clip(np.array(image), 0, 255).astype(np.uint8)
        im = ne.image_from_array(im)
        im.save('winning-novelty-{0:06d}.png'.format(pop.generation))

        if ne.scheme == 'gray':
            image = eval_gray_image(winner, width, height)
        elif ne.scheme == 'color':
            image = eval_color_image(winner, width, height)
        elif ne.scheme == 'mono':
            image = eval_mono_image(winner, width, height)
        else:
            raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme))

        float_image = np.array(image, dtype=np.float32) / 255.0
        ne.archive.append(float_image)
コード例 #28
0
def visualizeWinners(checkpoint, config, picdir, rounds):
    colors = ['red', 'yellow', 'green', 'blue']
    while len(checkpoint) < 4:
        checkpoint.extend(checkpoint)
    checkpoint = checkpoint[:4]
    print("Going to let fight: ", checkpoint)
    print("With colors: ", colors)
    nets = {}
    for i, c in enumerate(checkpoint):
        pop = population.Population(config)
        pop.load_checkpoint(c)
        trainfunc = partial(eval_fitness_internalfight, num_runs=10, steplength=200, x=16, y=16)
        pop.run(trainfunc, 1)
        winner = pop.statistics.best_genome()
        nets[c+str(i)] = nn.create_feed_forward_phenotype(winner)

    filelist = glob.glob(os.path.join(picdir, 'step*.png'))
    for f in filelist:
        os.remove(f)

    x = 40; y = 40
    game = ca.CellularAutomaton(initialState=ca.initializeHexagonal(x, y), param=ca.defaultParameters)
    for i,k in enumerate(nets.keys()):
        game.setNewSpecies(util.nicePositions4(i,x, y), k, colors[i])

    util.saveStatePicture(game.getState(), picdir)

    while game.step < rounds:
        state = game.getState()
        for s in game.findSpecies():
            try:
                game.setDecisions(s, netDecision(state, s, nets[s]))
            except:
                pass
        game.evolve()
        util.saveStatePicture(state, picdir)

    app = QApplication(sys.argv)
    pics = util.sort_nicely(glob.glob(os.path.join(picdir, 'step*.png')))
    gui = SimpleGUI(pics)

    gui.show()
    sys.exit(app.exec_())
コード例 #29
0
def main():

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'pacai_config')

    pe = parallel.ParallelEvaluator(8, runPacman)
    pop = population.Population(config_path)

    for i in range(0, 1):
        pop.run(pe.evaluate, 1)
        winner = pop.statistics.best_genome()
        print winner
        filename = "winner{0}".format(i)
        with open(filename, 'wb') as f:
            pickle.dump(winner, f)

        visualize.plot_stats(pop.statistics)
        visualize.plot_species(pop.statistics)
        visualize.draw_net(winner, view=True)
コード例 #30
0
ファイル: cppn.py プロジェクト: laal65/CPPN-experiments
def evolve(config, eval_func, pattern, generations):
    """
        eval_func = (net, w, h) -> bool_map of shape [w, h]
    """
    pop = population.Population(config)
    w, h = pattern.shape

    # Use closure to pass evaluate function.
    def evaluate_all(genomes):
        for g in genomes:
            net = nn.create_feed_forward_phenotype(g)
            out = eval_func(net, w, h)
            g.fitness = diff(pattern, out)

    pop.run(evaluate_all, generations)

    winner = pop.statistics.best_genome()
    net = nn.create_feed_forward_phenotype(winner)
    out = eval_func(net, w, h)

    return pop.statistics, out