コード例 #1
0
def test_weight_mut():
    a = Genome()
    a.add_node(Node(NodeType.inp, 1))
    a.add_node(Node(NodeType.inp, 2))
    a.add_node(Node(NodeType.out, 3))
    a.add_connection(Connection(1, 3, 0.5, True, 1))
    a.add_connection(Connection(2, 3, 1.0, True, 2))
    a.conn_innovation = 3
    a.node_innovation = 4

    a.render("render/weight_begin.gv")
    a.mutate_weights()
    a.render("render/weight_mutated.gv")
コード例 #2
0
    def test_adding_connection(self):
        """ Tests new connection addition operation. """

        # Setup
        genome = Genome()
        genome.initialize(3, 2)

        connections_new = [(1, 4), (1, 5), (2, 4), (3, 4)]
        genome.connection_genes = [
            Connection(a, b) for a, b in connections_new
        ]

        # Run
        connection = genome.add_connection()

        # Verify
        genome_connections = genome.get_connections()
        self.assertEqual(len(genome_connections), len(connections_new) + 1)

        self.assertTrue(
            all([isinstance(c, Connection) for c in genome_connections]))
コード例 #3
0
def test_crossover():
    a = Genome()
    for i in range(3):
        a.add_node(Node(NodeType.inp, i + 1))
    a.add_node(Node(NodeType.out, 4))
    a.add_node(Node(NodeType.hidden, 5))
    a.add_connection(Connection(1, 4, 1.0, True, 1))
    a.add_connection(Connection(2, 4, 1.0, False, 2))
    a.add_connection(Connection(3, 4, 1.0, True, 3))
    a.add_connection(Connection(2, 5, 1.0, True, 4))
    a.add_connection(Connection(5, 4, 1.0, True, 5))
    a.add_connection(Connection(1, 5, 1.0, True, 8))
    a.conn_innovation = 9
    a.node_innovation = 6

    b = Genome()
    for i in range(3):
        b.add_node(Node(NodeType.inp, i + 1))
    b.add_node(Node(NodeType.out, 4))
    b.add_node(Node(NodeType.hidden, 5))
    b.add_node(Node(NodeType.hidden, 6))
    b.add_connection(Connection(1, 4, 1.0, True, 1))
    b.add_connection(Connection(2, 4, 1.0, False, 2))
    b.add_connection(Connection(3, 4, 1.0, True, 3))
    b.add_connection(Connection(2, 5, 1.0, True, 4))
    b.add_connection(Connection(5, 4, 1.0, False, 5))
    b.add_connection(Connection(5, 6, 1.0, True, 6))
    b.add_connection(Connection(6, 4, 1.0, True, 7))
    b.add_connection(Connection(3, 5, 1.0, True, 9))
    b.add_connection(Connection(1, 6, 1.0, True, 10))
    a.conn_innovation = 11
    a.node_innovation = 7

    c = Genome.crossover(b, a)

    a.render("render/crossover_a.gv")
    b.render("render/crossover_b.gv")
    c.render("render/crossover_c.gv")