示例#1
0
文件: a_d.py 项目: arieli13/Practica
def run(config_file):
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file)

    # Create the population, which is the top-level object for a NEAT run.
    p = neat.Population(config)

    # Add a stdout reporter to show progress in the terminal.
    p.add_reporter(OwnReporter())
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    p.add_reporter(neat.Checkpointer(5, filename_prefix="./checkpoints/ckpt-"))

    #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4')

    # Run for up to 300 generations.
    winner = p.run(fitness_function, generations)

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

    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)

    node_names = {-1:'x_t', -2: 'y_t', -3: 'x_act', -4:'y_act', 0:'x_t+1', 1: 'y_t+1'}
    visualize.draw_net(config, winner, True, node_names=node_names, filename="./reports/Digraph.gv")
    test(winner_net)
    visualize.plot_stats(stats, ylog=False, view=True, filename="./reports/avg_fitness.svg")
    visualize.plot_species(stats, view=True, filename="./reports/species.svg")
示例#2
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)
示例#3
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)
def main(argv=None):
    # Use a command line argument to pass in the chromo file of an evolved net
    argv = sys.argv[1:]
    print argv
    if len(argv) != 1:
        print "Usage: You muset pass a chromosome file to be evaluated"
        return
    else:
        chromo_file = argv[0]
        log_file = argv[0] + ".log"

    # set up NEAT
    logFP = open(log_file, "w")
    chromoFP = open(chromo_file, "r")
    chromo = pickle.load(chromoFP)
    chromoFP.close()
    print chromo
    # Creates a visualization of the network's topology
    visualize.draw_net(chromo, "_"+chromo_file)
    config.load("marNovelty_config")
    chromosome.node_gene_type = genome.NodeGene


    # set up your simulator
    # myworld = World("Simulator", 1080, 280, 40)
    
    # myworld.readWorldConfigFile("easyConfig.txt")
    # myworld.readWorldConfigFile("intermediateConfig.txt")
    # myworld.readWorldConfigFile("testConfig.txt")

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

    myworld.getValidStand()
    myworld.getAirspace()

    mario = Mario(myworld, "Mario", 0, 5) #for test world

    # mario = Mario(myworld, "Mario", 0, 8) # for final world
    myworld.addMario(mario)
    myworld.makeVisible()
    mario.setBrain(neatBrain(chromo, logFP))


    for i in range(1500):
        if not mario.alive:
            break

        myworld.step()


    fitness = mario.getFitness()

    print "Objective fitness:", fitness, "coinScore:", mario.coinScore
    print "maxCoinScore: ", mario.world.maxCoinScore, "or:", mario.maxCoinScore
    print "Behavior:", mario.getBehavior()
    # wait for a mouse click before ending the program
    myworld.window.getMouse()
    logFP.write("Fitness %f\n" % fitness)
    logFP.close()
示例#5
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)
示例#6
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)
示例#7
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 spiking networks
    config.output_nodes = 2

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

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

    # Verify network output against training data.
    print '\nBest network output:'
    net = iznn.create_phenotype(winner)
    for inputData, outputData in zip(INPUTS, OUTPUTS):
        for j in range(MAX_TIME):
            output = net.advance([x * 10 for x in inputData])
            if output != [False, False]:
                break
        if output[0] and not output[1]:  # Network answered 1
            print "%r expected %d got 1" % (inputData, outputData)
        elif not output[0] and output[1]:  # Network answered 0
            print "%r expected %d got 0" % (inputData, outputData)
        else:
            print "%r expected %d got ?" % (inputData, outputData)

    # 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)
示例#8
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)
示例#9
0
def pool_func(arg):
    fitness_func, genome, shared_data, gi, pi = arg
    fitness = max(0, fitness_func(genome, shared_data))
    print("Generation: %d, Genome: %d --> Fitness: %f" % (gi, pi, fitness))
    sys.stdout.flush()
    draw_net(genome, view=False, \
     filename="./images/solution-g%d-p%d"%(gi, pi), show_disabled=True)
    return fitness
