def test_indexer():
    indexer0 = Indexer(0)
    assert indexer0.get_next() == 0
    assert indexer0.get_next() == 1
    assert indexer0.get_next() == 2

    indexer17 = Indexer(17)
    assert indexer17.get_next() == 17
    assert indexer17.get_next() == 18
    assert indexer17.get_next() == 19
Beispiel #2
0
class Neuron(object):
    """ A simple sigmoidal neuron """
    # TODO: Get rid of these global indexers.
    indexer = Indexer(0)

    def __init__(self, neuron_type, ID, bias, response, activation_type):
        assert neuron_type in ('INPUT', 'OUTPUT', 'HIDDEN')

        self.type = neuron_type
        self.ID = self.indexer.next(ID)
        self.bias = bias
        self.response = response
        self.activation = activation_functions.get(activation_type)

        self._synapses = []
        self.output = 0.0  # for recurrent networks all neurons must have an "initial state"

    def activate(self):
        """Activates the neuron"""
        assert self.type is not 'INPUT'
        z = self.bias + self.response * self._update_activation()
        return self.activation(z)

    def _update_activation(self):
        soma = 0.0
        for s in self._synapses:
            soma += s.incoming()
        return soma

    def create_synapse(self, s):
        self._synapses.append(s)

    def __repr__(self):
        return '{0:d} {1!s}'.format(self.ID, self.type)
Beispiel #3
0
    def __init__(self, config, reporters, stagnation):
        self.elitism = int(config.get('elitism'))
        self.survival_threshold = float(config.get('survival_threshold'))

        self.reporters = reporters
        self.genome_indexer = Indexer(1)
        self.stagnation = stagnation
        self.ancestors = {}
Beispiel #4
0
    def get_new_node_key(self, node_dict):
        if not hasattr(self, 'node_indexer'):
            self.node_indexer = Indexer(max(list(iterkeys(node_dict))) + 1)

        new_id = self.node_indexer.get_next()

        assert new_id not in node_dict

        return new_id
def test_indexer():
    indexer = Indexer(0)
    assert indexer.get_next() == 0
    assert indexer.get_next() == 1

    # TODO: Why doesn't Indexer remember its starting value given in the ctor?
    indexer.clear()
    assert indexer.get_next() == 1
    assert indexer.get_next() == 2
Beispiel #6
0
    def __init__(self, config, reporters):
        self.config = config
        params = config.get_type_config(self)
        self.elitism = int(params.get('elitism'))
        self.survival_threshold = float(params.get('survival_threshold'))

        self.reporters = reporters
        self.genome_indexer = Indexer(1)
        self.stagnation = config.stagnation_type(config, reporters)
Beispiel #7
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.species_indexer = Indexer(1)
        self.genome_indexer = Indexer(1)
        self.innovation_indexer = InnovationIndexer(0)
        self.reproduction = config.reproduction_type(self.config,
                                                     self.reporters,
                                                     self.genome_indexer,
                                                     self.innovation_indexer)

        self.species = []
        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._create_population()
        self._speciate(initial_population)
Beispiel #8
0
 def __init__(self, config):
     self.indexer = Indexer(1)
     self.species = {}
     self.to_species = {}
Beispiel #9
0
 def __init__(self, config):
     self.config = config
     self.indexer = Indexer(1)
     self.species = []
Beispiel #10
0
 def __init__(self, config, reporters):
     self.reporters = reporters
     self.indexer = Indexer(1)
     self.species = {}
     self.genome_to_species = {}