Esempio n. 1
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
Esempio n. 2
0
def get_topology3333(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-NR        /
                |     _____/
                |   /
              (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] = GruNoResetNodeGene(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

    # Uniformly sample the genome's GRU-component
    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

    # Create the connections
    genome.connections = dict()

    # input2gru
    key = (-1, 2)
    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

    # 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