Example #1
0
    def mutateAddNeuron(self):
        """
      Add a neuron at the location of a random gene.
      This is done by disabling the original gene, 
      then adding a new node and connections between
      the new node and the original gene nodes.
      """
        # get one random gene
        if self.genes:
            innov = random.sample(self.genes, 1)[0]
        else:
            return
        gene = self.genes[innov]

        node1 = gene.source_neuron
        node2 = gene.target_neuron
        # make sure our nodes aren't two consecutive integers
        if node1 + 1 > node2 - 1:
            return

        g = Gene()
        g.copy(gene)

        # create a new node
        node = random.randint(node1 + 1, node2 - 1)

        # make sure our node isn't the same as any existing nodes
        if node in self.nodes:
            return
        # should be a hidden node
        assert node >= 1 and node <= MAX_LAYER

        # morph the gene into two genes and add the node
        g1 = Gene()
        g1.source_neuron = node1
        g1.target_neuron = node
        g1.weight = 1
        g2 = Gene()
        g2.source_neuron = node
        g2.target_neuron = node2
        g2.weight = gene.weight
        self.nodes.add(node)

        # disable the original gene, replace it with the new ones
        g.enabled = False
        self.genes[innov] = g
        self.genes[max(gene_index) + 1] = g1
        self.genes[max(gene_index) + 2] = g2

        # add the new genes to the index
        gene_index[max(gene_index) + 1] = g1
        gene_index[max(gene_index) + 1] = g2
Example #2
0
    def addGene(self, node1, node2):
        """
      Add a randomly weighted gene between two nodes.
      
      Args:
         node1: the first node
         node2: the second node
      """
        # connect the two valid, unconnected nodes
        g = Gene()
        g.source_neuron = node1
        g.target_neuron = node2
        g.weight = random.random() * 4 - 2
        # check to see if the gene already exists in the index
        for index, gene in gene_index.items():
            if gene.source_neuron == node1 and gene.target_neuron == node2:
                # if found, use that same innovation number
                self.genes[index] = g
                return

        # if it's not in the index, add it and record in the index
        if gene_index:
            self.genes[max(gene_index) + 1] = g
            gene_index[max(gene_index) + 1] = g
        else:
            gene_index[0] = g
            self.genes[0] = g