Example #1
0
        /// <summary>
        /// Evolve one generation.
        /// </summary>
        /// <returns>True if termination has been reached, otherwise false.</returns>
        private bool EvolveOneGeneration()
        {
            if (_debugging)
            {
                timer.Reset();
                timer.Start();
                Console.WriteLine("Evolving a generation in GA");
            }


            try
            {
                foreach (var spec in Species)
                {
                    TaskExecutorGen.Add(() =>
                    {
                        spec.GenerateChildren();
                    });
                }
                if (!TaskExecutorGen.Start())
                {
                    throw new TimeoutException("The fitness evaluation rech the {0} timeout.".With(TaskExecutorGen.Timeout));
                }
            }
            finally
            {
                TaskExecutorGen.Stop();
                TaskExecutorGen.Clear();
            }


            if (_debugging)
            {
                Console.WriteLine("Evolved a generation. About to calculate fitness and end it. Time at {0}", timer.Elapsed.ToString());
                timer.Stop();
            }


            return(EndCurrentGeneration());
        }
Example #2
0
        /// <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);
        }