public List <Individual> GenerateDescendants(Individual father, Individual mother)
        {
            Individual mutatedFather = MutateMember(father);
            Individual mutatedMother = MutateMember(mother);

            if (StaticOperations.ValidateIndividual(mutatedFather) == false)
            {
                throw new NotSupportedException();
            }
            if (StaticOperations.ValidateIndividual(mutatedMother) == false)
            {
                throw new NotSupportedException();
            }

            List <Individual> crossOverIndividuals = PMX(mutatedMother, mutatedFather);

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

            return(crossOverIndividuals);
        }
예제 #2
0
        private List <Individual> CrossOverIndividuals(Individual mutatedMother, Individual mutatedFather)
        {
            // Descendants are created by randomly switching a randomly generated amount of hours
            // Only corresponding hours may be switched and only when both individuals have the same machines running
            Individual fatherClone = Individual.CloneRepresentation(mutatedFather);
            Individual motherClone = Individual.CloneRepresentation(mutatedMother);

            Random random = new Random();
            double chance = random.NextDouble();

            for (int i = 0; i < fatherClone.Schedule.Count; i++)
            {
                double value = random.NextDouble();
                if (value >= chance)
                {
                    if (IsCrossOverApplicable(fatherClone.Schedule[i], motherClone.Schedule[i]))
                    {
                        List <Machine> temp = fatherClone.Schedule[i];
                        fatherClone.Schedule[i] = motherClone.Schedule[i];
                        motherClone.Schedule[i] = temp;
                    }
                }
            }

            ReviseMachineStatus(fatherClone);
            ReviseMachineStatus(motherClone);
            List <Individual> descendants = new List <Individual>();

            if (StaticOperations.ValidateIndividual(motherClone) == false)
            {
                descendants.Add(mutatedMother);
            }
            else
            {
                descendants.Add(motherClone);
            }

            if (StaticOperations.ValidateIndividual(fatherClone) == false)
            {
                descendants.Add(mutatedFather);
            }
            else
            {
                descendants.Add(fatherClone);
            }

            return(descendants);
        }
예제 #3
0
        public List <Individual> NextGeneration()
        {
            List <Individual> newGeneration = new List <Individual>();
            // Get elite 10% of old population
            List <Individual> orderedOldGeneration = oldGeneration.OrderBy(item => item.Fitness).ToList();

            orderedOldGeneration.RemoveAll(item => StaticOperations.ValidateIndividual(item) == false);
            newGeneration.AddRange(orderedOldGeneration.Take(newGenerationCount / 10));
            // Fill up remaining spots with descendants
            List <Individual> orderedDescendants = descendants.OrderBy(item => item.Fitness).ToList();

            // Remove invalid descendants
            orderedDescendants.RemoveAll(item => StaticOperations.ValidateIndividual(item) == false);
            newGeneration.AddRange(orderedDescendants.Take(newGenerationCount - newGeneration.Count));

            return(newGeneration);
        }
예제 #4
0
        public List <Individual> GenerateDescendants(Individual mother, Individual father)
        {
            Individual mutatedMother = null;

            for (int i = 0; i < 5; i++)
            {
                mutatedMother = MutateIndividual(mother);
                if (StaticOperations.ValidateIndividual(mutatedMother))
                {
                    break;
                }
            }
            if (StaticOperations.ValidateIndividual(mutatedMother) == false)
            {
                mutatedMother = mother;
            }

            Individual mutatedFather = null;

            for (int i = 0; i < 5; i++)
            {
                mutatedFather = MutateIndividual(father);
                if (StaticOperations.ValidateIndividual(mutatedFather))
                {
                    break;
                }
            }
            if (StaticOperations.ValidateIndividual(mutatedFather) == false)
            {
                mutatedFather = father;
            }

            List <Individual> descendants = CrossOverIndividuals(mutatedMother, mutatedFather);

            if (StaticOperations.ValidateIndividual(descendants[0]) == false)
            {
                descendants[0] = mother;
            }
            if (StaticOperations.ValidateIndividual(descendants[1]) == false)
            {
                descendants[1] = father;
            }

            return(descendants);
        }
        public List <Individual> GenerateDescendants(Individual mother, Individual father)
        {
            Individual motherClone = Individual.CloneIndividual(mother);
            Individual fatherClone = Individual.CloneIndividual(father);

            double     value = random.NextDouble();
            Individual mutatedMother = null, mutatedFather = null;
            int        counter = 0;

            for (int i = 0; i < 5; i++)
            {
                mutatedMother = MutateIndividual(motherClone);
                if (StaticOperations.ValidateIndividual(mutatedMother))
                {
                    break;
                }
                counter++;
            }
            if (counter == 5)
            {
                mutatedMother = mother;
            }

            counter = 0;
            for (int i = 0; i < 5; i++)
            {
                mutatedFather = MutateIndividual(fatherClone);
                if (StaticOperations.ValidateIndividual(mutatedFather))
                {
                    break;
                }
                counter++;
            }
            if (counter == 5)
            {
                mutatedFather = father;
            }

            List <Individual> crossedOvers = CrossOverIndividuals(mutatedMother, mutatedFather);

            return(crossedOvers);
        }
