Cga(int inputs, int outputs) { m_Population = new List <CGenome> (); for (int i = 0; i < _params.NumGenomesToSpawn; i++) { m_Population.Add(new CGenome(nextGenomeID++, inputs, outputs)); } m_PopSize = m_Population.Count; // Create simple genome used for innovations database CGenome genome = new CGenome(1, inputs, outputs); m_vecSpecies = new List <CSpecies> (); innovation = new CInnovation(genome.GetLinks(), genome.GetNeurons()); vecSplits = new List <SplitDepth> (); // Create the network depth lookup table. Split(0, 1, 0); }
SelectGenesToBreed(CGenome mum, CGenome dad, ParentType best, List <SLinkGene> babyGenes, List <int> neuronIDs) { List <SLinkGene> mumLinks = mum.GetLinks(); List <SLinkGene> dadLinks = dad.GetLinks(); using (IEnumerator <SLinkGene> mumEnumerator = mumLinks.GetEnumerator()) using (IEnumerator <SLinkGene> dadEnumerator = dadLinks.GetEnumerator()) { bool hasMumMore = mumEnumerator.MoveNext(); bool hasDadMore = dadEnumerator.MoveNext(); SLinkGene selectedGene = mumEnumerator.Current; while (hasMumMore || hasDadMore) { if (!hasMumMore && hasDadMore) { if (best == ParentType.Dad) { selectedGene = dadEnumerator.Current; } hasDadMore = dadEnumerator.MoveNext(); } else if (!hasDadMore && hasMumMore) { if (best == ParentType.Mum) { selectedGene = mumEnumerator.Current; } hasMumMore = mumEnumerator.MoveNext(); } else if (mumEnumerator.Current.InnovationID < dadEnumerator.Current.InnovationID) { if (best == ParentType.Mum) { selectedGene = mumEnumerator.Current; } hasMumMore = mumEnumerator.MoveNext(); } else if (dadEnumerator.Current.InnovationID < mumEnumerator.Current.InnovationID) { if (best == ParentType.Dad) { selectedGene = dadEnumerator.Current; } hasDadMore = dadEnumerator.MoveNext(); } else if (dadEnumerator.Current.InnovationID == mumEnumerator.Current.InnovationID) { if (Random.value < 0.5f) { selectedGene = mumEnumerator.Current; } else { selectedGene = dadEnumerator.Current; } hasMumMore = mumEnumerator.MoveNext(); hasDadMore = dadEnumerator.MoveNext(); } if (babyGenes.Count == 0) { babyGenes.Add(new SLinkGene(selectedGene)); } else { if (babyGenes[babyGenes.Count - 1].InnovationID != selectedGene.InnovationID) { babyGenes.Add(new SLinkGene(selectedGene)); } } AddNeuronID(selectedGene.FromNeuron, neuronIDs); AddNeuronID(selectedGene.ToNeuron, neuronIDs); } } }