public void RealiseEvolution()
        {
            // Initialisation
            ReferenceList     referenceList  = new ReferenceList("tsp.riesenia");
            Initialisation    initialisation = new Initialisation(initialPopulationCount, referenceList);
            List <Individual> population     = initialisation.InitialisePopulation();

            // Evaluation
            Evaluation evaluation = new Evaluation(referenceList);

            foreach (Individual individual in population)
            {
                evaluation.EvaluateIndividual(individual);
            }

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

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

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

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

                // Evaluation
                foreach (Individual individual in descendants)
                {
                    evaluation.EvaluateIndividual(individual);
                }

                // Replacement
                Replacement replacement = new Replacement(population, descendants, population.Count);
                population = replacement.NextGeneration();

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

            SaveBestIndividualsToFile(referenceList);
        }
示例#2
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();
        }