예제 #6
0
        public List <Individual> GenerateDescendants(Individual mother, Individual father)
        {
            Individual motherClone = Individual.CloneRepresentation(mother);
            Individual fatherClone = Individual.CloneRepresentation(father);

            Individual mutatedMother = MutateIndividual(motherClone);
            Individual mutatedFather = MutateIndividual(fatherClone);

            if (StaticOperations.ValidateIndividual(mutatedMother) == false)
            {
                mutatedMother = Individual.CloneRepresentation(mother);
            }

            if (StaticOperations.ValidateIndividual(mutatedFather) == false)
            {
                mutatedFather = Individual.CloneRepresentation(father);
            }

            List <Individual> descendants = CrossOverIndividuals(mutatedMother, mutatedFather);

            return(descendants);
        }
예제 #7
0
        public static void DecoderTesting()
        {
            List <Point> references = new List <Point>();
            Point        reference  = new Point(3, 21);

            references.Add(reference);
            reference = new Point(29, 7);
            references.Add(reference);
            reference = new Point(6, 27);
            references.Add(reference);
            reference = new Point(8, 18);
            references.Add(reference);
            reference = new Point(28, 9);
            references.Add(reference);
            reference = new Point(14, 11);
            references.Add(reference);
            reference = new Point(9, 13);
            references.Add(reference);
            reference = new Point(31, 14);
            references.Add(reference);
            reference = new Point(30, 23);
            references.Add(reference);
            reference = new Point(23, 25);
            references.Add(reference);
            reference = new Point(20, 15);
            references.Add(reference);
            reference = new Point(11, 17);
            references.Add(reference);
            reference = new Point(27, 12);
            references.Add(reference);
            reference = new Point(12, 5);
            references.Add(reference);
            reference = new Point(30, 11);
            references.Add(reference);

            Individual individual = new Individual(references);

            bool status = StaticOperations.ValidateIndividual(individual);
        }
예제 #8
0
        private Individual MutateIndividual(Individual individual)
        {
            Individual individualClone = Individual.CloneRepresentation(individual);
            // Every hour needs to be mutated separately
            // after mutating one of the hours, values for
            // turned on time on machines need to be revised
            // Revision is needed only if start/terminate operators are used
            Random random = new Random();

            for (int i = 0; i < individualClone.Schedule.Count; i++)
            {
                double mutationChange = random.NextDouble();
                if (mutationChange <= 0.25)
                {
                    int mutationEffect = random.Next(0, 3);

                    // Increase mutation
                    if (mutationEffect == 0)
                    {
                        individualClone.Schedule[i] = IncreaseMutation(individualClone.Schedule[i],
                                                                       i == 0 ? null : individualClone.Schedule[i - 1], random);
                        if (StaticOperations.ValidateIndividual(individualClone) == false)
                        {
                            individualClone.Schedule[i] = StaticOperations.CloneMachineList(individual.Schedule[i]);
                        }
                    }
                    // Decrease mutation
                    if (mutationEffect == 1)
                    {
                        individualClone.Schedule[i] = DecreaseMutation(individualClone.Schedule[i],
                                                                       i == 0 ? null : individualClone.Schedule[i - 1], random);
                        if (StaticOperations.ValidateIndividual(individualClone) == false)
                        {
                            individualClone.Schedule[i] = StaticOperations.CloneMachineList(individual.Schedule[i]);
                        }
                    }
                    // Start mutation
                    if (mutationEffect == 2)
                    {
                        individualClone.Schedule[i] = StartMutation(individualClone.Schedule[i],
                                                                    i == 0 ? null : individualClone.Schedule[i - 1], random);
                        if (StaticOperations.ValidateIndividual(individualClone) == false)
                        {
                            individualClone.Schedule[i] = StaticOperations.CloneMachineList(individual.Schedule[i]);
                        }

                        ReviseMachineStatus(individualClone);
                    }
                    // Terminate mutation
                    if (mutationEffect == 3)
                    {
                        individualClone.Schedule[i] = TerminateMutation(individualClone.Schedule[i],
                                                                        i == 0 ? null : individualClone.Schedule[i - 1], random);
                        if (StaticOperations.ValidateIndividual(individualClone) == false)
                        {
                            individualClone.Schedule[i] = StaticOperations.CloneMachineList(individual.Schedule[i]);
                        }

                        ReviseMachineStatus(individualClone);
                    }
                }
            }

            return(individualClone);
        }
예제 #9
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);
        }
예제 #12
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();
        }