// Update fitness public bool UpdateFitness() { double fitness = 0.0, averageFitness = 0.0; int index = -1; for (int i = 0; i < _populationsize; ++i) { fitness = Fitness(_population[i]); averageFitness += fitness; if (_bestFitness < fitness) { _bestFitness = fitness; index = i; } else { } } if (_bestAverageFitness < averageFitness && index >= 0) { _bestAverageFitness = averageFitness; _bestSolution = DataCentre.DeepCopy(_population[index]); return(true); } else { } return(false); }
// Evolve the GA population public void Evolve() { while (_noupdate < _termination) { int[][] newpopulation = DataCentre.DeepCopy(_population); for (int i = 0; i < _populationsize; ++i) { DataCentre._random = new Random((int)DateTime.Now.Ticks); if (DataCentre._random.NextDouble() <= _pcrossover) { int j = Selection(i); int[] child = CrossOver(_population[i], _population[j]); if (Fitness(child) > Fitness(newpopulation[i])) { newpopulation[i] = child; } else { } } else { } DataCentre._random = new Random((int)DateTime.Now.Ticks); if (DataCentre._random.NextDouble() <= _pmutation) { newpopulation[i] = Mutation(newpopulation[i]); } else { } } _population = DataCentre.DeepCopy(newpopulation); if (UpdateFitness()) { _noupdate = 0; } else { ++_noupdate; } } }