コード例 #1
0
def test_minimal():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    config.save_best = True

    pop = Population(config)
    # run the simulation for up to 20 generations
    pop.run(eval_fitness, 20)

    # Test save_checkpoint with defaults
    pop.save_checkpoint()

    # get statistics
    best_unique = pop.statistics.best_unique_genomes(1)
    best_unique = pop.statistics.best_unique_genomes(2)
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 1
    assert all(f == 1 for f in avg_fitness)

    # Change fitness threshold and do another run.
    config.max_fitness_threshold = 1.1
    pop = Population(config)
    # runs the simulation for 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 20
    assert all(f == 1 for f in avg_fitness)
コード例 #2
0
def main():
    print("main")
    config = Config(config_path)
    # config.tcp_port += np.random.randint(0, 1000)
    config.cluster_evaluation = False
    # config.cluster_nodes_loc = "/home/adam/Workspace/pycharm/neat/nodes/"
    # config.cluster_main_loc = "/home/adam/Workspace/pycharm/neat/nodes/"
    # config.work_dir = "/home/adam/Results/local_0/"
    # config.ray_info_loc = "/home/cec0113/ray_info/info"

    # config.cluster_evaluation = True
    # ray.init()

    if os.path.isfile(config.work_dir + "autosave"):
        config = Neat.open(config.work_dir + "autosave").config

    print("Starting neat")
    if os.path.isfile(config.work_dir + "autosave"):
        print("opening")
        neat = Neat.open(config.work_dir + "autosave")
    else:
        print("initing")
        neat = Neat(config)
        neat.add_observer(CsvObserver(neat.config))
        neat.add_observer(ConsoleObserver(neat.config.work_dir, neat.config))
        neat.add_observer(PlotObserver(neat.config.work_dir, 25))
        neat.add_observer(RenderObserver(25))
        # neat.add_observer(AutosaveObserver(neat.config.work_dir, neat.config.walltime_sec, 25))

    print("start")
    print(neat.config.dataset_name)
    neat.start()
コード例 #3
0
    def __init__(self, config, initial_population=None):
        """
        :param config: Either a config.Config object or path to a configuration file.
        :param initial_population: Either an initial set of Genome instances to be used
               as the initial population, or None, in which case a randomized set of Genomes
               will be created automatically based on the configuration parameters.
        """

        # If config is not a Config object, assume it is a path to the config file.
        if not isinstance(config, Config):
            config = Config(config)

        # Configure statistics and reporting as requested by the user.
        self.reporters = ReporterSet()
        if config.collect_statistics:
            self.statistics = StatisticsReporter()
            self.add_reporter(self.statistics)
        else:
            self.statistics = None

        if config.report:
            self.add_reporter(StdOutReporter())

        self.config = config
        self.reproduction = config.reproduction_type(config, self.reporters)

        self.species = SpeciesSet(config)
        self.generation = -1
        self.total_evaluations = 0

        # Create a population if one is not given, then partition into species.
        if initial_population is None:
            initial_population = self.reproduction.create_new(config.pop_size)
        self.species.speciate(initial_population)
コード例 #4
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)
コード例 #5
0
    def __init__(self, config, initial_population=None):
        """
        :param config: Either a config.Config object or path to a configuration file.
        :param initial_population: Either an initial set of Genome instances to be used
               as the initial population, or None, in which case a randomized set of Genomes
               will be created automatically based on the configuration parameters.
        """

        # If config is not a Config object, assume it is a path to the config file.
        if not isinstance(config, Config):
            config = Config(config)

        # Configure statistics and reporting as requested by the user.
        self.reporters = ReporterSet()
        if config.collect_statistics:
            self.statistics = StatisticsReporter()
            self.add_reporter(self.statistics)
        else:
            self.statistics = None

        if config.report:
            self.add_reporter(StdOutReporter())

        self.config = config
        self.reproduction = config.reproduction_type(config, self.reporters)

        self.species = SpeciesSet(config)
        self.generation = -1
        self.total_evaluations = 0

        # Create a population if one is not given, then partition into species.
        if initial_population is None:
            initial_population = self.reproduction.create_new(config.pop_size)
        self.species.speciate(initial_population)
