public void UpdateBestIndividual() { // in ES, the best individual is the one with minimal fitness double minFitness = double.PositiveInfinity; int bestIndividualIndex = -1; // iterate through all individuals for (int index = 0; index < m_populationSize; index++) { double fitness = m_population[index].GetFitness(); if (fitness <= minFitness) { bestIndividualIndex = index; minFitness = fitness; } } // set the best individual base on bestIndividualIndex if (bestIndividualIndex < 0) { throw new Exception("bestIndividualIndex is negative!"); } else { m_bestIndividual = m_population[bestIndividualIndex]; } }
public Chromesome(Chromesome parent) { world = parent.world; data = new double[10]; stepSizes = new double[10]; parent.data.CopyTo(data, 0); parent.stepSizes.CopyTo(stepSizes, 0); singleStepSize = parent.singleStepSize; }
public void ToNextGeneration() { m_currentGeneration++; // (1,1) , always use children if (!Configuration.s_isParentCandidate) { for (int index = 0; index < m_populationSize; index++) { double originalFitness = m_population[index].GetFitness(); m_population[index].Mutate(); double mutatedFitness = m_population[index].GetFitness(); } } else // (1+1) { for (int index = 0; index < m_populationSize; index++) { Chromesome parent = m_population[index]; Chromesome child = new Chromesome(parent); // mutate the child child.Mutate(); // in this case, parent wins if (parent.GetFitness() < child.GetFitness()) { m_population[index] = parent; } else // in this case, child (after mutation) wins { m_population[index] = child; } } } // set up for successful mutation double originalBestFitness = BestIndividual.GetFitness(); // Update world status UpdateBestIndividual(); double mutatedBestFitness = BestIndividual.GetFitness(); if (mutatedBestFitness < originalBestFitness) { m_successfulMutationCount++; } }
public World(int _populationSize, Func <World, bool> _terminateCondition) { // fixed initialization m_currentGeneration = 0; m_successfulMutationCount = 0; // customed initialization m_populationSize = _populationSize; m_terminateCondition = _terminateCondition; // initialization base on customed variables m_population = new Chromesome[m_populationSize]; for (int index = 0; index < m_populationSize; index++) { m_population[index] = new Chromesome(this); } UpdateBestIndividual(); }