Пример #1
0
def get_invalid4(cfg: Config):
    """
    Genome with connection from start to recurrent node, and from another recurrent node to the output.
    Configuration:
       0   1
           |
     2>    3>
     |
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=4,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0
    genome.nodes[3] = SimpleNodeGene(key=3, cfg=cfg.genome)  # Hidden node
    genome.nodes[3].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 2), (2, 2), (3, 3), (3, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #2
0
def get_invalid5(cfg: Config):
    """
    Genome with connections between the hidden nodes and to one output node.
    Configuration:
       0   1
           |
        2--3
     
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=4,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0
    genome.nodes[3] = SimpleNodeGene(key=3, cfg=cfg.genome)  # Hidden node
    genome.nodes[3].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(2, 3), (3, 2), (3, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #3
0
def get_valid2(cfg: Config):
    """
    Network with a recurrent connection (at node 2).
    Configuration:
         0   1
        /
       2>
      / \
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=2,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 2), (-2, 2), (2, 0), (2, 2)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #4
0
def get_genome2(cfg: Config):
    """
    Genome with all biases set to 0, only simple hidden nodes used, all connections enabled with weight 1.
    Configuration:
        0   1
       /    |
      2     \
     /       |
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
            key=2,
            num_outputs=cfg.genome.num_outputs,
            bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0
    
    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 2), (2, 0), (-3, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True
    
    return genome
Пример #5
0
def get_valid1(cfg: Config):
    """
    Simple network with only one input and one output used.
    Configuration:
         0   1
             |
             |
             |
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=1,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(-3, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #6
0
def get_circular2(cfg: Config):
    """
    Genome with circular connections, not connected to the output genome.
    Configuration:
       0   1
       
     2---3
     |   |
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=2,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0
    genome.nodes[3] = SimpleNodeGene(key=3, cfg=cfg.genome)  # Hidden node
    genome.nodes[3].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 2), (2, 3), (3, 2), (-2, 3)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #7