コード例 #6
0
    def __init__(self, config: Config, initial_state=None):
        self.reporters: ReporterSet = ReporterSet()
        self.config = config
        stagnation: DefaultStagnation = config.stagnation_type(
            config.stagnation_config, self.reporters)
        self.reproduction: DefaultReproduction = config.reproduction_type(
            config.reproduction_config, self.reporters, stagnation)
        if config.fitness_criterion == "max":
            self.fitness_criterion = max
        elif config.fitness_criterion == "min":
            self.fitness_criterion = min
        elif config.fitness_criterion == "mean":
            self.fitness_criterion = mean
        elif not config.no_fitness_termination:
            raise RuntimeError("Unexpected fitness_criterion: {0!r}".format(
                config.fitness_criterion))

        if initial_state is None:
            # Create a population from scratch, then partition into species.
            self.population: Dict[
                int, DefaultGenome] = self.reproduction.create_new(
                    config.genome_type, config.genome_config, config.pop_size)
            self.species_set: DefaultSpeciesSet = config.species_set_type(
                config.species_set_config, self.reporters)
            self.generation: int = 0
            self.species_set.speciate(config, self.population, self.generation)
        else:
            self.population, self.species_set, self.generation = initial_state

        self.best_genome: Optional[DefaultGenome] = None
コード例 #7
0
ファイル: population.py プロジェクト: arkadyark/PongAI
    def __init__(self, config, initial_population=None):
        """
        :param config: Either a config.Config object or path to a configuration file.
        :param initial_population:
        """

        # If config is not a Config object, assume it is a path to the config file.
        if not isinstance(config, Config):
            config = Config(config)

        self.config = config
        self.diversity = config.diversity_type(self.config)
        self.species_indexer = Indexer(1)
        self.genome_indexer = Indexer(1)

        self.species = []
        self.generation_statistics = []
        self.most_fit_genomes = []
        self.generation = -1
        self.total_evaluations = 0

        if initial_population is None:
            initial_population = self._create_population()

        # Partition the population into species based on current configuration.
        self._speciate(initial_population)
コード例 #8
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)
コード例 #9
0
def test_minimal():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = Population(config)
    # run the simulation for up to 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 1
    assert all(f == 1 for f in avg_fitness)

    # Change fitness threshold and do another run.
    config.max_fitness_threshold = 1.1
    pop = Population(config)
    # runs the simulation for 20 generations
    pop.run(eval_fitness, 20)

    # get statistics
    avg_fitness = pop.statistics.get_average_fitness()
    assert len(avg_fitness) == 20
    assert all(f == 1 for f in avg_fitness)
コード例 #10
0
def neat_start(run: int, p_call: bool):
    print("main")
    config = Config(config_path)
    config.work_dir = config.work_dir[:-1] + "_" + str(run) + "/"

    neat = None
    try:
        if os.path.isfile(config.work_dir + "autosave"):
            neat = Neat.open(config.work_dir + "autosave")
    except EOFError:
        if os.path.isfile(config.work_dir + "autosave_prev"):
            neat = Neat.open(config.work_dir + "autosave_prev")

    if neat is not None:
        config = neat.config
        if config.done:
            neat_start(run + 1, p_call)

    files = os.listdir(config.cluster_main_loc)
    hostname = socket.gethostname()

    if any(hostname in h for h in files):
        if p_call:
            if config.cluster_evaluation:
                print("Starting node client")
                config.mp_max_proc = config.cluster_main_max_load
                ClusterNode(config, True)

                time.sleep(5)  # Wait for other cluster nodes to startup...
                subprocess.call([
                    "qsub", "-q", "qexp", "-l", "select=8:ncpus=24",
                    "/home/cec0113/myjob_multi"
                ])
            else:
                subprocess.call([
                    "qsub", "-q", "qexp", "-l", "select=1:ncpus=24",
                    "/home/cec0113/myjob_multi"
                ])

        p_call = False

        print("Starting neat")
        if neat is None:
            print("initing")
            neat = Neat(config)
            neat.add_observer(CsvObserver(neat.config))
            neat.add_observer(
                ConsoleObserver(neat.config.work_dir, neat.config))
            neat.add_observer(PlotObserver(neat.config.work_dir, 25))
            neat.add_observer(
                AutosaveObserver(neat.config.work_dir,
                                 neat.config.walltime_sec, 25))

        print("run")
        print("Start")
        neat.start()
        neat_start(run + 1, p_call)
    else:
        print("Starting node")
        ClusterNode(config).run()
コード例 #11
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)
コード例 #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
ファイル: xor2_spiking.py プロジェクト: gilzamir/neat-python
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()
コード例 #14
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:')

    # Create a network of "fast spiking" Izhikevich neurons, simulation time step 0.25 millisecond.
    net = iznn.create_phenotype(winner, **neuron_params)
    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
        num_steps = int(max_time / dt)
        for j in range(num_steps):
            t = dt * j
            output = net.advance(dt)

            # 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)