示例#10
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))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
示例#11
0
def main(argv=None):

    # Use a command line argument to pass in the chromo file of an evolved net
    argv = sys.argv[1:]
    print argv

    if len(argv) != 1:
        print "Usage: You must pass a chromo_file to be evaluated"
        return
    else:
        chromo_file = argv[0]
        log_file = argv[0] + ".log"

    # set up NEAT
    logFP = open(log_file, "w")
    chromoFP = open(chromo_file, "r")
    chromo = pickle.load(chromoFP)
    chromoFP.close()
    # print chromo

    # Creates a visualization of the network's topology
    visualize.draw_net(chromo, "_"+chromo_file)
    config.load("smallNEAT_config")
    chromosome.node_gene_type = genome.NodeGene

    # set up your simulator
    myworld = World("Simulator", 1080, 280, 40)
    myworld.readWorldConfigFile("hardSmallWorld.txt")
    myworld.getValidStand()
    myworld.getAirspace()

    mario = Mario(myworld, "Mario", 0, 5)
    myworld.addMario(mario)
    myworld.makeVisible()
    mario.setBrain(neatBrain(chromo, logFP))

 
    for i in range(1500):
        if not mario.alive:
            break

        myworld.step()

    fitness = mario.getFitness()
    # print 'COINSCORE: ', mario.coinScore
    # print 'max: ', mario.maxCoinScore
    # print "Fitness:", fitness

    # wait for a mouse click before ending the program
    myworld.window.getMouse()
    #logFP.write("Fitness %f\n" % fitness)
    logFP.close()
示例#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.epoch(eval_fitness, 200)

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

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

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12,12))
    for r, (inputData, outputData, t0, t1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("%r expected %.3f got %.3f" % (inputData, outputData, response))

        plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {%.1f, %.1f}" % inputData, fontsize=12)
        for i, s in neuron_data.items():
            if i not in net.inputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron %d" % i)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(axis='both', labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
示例#13
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"
示例#14
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"
    def save_stats(self, file_prefix):
        '''
        Save info about the network to a number of files with the given file prefix

        file_prefix: string
        returns: None
        '''
        best_genome = self.pop.statistics.best_genome()

        visualize.plot_stats(self.pop.statistics, filename="%s_stats.png" % file_prefix)
        visualize.plot_species(self.pop.statistics, filename="%s_stats.png" % file_prefix)
        visualize.draw_net(best_genome, view=False, filename="%s_stats.gv" % file_prefix)
        statistics.save_stats(self.pop.statistics, filename="%s_stats.csv" % file_prefix)
        statistics.save_species_count(self.pop.statistics, filename="%s_species_count.csv" % file_prefix)
        statistics.save_species_fitness(self.pop.statistics, filename="%s_species_fitness.csv" % file_prefix)
示例#16
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)
示例#17
0
文件: pacai.py 项目: krishal/pac-ai
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)
示例#18
0
def run():
    pop = population.Population('xor2_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_feed_forward_phenotype(winner)
    for inputs, expected in zip(INPUTS, OUTPUTS):
        output = net.serial_activate(inputs)
        print("expected %1.5f got %1.5f" % (expected, output[0]))

    print(nn.create_feed_forward_function(winner))

    # 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)
示例#19
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]
    if len(argv) != 1:
        print "Usage: python evaluateXOR.py chromo_file"
        return

    chromo_file = argv[0]
    fp = open(chromo_file, "r")
    chromo = pickle.load(fp)
    fp.close()
    print chromo
    visualize.draw_net(chromo, "_" + chromo_file)

    # Let's check if it's really solved the problem
    print '\nNetwork output:'
    brain = nn.create_ffphenotype(chromo)
    for i, inputs in enumerate(INPUTS):
        output = brain.sactivate(inputs)  # serial activation
        print "%1.5f \t %1.5f" % (OUTPUTS[i], output[0])
示例#20
0
def run():
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, "spole_ctrnn_config"))

    pop = population.Population(config, node_gene_type=genome.CTNodeGene)
    pop.epoch(evaluate_population, 2000, report=1, save_best=0)

    # saves the winner
    winner = pop.most_fit_genomes[-1]
    print "Number of evaluations: %d" % winner.ID
    with open("winner_chromosome", "w") as f:
        cPickle.dump(winner, f)

    print winner

    # Plot the evolution of the best/average fitness.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores, ylog=True, view=True)
    # Visualizes speciation
    visualize.plot_species(pop.species_log)
    # Visualize the best network.
    visualize.draw_net(winner, view=True)
