コード例 #1
0
        public IList <GAParametersChromosome> Execute(IList <GAParametersChromosome> parents)
        {
            GAParametersChromosome par1 = (GAParametersChromosome)parents[0].Copy();
            GAParametersChromosome par2 = (GAParametersChromosome)parents[1].Copy();


            IList <GAParametersChromosome> result = new List <GAParametersChromosome>();

            if (RandomNumberGeneratorProvider.Instance.NextDouble() > _crossoverProbability)
            {
                par1.IsEvaluated = true;
                par2.IsEvaluated = true;

                result.Add(par1);
                result.Add(par2);
                return(result);
            }

            Tuple <GAParameters, GAParameters> child = GAParameters.Cross(par1.GetValue(0), par2.GetValue(0));

            par1.SetValue(0, child.Item1);
            par1.IsEvaluated = false;

            par2.SetValue(0, child.Item2);
            par2.IsEvaluated = false;

            result.Add(par1);

            return(result);
        }
コード例 #2
0
        public GAParametersChromosome Execute(GAParametersChromosome chromosome)
        {
            GAParametersChromosome child = (GAParametersChromosome)chromosome.Copy();

            child.IsEvaluated = true;

            if (RandomNumberGeneratorProvider.Instance.NextDouble() <= _mutationProbability)
            {
                child.SetValue(0, GAParameters.Mutate(child.GetValue(0)));
                child.IsEvaluated = false;
            }

            return(child);
        }
コード例 #3
0
ファイル: GAParameters.cs プロジェクト: kvukusic/apr2016
        public static GAParameters CreateRandom()
        {
            Random r = RandomNumberGeneratorProvider.Instance;

            GAParameters g = new GAParameters();

            g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
            g.PopulationSizeIndex           = r.Next(0, PopulationSize.Length);
            g.CrossoverRateIndex            = r.Next(0, CrossoverRate.Length);
            g.MutationRateIndex             = r.Next(0, MutationRate.Length);
            g.ElitismRateIndex       = r.Next(0, ElitismRate.Length);
            g.GenerationGapIndex     = r.Next(0, GenerationGap.Length);
            g.GAVariantIndex         = r.Next(0, GAVariant.Length);
            g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
            g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
            g.MutationOperatorIndex  = r.Next(0, MutationOperator.Length);
            g.TournamentSizeIndex    = r.Next(0, TournamentSize.Length);
            g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);

            while (!g.IsValid())
            {
                g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                g.PopulationSizeIndex           = r.Next(0, PopulationSize.Length);
                g.CrossoverRateIndex            = r.Next(0, CrossoverRate.Length);
                g.MutationRateIndex             = r.Next(0, MutationRate.Length);
                g.ElitismRateIndex       = r.Next(0, ElitismRate.Length);
                g.GenerationGapIndex     = r.Next(0, GenerationGap.Length);
                g.GAVariantIndex         = r.Next(0, GAVariant.Length);
                g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                g.MutationOperatorIndex  = r.Next(0, MutationOperator.Length);
                g.TournamentSizeIndex    = r.Next(0, TournamentSize.Length);
                g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
            }

            return(g);
        }
