Exemplo n.º 1
0
        public void RealiseEvolution()
        {
            // Initialise population
            Initialisation    initialisation = new Initialisation(initialPopulationCount, goldFieldCount);
            List <Individual> population     = initialisation.InitialisePopulation();

            // Validate population
            for (int i = 0; i < population.Count; i++)
            {
                if (StaticOperations.ValidateIndividual(population[i]) == false)
                {
                    throw new NotSupportedException();
                }
            }

            // Evaluate population
            Evaluation evaluation = new Evaluation();

            for (int i = 0; i < population.Count; i++)
            {
                evaluation.EvaluateIndividual(population[i]);
            }

            // Evolution cycle
            for (int i = 0; i < evolutionCycles; i++)
            {
                Console.Write("# Epoch " + (i + 1));
                // Selection
                Selection selection = new Selection(population, population.Count);
                // Q tournament
                List <Individual> parents = selection.SelectParents(4);

                // Genetic operators
                List <Individual> descendants      = new List <Individual>();
                GeneticOperators  geneticOperators = new GeneticOperators();
                for (int j = 0; j < parents.Count; j = j + 2)
                {
                    descendants.AddRange(geneticOperators.GenerateDescendants(parents[j], parents[j + 1]));
                }

                // Evaluation
                for (int j = 0; j < descendants.Count; j++)
                {
                    evaluation.EvaluateIndividual(descendants[j]);
                }

                // Replacement
                Replacement replacement = new Replacement(population, descendants, population.Count);
                if (i - bestFitnessEpoch < 100)
                {
                    population = replacement.NextGeneration();
                }
                else
                {
                    population  = replacement.KillBestIndividuals();
                    bestFitness = double.MaxValue;
                }

                foreach (Individual individual in population)
                {
                    if (StaticOperations.ValidateIndividual(individual) == false)
                    {
                        throw new NotSupportedException();
                    }
                }

                // Save best member
                List <Individual> orderedPopulation = population.OrderBy(ind => ind.Fitness).ToList();
                bestIndividualsPerGeneration.Add(orderedPopulation[0]);
                Console.WriteLine(" Minimum fitness: " + orderedPopulation[0].Fitness + ".");

                if (orderedPopulation[0].Fitness < bestFitness)
                {
                    bestFitness      = orderedPopulation[0].Fitness;
                    bestFitnessEpoch = i;
                }

                if (orderedPopulation[0].Fitness == 0)
                {
                    break;
                }
            }

            SaveDataToFile();
        }