示例#21
0
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)
示例#22
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)
示例#23
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)
示例#24
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)
示例#25
0
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)
示例#26
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)

    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    winner = pop.statistics.best_genome()

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

    visualize.draw_net(winner, view=False, filename="xor2-all.gv")
    visualize.draw_net(winner, view=False, filename="xor2-enabled.gv", show_disabled=False)
    visualize.draw_net(winner, view=False, filename="xor2-enabled-pruned.gv", show_disabled=False, prune_unused=True)
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)
示例#27
0
文件: neat_nn.py 项目: ascott7/drop7
    def __init__(self):
        #config = Config('drop7_config')
        pop = population.Population('drop7_config')
        pop.run(self.eval_fitness, 100)

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

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

        # Verify network output against training data.
        #print('\nOutput:')
        self.winner_net = nn.create_feed_forward_phenotype(self.winner)
        #for inputs, expected in zip(xor_inputs, xor_outputs):
        #        output = winner_net.serial_activate(inputs)
        #            print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0]))

        visualize.plot_stats(pop.statistics)
        visualize.plot_species(pop.statistics)
        visualize.draw_net(self.winner, view=True, filename="drop7-all.gv")
        visualize.draw_net(self.winner, view=True, filename="drop7-enabled.gv", show_disabled=False)
        visualize.draw_net(self.winner, view=True, filename="drop7-enabled-pruned.gv", show_disabled=False, prune_unused=True)
示例#28
0
        outputs_test = []
        for xi in x[test_index]:
            outputs_test.append(np.argmax(winner_net.activate(xi)))

        scikitplot.metrics.plot_confusion_matrix(
            y[test_index],
            outputs_test,
            title="Three Classes Confusion Matrix: Fold " + str(n_fold),
            normalize=True,
            text_fontsize="small")
        plt.savefig("3-classes-cf-" + str(n_fold) + ".png")
        plt.clf()
        plt.close()

        vs.draw_net(config,
                    winner,
                    False,
                    filename='3-classes-winner-' + str(n_fold))
        with open('3-classes-winner-' + str(n_fold), 'wb') as f:
            pickle.dump(winner, f)

        with open('3-classes-results-' + str(n_fold), 'w') as f:
            f.write('\nBest genome:\n{!s}'.format(winner))

        accuracies_test.append(accuracy_score(y[test_index], outputs_test))
        precisions.append(
            precision_score(y[test_index],
                            outputs_test,
                            average='macro',
                            zero_division=0))
        recalls.append(
            recall_score(y[test_index],
示例#29
0
for clas, weight in zip(classes, weights):
    for experiment in experiments:
        
        dirname = os.path.join(clas, str(experiment) + '-wp', 'topogies')

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        file = []
        
        with open('config-feedforward-neuroevolution.txt', 'r') as f:
            file = f.readlines()

        file[64] = 'weight_max_value        = ' + str(weight) + '\n'
        file[65] = 'weight_min_value        = ' + str(weight * -1) + '\n'
        file[67] = 'weight_mutate_rate      = ' + str(experiment) + '\n'

        with open('config-feedforward-neuroevolution.txt', 'w') as f:
            f.writelines(file)
        
        for i in range(1, 11):            
            config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                    neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                    'config-feedforward-neuroevolution.txt')

            model_name = str(experiment) + '-winner-' + str(i)
            model_dir = os.path.join(clas, str(experiment) + '-wp', model_name)
            model = pickle.load(open(model_dir, 'rb'))
            node_names = {-1:'A', -2: 'B', 0:'A XOR B'}
            vs.draw_net(config, model, True, filename=str(clas) + str(experiment) + '-winner-' + str(i), node_names=node_names, prune_unused=True)
示例#30
0
        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)


# 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

pop = population.Population(config)
pe = parallel.ParallelEvaluator(4, evaluate_genome)
pop.run(pe.evaluate, 2000)

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

print(winner)

# Plot the evolution of the best/average fitness.
visualize.plot_stats(pop.statistics, ylog=True, filename="ctrnn_fitness.svg")
# Visualizes speciation
visualize.plot_species(pop.statistics, filename="ctrnn_speciation.svg")
# Visualize the best network.
visualize.draw_net(winner, view=True, filename="ctrnn_winner.gv")
示例#31
0
import gym
import torch
import neat.population as pop
import neat.experiments.pole_balancing.config as c
from neat.visualize import draw_net
from neat.phenotype.feed_forward import FeedForwardNet

neat = pop.Population(c.PoleBalanceConfig)
solution, generation = neat.run()

