/// <summary> /// Ends the current generation. /// </summary> /// <returns><c>true</c>, if current generation was ended, <c>false</c> otherwise.</returns> private bool EndCurrentGeneration() { EvaluateFitness(); Population.EndCurrentGeneration(); var handler = GenerationRan; if (handler != null) { handler(this, EventArgs.Empty); } if (Termination.HasReached(this)) { State = GeneticAlgorithmState.TerminationReached; handler = TerminationReached; if (handler != null) { handler(this, EventArgs.Empty); } return(true); } if (m_stopRequested) { TaskExecutor.Stop(); State = GeneticAlgorithmState.Stopped; } return(false); }
/// <summary> /// Resumes the last evolution of the genetic algorithm. /// <remarks> /// If genetic algorithm was not explicit Stop (calling Stop method), you will need provide a new extended Termination. /// </remarks> /// </summary> public void Resume() { try { lock (m_lock) { m_stopRequested = false; } if (Population.GenerationsNumber == 0) { throw new InvalidOperationException("Attempt to resume a genetic algorithm which was not yet started."); } if (Population.GenerationsNumber > 1) { if (Termination.HasReached(this)) { throw new InvalidOperationException("Attempt to resume a genetic algorithm with a termination ({0}) already reached. Please, specify a new termination or extend the current one.".With(Termination)); } State = GeneticAlgorithmState.Resumed; } if (EndCurrentGeneration()) { return; } bool terminationConditionReached = false; DateTime startDateTime; do { if (m_stopRequested) { break; } startDateTime = DateTime.Now; terminationConditionReached = EvolveOneGeneration(); TimeEvolving += DateTime.Now - startDateTime; }while (!terminationConditionReached); } catch { State = GeneticAlgorithmState.Stopped; throw; } }
public void EvolveNextGeneration() { try { lock (m_lock) { m_stopRequested = false; } if (Population.GenerationsNumber == 0) { throw new InvalidOperationException("Attempt to resume a genetic algorithm which was not yet started."); } if (Population.GenerationsNumber > 1) { if (Termination.HasReached(this)) { throw new InvalidOperationException("Attempt to resume a genetic algorithm with a termination ({0}) already reached. Please, specify a new termination or extend the current one.".With(Termination)); } State = GeneticAlgorithmState.Resumed; } if (EndCurrentGeneration()) { return; } bool terminationConditionReached = false; DateTime startDateTime; startDateTime = DateTime.Now; var parents = SelectParents(); var offspring = Cross(parents); Mutate(offspring); var newGenerationChromosomes = Reinsert(offspring, parents); Population.CreateNewGeneration(newGenerationChromosomes); TimeEvolving += DateTime.Now - startDateTime; } catch { State = GeneticAlgorithmState.Stopped; throw; } }
/// <summary>Assigns fitness function value to offsprings and registers new generation in a population.</summary> /// <param name="parents">Parents obtained form the ProduceOffsprings node.</param> /// <param name="offsprings">Offsprings obtained form the ProduceOffsprings node.</param> /// <param name="offspringsFitness">A list of fitness function results for offsprings.</param> /// <returns name="algorithm">A genetic algorithm instance supplemented with a new generation.</returns> public void ProduceNewGeneration(List <object> parents, List <object> offsprings, List <double> offspringsFitness) { if (State == GeneticAlgorithmState.TerminationReached) { return; } IList <IChromosome> parentsChromosomes = new List <IChromosome>(); foreach (object parent in parents) { parentsChromosomes.Add((IChromosome)parent); } IList <IChromosome> offspringsChromosomes = new List <IChromosome>(); foreach (object offspring in offsprings) { offspringsChromosomes.Add((IChromosome)offspring); offspringsChromosomes.Last().Fitness = offspringsFitness[offsprings.IndexOf(offspring)]; } Population.MinSize = Population.MaxSize; var newGenerationChromosomes = Reinsert(offspringsChromosomes, parentsChromosomes); while (newGenerationChromosomes.Count < Population.CurrentGeneration.Chromosomes.Count()) { newGenerationChromosomes = Reinsert(newGenerationChromosomes, parentsChromosomes); } Population.CreateNewGeneration(newGenerationChromosomes); Population.EndCurrentGeneration(); if (Termination.HasReached(this) || (Population.GenerationsNumber >= maxNumberOfIterations)) { State = GeneticAlgorithmState.TerminationReached; TimeEvolving = Timer.Elapsed; Timer.Stop(); } }
/// <summary> /// Ends the current generation. /// </summary> /// <returns><c>true</c>, if current generation was ended, <c>false</c> otherwise.</returns> private bool EndCurrentGeneration() { EvaluateFitness(); Population.EndCurrentGeneration(); if (GenerationRan != null) { GenerationRan(this, EventArgs.Empty); } if (Termination.HasReached(this)) { State = GeneticAlgorithmState.TerminationReached; if (TerminationReached != null) { TerminationReached(this, EventArgs.Empty); } //Console.ForegroundColor = ConsoleColor.DarkGreen; //Console.WriteLine("Pick List Genetic Algorithm termination criteria are met."); //Console.ResetColor(); return(true); } if (m_stopRequested) { TaskExecutor.Stop(); State = GeneticAlgorithmState.Stopped; } //Console.ForegroundColor = ConsoleColor.Cyan; //Console.WriteLine("Pick List Genetic Algorithm will continue to evolve..."); //Console.ResetColor(); //Console.WriteLine(); return(false); }
/// <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); }