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
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
Exemple #3
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
Exemple #4
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
Exemple #5
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
Exemple #6
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
Exemple #7
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
Exemple #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
Exemple #9
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
Exemple #10
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
Exemple #11
0
    def create_connection(self,
                          config: GenomeConfig,
                          input_key: int,
                          output_key: int,
                          weight: float = None):
        """Add a connection to the genome."""
        # Create the connection
        assert isinstance(input_key, int)
        assert isinstance(output_key, int)
        assert output_key >= 0  # output_key is not one of the inputs (sensor readings)
        assert input_key not in config.keys_output
        key = (input_key, output_key)
        connection = ConnectionGene(key, cfg=config)

        if weight:
            assert isinstance(weight, float)
            connection.weight = weight

        self.connections[key] = connection
        self.enable_connection(config=config, key=key)
Exemple #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
Exemple #13
0
def get_connections(receiving_key: int, sending_keys: set, cfg: GenomeConfig):
    """Create a dictionary of connection from the sending_keys to the receiving_key."""
    conn = dict()
    for k in sending_keys:
        conn[(k, receiving_key)] = ConnectionGene(key=(k, receiving_key), cfg=cfg)
    return conn
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
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
def get_connection_gene(key, config):
    return ConnectionGene(key, config)