Exemplo n.º 1
0
    def setUp(self):

        neuron_sigma = 0.5
        connection_sigma = 0.5
        PS = NumericParamSpec

        self.correct_types = set(
            ['Input', 'Simple', 'Sigmoid', 'Oscillator', 'default'])

        self.correct_sigmoid_spec = GeneSpec("Sigmoid",
                                             PS('bias', -1., 1., neuron_sigma),
                                             PS('gain', 0., 1., neuron_sigma))

        neuron_specs = [
            GeneSpec("Input"),
            self.correct_sigmoid_spec,
            GeneSpec("Simple", PS('bias', -1., 1., neuron_sigma),
                     PS('gain', 0., 1., neuron_sigma)),
            GeneSpec("Oscillator", PS("period", 0., 10., neuron_sigma),
                     PS("phase_offset", 0., 3.14, neuron_sigma),
                     PS("amplitude", 0., 10000., neuron_sigma)),
        ]

        connection_specs = [
            GeneSpec(
                "default",
                PS('weight', mutation_sigma=connection_sigma, mean_value=0.)),
        ]

        self.netspec = NetworkSpec(neuron_specs, connection_specs)
Exemplo n.º 2
0
def get_extended_mutation_spec(neuron_sigma, connection_sigma):
    neuron_specs = [
        GeneSpec("Input"),
        GeneSpec("Sigmoid", PS('bias', -1., 1., neuron_sigma),
                 PS('gain', 0., 1., neuron_sigma)),
        GeneSpec("Simple", PS('bias', -1., 1., neuron_sigma),
                 PS('gain', 0., 1., neuron_sigma)),
        GeneSpec("Oscillator", PS("period", 0., 10., neuron_sigma),
                 PS("phase_offset", 0., 3.14, neuron_sigma),
                 PS("amplitude", 0., 10000., neuron_sigma)),
        GeneSpec("Bias", PS('bias', -1., 1., neuron_sigma)),
        GeneSpec("DifferentialCPG", PS('bias', -1., 1.)),

        # these neurons (V-Neuron and X-Neuron) are for the nonlinear oscillator CPG model found in Ijspeert (2005):
        GeneSpec("V-Neuron", PS('alpha', 0.05, 10., neuron_sigma),
                 PS('tau', 1., 50., neuron_sigma),
                 PS('energy', 0., 25., neuron_sigma)),
        GeneSpec("X-Neuron", PS('tau', 0.01, 5.)),
    ]

    connection_specs = [
        GeneSpec("default",
                 PS('weight', mutation_sigma=connection_sigma, mean_value=0.)),
    ]

    return NetworkSpec(neuron_specs, connection_specs)
Exemplo n.º 3
0
    def test_loopback_protection(self):

        genome = self.loopback_test_genome.copy()

        net_spec = NetworkSpec([], [GeneSpec('new_connection')])

        mutator = Mutator(net_spec=net_spec)

        con_added = False
        con_added = mutator.add_connection_mutation(genome)
        # print("---------------- ----------------")
        # print(genome)
        self.assertTrue(con_added, msg="Connection should have been added")

        genome = self.loopback_test_genome.copy()

        mutator = Mutator(net_spec=net_spec,
                          pure_input_types=('input_type_1, input_type_2'))

        con_added = False
        con_added = mutator.add_connection_mutation(genome)
        # print("---------------- ----------------")
        # print(genome)
        self.assertFalse(con_added,
                         msg="Connection should have not been added")
Exemplo n.º 4
0
def get_mutation_spec(neuron_sigma, connection_sigma):
    neuron_specs = [
        GeneSpec("Input"),
        GeneSpec("Sigmoid", PS('bias', -1., 1., neuron_sigma),
                 PS('gain', 0., 1., neuron_sigma)),
        GeneSpec("Simple", PS('bias', -1., 1., neuron_sigma),
                 PS('gain', 0., 1., neuron_sigma)),
        GeneSpec("Oscillator", PS("period", 0., 10., neuron_sigma),
                 PS("phase_offset", 0., 3.14, neuron_sigma),
                 PS("amplitude", 0., 10000., neuron_sigma)),
    ]

    connection_specs = [
        GeneSpec("default",
                 PS('weight', mutation_sigma=connection_sigma, mean_value=0.)),
    ]

    return NetworkSpec(neuron_specs, connection_specs)
