// loops through all output nodes andcreates a connection from a random input node to the output, so that all output nodes are hooked up // should result in the minimum amount of connections required to have 'full' functionality public void CreateMinimumRandomConnections() { int numInputs = 0; int numOutputs = 0; List <GeneNodeNEAT> inputNodeList = new List <GeneNodeNEAT>(); List <GeneNodeNEAT> outputNodeList = new List <GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { numInputs++; inputNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { numOutputs++; outputNodeList.Add(nodeNEATList[i]); } } for (int o = 0; o < outputNodeList.Count; o++) { int inNodeID = (int)UnityEngine.Random.Range(0f, (float)inputNodeList.Count); float randomWeight = Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[inNodeID].id), GetInt3FromNodeIndex(outputNodeList[o].id), randomWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); } }
public override object Read(ES2Reader reader) { GeneLinkNEAT data = new GeneLinkNEAT(); Read(reader, data); return(data); }
public override void Write(object obj, ES2Writer writer) { GeneLinkNEAT data = (GeneLinkNEAT)obj; // Add your writer.Write calls here. writer.Write(0); // Version 0 is current version number // Make sure to edit Read() function to properly handle version control! // VERSION 0: writer.Write(data.enabled); writer.Write(data.fromNodeID); writer.Write(data.innov); writer.Write(data.toNodeID); writer.Write(data.weight); writer.Write(data.birthGen); }
public override void Read(ES2Reader reader, object c) { GeneLinkNEAT data = (GeneLinkNEAT)c; // Add your reader.Read calls here to read the data into the object. // Read the version number. int fileVersion = reader.Read <int>(); // VERSION 0: if (fileVersion >= 0) { data.enabled = reader.Read <bool>(); data.fromNodeID = reader.Read <Int3>(); data.innov = reader.Read <int>(); data.toNodeID = reader.Read <Int3>(); data.weight = reader.Read <float>(); data.birthGen = reader.Read <int>(); } }
public void AddNewRandomNode(int gen, int inno) { if (linkNEATList.Count > 0) { int linkID = (int)UnityEngine.Random.Range(0f, (float)linkNEATList.Count); linkNEATList[linkID].enabled = false; // disable old connection GeneNodeNEAT newHiddenNode = new GeneNodeNEAT(nodeNEATList.Count, GeneNodeNEAT.GeneNodeType.Hid, TransferFunctions.TransferFunction.RationalSigmoid, inno, 0, false, 0); nodeNEATList.Add(newHiddenNode); // add new node between old connection // create two new connections GeneLinkNEAT newLinkA = new GeneLinkNEAT(linkNEATList[linkID].fromNodeID, GetInt3FromNodeIndex(newHiddenNode.id), linkNEATList[linkID].weight, true, GetNextInnovNumber(), gen); GeneLinkNEAT newLinkB = new GeneLinkNEAT(GetInt3FromNodeIndex(newHiddenNode.id), linkNEATList[linkID].toNodeID, 1f, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLinkA); linkNEATList.Add(newLinkB); //Debug.Log("AddNewRandomNode() linkID: " + linkID.ToString() + ", "); } else { Debug.Log("No connections! Can't create new node!!!"); } }
public void AddNewExtraLink(float fromBias, int gen) // has a higher-than-random chance to create a new link with at least one node that is already connected { List <GeneNodeNEAT> eligibleFromNodes = new List <GeneNodeNEAT>(); List <GeneNodeNEAT> eligibleToNodes = new List <GeneNodeNEAT>(); bool reuseFromNode; // true = use an existing FROM node, false = use an existing TO node float randToOrFrom = UnityEngine.Random.Range(0f, 1f); if (randToOrFrom < fromBias) { reuseFromNode = true; } else { reuseFromNode = false; } for (int k = 0; k < linkNEATList.Count; k++) { // Populate eligible node lists with those nodes that are already connected: if (linkNEATList[k].enabled) { if (reuseFromNode) // reuse an exisiting fromNode { if (eligibleFromNodes.Contains(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].fromNodeID)])) { //Debug.Log("AddNewExtraLink() EligibleFromNode Already Contains Node " + linkNEATList[k].fromNodeID.ToString()); } else { eligibleFromNodes.Add(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].fromNodeID)]); } } else // reuse an exisitng TO node { if (eligibleToNodes.Contains(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].toNodeID)])) { //Debug.Log("AddNewExtraLink() EligibleToNode Already Contains Node " + linkNEATList[k].toNodeID.ToString()); } else { eligibleToNodes.Add(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].toNodeID)]); } } } } for (int i = 0; i < nodeNEATList.Count; i++) { if (reuseFromNode) // if re-using an exisitng FROM node, then get a TO node from random: { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleToNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { eligibleToNodes.Add(nodeNEATList[i]); } } else // if re-using an exisitng TO node, then get a FROM node from random: { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { eligibleFromNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleFromNodes.Add(nodeNEATList[i]); } } } if (eligibleFromNodes.Count > 0 && eligibleToNodes.Count > 0) { // make sure that there is at least 1 from and to node that is possible int fromNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleFromNodes.Count); int toNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleToNodes.Count); if (eligibleFromNodes[fromNodeID].id == eligibleToNodes[toNodeID].id) { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("AddNewExtraLink() Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { Debug.Log("AddNewExtraLink() New Link TO ITSELF: " + eligibleFromNodes[fromNodeID].id.ToString() + " Doing it anyway!"); float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } else { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("AddNewExtraLink() Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); Debug.Log("AddNewExtraLink() NEW LINK!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } } }
/*public bool AreEqual(Vector3 vec1, Vector3 vec2) { * bool isEqual = false; * if(vec1.x == vec2.x) { * if (vec1.y == vec2.y) { * if (vec1.z == vec2.z) { * isEqual = true; * } * } * } * return isEqual; * } */ public void AddNewRandomLink(int gen) { List <GeneNodeNEAT> eligibleFromNodes = new List <GeneNodeNEAT>(); List <GeneNodeNEAT> eligibleToNodes = new List <GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { eligibleFromNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleFromNodes.Add(nodeNEATList[i]); eligibleToNodes.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { eligibleToNodes.Add(nodeNEATList[i]); } } if (eligibleFromNodes.Count > 0 && eligibleToNodes.Count > 0) { int fromNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleFromNodes.Count); int toNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleToNodes.Count); if (eligibleFromNodes[fromNodeID].id == eligibleToNodes[toNodeID].id) { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { Debug.Log("New Link TO ITSELF: " + eligibleFromNodes[fromNodeID].id.ToString() + " Doing it anyway!"); float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } else { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { //Debug.Log("Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } } }
public void CreateInitialConnections(float connectedness, bool randomWeights) { int numInputs = 0; int numHidden = 0; int numOutputs = 0; List <GeneNodeNEAT> inputNodeList = new List <GeneNodeNEAT>(); List <GeneNodeNEAT> hiddenNodeList = new List <GeneNodeNEAT>(); List <GeneNodeNEAT> outputNodeList = new List <GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { numInputs++; inputNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { numHidden++; hiddenNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { numOutputs++; outputNodeList.Add(nodeNEATList[i]); } } if (numHidden > 0) // if there is a hidden layer: { float accumulatedLinkChance = UnityEngine.Random.Range(0f, 1f); // random offset for (int i = 0; i < inputNodeList.Count; i++) // input layer to hidden layer { for (int h = 0; h < hiddenNodeList.Count; h++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if (accumulatedLinkChance >= 1f) // if connectedness is 0.34, link will be created every third cycle, for example { float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[i].id), GetInt3FromNodeIndex(hiddenNodeList[h].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } for (int h = 0; h < hiddenNodeList.Count; h++) // hidden layer to output layer { for (int o = 0; o < outputNodeList.Count; o++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if (accumulatedLinkChance >= 1f) // if connectedness is 0.34, link will be created every third cycle, for example { float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(hiddenNodeList[h].id), GetInt3FromNodeIndex(outputNodeList[o].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } } else // no hidden layer: { float accumulatedLinkChance = UnityEngine.Random.Range(0f, 1f); // random offset for (int i = 0; i < inputNodeList.Count; i++) { for (int o = 0; o < outputNodeList.Count; o++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if (accumulatedLinkChance >= 1f) // if connectedness is 0.34, link will be created every third cycle, for example { float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[i].id), GetInt3FromNodeIndex(outputNodeList[o].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } } }
public Population BreedPopulation(ref Population sourcePopulation, int currentGeneration) { #region Pre-Crossover, Figuring out how many agents to breed etc. int LifetimeGeneration = currentGeneration + sourcePopulation.trainingGenerations; int totalNumWeightMutations = 0; //float totalWeightChangeValue = 0f; // go through species list and adjust fitness List<SpeciesBreedingPool> childSpeciesPoolsList = new List<SpeciesBreedingPool>(); // will hold agents in an internal list to facilitate crossover for (int s = 0; s < sourcePopulation.speciesBreedingPoolList.Count; s++) { SpeciesBreedingPool newChildSpeciesPool = new SpeciesBreedingPool(sourcePopulation.speciesBreedingPoolList[s].templateGenome, sourcePopulation.speciesBreedingPoolList[s].speciesID); // create Breeding Pools // copies the existing breeding pools but leaves their agentLists empty for future children childSpeciesPoolsList.Add(newChildSpeciesPool); // Add to list of pools } sourcePopulation.RankAgentArray(); // based on modified species fitness score, so compensated for species sizes Agent[] newAgentArray = new Agent[sourcePopulation.masterAgentArray.Length]; // Calculate total fitness score: float totalScore = 0f; if (survivalByRaffle) { for (int a = 0; a < sourcePopulation.populationMaxSize; a++) { // iterate through all agents totalScore += sourcePopulation.masterAgentArray[a].fitnessScoreSpecies; } } // Figure out How many Agents survive int numSurvivors = Mathf.RoundToInt(survivalRate * (float)sourcePopulation.populationMaxSize); //Depending on method, one at a time, select an Agent to survive until the max Number is reached int newChildIndex = 0; // For ( num Agents ) { for (int i = 0; i < numSurvivors; i++) { // If survival is by fitness score ranking: if (survivalByRank) { // Pop should already be ranked, so just traverse from top (best) to bottom (worst) newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[newChildIndex]; SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID); survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]); //SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species' newChildIndex++; } // if survival is completely random, as a control: if (survivalStochastic) { int randomAgent = UnityEngine.Random.Range(0, numSurvivors - 1); // Set next newChild slot to a randomly-chosen agent within the survivor faction -- change to full random? newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[randomAgent]; SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID); survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]); //SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species' newChildIndex++; } // if survival is based on a fitness lottery: if (survivalByRaffle) { // Try when Fitness is normalized from 0-1 float randomSlicePosition = UnityEngine.Random.Range(0f, totalScore); float accumulatedFitness = 0f; for (int a = 0; a < sourcePopulation.populationMaxSize; a++) { // iterate through all agents accumulatedFitness += sourcePopulation.masterAgentArray[a].fitnessScoreSpecies; // if accum fitness is on slicePosition, copy this Agent //Debug.Log("NumSurvivors: " + numSurvivors.ToString() + ", Surviving Agent " + a.ToString() + ": AccumFitness: " + accumulatedFitness.ToString() + ", RafflePos: " + randomSlicePosition.ToString() + ", TotalScore: " + totalScore.ToString() + ", newChildIndex: " + newChildIndex.ToString()); if (accumulatedFitness >= randomSlicePosition) { newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[a]; // add to next gen's list of agents SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID); survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]); //SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species' newChildIndex++; break; } } } } // Figure out how many new agents must be created to fill up the new population: int numNewChildAgents = sourcePopulation.populationMaxSize - numSurvivors; int numEligibleBreederAgents = Mathf.RoundToInt(breedingRate * (float)sourcePopulation.populationMaxSize); int currentRankIndex = 0; // Once the agents are ranked, trim the BreedingPools of agents that didn't make the cut for mating: if(useSpeciation) { for (int s = 0; s < sourcePopulation.speciesBreedingPoolList.Count; s++) { int index = 0; int failsafe = 0; int numAgents = sourcePopulation.speciesBreedingPoolList[s].agentList.Count; while (index < numAgents) { if (index < sourcePopulation.speciesBreedingPoolList[s].agentList.Count) { if (sourcePopulation.speciesBreedingPoolList[s].agentList[index].fitnessRank >= numEligibleBreederAgents) { sourcePopulation.speciesBreedingPoolList[s].agentList.RemoveAt(index); } else { index++; } } else { break; } failsafe++; if (failsafe > 500) { Debug.Log("INFINITE LOOP! hit failsafe 500 iters -- Trimming BreedingPools!"); break; } } //Debug.Log("BreedPopulation -- TRIMSpeciesPool# " + s.ToString() + ", id: " + sourcePopulation.speciesBreedingPoolList[s].speciesID.ToString() + ", Count: " + sourcePopulation.speciesBreedingPoolList[s].agentList.Count.ToString()); // } } float totalScoreBreeders = 0f; if (breedingByRaffle) { // calculate total fitness scores to determine lottery weights for (int a = 0; a < numEligibleBreederAgents; a++) { // iterate through all agents totalScoreBreeders += sourcePopulation.masterAgentArray[a].fitnessScoreSpecies; } } #endregion // Iterate over numAgentsToCreate : int newChildrenCreated = 0; while (newChildrenCreated < numNewChildAgents) { // Find how many parents random number btw min/max: int numParentAgents = 2; // UnityEngine.Random.Range(minNumParents, maxNumParents); int numChildAgents = 1; // defaults to one child, but: if (numNewChildAgents - newChildrenCreated >= 2) { // room for two more! numChildAgents = 2; } Agent[] parentAgentsArray = new Agent[numParentAgents]; // assume 2 for now? yes, so far.... List<GeneNodeNEAT>[] parentNodeListArray = new List<GeneNodeNEAT>[numParentAgents]; List<GeneLinkNEAT>[] parentLinkListArray = new List<GeneLinkNEAT>[numParentAgents]; Agent firstParentAgent = SelectAgentFromPopForBreeding(sourcePopulation, numEligibleBreederAgents, ref currentRankIndex); parentAgentsArray[0] = firstParentAgent; List<GeneNodeNEAT> firstParentNodeList = firstParentAgent.brainGenome.nodeNEATList; List<GeneLinkNEAT> firstParentLinkList = firstParentAgent.brainGenome.linkNEATList; //List<GeneNodeNEAT> firstParentNodeList = new List<GeneNodeNEAT>(); //List<GeneLinkNEAT> firstParentLinkList = new List<GeneLinkNEAT>(); //firstParentNodeList = firstParentAgent.brainGenome.nodeNEATList; //firstParentLinkList = firstParentAgent.brainGenome.linkNEATList; parentNodeListArray[0] = firstParentNodeList; parentLinkListArray[0] = firstParentLinkList; Agent secondParentAgent; SpeciesBreedingPool parentAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(sourcePopulation.speciesBreedingPoolList, firstParentAgent.speciesID); if (useSpeciation) { //parentAgentBreedingPool float randBreedOutsideSpecies = UnityEngine.Random.Range(0f, 1f); if (randBreedOutsideSpecies < interspeciesBreedingRate) { // Attempts to Found a new species // allowed to breed outside its own species: secondParentAgent = SelectAgentFromPopForBreeding(sourcePopulation, numEligibleBreederAgents, ref currentRankIndex); } else { // Selects mate only from within its own species: secondParentAgent = SelectAgentFromPoolForBreeding(parentAgentBreedingPool); } } else { secondParentAgent = SelectAgentFromPopForBreeding(sourcePopulation, numEligibleBreederAgents, ref currentRankIndex); } parentAgentsArray[1] = secondParentAgent; List<GeneNodeNEAT> secondParentNodeList = secondParentAgent.brainGenome.nodeNEATList; List<GeneLinkNEAT> secondParentLinkList = secondParentAgent.brainGenome.linkNEATList; //List<GeneNodeNEAT> secondParentNodeList = new List<GeneNodeNEAT>(); //List<GeneLinkNEAT> secondParentLinkList = new List<GeneLinkNEAT>(); //secondParentNodeList = secondParentAgent.brainGenome.nodeNEATList; //secondParentLinkList = secondParentAgent.brainGenome.linkNEATList; parentNodeListArray[1] = secondParentNodeList; parentLinkListArray[1] = secondParentLinkList; // Iterate over ChildArray.Length : // how many newAgents created for (int c = 0; c < numChildAgents; c++) { // for number of child Agents in floatArray[][]: Agent newChildAgent = new Agent(); List<GeneNodeNEAT> childNodeList = new List<GeneNodeNEAT>(); List<GeneLinkNEAT> childLinkList = new List<GeneLinkNEAT>(); GenomeNEAT childBrainGenome = new GenomeNEAT(); childBrainGenome.nodeNEATList = childNodeList; childBrainGenome.linkNEATList = childLinkList; int numEnabledLinkGenes = 0; if (useCrossover) { int nextLinkInnoA = 0; int nextLinkInnoB = 0; //int nextBodyNodeInno = 0; //int nextBodyAddonInno = 0; int failsafeMax = 500; int failsafe = 0; int parentListIndexA = 0; int parentListIndexB = 0; //int parentBodyNodeIndex = 0; bool parentDoneA = false; bool parentDoneB = false; bool endReached = false; int moreFitParent = 0; // which parent is more Fit if (parentAgentsArray[0].fitnessScoreSpecies < parentAgentsArray[1].fitnessScoreSpecies) { moreFitParent = 1; } else if (parentAgentsArray[0].fitnessScoreSpecies == parentAgentsArray[1].fitnessScoreSpecies) { moreFitParent = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 1f)); } // MATCH UP Links between both agents, if they have a gene with matching Inno#, then mixing can occur while (!endReached) { failsafe++; if(failsafe > failsafeMax) { Debug.Log("failsafe reached!"); break; } // inno# of next links: if(parentLinkListArray[0].Count > parentListIndexA) { nextLinkInnoA = parentLinkListArray[0][parentListIndexA].innov; } else { parentDoneA = true; } if (parentLinkListArray[1].Count > parentListIndexB) { nextLinkInnoB = parentLinkListArray[1][parentListIndexB].innov; } else { parentDoneB = true; } int innoDelta = nextLinkInnoA - nextLinkInnoB; // 0=match, neg= Aextra, pos= Bextra if (parentDoneA && !parentDoneB) { innoDelta = 1; } if (parentDoneB && !parentDoneA) { innoDelta = -1; } if (parentDoneA && parentDoneB) { // reached end of both parent's linkLists endReached = true; break; } if (innoDelta < 0) { // Parent A has an earlier link mutation //Debug.Log("newChildIndex: " + newChildIndex.ToString() + ", IndexA: " + parentListIndexA.ToString() + ", IndexB: " + parentListIndexB.ToString() + ", innoDelta < 0 (" + innoDelta.ToString() + ") -- moreFitP: " + moreFitParent.ToString() + ", nextLinkInnoA: " + nextLinkInnoA.ToString() + ", nextLinkInnoB: " + nextLinkInnoB.ToString()); if (moreFitParent == 0) { // Parent A is more fit: GeneLinkNEAT newChildLink = new GeneLinkNEAT(parentLinkListArray[0][parentListIndexA].fromNodeID, parentLinkListArray[0][parentListIndexA].toNodeID, parentLinkListArray[0][parentListIndexA].weight, parentLinkListArray[0][parentListIndexA].enabled, parentLinkListArray[0][parentListIndexA].innov, parentLinkListArray[0][parentListIndexA].birthGen); childLinkList.Add(newChildLink); if (parentLinkListArray[0][parentListIndexA].enabled) numEnabledLinkGenes++; } else { if(CheckForMutation(crossoverRandomLinkChance)) { // was less fit parent, but still passed on a gene!: GeneLinkNEAT newChildLink = new GeneLinkNEAT(parentLinkListArray[0][parentListIndexA].fromNodeID, parentLinkListArray[0][parentListIndexA].toNodeID, parentLinkListArray[0][parentListIndexA].weight, parentLinkListArray[0][parentListIndexA].enabled, parentLinkListArray[0][parentListIndexA].innov, parentLinkListArray[0][parentListIndexA].birthGen); childLinkList.Add(newChildLink); } } parentListIndexA++; } if (innoDelta > 0) { // Parent B has an earlier link mutation //Debug.Log("newChildIndex: " + newChildIndex.ToString() + ", IndexA: " + parentListIndexA.ToString() + ", IndexB: " + parentListIndexB.ToString() + ", innoDelta > 0 (" + innoDelta.ToString() + ") -- moreFitP: " + moreFitParent.ToString() + ", nextLinkInnoA: " + nextLinkInnoA.ToString() + ", nextLinkInnoB: " + nextLinkInnoB.ToString()); if (moreFitParent == 1) { // Parent B is more fit: GeneLinkNEAT newChildLink = new GeneLinkNEAT(parentLinkListArray[1][parentListIndexB].fromNodeID, parentLinkListArray[1][parentListIndexB].toNodeID, parentLinkListArray[1][parentListIndexB].weight, parentLinkListArray[1][parentListIndexB].enabled, parentLinkListArray[1][parentListIndexB].innov, parentLinkListArray[1][parentListIndexB].birthGen); childLinkList.Add(newChildLink); if (parentLinkListArray[1][parentListIndexB].enabled) numEnabledLinkGenes++; } else { if (CheckForMutation(crossoverRandomLinkChance)) { // was less fit parent, but still passed on a gene!: GeneLinkNEAT newChildLink = new GeneLinkNEAT(parentLinkListArray[1][parentListIndexB].fromNodeID, parentLinkListArray[1][parentListIndexB].toNodeID, parentLinkListArray[1][parentListIndexB].weight, parentLinkListArray[1][parentListIndexB].enabled, parentLinkListArray[1][parentListIndexB].innov, parentLinkListArray[1][parentListIndexB].birthGen); childLinkList.Add(newChildLink); } } parentListIndexB++; } if (innoDelta == 0) { // Match! float randParentIndex = UnityEngine.Random.Range(0f, 1f); float newWeightValue; if (randParentIndex < 0.5) { // ParentA wins: newWeightValue = parentLinkListArray[0][parentListIndexA].weight; } else { // ParentB wins: newWeightValue = parentLinkListArray[1][parentListIndexB].weight; } //Debug.Log("newChildIndex: " + newChildIndex.ToString() + ", IndexA: " + parentListIndexA.ToString() + ", IndexB: " + parentListIndexB.ToString() + ", innoDelta == 0 (" + innoDelta.ToString() + ") -- moreFitP: " + moreFitParent.ToString() + ", nextLinkInnoA: " + nextLinkInnoA.ToString() + ", nextLinkInnoB: " + nextLinkInnoB.ToString() + ", randParent: " + randParentIndex.ToString() + ", weight: " + newWeightValue.ToString()); GeneLinkNEAT newChildLink = new GeneLinkNEAT(parentLinkListArray[0][parentListIndexA].fromNodeID, parentLinkListArray[0][parentListIndexA].toNodeID, newWeightValue, parentLinkListArray[0][parentListIndexA].enabled, parentLinkListArray[0][parentListIndexA].innov, parentLinkListArray[0][parentListIndexA].birthGen); childLinkList.Add(newChildLink); if (parentLinkListArray[0][parentListIndexA].enabled) numEnabledLinkGenes++; parentListIndexA++; parentListIndexB++; } } // once childLinkList is built -- use nodes of the moreFit parent: for (int i = 0; i < parentNodeListArray[moreFitParent].Count; i++) { // iterate through all nodes in the parent List and copy them into fresh new geneNodes: GeneNodeNEAT clonedNode = new GeneNodeNEAT(parentNodeListArray[moreFitParent][i].id, parentNodeListArray[moreFitParent][i].nodeType, parentNodeListArray[moreFitParent][i].activationFunction, parentNodeListArray[moreFitParent][i].sourceAddonInno, parentNodeListArray[moreFitParent][i].sourceAddonRecursionNum, false, parentNodeListArray[moreFitParent][i].sourceAddonChannelNum); childNodeList.Add(clonedNode); } if (useMutation) { // BODY MUTATION: //PerformBodyMutation(ref childBodyGenome, ref childBrainGenome); // NEED TO ADJUST BRAINS IF MUTATION CHANGES #NODES!!!! // BRAIN MUTATION: if (numEnabledLinkGenes < 1) numEnabledLinkGenes = 1; for (int k = 0; k < childLinkList.Count; k++) { float mutateChance = mutationBlastModifier * masterMutationRate / (1f + (float)numEnabledLinkGenes * 0.15f); if (LifetimeGeneration - childLinkList[k].birthGen < newLinkBonusDuration) { float t = 1 - ((LifetimeGeneration - childLinkList[k].birthGen) / (float)newLinkBonusDuration); // t=0 means age of gene is same as bonusDuration, t=1 means it is brand new mutateChance = Mathf.Lerp(mutateChance, mutateChance * newLinkMutateBonus, t); } if (CheckForMutation(mutateChance)) { // Weight Mutation! //Debug.Log("Weight Mutation Link[" + k.ToString() + "] weight: " + childLinkList[k].weight.ToString() + ", mutate: " + MutateFloat(childLinkList[k].weight).ToString()); childLinkList[k].weight = MutateFloat(childLinkList[k].weight); totalNumWeightMutations++; } } if (CheckForMutation(mutationBlastModifier * mutationRemoveLinkChance)) { //Debug.Log("Remove Link Mutation Agent[" + newChildIndex.ToString() + "]"); childBrainGenome.RemoveRandomLink(); } if (CheckForMutation(mutationBlastModifier * mutationAddNodeChance)) { // Adds a new node //Debug.Log("Add Node Mutation Agent[" + newChildIndex.ToString() + "]"); childBrainGenome.AddNewRandomNode(LifetimeGeneration, GetNextAddonInnov()); } if (CheckForMutation(mutationBlastModifier * mutationRemoveNodeChance)) { // Adds a new node //Debug.Log("Add Node Mutation Agent[" + newChildIndex.ToString() + "]"); childBrainGenome.RemoveRandomNode(); } if (CheckForMutation(mutationBlastModifier * mutationAddLinkChance)) { // Adds new connection //Debug.Log("Add Link Mutation Agent[" + newChildIndex.ToString() + "]"); if (CheckForMutation(existingNetworkBias)) { childBrainGenome.AddNewExtraLink(existingFromNodeBias, LifetimeGeneration); } else { childBrainGenome.AddNewRandomLink(LifetimeGeneration); } } if (CheckForMutation(mutationBlastModifier * mutationActivationFunctionChance)) { TransferFunctions.TransferFunction newFunction; int randIndex = Mathf.RoundToInt(UnityEngine.Random.Range(0f, childNodeList.Count - 1)); int randomTF = (int)UnityEngine.Random.Range(0f, 12f); switch (randomTF) { case 0: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 1: newFunction = TransferFunctions.TransferFunction.Linear; break; case 2: newFunction = TransferFunctions.TransferFunction.Gaussian; break; case 3: newFunction = TransferFunctions.TransferFunction.Abs; break; case 4: newFunction = TransferFunctions.TransferFunction.Cos; break; case 5: newFunction = TransferFunctions.TransferFunction.Sin; break; case 6: newFunction = TransferFunctions.TransferFunction.Tan; break; case 7: newFunction = TransferFunctions.TransferFunction.Square; break; case 8: newFunction = TransferFunctions.TransferFunction.Threshold01; break; case 9: newFunction = TransferFunctions.TransferFunction.ThresholdNegPos; break; case 10: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 11: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 12: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; default: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; } if (childNodeList[randIndex].nodeType != GeneNodeNEAT.GeneNodeType.Out) { // keep outputs -1 to 1 range Debug.Log("ActivationFunction Mutation Node[" + randIndex.ToString() + "] prev: " + childNodeList[randIndex].activationFunction.ToString() + ", new: " + newFunction.ToString()); childNodeList[randIndex].activationFunction = newFunction; } } } else { Debug.Log("Mutation Disabled!"); } // THE BODY ==========!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!====================================================================================== CritterGenome childBodyGenome = new CritterGenome(); // create new body genome for Child // This creates the ROOT NODE!!!! // Clone Nodes & Addons from more fit parent to create new child body genome // crossover is on, so check for matching Nodes and Add-ons (based on Inno#'s) to determine when to mix Settings/Attributes: // Iterate over the nodes of the more fit parent: for (int i = 0; i < parentAgentsArray[moreFitParent].bodyGenome.CritterNodeList.Count; i++) { int currentNodeInno = parentAgentsArray[moreFitParent].bodyGenome.CritterNodeList[i].innov; if (i == 0) { // if this is the ROOT NODE: childBodyGenome.CritterNodeList[0].CopySettingsFromNode(parentAgentsArray[moreFitParent].bodyGenome.CritterNodeList[0]); // The root node was already created during the Constructor method of the CritterGenome, // ... so instead of creating a new one, just copy the settings } else { // NOT the root node, proceed normally: // Create new cloned node defaulted to the settings of the source( more-fit parent's) Node: CritterNode clonedCritterNode = parentAgentsArray[moreFitParent].bodyGenome.CritterNodeList[i].CloneThisCritterNode(); // Check other parent for same node: for (int j = 0; j < parentAgentsArray[1 - moreFitParent].bodyGenome.CritterNodeList.Count; j++) { if (parentAgentsArray[1 - moreFitParent].bodyGenome.CritterNodeList[j].innov == currentNodeInno) { // CROSSOVER NODE SETTINGS HERE!!! ---- If random dice roll > 0.5, use less fit parent's settings, otherwise leave as default BodyCrossover(ref clonedCritterNode, parentAgentsArray[1 - moreFitParent].bodyGenome.CritterNodeList[j]); } } childBodyGenome.CritterNodeList.Add(clonedCritterNode); } } // ADD-ONS!!!!!!!!!!!!!!!!!!!!!! BreedCritterAddons(ref childBodyGenome, ref parentAgentsArray[moreFitParent].bodyGenome, ref parentAgentsArray[1 - moreFitParent].bodyGenome); newChildAgent.bodyGenome = childBodyGenome; // ????? if (useMutation) { // BODY MUTATION: PerformBodyMutation(ref childBodyGenome, ref childBrainGenome); } } else { // no crossover: //=============================================================================================== for (int i = 0; i < parentNodeListArray[0].Count; i++) { // iterate through all nodes in the parent List and copy them into fresh new geneNodes: GeneNodeNEAT clonedNode = new GeneNodeNEAT(parentNodeListArray[0][i].id, parentNodeListArray[0][i].nodeType, parentNodeListArray[0][i].activationFunction, parentNodeListArray[0][i].sourceAddonInno, parentNodeListArray[0][i].sourceAddonRecursionNum, false, parentNodeListArray[0][i].sourceAddonChannelNum); childNodeList.Add(clonedNode); } for (int j = 0; j < parentLinkListArray[0].Count; j++) { //same thing with connections GeneLinkNEAT clonedLink = new GeneLinkNEAT(parentLinkListArray[0][j].fromNodeID, parentLinkListArray[0][j].toNodeID, parentLinkListArray[0][j].weight, parentLinkListArray[0][j].enabled, parentLinkListArray[0][j].innov, parentLinkListArray[0][j].birthGen); childLinkList.Add(clonedLink); if (parentLinkListArray[0][j].enabled) numEnabledLinkGenes++; } // MUTATION: if (useMutation) { // BODY MUTATION: //childBrainGenome.nodeNEATList = childNodeList //PerformBodyMutation(ref childBodyGenome, ref childBrainGenome); // BRAIN MUTATION: if (numEnabledLinkGenes < 1) numEnabledLinkGenes = 1; for (int k = 0; k < childLinkList.Count; k++) { float mutateChance = mutationBlastModifier * masterMutationRate / (1f + (float)numEnabledLinkGenes * 0.15f); if (LifetimeGeneration - childLinkList[k].birthGen < newLinkBonusDuration) { float t = 1 - ((LifetimeGeneration - childLinkList[k].birthGen) / (float)newLinkBonusDuration); // t=0 means age of gene is same as bonusDuration, t=1 means it is brand new mutateChance = Mathf.Lerp(mutateChance, mutateChance * newLinkMutateBonus, t); } if (CheckForMutation(mutateChance)) { // Weight Mutation! //Debug.Log("Weight Mutation Link[" + k.ToString() + "] weight: " + childLinkList[k].weight.ToString() + ", mutate: " + MutateFloat(childLinkList[k].weight).ToString()); childLinkList[k].weight = MutateFloat(childLinkList[k].weight); totalNumWeightMutations++; } } if (CheckForMutation(mutationBlastModifier * mutationRemoveLinkChance)) { //Debug.Log("Remove Link Mutation Agent[" + newChildIndex.ToString() + "]"); childBrainGenome.RemoveRandomLink(); } if (CheckForMutation(mutationBlastModifier * mutationAddNodeChance)) { // Adds a new node //Debug.Log("Add Node Mutation Agent[" + newChildIndex.ToString() + "]"); childBrainGenome.AddNewRandomNode(LifetimeGeneration, GetNextAddonInnov()); } if (CheckForMutation(mutationBlastModifier * mutationAddLinkChance)) { // Adds new connection //Debug.Log("Add Link Mutation Agent[" + newChildIndex.ToString() + "]"); if(CheckForMutation(existingNetworkBias)) { childBrainGenome.AddNewExtraLink(existingFromNodeBias, LifetimeGeneration); } else { childBrainGenome.AddNewRandomLink(LifetimeGeneration); } } if (CheckForMutation(mutationBlastModifier * mutationActivationFunctionChance)) { TransferFunctions.TransferFunction newFunction; int randIndex = Mathf.RoundToInt(UnityEngine.Random.Range(0f, childNodeList.Count - 1)); int randomTF = (int)UnityEngine.Random.Range(0f, 12f); switch (randomTF) { case 0: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 1: newFunction = TransferFunctions.TransferFunction.Linear; break; case 2: newFunction = TransferFunctions.TransferFunction.Gaussian; break; case 3: newFunction = TransferFunctions.TransferFunction.Abs; break; case 4: newFunction = TransferFunctions.TransferFunction.Cos; break; case 5: newFunction = TransferFunctions.TransferFunction.Sin; break; case 6: newFunction = TransferFunctions.TransferFunction.Tan; break; case 7: newFunction = TransferFunctions.TransferFunction.Square; break; case 8: newFunction = TransferFunctions.TransferFunction.Threshold01; break; case 9: newFunction = TransferFunctions.TransferFunction.ThresholdNegPos; break; case 10: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 11: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; case 12: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; default: newFunction = TransferFunctions.TransferFunction.RationalSigmoid; break; } if (childNodeList[randIndex].nodeType != GeneNodeNEAT.GeneNodeType.Out) { // keep outputs -1 to 1 range Debug.Log("ActivationFunction Mutation Node[" + randIndex.ToString() + "] prev: " + childNodeList[randIndex].activationFunction.ToString() + ", new: " + newFunction.ToString()); childNodeList[randIndex].activationFunction = newFunction; } } //for (int t = 0; t < childNodeList.Count; t++) { //} } else { Debug.Log("Mutation Disabled!"); } // THE BODY!!!!! ++++++++++++++++++++++================+++++++++++++++++++===============+++++++++++++++++++===================+++++++++++++++++============== CritterGenome childBodyGenome = new CritterGenome(); // create new body genome for Child // Iterate over the nodes of the more fit parent: for (int i = 0; i < parentAgentsArray[0].bodyGenome.CritterNodeList.Count; i++) { int currentNodeInno = parentAgentsArray[0].bodyGenome.CritterNodeList[i].innov; if (i == 0) { // if this is the ROOT NODE: childBodyGenome.CritterNodeList[0].CopySettingsFromNode(parentAgentsArray[0].bodyGenome.CritterNodeList[0]); // The root node was already created during the Constructor method of the CritterGenome, // ... so instead of creating a new one, just copy the settings } else { // NOT the root node, proceed normally: // Create new cloned node defaulted to the settings of the source( more-fit parent's) Node: CritterNode clonedCritterNode = parentAgentsArray[0].bodyGenome.CritterNodeList[i].CloneThisCritterNode(); childBodyGenome.CritterNodeList.Add(clonedCritterNode); } } // ADD-ONS!!!!!!!!!!!!!!!!!!!!!! BreedCritterAddons(ref childBodyGenome, ref parentAgentsArray[0].bodyGenome, ref parentAgentsArray[0].bodyGenome); newChildAgent.bodyGenome = childBodyGenome; if (useMutation) { // BODY MUTATION: PerformBodyMutation(ref childBodyGenome, ref childBrainGenome); } } newChildAgent.brainGenome = childBrainGenome; //newChildAgent.brainGenome.nodeNEATList = childNodeList; //newChildAgent.brainGenome.linkNEATList = childLinkList; BrainNEAT childBrain = new BrainNEAT(newChildAgent.brainGenome); childBrain.BuildBrainNetwork(); newChildAgent.brain = childBrain; //Debug.Log("NEW CHILD numNodes: " + newChildAgent.brainGenome.nodeNEATList.Count.ToString() + ", #Neurons: " + newChildAgent.brain.neuronList.Count.ToString()); //newChildAgent.bodyGenome.PreBuildCritter(0.8f); // Species: if (useSpeciation) { float randAdoption = UnityEngine.Random.Range(0f, 1f); if (randAdoption < adoptionRate) { // Attempts to Found a new species bool speciesGenomeMatch = false; for (int s = 0; s < childSpeciesPoolsList.Count; s++) { float geneticDistance = GenomeNEAT.MeasureGeneticDistance(newChildAgent.brainGenome, childSpeciesPoolsList[s].templateGenome, neuronWeight, linkWeight, weightWeight, normalizeExcess, normalizeDisjoint, normalizeLinkWeight); if (geneticDistance < speciesSimilarityThreshold) { speciesGenomeMatch = true; //agent.speciesID = speciesBreedingPoolList[s].speciesID; // this is done inside the AddNewAgent method below v v v childSpeciesPoolsList[s].AddNewAgent(newChildAgent); //Debug.Log(" NEW CHILD (" + newChildIndex.ToString() + ") SortAgentIntoBreedingPool dist: " + geneticDistance.ToString() + ", speciesIDs: " + newChildAgent.speciesID.ToString() + ", " + childSpeciesPoolsList[s].speciesID.ToString() + ", speciesCount: " + childSpeciesPoolsList[s].agentList.Count.ToString()); break; } } if (!speciesGenomeMatch) { SpeciesBreedingPool newSpeciesBreedingPool = new SpeciesBreedingPool(newChildAgent.brainGenome, sourcePopulation.GetNextSpeciesID()); // creates new speciesPool modeled on this agent's genome newSpeciesBreedingPool.AddNewAgent(newChildAgent); // add this agent to breeding pool childSpeciesPoolsList.Add(newSpeciesBreedingPool); // add new speciesPool to the population's list of all active species //Debug.Log(" NEW CHILD (" + newChildIndex.ToString() + ") SortAgentIntoBreedingPool NO MATCH!!! -- creating new BreedingPool " + newSpeciesBreedingPool.speciesID.ToString() + ", newChildAgentSpeciesID: " + newChildAgent.speciesID.ToString()); } } else { // joins parent species automatically: SpeciesBreedingPool newSpeciesBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, parentAgentBreedingPool.speciesID); newSpeciesBreedingPool.AddNewAgent(newChildAgent); // add this agent to breeding pool //Debug.Log(" NEW CHILD (" + newChildIndex.ToString() + ") NO ADOPTION SortAgentIntoBreedingPool speciesIDs: " + newChildAgent.speciesID.ToString() + ", " + newSpeciesBreedingPool.speciesID.ToString() + ", speciesCount: " + newSpeciesBreedingPool.agentList.Count.ToString()); } } else { // joins parent species automatically: SpeciesBreedingPool newSpeciesBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, parentAgentBreedingPool.speciesID); newSpeciesBreedingPool.AddNewAgent(newChildAgent); // add this agent to breeding pool } newChildAgent.parentFitnessScoreA = sourcePopulation.masterAgentArray[newChildIndex].fitnessScore; newAgentArray[newChildIndex] = newChildAgent; newChildIndex++; // new child created! newChildrenCreated++; } } /*Debug.Log("Finished Crossover! childSpeciesPoolsList:"); for (int i = 0; i < sourcePopulation.speciesBreedingPoolList.Count; i++) { string poolString = " Child Species ID: " + sourcePopulation.speciesBreedingPoolList[i].speciesID.ToString(); for (int j = 0; j < sourcePopulation.speciesBreedingPoolList[i].agentList.Count; j++) { poolString += ", member# " + j.ToString() + ", species: " + sourcePopulation.speciesBreedingPoolList[i].agentList[j].speciesID.ToString() + ", fitRank: " + sourcePopulation.speciesBreedingPoolList[i].agentList[j].fitnessRank.ToString(); } Debug.Log(poolString); }*/ // Clear out extinct species: int listIndex = 0; for (int s = 0; s < childSpeciesPoolsList.Count; s++) { if (listIndex >= childSpeciesPoolsList.Count) { Debug.Log("end childSpeciesPoolsList " + childSpeciesPoolsList.Count.ToString() + ", index= " + listIndex.ToString()); break; } else { if (childSpeciesPoolsList[listIndex].agentList.Count == 0) { // if empty: //Debug.Log("Species " + childSpeciesPoolsList[listIndex].speciesID.ToString() + " WENT EXTINCT!!! --- childSpeciesPoolsList[" + listIndex.ToString() + "] old Count: " + childSpeciesPoolsList.Count.ToString() + ", s: " + s.ToString()); childSpeciesPoolsList.RemoveAt(listIndex); //s--; // see if this works } else { listIndex++; } } } Debug.Log("Finished Crossover! totalNumWeightMutations: " + totalNumWeightMutations.ToString() + ", mutationBlastModifier: " + mutationBlastModifier.ToString() + ", bodyMutationBlastModifier: " + bodyMutationBlastModifier.ToString() + ", LifetimeGeneration: " + LifetimeGeneration.ToString() + ", currentGeneration: " + currentGeneration.ToString() + ", sourcePopulation.trainingGenerations: " + sourcePopulation.trainingGenerations.ToString()); sourcePopulation.masterAgentArray = newAgentArray; sourcePopulation.speciesBreedingPoolList = childSpeciesPoolsList; /*Debug.Log("Finished Crossover! sourcePopulation.speciesBreedingPoolList:"); for (int i = 0; i < sourcePopulation.speciesBreedingPoolList.Count; i++) { string poolString = "New Species ID: " + sourcePopulation.speciesBreedingPoolList[i].speciesID.ToString(); for (int j = 0; j < sourcePopulation.speciesBreedingPoolList[i].agentList.Count; j++) { poolString += ", member# " + j.ToString() + ", species: " + sourcePopulation.speciesBreedingPoolList[i].agentList[j].speciesID.ToString() + ", fitRank: " + sourcePopulation.speciesBreedingPoolList[i].agentList[j].fitnessRank.ToString(); } Debug.Log(poolString); }*/ return sourcePopulation; }
public override object Read(ES2Reader reader) { GeneLinkNEAT data = new GeneLinkNEAT(); Read(reader, data); return data; }
public void AddNewRandomNode(int gen, int inno) { if(linkNEATList.Count > 0) { int linkID = (int)UnityEngine.Random.Range(0f, (float)linkNEATList.Count); linkNEATList[linkID].enabled = false; // disable old connection GeneNodeNEAT newHiddenNode = new GeneNodeNEAT(nodeNEATList.Count, GeneNodeNEAT.GeneNodeType.Hid, TransferFunctions.TransferFunction.RationalSigmoid, inno, 0, false, 0); nodeNEATList.Add(newHiddenNode); // add new node between old connection // create two new connections GeneLinkNEAT newLinkA = new GeneLinkNEAT(linkNEATList[linkID].fromNodeID, GetInt3FromNodeIndex(newHiddenNode.id), linkNEATList[linkID].weight, true, GetNextInnovNumber(), gen); GeneLinkNEAT newLinkB = new GeneLinkNEAT(GetInt3FromNodeIndex(newHiddenNode.id), linkNEATList[linkID].toNodeID, 1f, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLinkA); linkNEATList.Add(newLinkB); //Debug.Log("AddNewRandomNode() linkID: " + linkID.ToString() + ", "); } else { Debug.Log("No connections! Can't create new node!!!"); } }
public void AddNewExtraLink(float fromBias, int gen) { // has a higher-than-random chance to create a new link with at least one node that is already connected List<GeneNodeNEAT> eligibleFromNodes = new List<GeneNodeNEAT>(); List<GeneNodeNEAT> eligibleToNodes = new List<GeneNodeNEAT>(); bool reuseFromNode; // true = use an existing FROM node, false = use an existing TO node float randToOrFrom = UnityEngine.Random.Range(0f, 1f); if(randToOrFrom < fromBias) { reuseFromNode = true; } else { reuseFromNode = false; } for (int k = 0; k < linkNEATList.Count; k++) { // Populate eligible node lists with those nodes that are already connected: if(linkNEATList[k].enabled) { if(reuseFromNode) { // reuse an exisiting fromNode if (eligibleFromNodes.Contains(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].fromNodeID)])) { //Debug.Log("AddNewExtraLink() EligibleFromNode Already Contains Node " + linkNEATList[k].fromNodeID.ToString()); } else { eligibleFromNodes.Add(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].fromNodeID)]); } } else { // reuse an exisitng TO node if (eligibleToNodes.Contains(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].toNodeID)])) { //Debug.Log("AddNewExtraLink() EligibleToNode Already Contains Node " + linkNEATList[k].toNodeID.ToString()); } else { eligibleToNodes.Add(nodeNEATList[GetNodeIndexFromInt3(linkNEATList[k].toNodeID)]); } } } } for (int i = 0; i < nodeNEATList.Count; i++) { if (reuseFromNode) { // if re-using an exisitng FROM node, then get a TO node from random: if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleToNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { eligibleToNodes.Add(nodeNEATList[i]); } } else { // if re-using an exisitng TO node, then get a FROM node from random: if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { eligibleFromNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleFromNodes.Add(nodeNEATList[i]); } } } if (eligibleFromNodes.Count > 0 && eligibleToNodes.Count > 0) { // make sure that there is at least 1 from and to node that is possible int fromNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleFromNodes.Count); int toNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleToNodes.Count); if (eligibleFromNodes[fromNodeID].id == eligibleToNodes[toNodeID].id) { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("AddNewExtraLink() Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { Debug.Log("AddNewExtraLink() New Link TO ITSELF: " + eligibleFromNodes[fromNodeID].id.ToString() + " Doing it anyway!"); float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } else { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("AddNewExtraLink() Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); Debug.Log("AddNewExtraLink() NEW LINK!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } } }
/*public bool AreEqual(Vector3 vec1, Vector3 vec2) { bool isEqual = false; if(vec1.x == vec2.x) { if (vec1.y == vec2.y) { if (vec1.z == vec2.z) { isEqual = true; } } } return isEqual; } */ public void AddNewRandomLink(int gen) { List<GeneNodeNEAT> eligibleFromNodes = new List<GeneNodeNEAT>(); List<GeneNodeNEAT> eligibleToNodes = new List<GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { eligibleFromNodes.Add(nodeNEATList[i]); } else if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { eligibleFromNodes.Add(nodeNEATList[i]); eligibleToNodes.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { eligibleToNodes.Add(nodeNEATList[i]); } } if(eligibleFromNodes.Count > 0 && eligibleToNodes.Count > 0) { int fromNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleFromNodes.Count); int toNodeID = (int)UnityEngine.Random.Range(0f, (float)eligibleToNodes.Count); if (eligibleFromNodes[fromNodeID].id == eligibleToNodes[toNodeID].id) { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { Debug.Log("Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { Debug.Log("New Link TO ITSELF: " + eligibleFromNodes[fromNodeID].id.ToString() + " Doing it anyway!"); float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } else { // Check if this link already exists: bool linkExists = false; for (int i = 0; i < linkNEATList.Count; i++) { if (GetNodeIndexFromInt3(linkNEATList[i].toNodeID) == eligibleToNodes[toNodeID].id && GetNodeIndexFromInt3(linkNEATList[i].fromNodeID) == eligibleFromNodes[fromNodeID].id) { //Debug.Log("Attempted to add link but it already exists!!! from: " + eligibleFromNodes[fromNodeID].id.ToString() + ", to: " + eligibleToNodes[toNodeID].id.ToString()); linkExists = true; } } if (!linkExists) { float randomWeight = Gaussian.GetRandomGaussian() * 0.2f; //0f; // start zeroed to give a chance to try both + and - //Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(eligibleFromNodes[fromNodeID].id), GetInt3FromNodeIndex(eligibleToNodes[toNodeID].id), randomWeight, true, GetNextInnovNumber(), gen); linkNEATList.Add(newLink); } } } }
// loops through all output nodes andcreates a connection from a random input node to the output, so that all output nodes are hooked up // should result in the minimum amount of connections required to have 'full' functionality public void CreateMinimumRandomConnections() { int numInputs = 0; int numOutputs = 0; List<GeneNodeNEAT> inputNodeList = new List<GeneNodeNEAT>(); List<GeneNodeNEAT> outputNodeList = new List<GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if(nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { numInputs++; inputNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { numOutputs++; outputNodeList.Add(nodeNEATList[i]); } } for(int o = 0; o < outputNodeList.Count; o++) { int inNodeID = (int)UnityEngine.Random.Range(0f, (float)inputNodeList.Count); float randomWeight = Gaussian.GetRandomGaussian(); GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[inNodeID].id), GetInt3FromNodeIndex(outputNodeList[o].id), randomWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); } }
public void CreateInitialConnections(float connectedness, bool randomWeights) { int numInputs = 0; int numHidden = 0; int numOutputs = 0; List<GeneNodeNEAT> inputNodeList = new List<GeneNodeNEAT>(); List<GeneNodeNEAT> hiddenNodeList = new List<GeneNodeNEAT>(); List<GeneNodeNEAT> outputNodeList = new List<GeneNodeNEAT>(); for (int i = 0; i < nodeNEATList.Count; i++) { if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.In) { numInputs++; inputNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Hid) { numHidden++; hiddenNodeList.Add(nodeNEATList[i]); } if (nodeNEATList[i].nodeType == GeneNodeNEAT.GeneNodeType.Out) { numOutputs++; outputNodeList.Add(nodeNEATList[i]); } } if(numHidden > 0) { // if there is a hidden layer: float accumulatedLinkChance = UnityEngine.Random.Range(0f, 1f); // random offset for (int i = 0; i < inputNodeList.Count; i++) { // input layer to hidden layer for (int h = 0; h < hiddenNodeList.Count; h++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if (accumulatedLinkChance >= 1f) { // if connectedness is 0.34, link will be created every third cycle, for example float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[i].id), GetInt3FromNodeIndex(hiddenNodeList[h].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } for (int h = 0; h < hiddenNodeList.Count; h++) { // hidden layer to output layer for (int o = 0; o < outputNodeList.Count; o++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if (accumulatedLinkChance >= 1f) { // if connectedness is 0.34, link will be created every third cycle, for example float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(hiddenNodeList[h].id), GetInt3FromNodeIndex(outputNodeList[o].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } } else { // no hidden layer: float accumulatedLinkChance = UnityEngine.Random.Range(0f, 1f); // random offset for (int i = 0; i < inputNodeList.Count; i++) { for(int o = 0; o < outputNodeList.Count; o++) { accumulatedLinkChance += connectedness; // currently deterministic.... is this ok? if(accumulatedLinkChance >= 1f) { // if connectedness is 0.34, link will be created every third cycle, for example float initialWeight = 0f; if (randomWeights) { initialWeight = Gaussian.GetRandomGaussian(); } GeneLinkNEAT newLink = new GeneLinkNEAT(GetInt3FromNodeIndex(inputNodeList[i].id), GetInt3FromNodeIndex(outputNodeList[o].id), initialWeight, true, GetNextInnovNumber(), 0); linkNEATList.Add(newLink); accumulatedLinkChance -= 1f; } } } } }