コード例 #15
0
    def setUp(self):
        local_dir = os.path.dirname(__file__)
        indexer = InnovationIndexer(0)
        config = Config(os.path.join(local_dir, 'config.txt'))

        self.genome_config = {
            'inputs': ['in0', 'in1'],
            'outputs': [('out0', 'sigmoid')],
            'modules': []
        }
        config.genome_config = self.genome_config
        self.genome = CellGenome.create_unconnected(1, config)
コード例 #16
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()
コード例 #17
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]
コード例 #18
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)
コード例 #19
0
def check_self_crossover(genome_type):
    # Check that self-crossover produces a genetically identical child (with a different ID).
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    c = genome_type.create_unconnected(1, config)
    c.connect_full()
    c.fitness = 0.0

    cnew = c.crossover(c, 2)
    assert cnew.ID != c.ID
    assert len(cnew.conn_genes) == len(c.conn_genes)
    for kold, vold in cnew.conn_genes.items():
        print(kold, vold)
        print(c.conn_genes)
        assert kold in c.conn_genes
        vnew = c.conn_genes[kold]
        assert vold.is_same_innov(vnew)

        assert vnew.weight == vold.weight
        assert vnew.in_node_id == vold.in_node_id
        assert vnew.out_node_id == vold.out_node_id
        assert vnew.enabled == vold.enabled

    assert len(cnew.node_genes) == len(c.node_genes)
    for kold, vold in cnew.node_genes.items():
        assert kold in c.node_genes
        vnew = c.node_genes[kold]

        assert vnew.ID == vold.ID
        assert vnew.type == vold.type
        assert vnew.bias == vold.bias
        assert vnew.response == vold.response
        assert vnew.activation_type == vold.activation_type
コード例 #20
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)
コード例 #21
0
def test_nonexistent_config():
    """Check that attempting to open a non-existent config file raises
    an Exception with appropriate message."""
    passed = False
    try:
        c = Config('wubba-lubba-dub-dub')
    except Exception as e:
        passed = 'No such config file' in str(e)
    assert passed
コード例 #22
0
ファイル: test_genomes.py プロジェクト: WildBill567/nn-toy
 def test_randomly_created_genome(self):
     config = self.config
     config.num_outputs = 3
     config.num_inputs = 3
     genome = Genome(config)
     self.config = Config('../tests/conf_tester.conf')
     assert len(genome.output_genes) == 3, \
         "Genome should have 3 outputs, has %i" % len(genome.output_genes)
     assert len(genome.input_genes) == 3, \
         "Genome should have 3 inputs, has %i" % len(genome.input_genes)
コード例 #23
0
 def fit(self, input_matrix, target_vector, metric, verbose=False):
     self.input_matrix = input_matrix
     self.target_vector = target_vector
     self.metric = metric
     self._write_configuration()
     self.configuration = Config(DefaultGenome, DefaultReproduction, DefaultSpeciesSet, DefaultStagnation, get_configuration_path())
     remove_configuration()
     self._run(verbose)
     self.input_matrix = None
     self.target_vector = None
コード例 #24
0
def test_bad_config_activation():
    """Check that an unknown activation function raises an Exception with
    the appropriate message."""
    passed = False
    try:
        local_dir = os.path.dirname(__file__)
        c = Config(os.path.join(local_dir, 'bad_configuration1'))
    except Exception as e:
        passed = 'Invalid activation function name' in str(e)
    assert passed
コード例 #25
0
ファイル: test_genomes.py プロジェクト: WildBill567/nn-toy
 def setUp(self):
     self.config = Config('../tests/conf_tester.conf')
     NodeGene.counter = 1
     LinkGene.innovation_counter = 0
     self.test_node_genes = [NodeGene(node_type='INPUT',),
                             NodeGene(node_type='INPUT'),
                             NodeGene(node_type='OUTPUT'),
                             NodeGene(node_type='OUTPUT')]
     self.test_link_genes = [LinkGene(2, 3, weight=1), LinkGene(1, 3, weight=1)]
     self.genome = Genome(self.config, node_genes=self.test_node_genes, link_genes=self.test_link_genes)