Exemplo n.º 5
0
    connection_param_mut_proba=
    0.5,  # probability to mutate each single connection in the genome
    structural_augmentation_proba=
    0.8,  # probability to augment the topology of a newly created genome 
    structural_removal_proba=
    0.0,  # probability to diminish the topology of a newly created genome
    speciation_threshold=
    0.005  # genomes that are more similar than this value will be considered the same species
)
#### ###### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### ####

net_spec = NetworkSpec([
    GeneSpec('input', NPS('layer', ['input'], mutable=False)),
    GeneSpec('sigmoid', PS('bias', -1., 1., neuron_sigma, mutable=True),
             PS('gain', 0, 1., neuron_sigma, mutable=True),
             NPS('layer', ['hidden'], mutable=False))
], [
    GeneSpec(
        'default',
        PS('weight', mutation_sigma=conn_sigma, mean_value=0., mutable=True))
])

mut = Mutator(net_spec, allowed_neuron_types=['sigmoid'])

neat_obj = NEAT(mutator=mut, **conf)

genome = neat_obj.get_init_genome(in1=neuron('input',
                                             protected=True,
                                             layer='input'),
                                  in2=neuron('input',
                                             protected=True,
                                             layer='input'),
Exemplo n.º 6
0
class TestSpecs(unittest.TestCase):
    def setUp(self):

        neuron_sigma = 0.5
        connection_sigma = 0.5
        PS = NumericParamSpec

        self.correct_types = set(
            ['Input', 'Simple', 'Sigmoid', 'Oscillator', 'default'])

        self.correct_sigmoid_spec = GeneSpec("Sigmoid",
                                             PS('bias', -1., 1., neuron_sigma),
                                             PS('gain', 0., 1., neuron_sigma))

        neuron_specs = [
            GeneSpec("Input"),
            self.correct_sigmoid_spec,
            GeneSpec("Simple", PS('bias', -1., 1., neuron_sigma),
                     PS('gain', 0., 1., neuron_sigma)),
            GeneSpec("Oscillator", PS("period", 0., 10., neuron_sigma),
                     PS("phase_offset", 0., 3.14, neuron_sigma),
                     PS("amplitude", 0., 10000., neuron_sigma)),
        ]

        connection_specs = [
            GeneSpec(
                "default",
                PS('weight', mutation_sigma=connection_sigma, mean_value=0.)),
        ]

        self.netspec = NetworkSpec(neuron_specs, connection_specs)

    def test_numeric_spec(self):
        print("Testing NumericParamSpec")

        ps = NumericParamSpec('numpar',
                              -87,
                              17,
                              mutation_sigma=0.5,
                              mean_value=5.)
        self.assertEquals(-87,
                          ps.put_within_bounds(-999),
                          msg="NumericParamSpec.put_within_bounds() : Failed")

        self.assertEquals(17,
                          ps.put_within_bounds(999),
                          msg="NumericParamSpec.put_within_bounds() : Failed")

    def test_network_spec(self):
        print("Testing NetworkSpec")

        correct_types = set(
            ['Input', 'Simple', 'Sigmoid', 'Oscillator', 'default'])

        self.assertEquals(
            self.correct_types,
            set(self.netspec.gene_types()),
            msg="NetworkSpec.gene_types() method gave incorrect result")

        self.assertEquals(
            self.correct_types,
            set(gt for gt in self.netspec),
            msg=("Iteration through NetworkSpec instance did not work"
                 " ([gt for gt in self.netspec] gave incorrect result)"))

        self.assertEquals(
            self.netspec['Sigmoid'],
            self.correct_sigmoid_spec,
            msg="NetworkSpec's [] operator (__getitem__) gave incorrect result"
        )