if solution is not None:
    print('Found a Solution')
    draw_net(solution,
             view=True,
             filename='./images/pole-balancing-solution',
             show_disabled=True)

    # OpenAI Gym
    env = gym.make('LongCartPole-v0')
    done = False
    observation = env.reset()

    fitness = 0
    phenotype = FeedForwardNet(solution, c.PoleBalanceConfig)

    while not done:
        env.render()
        input = torch.tensor([observation], device=c.DEVICE)

        pred = round(float(phenotype(input)))
        observation, reward, done, info = env.step(pred)
示例#32
0
            #error_stanley += math.fabs(output[0] - OUTPUTS[i])

        #chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43)
        chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS))

population.Population.evaluate = eval_fitness

pop = population.Population()
pop.epoch(300, report=True, save_best=False)

winner = pop.stats[0][-1]
print 'Number of evaluations: %d' %winner.id

# Visualize the winner network (requires PyDot)
visualize.draw_net(winner) # best chromosome

# Plots the evolution of the best/average fitness (requires Biggles)
visualize.plot_stats(pop.stats)
# Visualizes speciation
visualize.plot_species(pop.species_log)

# Let's check if it's really solved the problem
print '\nBest network output:'
brain = nn.create_ffphenotype(winner)
for i, inputs in enumerate(INPUTS):
    output = brain.sactivate(inputs) # serial activation
    print "%1.5f \t %1.5f" %(OUTPUTS[i], output[0])

# saves the winner
#file = open('winner_chromosome', 'w')
示例#33
0
                # the cart/pole has run/inclined out of the limits
                break

        chromo.fitness = fitness
    print 'Spikes:', spikes, ', Firing rate:', spikes / MAX_TIME, '(spikes/ms)'

if __name__ == "__main__":

    config.load('spole_config')

    # Temporary workaround
    chromosome.node_gene_type = genome.NodeGene

    population.Population.evaluate = evaluate_population
    pop = population.Population()
    pop.epoch(200, report=1, save_best=0)
    #pop.epoch(500, report=1, save_best=0)

    print 'Number of evaluations: %d' % (pop.stats[0][-1]).id

    # visualize the best topology
    visualize.draw_net(pop.stats[0][-1])  # best chromosome
    # Plots the evolution of the best/average fitness
    visualize.plot_stats(pop.stats)
    visualize.plot_species(pop.species_log)

    # saves the winner
    file = open('winner_chromosome', 'w')
    pickle.dump(pop.stats[0][-1], file)
    file.close()
