public void RealizeEvolution()
        {
            Initialisation initialisation = new Initialisation(initialPopulationCount);
            // Initialisation - validated
            List <Path> population = initialisation.GenerateInitialPopulation();

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

            evaluation.EvaluatePopulation(population);
            // Encoding
            List <Representation> representations = new List <Representation>();
            Decoder decoder = new Decoder();

            foreach (Path path in population)
            {
                Representation representation = decoder.EncodePath(path);
                representations.Add(representation);
            }
            // Evolution cycle
            for (int i = 0; i < evolutionCycles; i++)
            {
                // Selection
                Selection             selection = new Selection(parentsCount, representations);
                List <Representation> parents   = selection.SelectParents();
                // Genetic operator - validated
                GeneticOperator       geneticOperator = new GeneticOperator(descendantsCount, parents);
                List <Representation> descendants     = geneticOperator.GenerateDescendants();
                // Decoding
                List <Path> descendantPaths = new List <Path>();
                foreach (Representation representation in descendants)
                {
                    Path path = decoder.DecodeRepresentation(representation);
                    if (StaticOperations.ValidatePath(path) == false)
                    {
                        throw new NotSupportedException();
                    }
                    descendantPaths.Add(path);
                }
                // Evaluation
                evaluation.EvaluatePopulation(descendantPaths);
                for (int j = 0; j < descendants.Count; j++)
                {
                    descendants[j].Fitness = descendantPaths[j].Fitness;
                }
                // Replacement
                Replacement replacement = new Replacement(parents, descendants, initialPopulationCount);
                representations = replacement.NextGeneration();
                // Save to export file
                SaveTwoBestMembers(representations);
            }
        }
예제 #2
0
        public void RealizeEvolution()
        {
            Random         random         = new Random();
            Initialisation initialisation = new Initialisation(initialPopulationCount);
            // Initialisation - validated
            List <Path> population = initialisation.GenerateInitialPopulation();

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

            evaluation.EvaluatePopulation(population);
            // Encoding
            List <Representation> representations = new List <Representation>();
            Decoder decoder = new Decoder();

            foreach (Path path in population)
            {
                Representation representation = decoder.EncodePath(path);
                representations.Add(representation);
            }
            // Evolution cycle
            for (int i = 0; i < evolutionCycles; i++)
            {
                Console.Write("Epoch #" + i + ".");
                // Reinitialisation happens every 1/10th iteration and randomly resets half of population
                // Elite 10% is untouched by this process
                if ((i % 500) == 0 && i != 0)
                {
                    ReinitializePopulation(representations, (int)(3 * initialPopulationCount / 4));
                }
                // Remap fitness using exponential remapping
                //RemapFitness(representations, remapParameter);
                // Selection
                Selection             selection = new Selection(parentsCount, representations);
                List <Representation> parents   = selection.SelectParents();
                //List<Representation> parents = selection.SelectCombinedParents();
                // Genetic operator - validated
                GeneticOperator       geneticOperator = new GeneticOperator(descendantsCount, parents);
                List <Representation> descendants     = geneticOperator.GenerateDescendants();
                // Decoding
                List <Path> descendantPaths = new List <Path>();
                foreach (Representation representation in descendants)
                {
                    Path path = decoder.DecodeRepresentation(representation);
                    if (StaticOperations.ValidatePath(path) == false)
                    {
                        throw new NotSupportedException();
                    }
                    descendantPaths.Add(path);
                }
                // Evaluation
                evaluation.EvaluatePopulation(descendantPaths);
                for (int j = 0; j < descendants.Count; j++)
                {
                    descendants[j].Fitness = descendantPaths[j].Fitness;
                }
                // Revaluate current population after fitness remapping
                //List<Path> currentPaths = new List<Path>();
                //foreach (Representation representation in representations)
                //{
                //    Path path = decoder.DecodeRepresentation(representation);
                //    currentPaths.Add(path);
                //}
                //evaluation.EvaluatePopulation(currentPaths);
                //for (int j = 0; j < representations.Count; j++)
                //{
                //    representations[j].Fitness = currentPaths[j].Fitness;
                //}
                // Replacement
                Replacement replacement = new Replacement(representations, descendants, initialPopulationCount);
                //representations = replacement.GenerationReplacement();
                //representations = replacement.NextGeneration();
                representations = replacement.DuplicationElimination(7, representations.Count / 20 < 3 ? representations.Count / 20 : 3, 20);
                Console.Write(" Maximum fitness: " + representations.Max(item => item.Fitness));
                // Save to export file
                SaveSixBestMembers(representations);
            }
        }