/// <summary>
        /// Creates instance of genetic algorithm
        /// </summary>
        /// <param name="graph">Graph on which algorithm will operate</param>
        /// <param name="problem">Problem for which algorithm will attempt to find solution</param>
        /// <param name="geneticOperators">Genetic operators used for breeding new generations</param>
        /// <param name="settings">General settings for solution finding process</param>
        /// <param name="startingPopulation">Starting population for algorithm</param>
        //TODO: Introduce SavedState complex type
        public GeneticAlgorithm(Graph graph, IProblem problem, GeneticOperators geneticOperators,
                                GeneticAlgorithmSettings settings, ICollection <Individual> startingPopulation = null,
                                uint currentGeneration = 0, Guid?id = null)
            : base(graph, problem, id)
        {
            if (geneticOperators == null)
            {
                throw new ArgumentNullException(nameof(geneticOperators));
            }
            if (!geneticOperators.IsValid())
            {
                throw new ArgumentException("Genetic operators are not valid", nameof(geneticOperators));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            GeneticOperators = geneticOperators;
            Settings         = settings;
            if (startingPopulation == null || startingPopulation.Count == 0 || !startingPopulation.All(IsIndividualValid))
            {
                GenerateInitialPopulation();
            }
            else
            {
                CurrentPopulation = new SortedSet <Individual>();
                foreach (var element in startingPopulation)
                {
                    CurrentPopulation.Add(element);
                }
            }

            CurrentGeneration = currentGeneration;
        }
        /// <summary>
        /// Creates instance of genetic algorithm
        /// </summary>
        /// <param name="graph">Graph on which algorithm will operate</param>
        /// <param name="problem">Problem for which algorithm will attempt to find solution</param>
        /// <param name="geneticOperators">Genetic operators used for breeding new generations</param>
        /// <param name="settings">General settings for solution finding process</param>
        /// <param name="startingPopulation">Starting population for algorithm</param>
        //TODO: Introduce SavedState complex type
        public GeneticAlgorithm(Graph graph, IProblem problem, GeneticOperators geneticOperators, 
            GeneticAlgorithmSettings settings, ICollection<Individual> startingPopulation = null, 
            uint currentGeneration = 0, Guid? id = null)
            : base(graph, problem, id)
        {
            if (geneticOperators == null)
                throw new ArgumentNullException(nameof(geneticOperators));
            if (!geneticOperators.IsValid())
                throw new ArgumentException("Genetic operators are not valid", nameof(geneticOperators));
            if (settings == null)
                throw new ArgumentNullException(nameof(settings));
            GeneticOperators = geneticOperators;
            Settings = settings;
            if (startingPopulation == null || startingPopulation.Count == 0 || !startingPopulation.All(IsIndividualValid))
                GenerateInitialPopulation();
            else
            {
                CurrentPopulation = new SortedSet<Individual>();
                foreach (var element in startingPopulation)
                    CurrentPopulation.Add(element);
            }

            CurrentGeneration = currentGeneration;
        }
Beispiel #3
0
        public void RealiseEvolution()
        {
            // Initialisation
            Initialisation    initialisation = new Initialisation(initialPopulationCount, scale);
            List <Individual> population     = initialisation.GenerateInitialPopulation();

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

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

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

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

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

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

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

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

                List <Individual> orderedPopulation = population.OrderBy(item => item.Fitness).ToList();
                Console.WriteLine("Best individual has fitness: " + orderedPopulation[0].Fitness);
            }

            SaveBestIndividualIntoFile(population[0]);
        }
        public void RealiseEvolution()
        {
            // Initialise population - validated
            Initialisation    initialisation = new Initialisation(initialPopulationCount);
            List <Individual> population     = initialisation.GenerateInitialPopulation();

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

            // Evaluate synthethic fitness of population
            Evaluation evaluation = new Evaluation(firstCriteria, secondCriteria, thirdCriteria);

            foreach (Individual individual in population)
            {
                // Only criteria with true value will be considered
                evaluation.EvaluateIndividual(individual);
            }

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

                // 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
                foreach (Individual individual in descendants)
                {
                    // Only criteria with true value will be considered
                    evaluation.EvaluateIndividual(individual);
                }

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

                // Save best individual
                if (firstCriteria)
                {
                    List <Individual> paretSetSurvivors = population.Where(ind => ind.Fitness == 1 && ind.FitnessVector[0] == 1).ToList();
                    if (paretSetSurvivors.Count == 0)
                    {
                        bestIndividuals.Add(population[0]);
                    }
                    else
                    {
                        paretSetSurvivors.Shuffle();
                        bestIndividuals.Add(paretSetSurvivors[0]);
                    }

                    Console.WriteLine(" Paret set count: " + EvaluateParetSet(population));
                }
                else
                {
                    List <Individual> paretSetSurvivors = population.Where(ind => ind.Fitness == 1).ToList();
                    paretSetSurvivors.Shuffle();
                    bestIndividuals.Add(paretSetSurvivors[0]);
                    Console.WriteLine(" Paret set count: " + EvaluateParetSet(population));
                }
            }

            SaveBestIndividualsInFile();
        }
        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);
        }
Beispiel #6
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();
        }