コード例 #4
0
ファイル: GAParameters.cs プロジェクト: kvukusic/apr2016
        public static Tuple <GAParameters, GAParameters> Cross(GAParameters g1, GAParameters g2)
        {
            var r    = RandomNumberGeneratorProvider.Instance;
            var prob = r.NextDouble();

            if (prob < 0.05)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
                }
                else
                {
                    g2.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
                }
            }
            else if (prob < 0.1)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
                }
                else
                {
                    g2.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
                }
            }
            else if (prob < 0.15)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.MutationRateIndex = r.Next(0, MutationRate.Length);
                }
                else
                {
                    g2.MutationRateIndex = r.Next(0, MutationRate.Length);
                }
            }
            else if (prob < 0.20)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                }
                else
                {
                    g2.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                }
            }
            else if (prob < 0.25)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.GAVariantIndex = r.Next(0, GAVariant.Length);
                }
                else
                {
                    g2.GAVariantIndex = r.Next(0, GAVariant.Length);
                }
            }
            else if (prob < 0.30)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                }
                else
                {
                    g2.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                }
            }
            else if (prob < 0.35)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                }
                else
                {
                    g2.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                }
            }
            else if (prob < 0.40)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
                }
                else
                {
                    g2.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
                }
            }
            else if (prob < 0.45)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
                }
                else
                {
                    g2.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
                }
            }
            else if (prob < 0.50)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.ElitismRateIndex = r.Next(0, ElitismRate.Length);
                }
                else
                {
                    g2.ElitismRateIndex = r.Next(0, ElitismRate.Length);
                }
            }
            else if (prob < 0.50)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.GenerationGapIndex = r.Next(0, GenerationGap.Length);
                }
                else
                {
                    g2.GenerationGapIndex = r.Next(0, GenerationGap.Length);
                }
            }
            else if (prob < 0.50)
            {
                if (r.NextDouble() < 0.5)
                {
                    g1.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
                }
                else
                {
                    g2.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
                }
            }

            while (!g1.IsValid() && !g2.IsValid())
            {
                if (prob < 0.05)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
                    }
                    else
                    {
                        g2.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
                    }
                }
                else if (prob < 0.1)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
                    }
                    else
                    {
                        g2.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
                    }
                }
                else if (prob < 0.15)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.MutationRateIndex = r.Next(0, MutationRate.Length);
                    }
                    else
                    {
                        g2.MutationRateIndex = r.Next(0, MutationRate.Length);
                    }
                }
                else if (prob < 0.20)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                    }
                    else
                    {
                        g2.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                    }
                }
                else if (prob < 0.25)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.GAVariantIndex = r.Next(0, GAVariant.Length);
                    }
                    else
                    {
                        g2.GAVariantIndex = r.Next(0, GAVariant.Length);
                    }
                }
                else if (prob < 0.30)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                    }
                    else
                    {
                        g2.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                    }
                }
                else if (prob < 0.35)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                    }
                    else
                    {
                        g2.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                    }
                }
                else if (prob < 0.40)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
                    }
                    else
                    {
                        g2.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
                    }
                }
                else if (prob < 0.45)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
                    }
                    else
                    {
                        g2.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
                    }
                }
                else if (prob < 0.50)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.ElitismRateIndex = r.Next(0, ElitismRate.Length);
                    }
                    else
                    {
                        g2.ElitismRateIndex = r.Next(0, ElitismRate.Length);
                    }
                }
                else if (prob < 0.50)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.GenerationGapIndex = r.Next(0, GenerationGap.Length);
                    }
                    else
                    {
                        g2.GenerationGapIndex = r.Next(0, GenerationGap.Length);
                    }
                }
                else if (prob < 0.50)
                {
                    if (r.NextDouble() < 0.5)
                    {
                        g1.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
                    }
                    else
                    {
                        g2.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
                    }
                }
            }

            return(new Tuple <GAParameters, GAParameters>(g1, g2));
        }
コード例 #5
0
ファイル: GAParameters.cs プロジェクト: kvukusic/apr2016
        public static GAParameters Mutate(GAParameters parameters)
        {
            var g   = parameters.Copy();
            var r   = RandomNumberGeneratorProvider.Instance;
            var idx = r.Next(0, 13);

            if (idx == 0)
            {
                g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
            }
            else if (idx == 1)
            {
                g.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
            }
            else if (idx == 2)
            {
                g.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
            }
            else if (idx == 3)
            {
                g.MutationRateIndex = r.Next(0, MutationRate.Length);
            }
            else if (idx == 4)
            {
                g.ElitismRateIndex = r.Next(0, ElitismRate.Length);
            }
            else if (idx == 5)
            {
                g.GenerationGapIndex = r.Next(0, GenerationGap.Length);
            }
            else if (idx == 6)
            {
                g.GAVariantIndex = r.Next(0, GAVariant.Length);
            }
            else if (idx == 7)
            {
                g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
            }
            else if (idx == 8)
            {
                g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
            }
            else if (idx == 9)
            {
                g.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
            }
            else if (idx == 10)
            {
                g.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
            }
            else if (idx == 11)
            {
                g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
            }

            while (!g.IsValid())
            {
                if (idx == 0)
                {
                    g.ChromosomeRepresentationIndex = r.Next(0, ChromosomeRepresentation.Length);
                }
                else if (idx == 1)
                {
                    g.PopulationSizeIndex = r.Next(0, PopulationSize.Length);
                }
                else if (idx == 2)
                {
                    g.CrossoverRateIndex = r.Next(0, CrossoverRate.Length);
                }
                else if (idx == 3)
                {
                    g.MutationRateIndex = r.Next(0, MutationRate.Length);
                }
                else if (idx == 4)
                {
                    g.ElitismRateIndex = r.Next(0, ElitismRate.Length);
                }
                else if (idx == 5)
                {
                    g.GenerationGapIndex = r.Next(0, GenerationGap.Length);
                }
                else if (idx == 6)
                {
                    g.GAVariantIndex = r.Next(0, GAVariant.Length);
                }
                else if (idx == 7)
                {
                    g.SelectionOperatorIndex = r.Next(0, SelectionOperator.Length);
                }
                else if (idx == 8)
                {
                    g.CrossoverOperatorIndex = r.Next(0, CrossoverOperator.Length);
                }
                else if (idx == 9)
                {
                    g.MutationOperatorIndex = r.Next(0, MutationOperator.Length);
                }
                else if (idx == 10)
                {
                    g.TournamentSizeIndex = r.Next(0, TournamentSize.Length);
                }
                else if (idx == 11)
                {
                    g.SelectivePressureIndex = r.Next(0, SelectivePressure.Length);
                }
            }

            return(g);
        }