コード例 #26
0
def test_config_options():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

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

    for hn in (0, 1, 2):
        config.hidden_nodes = hn
        for fc in (0, 1):
            config.fully_connected = fc
            for act in activation_functions.functions.keys():
                config.allowed_activation = [act]
                for ff in (0, 1):
                    config.feedforward = ff

                    pop = Population(config)
                    pop.run(eval_fitness, 250)
コード例 #27
0
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'))
    with open('winner.pkl', 'rb') as f:
        winner = pickle.load(f)
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    print('Score:', dino_api.play_game(GetCommand(winner_net)))
コード例 #28
0
def check_add_connection(genome_type, feed_forward):
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    config.input_nodes = 3
    config.output_nodes = 4
    config.hidden_nodes = 5
    config.feedforward = feed_forward
    N = config.input_nodes + config.hidden_nodes + config.output_nodes

    connections = {}
    for a in range(100):
        g = genome_type.create_unconnected(a, config)
        g.add_hidden_nodes(config.hidden_nodes)
        for b in range(1000):
            g.mutate_add_connection()
        for c in g.conn_genes.values():
            connections[c.key] = connections.get(c.key, 0) + 1

    # TODO: The connections should be returned to the caller and checked
    # against the constraints/assumptions particular to the network type.
    for i in range(N):
        values = []
        for j in range(N):
            values.append(connections.get((i, j), 0))
        print("{0:2d}: {1}".format(i, " ".join("{0:3d}".format(x) for x in values)))
コード例 #29
0
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

    initial_pop = []
    # initial_pop = [138, 74, 40, 84, 97, 133, 127, 60, 102, 70, 0, 1, 2]
    for root, dirs, files in os.walk('best'):
        for file_name in files:
            with open(os.path.join('best', file_name), 'rb') as f:
                initial_pop.append(pickle.load(f))
    if not initial_pop:
        initial_pop = None

    pop = population.Population(config, initial_pop)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.Checkpointer(5))
    pop.run(eval_fitness, 100)
コード例 #30
0
def check_simple(genome_type):
    indexer = InnovationIndexer(0)
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    c1 = genome_type.create_unconnected(1, config)
    c1.connect_full(indexer)

    # add two hidden nodes
    c1.add_hidden_nodes(2)

    # apply some mutations
    c1.mutate_add_node(indexer)
    c1.mutate_add_connection(indexer)
コード例 #31
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'))
    config.report = False

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

    winner = pop.statistics.best_genome()

    num_hidden = 0
    for ng in winner.node_genes.values():
        if ng.type == 'HIDDEN':
            num_hidden += 1

    num_connections = len(winner.conn_genes)
    num_enabled = 0
    for cg in winner.conn_genes.values():
        if cg.enabled:
            num_enabled += 1

    return pop.generation, num_hidden, num_connections, num_enabled
コード例 #32
0
def check_simple(genome_type):
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))
    c1 = genome_type.create_unconnected(1, config)
    c1.connect_full()
    repr(c1)
    str(c1)

    # add two hidden nodes
    c1.add_hidden_nodes(2)
    repr(c1)
    str(c1)

    # apply some mutations
    c1.mutate_add_node()
    c1.mutate_add_connection()
    repr(c1)
    str(c1)
コード例 #33
0
def test_checkpoint():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 0.99

    # creates the population
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'test_configuration'))

    pop = Population(config)
    pop.run(eval_fitness, 20)

    t = tempfile.NamedTemporaryFile(delete=False)
    t.close()
    pop.save_checkpoint(t.name)

    pop2 = Population(config)
    pop2.load_checkpoint(t.name)
コード例 #34
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)
コード例 #35
0
ファイル: test_phenome.py プロジェクト: WildBill567/nn-toy
 def setUp(self):
     self.config = Config('../tests/conf_tester.conf')
     NodeGene.counter = 1
     self.test_node_genes = [
         NodeGene(node_type='INPUT', activation='identity'),
         NodeGene(node_type='INPUT', activation='identity'),
         NodeGene(node_type='OUTPUT', activation='identity'),
         NodeGene(node_type='OUTPUT', activation='identity')
     ]
     self.test_link_genes = [
         LinkGene(1, 3, weight=1),
         LinkGene(2, 3, weight=1),
         LinkGene(1, 4, weight=1),
         LinkGene(2, 4, weight=1),
         LinkGene(0, 3, weight=1),
         LinkGene(0, 4, weight=1)
     ]
     self.genome = Genome(
         self.config,
         node_genes=self.test_node_genes,
         link_genes=self.test_link_genes,
     )
     self.phenome = FeedForwardPhenome(self.genome, self.config)
