Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/20 21:51:44 (4 years ago)
Author:
dleko
Message:

#2825 Implement recombination.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3.cs

    r17619 r17657  
    11using System;
    22using System.Collections.Generic;
     3using System.Diagnostics;
    34using System.Linq;
    45using System.Threading;
     
    6465        private const string SetSeedRandomlyName = "SetSeedRandomly";
    6566        private const string PopulationSizeName = "PopulationSize";
    66         private const string CrossoverProbabilityName = "CrossOverProbability";
     67        private const string CrossoverProbabilityName = "CrossoverProbability";
     68        private const string CrossoverContiguityName = "CrossoverContiguity";
    6769        private const string MutationProbabilityName = "MutationProbability";
    6870        private const string MaximumGenerationsName = "MaximumGenerations";
     
    98100        }
    99101
     102        private IFixedValueParameter<DoubleValue> CrossoverContiguityParameter
     103        {
     104            get { return (IFixedValueParameter<DoubleValue>)Parameters[CrossoverContiguityName]; }
     105        }
     106
    100107        private IFixedValueParameter<PercentValue> MutationProbabilityParameter
    101108        {
     
    124131
    125132        public PercentValue CrossoverProbability => CrossoverProbabilityParameter.Value;
     133
     134        public DoubleValue CrossoverContiguity => CrossoverContiguityParameter.Value;
    126135
    127136        public PercentValue MutationProbability => MutationProbabilityParameter.Value;
     
    159168            Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    160169            Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    161             Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "The size of the population of Individuals.", new IntValue(100)));
     170            Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "The size of the population of Individuals.", new IntValue(200)));
    162171            Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityName, "The probability that the crossover operator is applied on two parents.", new PercentValue(0.9)));
     172            Parameters.Add(new FixedValueParameter<DoubleValue>(CrossoverContiguityName, "The contiguity value for the Simulated Binary Crossover that specifies how close a child should be to its parents (larger value means closer). The value must be greater than or equal than 0. Typical values are in the range [2;5]."));
    163173            Parameters.Add(new FixedValueParameter<PercentValue>(MutationProbabilityName, "The probability that the mutation operator is applied on a Individual.", new PercentValue(0.05)));
    164174            Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
     
    195205
    196206            InitFields();
     207            InitResults();
    197208            InitReferencePoints();
    198             InitResults();
    199209            Analyze();
    200210        }
     
    218228                RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);
    219229
    220                 solutions.Add(new Solution(StorableConstructorFlag.Default)
    221                 {
    222                     Chromosome = randomRealVector
    223                 });
     230                solutions.Add(new Solution(randomRealVector));
    224231                solutions[i].Fitness = Evaluate(solutions[i].Chromosome);
    225232            }
     
    236243        private void InitResults()
    237244        {
    238             Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", Utility.ConvertToDoubleMatrix(ReferencePoints)));
     245            Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", new DoubleMatrix()));
    239246            Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix()));
    240247        }
     
    288295
    289296            // Do non-dominated sort
    290             var qualities = Utility.ToFitnessMatrix(solutions);
     297            var qualities = Utility.ToFitnessMatrix(rt);
    291298            // compute the pareto fronts using the DominationCalculator and discard the qualities
    292299            // part in the inner tuples
     
    578585        private List<Solution> Recombine(List<Solution> solutions)
    579586        {
    580             throw new NotImplementedException();
     587            List<Solution> childSolutions = new List<Solution>();
     588
     589            for (int i = 0; i < solutions.Count; i += 2)
     590            {
     591                int parentIndex1 = random.Next(solutions.Count);
     592                int parentIndex2 = random.Next(solutions.Count);
     593                // ensure that the parents are not the same object
     594                if (parentIndex1 == parentIndex2) parentIndex2 = (parentIndex2 + 1) % solutions.Count;
     595                var parent1 = solutions[parentIndex1];
     596                var parent2 = solutions[parentIndex2];
     597
     598                // Do crossover with crossoverProbabilty == 1 in order to guarantee that a crossover happens
     599                var children = SimulatedBinaryCrossover.Apply(random, Problem.Encoding.Bounds, parent1.Chromosome, parent2.Chromosome, 1);
     600                Debug.Assert(children != null);
     601
     602                var child1 = new Solution(children.Item1);
     603                var child2 = new Solution(children.Item2);
     604                child1.Fitness = Evaluate(child1.Chromosome);
     605                child2.Fitness = Evaluate(child1.Chromosome);
     606
     607                childSolutions.Add(child1);
     608                childSolutions.Add(child2);
     609            }
     610
     611            return childSolutions;
    581612        }
    582613
    583614        private List<Solution> Mutate(List<Solution> solutions)
    584615        {
    585             throw new NotImplementedException();
     616            foreach (var solution in solutions)
     617            {
     618                UniformOnePositionManipulator.Apply(random, solution.Chromosome, Problem.Encoding.Bounds);
     619            }
     620            return solutions;
    586621        }
    587622
Note: See TracChangeset for help on using the changeset viewer.