Esempio n. 1
0
def create_feed_forward_phenotype(genome):
    """ Receives a genome and returns its phenotype (a neural network). """

    # Gather inputs and expressed connections.
    input_nodes = [
        ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT'
    ]
    output_nodes = [
        ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT'
    ]
    connections = [(cg.in_node_id, cg.out_node_id)
                   for cg in genome.conn_genes.values() if cg.enabled]

    layers = find_feed_forward_layers(input_nodes, connections)
    node_evals = []
    used_nodes = set(input_nodes + output_nodes)
    for layer in layers:
        for node in layer:
            inputs = []
            # TODO: This could be more efficient.
            for cg in genome.conn_genes.values():
                if cg.out_node_id == node and cg.enabled:
                    inputs.append((cg.in_node_id, cg.weight))
                    used_nodes.add(cg.in_node_id)

            used_nodes.add(node)
            ng = genome.node_genes[node]
            activation_function = activation_functions.get(ng.activation_type)
            node_evals.append(
                (node, activation_function, ng.bias, ng.response, inputs))

    return FeedForwardNetwork(max(used_nodes), input_nodes, output_nodes,
                              node_evals)
Esempio n. 2
0
def create_recurrent_phenotype(genome):
    """ Receives a genome and returns its phenotype (a recurrent neural network). """

    # Gather inputs and expressed connections.
    input_nodes = [
        ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT'
    ]
    output_nodes = [
        ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT'
    ]

    node_inputs = {}
    used_nodes = set(input_nodes + output_nodes)
    for cg in genome.conn_genes.values():
        if not cg.enabled:
            continue

        used_nodes.add(cg.out_node_id)
        used_nodes.add(cg.in_node_id)

        if cg.out_node_id not in node_inputs:
            node_inputs[cg.out_node_id] = [(cg.in_node_id, cg.weight)]
        else:
            node_inputs[cg.out_node_id].append((cg.in_node_id, cg.weight))

    node_evals = []
    for onode, inputs in node_inputs.items():
        ng = genome.node_genes[onode]
        activation_function = activation_functions.get(ng.activation_type)
        node_evals.append(
            (onode, activation_function, ng.bias, ng.response, inputs))

    return RecurrentNetwork(max(used_nodes), input_nodes, output_nodes,
                            node_evals)
Esempio n. 3
0
def create_feed_forward_phenotype(genome):
    """ Receives a genome and returns its phenotype (a neural network). """

    # Gather inputs and expressed connections.
    input_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT']
    output_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT']
    connections = [(cg.in_node_id, cg.out_node_id) for cg in genome.conn_genes.values() if cg.enabled]

    layers = find_feed_forward_layers(input_nodes, connections)
    node_evals = []
    used_nodes = set(input_nodes + output_nodes)
    for layer in layers:
        for node in layer:
            inputs = []
            # TODO: This could be more efficient.
            for cg in genome.conn_genes.values():
                if cg.out_node_id == node and cg.enabled:
                    inputs.append((cg.in_node_id, cg.weight))
                    used_nodes.add(cg.in_node_id)

            used_nodes.add(node)
            ng = genome.node_genes[node]
            activation_function = activation_functions.get(ng.activation_type)
            node_evals.append((node, activation_function, ng.bias, ng.response, inputs))

    return FeedForwardNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
Esempio n. 4
0
def create_recurrent_phenotype(genome):
    """ Receives a genome and returns its phenotype (a recurrent neural network). """

    # Gather inputs and expressed connections.
    input_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT']
    output_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT']

    node_inputs = {}
    used_nodes = set(input_nodes + output_nodes)
    for cg in genome.conn_genes.values():
        if not cg.enabled:
            continue

        used_nodes.add(cg.out_node_id)
        used_nodes.add(cg.in_node_id)

        if cg.out_node_id not in node_inputs:
            node_inputs[cg.out_node_id] = [(cg.in_node_id, cg.weight)]
        else:
            node_inputs[cg.out_node_id].append((cg.in_node_id, cg.weight))

    node_evals = []
    for onode, inputs in node_inputs.items():
        ng = genome.node_genes[onode]
        activation_function = activation_functions.get(ng.activation_type)
        node_evals.append((onode, activation_function, ng.bias, ng.response, inputs))

    return RecurrentNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
Esempio n. 5
0
    def __init__(self, neuron_type, ID, bias, response, activation_type):
        assert neuron_type in ('INPUT', 'OUTPUT', 'HIDDEN')

        self.type = neuron_type
        self.ID = self.indexer.next(ID)
        self.bias = bias
        self.response = response
        self.activation = activation_functions.get(activation_type)

        self._synapses = []
        self.output = 0.0  # for recurrent networks all neurons must have an "initial state"
Esempio n. 6
0
    def __init__(self, neuron_type, ID, bias, response, activation_type):
        assert neuron_type in ('INPUT', 'OUTPUT', 'HIDDEN')

        self.type = neuron_type
        self.ID = self.indexer.next(ID)
        self.bias = bias
        self.response = response
        self.activation = activation_functions.get(activation_type)

        self._synapses = []
        self.output = 0.0  # for recurrent networks all neurons must have an "initial state"