示例#34
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))

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1,
            neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(
            inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title(
            "Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData),
            fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
            fitness += 1
            if abs(x) >= 2.4 or abs(theta) >= angle_limit:
                # if abs(theta) > angle_limit: # Igel (p. 5) uses theta criteria only
                # the cart/pole has run/inclined out of the limits
                break

        chromo.fitness = fitness


if __name__ == "__main__":
    # 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, 'spole_config'))

    pop = population.Population(config)
    pop.epoch(evaluate_population, 200)

    # Save the winner,
    winner = pop.most_fit_genomes[-1]
    print 'Number of evaluations: %d' % winner.ID
    with open('winner_chromosome', 'w') as f:
        cPickle.dump(winner, f)

    # Plot the evolution of the best/average fitness.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores, ylog=True)
    # Visualizes speciation
    visualize.plot_species(pop.species_log)
    # Visualize the best network.
    visualize.draw_net(winner, view=True)
示例#36
0
    def run(self, pool=None, shared_data=None):
        allgenfitnesses = []
        for generation in range(1, self.Config.NUMBER_OF_GENERATIONS):
            # ****** BYME: Neuro-evolution accures here *******
            # Get Fitness of Every Genome
            if pool != None:
                ll = len(self.population)
                args = zip(list(it.repeat(self.Config.fitness_fn, ll)), \
                 self.population, list(it.repeat(shared_data, ll)), \
                 list(it.repeat(generation, ll)), range(ll))
                fitnesses = list(pool.map(pool_func, args))
                for genome, fitness in zip(self.population, fitnesses):
                    genome.fitness = fitness
            else:
                for genome in tqdm(self.population):
                    genome.fitness = max(0, self.Config.fitness_fn(genome))

            allfitnesses_onegen = [g.fitness for g in self.population]
            allfitnesses_onegen.sort()
            allgenfitnesses.append(allfitnesses_onegen)

            best_genome = utils.get_best_genome(self.population)
            draw_net(best_genome, view=False, \
             filename="./images/solution-best-g%d"%(generation), show_disabled=True)

            # Reproduce
            all_fitnesses = []
            remaining_species = []

            for species, is_stagnant in Species.stagnation(
                    self.species, generation):
                if is_stagnant:
                    self.species.remove(species)
                else:
                    all_fitnesses.extend(g.fitness for g in species.members)
                    remaining_species.append(species)

            min_fitness = min(all_fitnesses)
            max_fitness = max(all_fitnesses)

            fit_range = max(1.0, (max_fitness - min_fitness))
            for species in remaining_species:
                # Set adjusted fitness
                avg_species_fitness = np.mean(
                    [g.fitness for g in species.members])
                species.adjusted_fitness = (avg_species_fitness -
                                            min_fitness) / fit_range

            adj_fitnesses = [s.adjusted_fitness for s in remaining_species]
            adj_fitness_sum = sum(adj_fitnesses)

            # Get the number of offspring for each species
            new_population = []
            for species in remaining_species:
                if species.adjusted_fitness > 0:
                    size = max(
                        2,
                        int((species.adjusted_fitness / adj_fitness_sum) *
                            self.Config.POPULATION_SIZE))
                else:
                    size = 2

                # sort current members in order of descending fitness
                cur_members = species.members
                cur_members.sort(key=lambda g: g.fitness, reverse=True)
                species.members = []  # reset

                # save top individual in species
                new_population.append(cur_members[0])
                size -= 1

                # Only allow top x% to reproduce
                purge_index = int(self.Config.PERCENTAGE_TO_SAVE *
                                  len(cur_members))
                purge_index = max(2, purge_index)
                cur_members = cur_members[:purge_index]

                for i in range(size):
                    parent_1 = random.choice(cur_members)
                    parent_2 = random.choice(cur_members)

                    child = crossover(parent_1, parent_2, self.Config)
                    mutate(child, self.Config)
                    new_population.append(child)

            # Set new population
            self.population = new_population
            Population.current_gen_innovation = []

            # Speciate
            for genome in self.population:
                self.speciate(genome, generation)

            if best_genome.fitness >= self.Config.FITNESS_THRESHOLD:
                o = open("allgenfitnesses.txt", "w")
                for allfitnesses_onegen in allgenfitnesses:
                    o.write(str(allfitnesses_onegen) + "\n")
                o.close()
                return best_genome, generation

            # Generation Stats
            if self.Config.VERBOSE:
                print('Finished Generation', generation)
                print('Best Genome Fitness:', best_genome.fitness)
                if hasattr(best_genome, "avgloss"):
                    print('Best Genome Loss:', best_genome.avgloss)
                print('Best Genome Length', len(best_genome.connection_genes))
                print()

        o = open("allgenfitnesses.txt", "w")
        for allfitnesses_onegen in allgenfitnesses:
            o.write(str(allfitnesses_onegen) + "\n")
        o.close()

        return None, None
示例#37
0
import random
import cPickle as pickle
import math

from scraper import Scraper
from neat import visualize
from neat.nn import nn_pure as nn

stats_scraper = Scraper()
games = stats_scraper.get_games()

file = open("winner_chromosome")
chromo = pickle.load(file)
best_net = nn.create_ffphenotype(chromo)
file.close()
visualize.draw_net(chromo)
print '\nBest network output:'
brain = nn.create_ffphenotype(chromo)
print brain.neurons
print brain.synapses

file = open("shapes")
shapes = pickle.load(file)
file.close()

j = 0
s = float(0)
tp = float(0)
fp = float(0)
fn = float(0)
hw = float(0)
示例#38
0
    # you can also set the configs in dpole_config as long
    # as you have two config files for each type of experiment
    config.Config.input_nodes = 3

    # neuron model type
    chromosome.node_gene_type = genome.NodeGene
    #chromosome.node_gene_type = genome.CTNodeGene

    population.Population.evaluate = evaluate_population
    pop = population.Population()
    pop.epoch(500, report=1, save_best=0)

    winner = pop.stats[0][-1]

    # visualize the best topology
    visualize.draw_net(winner) # best chromosome
    # Plots the evolution of the best/average fitness
    visualize.plot_stats(pop.stats)
    # Visualizes speciation
    visualize.plot_species(pop.species_log)

    print 'Number of evaluations: %d' %winner.id
    print 'Winner score: %d' %winner.score
    from time import strftime
    date = strftime("%Y_%m_%d_%Hh%Mm%Ss")
    # saves the winner
    file = open('winner_'+date, 'w')
    pickle.dump(winner, file)
    file.close()

    #print winner
示例#39
0
for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated):
    times = [t for t, v in neuron_data.values()[0]]
    nodes = list(neuron_data.keys())
    time_data = []
    for i, t in enumerate(times):
        tdata = {}
        for n in nodes:
            tdata[n] = neuron_data[n][i][1]

        tdata[0] = 0.0 if inputData[0] == 0 else -75.0
        tdata[1] = 0.0 if inputData[1] == 0 else -75.0

        time_data.append(tdata)

    for ti, (t, tdata) in enumerate(zip(times, time_data)):
        node_colors = {}
        for n, v in tdata.items():
            node_colors[n] = voltage_to_color(v)

        fn = 'spiking-{0:04d}'.format(len(filenames))
        dot = visualize.draw_net(winner, filename=fn, view=False, node_names=node_names, node_colors=node_colors,
                                 fmt='png', show_disabled=False, prune_unused=True)
        filenames.append(dot.filename + '.png')

clip = ImageSequenceClip(filenames, fps=30)
clip.write_videofile('spiking.mp4', codec="mpeg4", bitrate="2000k")

for fn in filenames:
    os.unlink(fn)
    os.unlink(fn[:-4])
示例#40
0
    task.max_samples = 1000

    i = 0
    while os.path.isfile(os.path.join(path, "checkpoint_%d" % i)):
        checkpoint = os.path.join(path, "checkpoint_%d" % i)
        print("load checkpoint %d" % i)
        pop = population.Population('examples/neat/neat_config',
                                    checkpoint_file=checkpoint)

        for n in reversed(range(1,
                                min(best_n, len(pop.most_fit_genomes)) + 1)):
            control.pause()
            input("run %d. best in checkpoint %d" % (n, i))

            winner = pop.most_fit_genomes[-1]

            agent = NeatAgent(winner, True)

            for state in range(control.get_randomize_states()):
                control.randomize(state)
                task.f(agent)
                control.pause()

            visualize.draw_net(winner, view=False)

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

        i += 1
示例#41
0
            fitness += 1
            if (abs(x) >= 2.4 or abs(theta) >= twelve_degrees):
            #if abs(theta) > twelve_degrees: # Igel (p. 5) uses theta criteria only
                # the cart/pole has run/inclined out of the limits
                break
                
        chromo.fitness = fitness

if __name__ == "__main__":
     
    config.load('spole_config') 

    # Temporary workaround
    chromosome.node_gene_type = genome2.NodeGene
    
    population.Population.evaluate = evaluate_population
    pop = population.Population()
    pop.epoch(200, report=1, save_best=0)
    
    print 'Number of evaluations: %d' %(pop.stats[0][-1]).id
    
    # visualize the best topology
    visualize.draw_net(pop.stats[0][-1]) # best chromosome
    # Plots the evolution of the best/average fitness
    #visualize.plot_stats(pop.stats)
    
    # saves the winner
    file = open('winner_chromosome', 'w')
    pickle.dump(pop.stats[0][-1], file)
    file.close()
示例#42
0
import pickle
from neat import nn, parallel, population, visualize
from neat.config import Config
from neat.math_util import mean

with open('best.pkl', 'rb') as f:
    winner = pickle.load(f)
print(winner)

#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)
示例#43
0
road=10
roadFile=open('Roads/'+str(road)+'.road','rb')
byteList=b''
for i in roadFile.readlines():
    byteList+=i
trackDim=pickle.loads(byteList)[0]
segments=pickle.loads(byteList)[1]
start=pickle.loads(byteList)[2]
offset=[0,0]
config=neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, "NEATConfig.txt")
AI=open('bestCar2.nn','rb')
allBytes=b''
for i in AI.readlines():
    allBytes+=i