0
def get_pruned2(cfg: Config):
    """
    Genome with partially valid connections and nodes (dangling node on other hidden node).
    Configuration:
       0   1
      /
     2---3>
     |
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=2,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0
    genome.nodes[3] = SimpleNodeGene(key=3, cfg=cfg.genome)  # Hidden node
    genome.nodes[3].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 2), (2, 0), (2, 3), (3, 3)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #8
0
def get_invalid3(cfg: Config):
    """
    Genome without connections to the input nodes.
    Configuration:
       0   1
           |
           2>
          
    -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
        key=3,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    genome.nodes[2] = SimpleNodeGene(key=2, cfg=cfg.genome)  # Hidden node
    genome.nodes[2].bias = 0

    # Reset the connections
    genome.connections = dict()
    for key in [(2, 2), (2, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True

    return genome
Пример #9
0
def get_topology22222(gid: int, cfg: Config):
    """
    Create a uniformly and randomly sampled genome of fixed topology:
    Sigmoid with bias 1.5 --> Actuation default of 95,3%
      (key=0, bias=1.5)      (key=1, bias=?)
                     ____ /   /
                   /         /
               SRU-X        /
                |     _____/
                |   /
              (key=-1)
    """
    # Create an initial dummy genome with fixed configuration
    genome = Genome(
        key=gid,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )

    # Setup the parameter-ranges
    conn_range = cfg.genome.weight_max_value - cfg.genome.weight_min_value
    bias_range = cfg.genome.bias_max_value - cfg.genome.bias_min_value
    rnn_range = cfg.genome.rnn_max_value - cfg.genome.rnn_min_value

    # Create the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 1.5  # Drive with 0.953 actuation by default
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = random(
    ) * bias_range + cfg.genome.bias_min_value  # Uniformly sampled bias
    genome.nodes[2] = ConvexSruNodeGene(key=2, cfg=cfg.genome,
                                        input_keys=[-1])  # Hidden node
    genome.nodes[2].bias = 0  # Bias is irrelevant for GRU-node

    # Create the connections
    genome.connections = dict()

    # input2gru
    key = (-1, 2)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[key].weight = 1  # Simply forward distance
    genome.connections[key].enabled = True

    # gru2output - Uniformly sampled
    key = (2, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[key].weight = 3  # Enforce capabilities of full spectrum
    genome.connections[key].enabled = True

    # input2output - Uniformly sampled
    key = (-1, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = random() * conn_range + cfg.genome.weight_min_value
    genome.connections[key].enabled = True

    genome.update_rnn_nodes(config=cfg.genome)
    return genome
Пример #10
0
def get_topology2(cfg, random_init: bool = False):
    """
    Simple genome with only two connections:
        0   1
       /    |
      2    /
       \  /
       -1
    """
    # Create an initial dummy genome with fixed configuration
    genome = Genome(
        key=0,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )

    # Create the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0  # Drive with 0.5 actuation by default
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0  # Drive with 0.5 actuation by default
    genome.nodes[2] = GruNodeGene(key=2,
                                  cfg=cfg.genome,
                                  input_keys=[-1],
                                  input_keys_full=[-1])  # Hidden node
    genome.nodes[2].bias = 0  # Bias is irrelevant for GRU-node

    if not random_init:
        genome.nodes[2].bias_h = np.zeros((3, ))
        genome.nodes[2].weight_xh_full = np.zeros((3, 1))
        genome.nodes[2].weight_hh = np.zeros((3, 1))

    # Create the connections
    genome.connections = dict()

    # Input-GRU
    key = (-1, 2)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[key].weight = 1  # Simply forward distance
    genome.connections[key].enabled = True

    # GRU-Output
    key = (2, 0)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = 3  # Increase magnitude to be a value between -3..3 (possible to steer left wheel)
    genome.connections[key].enabled = True

    # GRU-Output
    key = (-1, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[key].weight = -1
    genome.connections[key].enabled = True

    genome.update_rnn_nodes(config=cfg.genome)
    return genome
Пример #11
0
 def test_distance_failure(self):
     """> Test if the distance-parameter is updated correctly after unsuccessful read."""
     # Folder must be root to load in make_net properly
     if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')
     
     # Setup the nodes to compare
     cfg = Config()
     output_node = OutputNodeGene(key=0, cfg=cfg.genome)
     simple_node = SimpleNodeGene(key=1, cfg=cfg.genome)
     
     # Create an empty NodeComparingCache
     cache = NodeComparingCache()
     
     # Fetch the result of the distance-measure
     result = cache(
             node0=output_node,
             node1=simple_node,
             conn0={},
             conn1={},
             cfg=cfg.genome,
     )
     
     # Check the distance-parameter
     self.assertEqual(result, (False, None))
     self.assertEqual(cache.comparable[(0, 1)], False)
     self.assertFalse((1, 0) in cache.comparable)
Пример #12
0
def get_deep_genome3(cfg: Config):
    """
    Genome with all biases set to 0, only simple hidden nodes used, all connections enabled with weight 1.
    Configuration:
        0       1
        |       |
        16      |
        |  \    |
        15  |   |
        |   |   |
        14  |   |
        |   |   |
       -1  -2  -3
    """
    # Create a dummy genome
    genome = Genome(
            key=3,
            num_outputs=cfg.genome.num_outputs,
            bot_config=cfg.bot,
    )
    # Reset the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 0
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0
    for k in [14, 15, 16]:
        genome.nodes[k] = SimpleNodeGene(key=k, cfg=cfg.genome)  # Hidden node
        genome.nodes[k].bias = 0
    
    # Reset the connections
    genome.connections = dict()
    for key in [(-1, 14), (14, 15), (15, 16), (16, 0), (-2, 16), (-3, 1)]:
        genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
        genome.connections[key].weight = 1
        genome.connections[key].enabled = True
    
    return genome
Пример #13
0
 def test_different_type(self):
     """> Test if False is returned when comparing nodes of different type."""
     # Folder must be root to load in make_net properly
     if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..')
     
     # Setup the nodes to compare
     cfg = Config()
     output_node = OutputNodeGene(key=0, cfg=cfg.genome)
     simple_node = SimpleNodeGene(key=1, cfg=cfg.genome)
     
     # Create an empty NodeComparingCache
     cache = NodeComparingCache()
     
     # Fetch the result of the distance-measure
     result = cache(
             node0=output_node,
             node1=simple_node,
             conn0={},
             conn1={},
             cfg=cfg.genome,
     )
     self.assertEqual(result, (False, None))
Пример #14
0
 def create_output_node(config: GenomeConfig, node_id: int):
     node = OutputNodeGene(node_id, cfg=config)
     return node
Пример #15
0
def get_topology1(gid: int, cfg: Config):
    """
    Create a uniformly and randomly sampled genome of fixed topology:
      (key=0, bias=1.5)  (key=1, bias=0)
                     ____ /   /
                   /         /
                GRU         /
                |     _____/
                |   /
              (key=-1)
    """
    # Create an initial dummy genome with fixed configuration
    genome = Genome(
        key=gid,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )

    # Create the nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 1.5  # Drive with full actuation by default
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    genome.nodes[1].bias = 0  # Drive with 0.5 actuation by default
    genome.nodes[2] = GruNodeGene(key=2,
                                  cfg=cfg.genome,
                                  input_keys=[-1],
                                  input_keys_full=[-1])  # Hidden node
    genome.nodes[2].bias = 0  # Bias is irrelevant for GRU-node

    # Setup the parameter-ranges
    conn_range = cfg.genome.weight_max_value - cfg.genome.weight_min_value
    bias_range = cfg.genome.bias_max_value - cfg.genome.bias_min_value
    rnn_range = cfg.genome.rnn_max_value - cfg.genome.rnn_min_value

    # Uniformly sample the genome's GRU-component
    genome.nodes[2].bias_h = rand_arr(
        (3, )) * bias_range + cfg.genome.bias_min_value
    genome.nodes[2].weight_xh_full = rand_arr(
        (3, 1)) * rnn_range + cfg.genome.weight_min_value
    genome.nodes[2].weight_hh = rand_arr(
        (3, 1)) * rnn_range + cfg.genome.weight_min_value

    # Create the connections
    genome.connections = dict()

    # input2gru
    key = (-1, 2)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[key].weight = 1  # Simply forward distance
    genome.connections[key].enabled = True

    # gru2output - Uniformly sampled
    key = (2, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = random() * conn_range + cfg.genome.weight_min_value
    genome.connections[key].enabled = True

    # input2output - Uniformly sampled
    key = (-1, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = random() * conn_range + cfg.genome.weight_min_value
    genome.connections[key].enabled = True

    genome.update_rnn_nodes(config=cfg.genome)
    return genome
Пример #16
0
def get_output_node_gene(key, config):
    return OutputNodeGene(key, config)
Пример #17
0
def get_topology(pop_name, gid: int, cfg: Config):
    """
    Create a uniformly and randomly sampled genome of fixed topology:
    Sigmoid with bias 1.5 --> Actuation default of 95,3%
      (key=0, bias=1.5)   (key=1, bias=?)
                     ____ /   /
                   /         /
                GRU         /
                |     _____/
                |   /
              (key=-1)
    """
    # Create an initial dummy genome with fixed configuration
    genome = Genome(
        key=gid,
        num_outputs=cfg.genome.num_outputs,
        bot_config=cfg.bot,
    )

    # Setup the parameter-ranges
    conn_range = cfg.genome.weight_max_value - cfg.genome.weight_min_value
    bias_range = cfg.genome.bias_max_value - cfg.genome.bias_min_value
    rnn_range = cfg.genome.rnn_max_value - cfg.genome.rnn_min_value

    # Create the output nodes
    genome.nodes[0] = OutputNodeGene(key=0, cfg=cfg.genome)  # OutputNode 0
    genome.nodes[0].bias = 1.5  # Drive with 0.953 actuation by default
    genome.nodes[1] = OutputNodeGene(key=1, cfg=cfg.genome)  # OutputNode 1
    if pop_name in [P_BIASED]:
        genome.nodes[1].bias = normal(
            1.5,
            .1)  # Initially normally distributed around bias of other output
    else:
        genome.nodes[1].bias = random(
        ) * bias_range + cfg.genome.bias_min_value  # Uniformly sampled bias

    # Setup the recurrent unit
    if pop_name in [P_GRU_NR]:
        genome.nodes[2] = GruNoResetNodeGene(key=2,
                                             cfg=cfg.genome,
                                             input_keys=[-1],
                                             input_keys_full=[-1])  # Hidden
        genome.nodes[2].bias_h = rand_arr(
            (2, )) * bias_range + cfg.genome.bias_min_value
        genome.nodes[2].weight_xh_full = rand_arr(
            (2, 1)) * rnn_range + cfg.genome.weight_min_value
        genome.nodes[2].weight_hh = rand_arr(
            (2, 1)) * rnn_range + cfg.genome.weight_min_value
    else:
        genome.nodes[2] = GruNodeGene(key=2,
                                      cfg=cfg.genome,
                                      input_keys=[-1],
                                      input_keys_full=[-1])  # Hidden node
        genome.nodes[2].bias_h = rand_arr(
            (3, )) * bias_range + cfg.genome.bias_min_value
        genome.nodes[2].weight_xh_full = rand_arr(
            (3, 1)) * rnn_range + cfg.genome.weight_min_value
        genome.nodes[2].weight_hh = rand_arr(
            (3, 1)) * rnn_range + cfg.genome.weight_min_value
    genome.nodes[2].bias = 0  # Bias is irrelevant for GRU-node

    # Create the connections
    genome.connections = dict()

    # input2gru - Uniformly sampled on the positive spectrum
    key = (-1, 2)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    if pop_name in [P_BIASED]:
        genome.connections[
            key].weight = 6  # Maximize connection, GRU can always lower values flowing through
    else:
        genome.connections[
            key].weight = random() * conn_range + cfg.genome.weight_min_value
    genome.connections[key].enabled = True

    # gru2output - Uniformly sampled on the positive spectrum
    key = (2, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = random() * conn_range + cfg.genome.weight_min_value
    if pop_name in [P_BIASED]:
        genome.connections[key].weight = abs(
            genome.connections[key].weight)  # Always positive!
    genome.connections[key].enabled = True

    # input2output - Uniformly sampled
    key = (-1, 1)
    genome.connections[key] = ConnectionGene(key=key, cfg=cfg.genome)
    genome.connections[
        key].weight = random() * conn_range + cfg.genome.weight_min_value
    if pop_name in [P_BIASED]:
        genome.connections[key].weight = -abs(
            genome.connections[key].weight)  # Always negative!
    genome.connections[key].enabled = True

    # Enforce the topology constraints
    enforce_topology(pop_name=pop_name, genome=genome)

    genome.update_rnn_nodes(config=cfg.genome)
    return genome