コード例 #36
0
    def setUp(self):
        local_dir = os.path.dirname(__file__)
        self.indexer = InnovationIndexer(0)
        self.config = Config(os.path.join(local_dir, 'config.txt'))

        self.prob_add = 0
        self.prob_remove = 0
        self.min_genes = 2
        self.max_genes = 3

        self.genome_config = {
            'inputs': ['in0', 'in1'],
            'outputs': [('out0', 'sigmoid')],
            'modules': [(Signal0Module, {
                'prob_add': self.prob_add,
                'prob_remove': self.prob_remove,
                'min_genes': self.min_genes,
                'max_genes': self.max_genes
            })]
        }

        self.config.genome_config = self.genome_config
        self.genome = CellGenome.create_unconnected(1, self.config)
        self.genome.connect_full(self.indexer)
コード例 #37
0
def test_config_options():
    # sample fitness function
    def eval_fitness(population):
        for individual in population:
            individual.fitness = 1.0

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

    for hn in (0, 1, 2):
        config.hidden_nodes = hn
        for fc in (0, 1):
            config.fully_connected = fc
            for act in activation_functions.functions.keys():
                config.allowed_activation = [act]
                for ff in (0, 1):
                    config.feedforward = ff

                    pop = Population(config)
                    pop.run(eval_fitness, 250)
コード例 #38
0
ファイル: cppn.py プロジェクト: laal65/CPPN-experiments
    return pop.statistics, out
    # return winner.fitness, out


def evolve_cppn(config, pattern, generations):
    return evolve(config, express_cppn, pattern, generations)


def evolve_recurrent_cppn(config, pattern, max_steps, generations):
    def eval_func(net, w, h):
        return express_recurrent_cppn(net, w, h, max_steps)

    return evolve(config, eval_func, pattern, generations)


if __name__ == '__main__':
    import numpy as np
    from neat.config import Config  # Python neat library
    from neat import nn
    import patterns
    target = patterns.checkerboard(16, 16, 4)
    config = Config('config.txt')
    config.pop_size = 50

    config.hidden_nodes = 0
    config.initial_connection = 'fully_connected'

    config.input_nodes = 7
    evolve_recurrent_cppn(config, target, 16, 50)
コード例 #39
0
ファイル: ctrnn_evolve.py プロジェクト: acontry/neat-python
            # without exceeding these limits.
            if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
                break

            fitness += 1.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.
コード例 #40
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()
コード例 #41
0
def test_iznn_evolve():
    """This is a stripped-down copy of the XOR2 spiking example."""

    # Network inputs and expected outputs.
    xor_inputs = ((0, 0), (0, 1), (1, 0), (1, 1))
    xor_outputs = (0, 1, 1, 0)

    # Maximum amount of simulated time (in milliseconds) to wait for the network to produce an output.
    max_time = 50.0

    def compute_output(t0, t1):
        '''Compute the network's output based on the "time to first spike" of the two output neurons.'''
        if t0 is None or t1 is None:
            # If one of the output neurons failed to fire within the allotted time,
            # give a response which produces a large error.
            return -1.0
        else:
            # If the output neurons fire within 1.0 milliseconds of each other,
            # the output is 1, and if they fire more than 11 milliseconds apart,
            # the output is 0, with linear interpolation between 1 and 11 milliseconds.
            response = 1.1 - 0.1 * abs(t0 - t1)
            return max(0.0, min(1.0, response))

    def simulate(genome):
        # Create a network of Izhikevich neurons based on the given genome.
        net = iznn.create_phenotype(genome, **iznn.THALAMO_CORTICAL_PARAMS)
        dt = 0.25
        sum_square_error = 0.0
        simulated = []
        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(dt)

                # 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)
            sum_square_error += (response - outputData) ** 2

            simulated.append(
                (inputData, outputData, t0, t1, v0, v1, neuron_data))

        return sum_square_error, simulated

    def eval_fitness(genomes):
        for genome in genomes:
            sum_square_error, simulated = simulate(genome)
            genome.fitness = 1 - sum_square_error

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

    # TODO: This is a little hackish, but will a user ever want to do it?
    # If so, provide a convenience method on Config for it.
    for i, tc in enumerate(config.type_config['DefaultStagnation']):
        if tc[0] == 'species_fitness_func':
            config.type_config['DefaultStagnation'][i] = (tc[0], 'median')

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

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

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()

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

    repr(winner)
    str(winner)
    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)