carGenome=pickle.loads(allBytes)
visualize.draw_net(config,carGenome,filename='network',view=False,fmt='png')
carNet=neat.nn.FeedForwardNetwork.create(carGenome,config)
def sigmoid(x,scale):
    return (e**x)/((e**x)+1)*scale+0.5
def bezierCurve(res,*pointlist):
    allPoints = [((1 - (i / res)) * ((1 - (i / res)) * pointlist[0][0] + (i / res) * pointlist[1][0]) + (i / res) * ((1 - (i / res)) * pointlist[1][0] + (i / res) * pointlist[2][0]),(1 - (i / res)) * ((1 - (i / res)) * pointlist[0][1] + (i / res) * pointlist[1][1]) + (i / res) * ((1 - (i / res)) * pointlist[1][1] + (i / res) * pointlist[2][1])) for i in range(res+1)]
    return allPoints
def bezFirstDer(res,*pointlist):
    allPoints = [tuple([2*(1-i/res)*(pointlist[1][x]-pointlist[0][x])+2*(i/res)*(pointlist[2][x]-pointlist[1][x]) for x in range(2)]) for i in range(res + 1)]
    return allPoints
allBezierPoints=[bezierCurve(100,*i) for i in segments]
lastMouse=False
zoom=-log(7,e)
#zoom=-6
#zoom=2.054
#zoom=6
示例#44
0
    pop = neat.population.Population(config)
    stats = neat.statistics.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))
    winner = pop.run(eval_fitness, generations)
    return winner, stats


