예제 #1
0
        static Tuple <int, int, int, int> Execute(GeneticAlgorithm ga)
        {
            int       firstHit = -1;
            Stopwatch sw       = new Stopwatch();

            sw.Start();

            int generation = 0;
            List <Individual> population = ga.GenerateInitialPopulation(); //generate initial population
            Individual        Fittest    = ga.GetFittest(population);      //get the individual having the fittest value from initial the population

            double lowestParent, highestChild;

            while (true) //repeat while the highest fitness value of the offspring is bigger than the lowest fitness value of the parent
            {
                generation++;
                List <Individual> offspring = ga.Recombination(population); //do recombination (crossover)

                lowestParent = population.Select(x => x.Fitness).Min();
                highestChild = offspring.Select(x => x.Fitness).Max();

                if (highestChild <= lowestParent)
                {
                    break;
                }

                List <Individual> nextGeneration = ga.GenerateNextGeneration(population, offspring); //do selection

                Individual runFittest = ga.GetFittest(nextGeneration);                               //ge the fittest individual from population

                if (runFittest.Binarystring == ga.globalOptimum && firstHit == -1)
                {
                    firstHit = generation;
                }

                population = nextGeneration;
            }
            sw.Stop();

            return(new Tuple <int, int, int, int>(firstHit, generation, ga.ff.getFctEval(), (int)sw.ElapsedMilliseconds));
        }