def crossover_node(mother, father):
    child = network()
    child.num_of_inputs = mother.num_of_inputs

    tr = {}
    for i in xrange(mother.num_of_inputs):
        tr[i] = i

    for bias in (mother.bias_node, father.bias_node):
        tr[bias] = child.bias_node

    for mother_node, father_node in zip(mother.hidden_nodes, father.hidden_nodes):
        child_node = node(mother_node.activation_function, 1)
        tr[mother_node] = child_node
        tr[father_node] = child_node

        #choose one node to pass on
        _choice = sample([mother_node, father_node], 1)[0]

        #map the weights and bias of that node to the child node
        for keynode, weight in _choice.weights.items():
            child_node.weights[tr[keynode]] = weight

        child.hidden_nodes.append(child_node)

    for mother_node, father_node in zip(mother.output_nodes, father.output_nodes):
        child_node = node(mother_node.activation_function, 1)
        tr[mother_node] = child_node
        tr[father_node] = child_node

        #choose one node to pass on
        _choice = sample([mother_node, father_node], 1)[0]

        #map the weights and bias of that node to the child node
        for keynode, weight in _choice.weights.items():
            child_node.weights[tr[keynode]] = weight

        child.output_nodes.append(child_node)

    return child
 def test4NodesTest(self):
     net = network()
     #input_number = 2 # 2 inputs
     #net.num_of_inputs = input_number
     #inputs = range(input_number)
 
     biasnode = net.bias_node
     
     inputs = range(2)
     
     my_node = Node("linear")
     for in_idx in inputs:
         #connect with inputs with weight 1.0
         connect_node(my_node, in_idx, 1.0)
         
     out_val = my_node.output([1.0,1.0])
     print("Output of node with (1,1) is: {0}".format(out_val))
     assert(out_val == 2.0) # Simply the sum of the inputs
     
     #connect to bias
     connect_node(my_node, biasnode, 1.0)
     out_val = my_node.output([1.0,1.0])
     print("Output of node with (1,1) and bias is: {0}".format(out_val))
     assert(out_val == 3.0) # Simply the sum of the inputs
Exemple #3
0
                    elif not top_networks[i]:
                        top_networks[i] = comp_net
                        break

            logger.info("Generation {0}, best trn: {1} val: {2}, top trn: {3}".format(generation, best_error, best_val_error, error[top_networks[0]]))
            #glogger.debugPlot('Test Error\nMutation rate: ' + str(mutation_chance), best_error, style = 'r-', subset='best')
            #glogger.debugPlot('Test Error\nMutation rate: ' + str(mutation_chance), error[top_networks[0]], style = 'b-', subset='top')

            #Select the best 5 networks, mate them randomly and create 50 new networks
            population = []
            for child_index in xrange(population_size):
                [mother, father] = sample(top_networks, 2)
                if random() < cross_over_chance:
                    father = mother #Will create full mutation, no cross-over

                population.append(network())
                population[child_index].num_of_inputs = mother.num_of_inputs
                bias_child = population[child_index].bias_node
                #Hidden layer
                for mother_node, father_node in zip(mother.hidden_nodes, father.hidden_nodes):
                    hidden = node(mother_node.activation_function, random_range)
                    weights = {}
                    for input_number in xrange(mother.num_of_inputs):
                        choice = sample([mother_node, father_node], 1)[0]
                        weights[input_number] = choice.weights[input_number]
                    choice = sample([(mother, mother_node), (father, father_node)], 1)[0]
                    weights[bias_child] = choice[1].weights[choice[0].bias_node]
                    
                    _all = range(mother.num_of_inputs)
                    _all.append(bias_child)
                    connect_nodes(hidden, _all, weights)