Ejemplo n.º 1
0
def run():
    t0 = time.time()

    # 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'))

    num_workers = 6
    pool = Pool(num_workers)
    print "Starting with %d workers" % num_workers

    def fitness(chromosomes):
        return eval_fitness(chromosomes, pool)

    pop = population.Population(config)
    pop.epoch(fitness, 400)

    print "total evolution time %.3f sec" % (time.time() - t0)
    print "time per generation %.3f sec" % ((time.time() - t0) / pop.generation)

    winner = pop.most_fit_genomes[-1]
    print 'Number of evaluations: %d' % winner.ID

    # Verify network output against training data.
    print '\nBest network output:'
    net = nn.create_fast_feedforward_phenotype(winner)
    for i, inputs in enumerate(INPUTS):
        output = net.sactivate(inputs)  # serial activation
        print "%1.5f \t %1.5f" % (OUTPUTS[i], output[0])

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)
Ejemplo n.º 2
0
def parallel_evaluation(chromo):
    """ This function will run in parallel """
    net = nn.create_fast_feedforward_phenotype(chromo)

    error = 0.0
    for inputData, outputData in zip(INPUTS, OUTPUTS):
        # serial activation
        output = net.sactivate(inputData)
        error += (output[0] - outputData) ** 2

    return 1 - math.sqrt(error / len(OUTPUTS))
Ejemplo n.º 3
0
def eval_fitness(genomes):
    for g in genomes:
        net = nn.create_fast_feedforward_phenotype(g)

        error = 0.0
        for i, inputs in enumerate(INPUTS):
            # Serial activation propagates the inputs through the entire network.
            output = net.sactivate(inputs)
            error += (output[0] - OUTPUTS[i]) ** 2

        g.fitness = 1 - math.sqrt(error / len(OUTPUTS))
Ejemplo n.º 4
0
    def run(self):
        local_dir = os.path.dirname(__file__)
        config = Config(os.path.join(local_dir, "evolve_config"))

        pop = population.Population(config)
        pop.epoch(self.eval_fitness, 300)

        winner = pop.most_fit_genomes[-1]

        self.current_nn = winner

        print "Number of evaluations: %d" % winner.ID

        # Verify network output against training data.
        print "\nBest network output:"
        net = nn.create_fast_feedforward_phenotype(winner)
Ejemplo n.º 5
0
    def eval_fitness(self, genomes):
        for g in genomes:
            while not self.on:
                # case when we dont want to be evaluating the genomes
                sleep(5)

            start_sensors = self.sensor_values
            net = nn.create_fast_feedforward_phenotype(g)
            self.current_nn = net

            # while start_sensors == None:
            #    print self.sensor_values
            #    start_sensors  = self.sensor_values  #this will be the current sensors from the robot
            # sleep(5)
            print "start sensor_values:", self.sensor_values
            print "NEat sleeping"
            sleep(20)
            print "done sleeping"
            print "FINISH sensor_values:", self.sensor_values

            finish_sensors = self.sensor_values
            g.fitness = self.fit_func(start_sensors, finish_sensors)
Ejemplo n.º 6
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'))

    pop = population.Population(config)
    pop.epoch(eval_fitness, 300)

    winner = pop.most_fit_genomes[-1]
    print 'Number of evaluations: %d' % winner.ID

    # Verify network output against training data.
    print '\nBest network output:'
    net = nn.create_fast_feedforward_phenotype(winner)
    for i, inputs in enumerate(INPUTS):
        output = net.sactivate(inputs)  # serial activation
        print "%1.5f \t %1.5f" % (OUTPUTS[i], output[0])

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)