# If run as script.
if __name__ == '__main__':
    result = run(1000)
    winner = result[0]
    stats = result[1]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs
        output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save net if wished reused and draw it to a file.
    with open('winner_neat_xor.pkl', 'wb') as output:
        pickle.dump(winner_net, output, pickle.HIGHEST_PROTOCOL)
    draw_net(winner_net, filename="neat_xor_winner")
    plot_stats(stats, ylog=False, view=True, filename='avg_fitness_neat.svg')
    plot_species(stats, view=True, filename='speciation_neat.svg')
示例#45
0
# Test the performance of the best genome produced by nn_evolve.py.

# Test the performance of the best genome produced by nn_evolve.py.
from __future__ import print_function

import pickle

from neat import nn, parallel, population, visualize

# load the winner
with open('nn_pop0', 'rb') as f:
    pop = pickle.load(f)

winner = pop.statistics.best_genome()

# Plot the evolution of the best/average fitness.
visualize.plot_stats(pop.statistics, ylog=True, filename="nn_fitness.svg")
# Visualizes speciation
visualize.plot_species(pop.statistics, filename="nn_speciation.svg")
# Visualize the best network.
visualize.draw_net(winner, view=False, filename="nn_winner.gv")
visualize.draw_net(winner, view=False, filename="nn_winner-enabled.gv", show_disabled=False)
visualize.draw_net(winner, view=False, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True)
示例#46
0
from cart_pole import discrete_actuator_force
from fitness import evaluate_population


# Use the CTRNN network phenotype and the discrete actuator force function.
def fitness_function(genomes):
    evaluate_population(genomes, ctrnn.create_phenotype, discrete_actuator_force)

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

pop = population.Population(config, node_gene_type=genes.CTNodeGene)
pop.epoch(fitness_function, 2000, report=1, save_best=0)

# Save the winner.
winner = pop.most_fit_genomes[-1]
print('Number of evaluations: %d' % winner.ID)
with open('ctrnn_winner_genome', 'wb') as f:
    pickle.dump(winner, f)

print(winner)

# Plot the evolution of the best/average fitness.
visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores, ylog=True, filename="ctrnn_fitness.svg")
# Visualizes speciation
visualize.plot_species(pop.species_log, filename="ctrnn_speciation.svg")
# Visualize the best network.
visualize.draw_net(winner, view=True, filename="ctrnn_winner.gv")
示例#47
0
        outputs_train = []
        for xi in x[train_index]:
            outputs_train.append(np.argmax(winner_net.activate(xi)))           
        
        accuracies_train.append(accuracy_score(y[train_index], outputs_train))

        outputs_test = []
        for xi in x[test_index]:
            outputs_test.append(np.argmax(winner_net.activate(xi)))
        
        scikitplot.metrics.plot_confusion_matrix(y[test_index], outputs_test, title="Four Classes Confusion Matrix: Fold " + str(n_fold) , normalize=True, text_fontsize="small")
        plt.savefig("4-classes-cf-" + str(n_fold) + ".png")
        plt.clf()
        plt.close()

        vs.draw_net(config, winner, False, filename='4-classes-winner-' + str(n_fold), show_disabled=False)
        with open('4-classes-winner-' + str(n_fold), 'wb') as f:
            pickle.dump(winner, f)
        
        with open('4-classes-results-' + str(n_fold), 'w') as f:
            f.write('\nBest genome:\n{!s}'.format(winner))
        
        accuracies_test.append(accuracy_score(y[test_index], outputs_test))
        precisions.append(precision_score(y[test_index], outputs_test, average='macro', zero_division=0))
        recalls.append(recall_score(y[test_index], outputs_test, average='macro', zero_division=0))
        f1_scores.append(f1_score(y[test_index], outputs_test, average='macro', zero_division=0))
        
        n_fold += 1

    with open("results-4-classes.csv", 'w', encoding='UTF8', newline='') as csvfile:
        writer = csv.writer(csvfile)
