/// <summary> /// Evaluates the fitness. /// </summary> private void EvaluateFitness(CCESpecies spec) { // For each individual we run fitness with a set // of best chromosomes, and a random set // // BestChromosomeList can be used // Get random from spec.Population.CurrentGeneration.Chromosomes // -- also verify that it is not the same as the best // --- just take any but the first in list (it is ordered) // // Use fitness function to get fitness for both sets // Fitness given is best of the two try { CurrentSpec = spec; var coll = spec.Offspring; if (coll == null) { coll = spec.Population.CurrentGeneration.Chromosomes; } foreach (var individual in coll) { if (individual.Fitness.HasValue) { continue; } List <IChromosome> RandomList = new List <IChromosome>(); for (int o = 0; o < Species.Count; o++) { if (Object.ReferenceEquals(Species[o], spec)) { // this is to keep it in the same order as the species list, always RandomList.Add(individual); continue; } int randIndex; if (Species[o].Population.CurrentGeneration.Chromosomes.Count > 1) { randIndex = RandomizationProvider.Current.GetInt(0, Species[o].Population.CurrentGeneration.Chromosomes.Count - 1); } else { randIndex = 0; } RandomList.Add(Species[o].Population.CurrentGeneration.Chromosomes[randIndex]); } TaskExecutorFit.Add(() => { RunEvaluateFitness(individual, RandomList); }); } if (!TaskExecutorFit.Start()) { throw new TimeoutException("The fitness evaluation rech the {0} timeout.".With(TaskExecutorFit.Timeout)); } } finally { TaskExecutorFit.Stop(); TaskExecutorFit.Clear(); } }
/// <summary> /// Ends the current generation. /// </summary> /// <returns><c>true</c>, if current generation was ended, <c>false</c> otherwise.</returns> private bool EndCurrentGeneration() { if (_debugging) { Console.WriteLine("Starting fitness evaluation"); timer.Reset(); timer.Start(); } // Insert multithreading here? try { foreach (var spec in Species) { TaskExecutorFitMaster.Add(() => { EvaluateFitness(spec); }); } if (!TaskExecutorFitMaster.Start()) { throw new TimeoutException("The fitness evaluation rech the {0} timeout.".With(TaskExecutorFitMaster.Timeout)); } } finally { TaskExecutorFitMaster.Stop(); TaskExecutorFitMaster.Clear(); } if (_debugging) { Console.WriteLine("Evolved a generation. About to calculate fitness and end it. Time at {0}", timer.Elapsed.ToString()); timer.Stop(); } if (_debugging) { Console.WriteLine("Calculating Fitness done. Timer at {0}", timer.Elapsed); timer.Stop(); timer.Reset(); Console.WriteLine("Starting reinsertion"); timer.Start(); } foreach (var spec in Species) { spec.EndCurrentGeneration(); } if (_debugging) { Console.WriteLine("Reinsertion done. Timer at {0}", timer.Elapsed); timer.Stop(); } GenerationsNumber++; //update uber best set if (UberBestSet == null || UberBestSet.Count == 0) { UberBestSet = new List <IChromosome> (); foreach (IChromosome c in BestChromosomeSet) { UberBestSet.Add(c.Clone()); } } else { for (int i = 0; i < BestChromosomeSet.Count; i++) { if (UberBestSet [i].Fitness < BestChromosomeSet [i].Fitness) { UberBestSet [i] = BestChromosomeSet [i].Clone(); } } } if (GenerationRan != null) { GenerationRan(this, EventArgs.Empty); } if (Termination.HasReached(this)) { State = GeneticAlgorithmState.TerminationReached; if (TerminationReached != null) { TerminationReached(this, EventArgs.Empty); } return(true); } if (m_stopRequested) { TaskExecutorGen.Stop(); TaskExecutorFit.Stop(); State = GeneticAlgorithmState.Stopped; } return(false); }