def mutateAddNode(self): newNode = THGene.THNodeGene(0, "Hidden", False, False) connIndex = random.randint(0, self.numConns - 1) existingConn = self.connGenes[connIndex] self.connGenes[connIndex].disable() #disable existing conn # add new incoming nodes #newNode.incoming.append(self.connGenes[connIndex].input) #self.connGenes[connIndex].output.incoming.append(newNode) self.addNodeGene(newNode) #append the newNodeGene print("MUTATE LOCALID BELOW") print(newNode.localID) # create 2 new ConnGenes newInputConn = THGene.THConnGene(existingConn.input, newNode, existingConn.weight, True) newOutputConn = THGene.THConnGene(newNode, existingConn.output, 1, True) self.addConnGene(newInputConn) self.addConnGene(newOutputConn) self.fitness = 0
def mutateAddConn(self): validINodes = [] validONodes = [] for i in range(0, self.numNodes): if (self.nodeGenes[i].isInput == True or (self.nodeGenes[i].isInput == False and self.nodeGenes[i].isOutput == False)): validINodes.append(self.nodeGenes[i]) if (self.nodeGenes[i].isOutput == True or (self.nodeGenes[i].isInput == False and self.nodeGenes[i].isOutput == False)): validONodes.append(self.nodeGenes[i]) iIndex = random.randint(0, len(validINodes) - 1) oIndex = random.randint(0, len(validONodes) - 1) if (validONodes[oIndex].localID == validINodes[iIndex].localID and oIndex != len(validONodes) - 1): oIndex += 1 elif (validONodes[oIndex].localID == validINodes[iIndex].localID and oIndex != 0): oIndex -= 1 newConn = THGene.THConnGene(validINodes[iIndex], validONodes[oIndex], 1, True) #validONodes[oIndex].incoming.append(validINodes[iIndex]) if (self.containsConn(newConn) == False): self.addConnGene(newConn) self.fitness = 0
def generateConns(count): conns = [] #""" Create a global variable to store innovationID and nodes before any further progress can be made on this""" for i in range(0, count): node1 = random.randint(0, NNDatabase.numNodeGenes) node2 = random.randint(0, NNDatabase.numNodeGenes) if node1 > node2 or node1 == node2: i -= 1 continue newConn = THGene.THConnGene(NNDatabase.nodeGenes[node1], NNDatabase.nodeGenes[node2], 1, True) #NNDatabase.innovationID += 1 conns.append(newConn)
hidden1 = THGene.THNodeGene(0, "hidden", False, False) output1 = NNDatabase.outputs[3] output2 = NNDatabase.outputs[0] #add nodes testNetwork.addNodeGene(input1) testNetwork.addNodeGene(input2) testNetwork.addNodeGene(input3) testNetwork.addNodeGene(output1) testNetwork.addNodeGene(hidden1) testNetwork.addNodeGene(output2) #create conns conn1 = THGene.THConnGene(testNetwork.nodeGenes[0], testNetwork.nodeGenes[3], 0.7, True) conn2 = THGene.THConnGene(testNetwork.nodeGenes[1], testNetwork.nodeGenes[3], 0.5, False) conn3 = THGene.THConnGene(testNetwork.nodeGenes[2], testNetwork.nodeGenes[3], 0.5, True) conn4 = THGene.THConnGene(testNetwork.nodeGenes[1], testNetwork.nodeGenes[4], 0.2, True) conn5 = THGene.THConnGene(testNetwork.nodeGenes[4], testNetwork.nodeGenes[3], 0.4, True) conn6 = THGene.THConnGene(testNetwork.nodeGenes[0], testNetwork.nodeGenes[4], 0.6, True) conn7 = THGene.THConnGene(testNetwork.nodeGenes[0], testNetwork.nodeGenes[5], 0.8, True) conn8 = THGene.THConnGene(testNetwork.nodeGenes[4], testNetwork.nodeGenes[5], 1, True)