示例#48
0
文件: render.py 项目: qw/2048-neat
import pickle
import pygame

import evolve_2048
from neat import nn, visualize

evolve_2048.W = 1000
evolve_2048.H = 1000

pb = evolve_2048.PictureBreeder(128, 128, 1500, 1500, 1280, 1024, 'color', 4)

with open("genome-20219-701.bin", "rb") as f:
    g = pickle.load(f)
    print g
    node_names = {0: 'x', 1: 'y', 2: 'gray'}
    visualize.draw_net(g,
                       view=True,
                       filename="picture-net.gv",
                       show_disabled=False,
                       prune_unused=True,
                       node_names=node_names)

    net = nn.create_feed_forward_phenotype(g)
    pb.make_high_resolution(g)
示例#49
0
import neat.experiments.mountain_climbing.config as c
from neat.visualize import draw_net
from neat.phenotype.feed_forward import FeedForwardNet

logger = logging.getLogger(__name__)

logger.info(c.MountainClimbConfig.DEVICE)
neat = pop.Population(c.MountainClimbConfig)
solution, generation = neat.run()

if solution is not None:
    logger.info(
        f'Found a Solution at generation {generation} with fitness {solution.fitness}'
    )
    draw_net(solution,
             view=True,
             filename='./images/mountain-climb-solution',
             show_disabled=True)

    # OpenAI Gym
    env = gym.make('MountainCarContinuous-v0')
    done = False
    observation = env.reset()

    fitness = 0
    phenotype = FeedForwardNet(solution, c.MountainClimbConfig)

    while not done:
        env.render()
        input = torch.Tensor([observation]).to(c.MountainClimbConfig.DEVICE)

        pred = [round(float(phenotype(input)))]
示例#50
0
    neat = pop.Population(c.XORConfig)
    solution, generation = neat.run()

    if solution is not None:
        avg_num_generations = ((avg_num_generations * num_of_solutions) +
                               generation) / (num_of_solutions + 1)
        min_num_generations = min(generation, min_num_generations)

        num_hidden_nodes = len(
            [n for n in solution.node_genes if n.type == 'hidden'])
        avg_num_hidden_nodes = ((avg_num_hidden_nodes * num_of_solutions) +
                                num_hidden_nodes) / (num_of_solutions + 1)
        min_hidden_nodes = min(num_hidden_nodes, min_hidden_nodes)
        max_hidden_nodes = max(num_hidden_nodes, max_hidden_nodes)
        if num_hidden_nodes == 1:
            found_minimal_solution += 1

        num_of_solutions += 1
        draw_net(solution,
                 view=True,
                 filename='./images/solution-' + str(num_of_solutions),
                 show_disabled=True)

print('Total Number of Solutions: ', num_of_solutions)
print('Average Number of Hidden Nodes in a Solution', avg_num_hidden_nodes)
print('Solution found on average in:', avg_num_generations, 'generations')
print('Minimum number of hidden nodes:', min_hidden_nodes)
print('Maximum number of hidden nodes:', max_hidden_nodes)
print('Minimum number of generations:', min_num_generations)
print('Found minimal solution:', found_minimal_solution, 'times')
示例#51
0
import pickle
import pygame

import evolve
from neat import nn, visualize

evolve.W = 1000
evolve.H = 1000

pb = evolve.PictureBreeder(128, 128, 1500, 1500, 1280, 1024, 'color', 4)

with open("genome-20219-701.bin", "rb") as f:
    g = pickle.load(f)
    print g
    node_names = {0: 'x', 1: 'y', 2: 'gray'}
    visualize.draw_net(g, view=True, filename="picture-net.gv",
                       show_disabled=False, prune_unused=True, node_names=node_names)

    net = nn.create_feed_forward_phenotype(g)
    pb.make_high_resolution(g)