コード例 #6
0
        public void Evaluate(GAParametersChromosome chromosome)
        {
            // if (chromosome.IsEvaluated) return;

            _evaluations++;

            GAParameters parameters = chromosome.GetValue(0);

            _function.Clear();

            int iters            = 10;
            int childEvaluations = 10000;
            int maxGenerations   = 100;

            double[] childGAFitnesses = new double[iters];

            var chromosomeRepresentation = GAParameters.ChromosomeRepresentation[parameters.ChromosomeRepresentationIndex];

            for (int i = 0; i < iters; i++)
            {
                if (chromosomeRepresentation == GAParameters.ChromosomeRepresentationType.Binary)
                {
                    IProblem <BinaryChromosome> problem = new FunctionMinimizationBinaryProblem(_function, _functionDimension, 4, -50, 150);
                    int    populationSize = GAParameters.PopulationSize[parameters.PopulationSizeIndex];
                    double crossoverRate  = GAParameters.CrossoverRate[parameters.CrossoverRateIndex];
                    ICrossoverOperator <BinaryChromosome> crossoverOperator;
                    ISelectionOperator <BinaryChromosome> selectionOperator;
                    int    tournamentSize    = GAParameters.TournamentSize[parameters.TournamentSizeIndex];
                    double selectivePressure = GAParameters.SelectivePressure[parameters.SelectivePressureIndex];
                    IMutationOperator <BinaryChromosome> mutationOperator;
                    double mutationRate             = GAParameters.MutationRate[parameters.MutationRateIndex];
                    GeneticAlgorithmVariant variant = GAParameters.GAVariant[parameters.GAVariantIndex];
                    double elitismRate   = GAParameters.ElitismRate[parameters.ElitismRateIndex];
                    double generationGap = GAParameters.GenerationGap[parameters.GenerationGapIndex];

                    var crossOperatorType = GAParameters.CrossoverOperator[parameters.CrossoverOperatorIndex];
                    if (crossOperatorType == GAParameters.CrossoverOperatorType.OnePoint)
                    {
                        crossoverOperator = new OnePointCrossoverOperator(crossoverRate);
                    }
                    else
                    {
                        crossoverOperator = new TwoPointCrossoverOperator(crossoverRate);
                    }

                    var mutationOperatorType = GAParameters.MutationOperator[parameters.MutationOperatorIndex];
                    if (mutationOperatorType == GAParameters.MutationOperatorType.BinaryBoundary)
                    {
                        mutationOperator = new BinaryBoundaryMutationOperator(mutationRate);
                    }
                    else if (mutationOperatorType == GAParameters.MutationOperatorType.BitFlip)
                    {
                        mutationOperator = new BitFlipMutationOperator(mutationRate);
                    }
                    else
                    {
                        mutationOperator = new SimpleMutationOperator(mutationRate);
                    }

                    if (GAParameters.SelectionOperator[parameters.SelectionOperatorIndex] == GAParameters.SelectionOperatorType.Tournament)
                    {
                        selectionOperator = new TournamentSelectionOperator <BinaryChromosome>(tournamentSize, problem);
                    }
                    else
                    {
                        selectionOperator = new RouletteWheelSelectionOperator <BinaryChromosome>(selectivePressure, problem);
                    }

                    IGeneticAlgorithm <BinaryChromosome> ga;

                    if (variant == GeneticAlgorithmVariant.Generational)
                    {
                        ga = new GenerationalGeneticAlgorithm <BinaryChromosome>(
                            problem,
                            populationSize,
                            selectionOperator,
                            crossoverOperator,
                            mutationOperator,
                            new CompositeTerminationCondition(
                                new MaxEvaluationsTerminationCondition(childEvaluations),
                                new MaxGenerationsTerminationCondition(maxGenerations)
                                ),
                            elitismRate
                            );
                    }
                    else
                    {
                        ga = new SteadyStateGeneticAlgorithm <BinaryChromosome>(
                            problem,
                            populationSize,
                            selectionOperator,
                            crossoverOperator,
                            mutationOperator,
                            new CompositeTerminationCondition(
                                new MaxEvaluationsTerminationCondition(childEvaluations),
                                new MaxGenerationsTerminationCondition(maxGenerations)
                                ),
                            generationGap,
                            new WorstFitnessReplacementOperator <BinaryChromosome>(problem)
                            );
                    }

                    // new GeneticAlgorithmRunner<BinaryChromosome>(ga).Run();

                    ga.Run();

                    childGAFitnesses[i] = ga.BestIndividual.Fitness;
                }
                else
                {
                    IProblem <FloatingPointChromosome> problem = new FunctionMinimizationFloatingPointProblem(_function, _functionDimension, -50, 150);
                    int    populationSize = GAParameters.PopulationSize[parameters.PopulationSizeIndex];
                    double crossoverRate  = GAParameters.CrossoverRate[parameters.CrossoverRateIndex];
                    ICrossoverOperator <FloatingPointChromosome> crossoverOperator;
                    ISelectionOperator <FloatingPointChromosome> selectionOperator;
                    int    tournamentSize    = GAParameters.TournamentSize[parameters.TournamentSizeIndex];
                    double selectivePressure = GAParameters.SelectivePressure[parameters.SelectivePressureIndex];
                    IMutationOperator <FloatingPointChromosome> mutationOperator;
                    double mutationRate             = GAParameters.MutationRate[parameters.MutationRateIndex];
                    GeneticAlgorithmVariant variant = GAParameters.GAVariant[parameters.GAVariantIndex];
                    double elitismRate   = GAParameters.ElitismRate[parameters.ElitismRateIndex];
                    double generationGap = GAParameters.GenerationGap[parameters.GenerationGapIndex];

                    var crossOperatorType = GAParameters.CrossoverOperator[parameters.CrossoverOperatorIndex];
                    if (crossOperatorType == GAParameters.CrossoverOperatorType.Arithmetic)
                    {
                        crossoverOperator = new ArithmeticCrossoverOperator(crossoverRate);
                    }
                    else
                    {
                        crossoverOperator = new HeuristicCrossoverOperator(crossoverRate);
                    }

                    var mutationOperatorType = GAParameters.MutationOperator[parameters.MutationOperatorIndex];
                    if (mutationOperatorType == GAParameters.MutationOperatorType.FloatingPointBoundary)
                    {
                        mutationOperator = new FloatingPointBoundaryMutationOperator(mutationRate);
                    }
                    else if (mutationOperatorType == GAParameters.MutationOperatorType.Uniform)
                    {
                        mutationOperator = new UniformMutationOperator(mutationRate);
                    }
                    else
                    {
                        mutationOperator = new GaussianMutationOperator(mutationRate);
                    }

                    if (GAParameters.SelectionOperator[parameters.SelectionOperatorIndex] == GAParameters.SelectionOperatorType.Tournament)
                    {
                        selectionOperator = new TournamentSelectionOperator <FloatingPointChromosome>(tournamentSize, problem);
                    }
                    else
                    {
                        selectionOperator = new RouletteWheelSelectionOperator <FloatingPointChromosome>(selectivePressure, problem);
                    }

                    IGeneticAlgorithm <FloatingPointChromosome> ga;

                    if (variant == GeneticAlgorithmVariant.Generational)
                    {
                        ga = new GenerationalGeneticAlgorithm <FloatingPointChromosome>(
                            problem,
                            populationSize,
                            selectionOperator,
                            crossoverOperator,
                            mutationOperator,
                            new CompositeTerminationCondition(
                                new MaxEvaluationsTerminationCondition(childEvaluations),
                                new MaxGenerationsTerminationCondition(maxGenerations)
                                ),
                            elitismRate
                            );
                    }
                    else
                    {
                        ga = new SteadyStateGeneticAlgorithm <FloatingPointChromosome>(
                            problem,
                            populationSize,
                            selectionOperator,
                            crossoverOperator,
                            mutationOperator,
                            new CompositeTerminationCondition(
                                new MaxEvaluationsTerminationCondition(childEvaluations),
                                new MaxGenerationsTerminationCondition(maxGenerations)
                                ),
                            generationGap,
                            new WorstFitnessReplacementOperator <FloatingPointChromosome>(problem)
                            );
                    }

                    // new GeneticAlgorithmRunner<FloatingPointChromosome>(ga).Run();

                    ga.Run();

                    childGAFitnesses[i] = ga.BestIndividual.Fitness;
                }
            }

            chromosome.Fitness = childGAFitnesses.Average();
        }
コード例 #7
0
 public GAParametersChromosome CreateChromosome()
 {
     return(new GAParametersChromosome(this, GAParameters.CreateRandom()));
 }