Пример #1
0
        // TODO: Write results to InnerResult.
        public InnerResult start(int population_size, int max_generations = int.MaxValue, bool silent = false)
        {
            int last_index      = population_size - 1;
            int convergence_hit = -1;

            current_generation = 0;
            int first_goal_hit = -1;

            population = new List <T>();

            //Console.WriteLine("Going to run algorithm. Max generations: {0}, Min fitness: {1}", goal.MaxGenerations, goal.MinFitness);

            var stopwatch = System.Diagnostics.Stopwatch.StartNew();

            population = generator.Generate(population_size, genome_size);

            population[0].FunctionEvaluations = 0;

            do
            {
                shufflePopulation();
                procreatePopulation(population_size);

                //sortPopulation();
                //selectPopulation(population_size);

                population = selector.DoSelection(population, population_size).ToList();

                if (!silent)
                {
                    printPopulation();
                }

                //Dit moet nog even netter worden gemaakt eigenlijk met Goal enzo...:
                if (population[0].Fitness == 2)
                {
                    break;
                }

                current_generation++;

                if (first_goal_hit < 0 && population[0].Fitness >= goal.MinFitness)
                {
                    first_goal_hit = current_generation;
                }

                // No new genes entered the population (whatever the genome as, the last one should have shifted.
                // Comparing pointers here
                if ((IGenome)previous_last == (IGenome)population[last_index])
                {
                    convergence_hit = current_generation;
                    break;
                }

                // Or if all genomes are the same, possibly costly operation.
                if (converged(last_index))
                {
                    convergence_hit = current_generation;
                    break;
                }

                if (current_generation >= max_generations)
                {
                    break;
                }
            } while (true);

            stopwatch.Stop();

            Console.WriteLine(results(population_size));

            InnerResult res = new InnerResult();

            res.FirstHitGeneration = first_goal_hit;
            res.Success            = goal.AchievedFitnessGoal(population[0].Fitness);

            res.ConvergenceGeneration = convergence_hit;
            res.CPUTime             = stopwatch.ElapsedTicks;
            res.FunctionEvaluations = population[0].FunctionEvaluations;
            res.BestScore           = population[0].Fitness;
            res.BestSolution        = population[0].Data;

            if (best_result == null || population[0].Fitness > best_result.Fitness)
            {
                best_result = population[0];
            }

            return(res);
        }