AdjustFitnesses() { float total = 0; for (int i = 0; i < m_vecMembers.Count; i++) { CGenome member = m_vecMembers[i]; float fitness = member.Fitness(); if (m_iAge < _params.YoungAgeThreshold) { fitness *= _params.YoungFitnessBonus; } else if (m_iAge > _params.OldAgeThreshold) { fitness *= _params.OldAgePenalty; } total += fitness; // Calculation of fitness sharing float adjustedFitness = fitness / m_vecMembers.Count; member.SetAdjustedFitness(adjustedFitness); } }
AddMember(CGenome genome) { if (genome.Fitness() > m_dBestFitness) { m_dBestFitness = genome.Fitness(); m_iGensWithoutImproving = 0; m_Leader = genome; } m_vecMembers.Add(genome); /* * m_vecMembers.Sort(delegate (CGenome a, CGenome b) { * return b.Fitness().CompareTo(a.Fitness()); * });*/ }
CSpecies(CGenome leader, int speciesID) { m_vecMembers = new List <CGenome> (); m_iSpeciesID = speciesID; m_dBestFitness = leader.Fitness(); m_Leader = leader; m_vecMembers.Add(leader); }
TournamentSelection(int numSelections) { double bestFitness = 0; CGenome best = m_Population[0]; for (int i = 0; i < numSelections; i++) { CGenome genome = m_Population[Random.Range(0, m_Population.Count)]; if (genome.Fitness() > bestFitness) { best = genome; bestFitness = genome.Fitness(); } } return(best); }
DetermineBestParent(CGenome mum, CGenome dad) { if (mum.Fitness() == dad.Fitness()) { if (mum.NumGenes() == dad.NumGenes()) { if (Random.value < 0.5) { return(ParentType.Mum); } else { return(ParentType.Dad); } } else { /* * Choose the parent with the fewest genes because * the fitness is the same. */ if (mum.NumGenes() < dad.NumGenes()) { return(ParentType.Mum); } else { return(ParentType.Dad); } } } else { if (mum.Fitness() > dad.Fitness()) { return(ParentType.Mum); } else { return(ParentType.Dad); } } }
SpawnLeaders(List <CGenome> newPopulation) { for (int i = 0; i < m_vecSpecies.Count; i++) { CSpecies species = m_vecSpecies[i]; CGenome baby = species.Leader(); Debug.Log("spawning leader (" + baby.Fitness() + "): " + baby.ID() + " for " + species.ID()); newPopulation.Add(baby); } return(newPopulation.Count); }