Changeset 17693


Ignore:
Timestamp:
07/22/20 20:07:22 (3 weeks ago)
Author:
dleko
Message:

#2825 Remove unnecessary parameter. Refactoring.

Location:
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3
Files:
2 edited

Legend:

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

    r17692 r17693  
    2626    public class NSGA3 : BasicAlgorithm
    2727    {
    28         public override bool SupportsPause => false;
     28        public override bool SupportsPause => true;
    2929
    3030        #region ProblemProperties
     
    6969        // Parameter Names
    7070
     71        private const string PopulationSizeName = "Population Size";
     72        private const string MaximumGenerationsName = "Maximum Generations";
     73        private const string CrossoverProbabilityName = "Crossover Probability";
     74        private const string MutationProbabilityName = "Mutation Probability";
     75        private const string DominateOnEqualQualitiesName = "Dominate On Equal Qualities";
     76        private const string SetSeedRandomlyName = "Set Seed Randomly";
    7177        private const string SeedName = "Seed";
    72         private const string SetSeedRandomlyName = "Set Seed Randomly";
    73         private const string PopulationSizeName = "Population Size";
    74         private const string CrossoverProbabilityName = "Crossover Probability";
    75         private const string CrossoverContiguityName = "Crossover Contiguity";
    76         private const string MutationProbabilityName = "Mutation Probability";
    77         private const string MaximumGenerationsName = "Maximum Generations";
    78         private const string DominateOnEqualQualitiesName = "Dominate On Equal Qualities";
    7978
    8079        // Results Names
     
    9190        #region ParameterProperties
    9291
     92        private IFixedValueParameter<IntValue> PopulationSizeParameter
     93        {
     94            get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
     95        }
     96
     97        private IFixedValueParameter<IntValue> MaximumGenerationsParameter
     98        {
     99            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
     100        }
     101
     102        private IFixedValueParameter<PercentValue> CrossoverProbabilityParameter
     103        {
     104            get { return (IFixedValueParameter<PercentValue>)Parameters[CrossoverProbabilityName]; }
     105        }
     106
     107        private IFixedValueParameter<PercentValue> MutationProbabilityParameter
     108        {
     109            get { return (IFixedValueParameter<PercentValue>)Parameters[MutationProbabilityName]; }
     110        }
     111
     112        private IFixedValueParameter<BoolValue> DominateOnEqualQualitiesParameter
     113        {
     114            get { return (IFixedValueParameter<BoolValue>)Parameters[DominateOnEqualQualitiesName]; }
     115        }
     116
     117        private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter
     118        {
     119            get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
     120        }
     121
    93122        private IFixedValueParameter<IntValue> SeedParameter
    94123        {
     
    96125        }
    97126
    98         private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter
    99         {
    100             get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
    101         }
    102 
    103         private IFixedValueParameter<IntValue> PopulationSizeParameter
    104         {
    105             get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
    106         }
    107 
    108         private IFixedValueParameter<PercentValue> CrossoverProbabilityParameter
    109         {
    110             get { return (IFixedValueParameter<PercentValue>)Parameters[CrossoverProbabilityName]; }
    111         }
    112 
    113         private IFixedValueParameter<DoubleValue> CrossoverContiguityParameter
    114         {
    115             get { return (IFixedValueParameter<DoubleValue>)Parameters[CrossoverContiguityName]; }
    116         }
    117 
    118         private IFixedValueParameter<PercentValue> MutationProbabilityParameter
    119         {
    120             get { return (IFixedValueParameter<PercentValue>)Parameters[MutationProbabilityName]; }
    121         }
    122 
    123         private IFixedValueParameter<IntValue> MaximumGenerationsParameter
    124         {
    125             get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
    126         }
    127 
    128         private IFixedValueParameter<BoolValue> DominateOnEqualQualitiesParameter
    129         {
    130             get { return (IFixedValueParameter<BoolValue>)Parameters[DominateOnEqualQualitiesName]; }
    131         }
    132 
    133127        #endregion ParameterProperties
    134128
    135129        #region Properties
    136130
     131        public IntValue PopulationSize => PopulationSizeParameter.Value;
     132
     133        public IntValue MaximumGenerations => MaximumGenerationsParameter.Value;
     134
     135        public PercentValue CrossoverProbability => CrossoverProbabilityParameter.Value;
     136
     137        public PercentValue MutationProbability => MutationProbabilityParameter.Value;
     138
     139        public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value;
     140
     141        public BoolValue SetSeedRandomly => SetSeedRandomlyParameter.Value;
    137142        public IntValue Seed => SeedParameter.Value;
    138 
    139         public BoolValue SetSeedRandomly => SetSeedRandomlyParameter.Value;
    140 
    141         public IntValue PopulationSize => PopulationSizeParameter.Value;
    142 
    143         public PercentValue CrossoverProbability => CrossoverProbabilityParameter.Value;
    144 
    145         public DoubleValue CrossoverContiguity => CrossoverContiguityParameter.Value;
    146 
    147         public PercentValue MutationProbability => MutationProbabilityParameter.Value;
    148 
    149         public IntValue MaximumGenerations => MaximumGenerationsParameter.Value;
    150 
    151         public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value;
    152143
    153144        // todo: create one property for the Generated Reference Points and one for the current
     
    164155        }
    165156
     157        public IntValue ResultsCurrentGeneration
     158        {
     159            get { return (IntValue)Results[CurrentGenerationResultName].Value; }
     160            set { Results[CurrentGenerationResultName].Value = value; }
     161        }
     162
     163        public DoubleValue ResultsGenerationalDistance
     164        {
     165            get { return (DoubleValue)Results[GenerationalDistanceResultName].Value; }
     166            set { Results[GenerationalDistanceResultName].Value = value; }
     167        }
     168
     169        public DoubleValue ResultsInvertedGenerationalDistance
     170        {
     171            get { return (DoubleValue)Results[InvertedGenerationalDistanceResultName].Value; }
     172            set { Results[InvertedGenerationalDistanceResultName].Value = value; }
     173        }
     174
     175        public ParetoFrontScatterPlot ResultsScatterPlot
     176        {
     177            get { return (ParetoFrontScatterPlot)Results[ScatterPlotResultName].Value; }
     178            set { Results[ScatterPlotResultName].Value = value; }
     179        }
     180
    166181        public DoubleMatrix ResultsSolutions
    167182        {
     
    170185        }
    171186
    172         public IntValue ResultsCurrentGeneration
    173         {
    174             get { return (IntValue)Results[CurrentGenerationResultName].Value; }
    175             set { Results[CurrentGenerationResultName].Value = value; }
    176         }
    177 
    178         public DoubleValue ResultsGenerationalDistance
    179         {
    180             get { return (DoubleValue)Results[GenerationalDistanceResultName].Value; }
    181             set { Results[GenerationalDistanceResultName].Value = value; }
    182         }
    183 
    184         public DoubleValue ResultsInvertedGenerationalDistance
    185         {
    186             get { return (DoubleValue)Results[InvertedGenerationalDistanceResultName].Value; }
    187             set { Results[InvertedGenerationalDistanceResultName].Value = value; }
    188         }
    189 
    190         public ParetoFrontScatterPlot ResultsScatterPlot
    191         {
    192             get { return (ParetoFrontScatterPlot)Results[ScatterPlotResultName].Value; }
    193             set { Results[ScatterPlotResultName].Value = value; }
    194         }
    195 
    196187        #endregion ResultsProperties
    197188
     
    200191        public NSGA3() : base()
    201192        {
     193            Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "The size of the population of Individuals.", new IntValue(200)));
     194            Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
     195            Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityName, "The probability that the crossover operator is applied on two parents.", new PercentValue(1.0)));
     196            Parameters.Add(new FixedValueParameter<PercentValue>(MutationProbabilityName, "The probability that the mutation operator is applied on a Individual.", new PercentValue(0.05)));
     197            Parameters.Add(new FixedValueParameter<BoolValue>(DominateOnEqualQualitiesName, "Flag which determines wether Individuals with equal quality values should be treated as dominated.", new BoolValue(false)));
     198            Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    202199            Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    203             Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    204             Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "The size of the population of Individuals.", new IntValue(200)));
    205             Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityName, "The probability that the crossover operator is applied on two parents.", new PercentValue(1.0)));
    206             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]."));
    207             Parameters.Add(new FixedValueParameter<PercentValue>(MutationProbabilityName, "The probability that the mutation operator is applied on a Individual.", new PercentValue(0.05)));
    208             Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
    209             Parameters.Add(new FixedValueParameter<BoolValue>(DominateOnEqualQualitiesName, "Flag which determines wether Individuals with equal quality values should be treated as dominated.", new BoolValue(false)));
    210200        }
    211201
     
    222212            // todo: don't forget to clone storable fields
    223213            random = cloner.Clone(original.random);
    224             solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToList() : null;
    225             referencePoints = original.referencePoints != null ? original.referencePoints.Select(r =>
     214            solutions = original.solutions?.Select(cloner.Clone).ToList();
     215            referencePoints = original.referencePoints?.Select(r =>
    226216            {
    227217                var refPoint = new ReferencePoint(random, r.NumberOfDimensions);
    228218                r.Values.CopyTo(refPoint.Values, 0);
    229219                return refPoint;
    230             }).ToList() : null;
     220            }).ToList();
    231221        }
    232222
     
    244234            base.Initialize(cancellationToken);
    245235
    246             // Set population size
    247             int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(NumberOfObjectives);
    248             int pop = ((numberOfGeneratedReferencePoints + 3) / 4) * 4;
    249             PopulationSize.Value = pop;
    250 
    251             // Set mutation probability
    252             double mutationProbability = 1.0 / Problem.Encoding.Length;
    253             MutationProbability.Value = mutationProbability;
    254 
     236            SetParameters();
    255237            InitResults();
    256238            InitFields();
     
    258240        }
    259241
     242        private void SetParameters()
     243        {
     244            // Set population size
     245            int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(NumberOfObjectives);
     246            PopulationSize.Value = ReferencePoint.GetPopulationSizeForReferencePoints(numberOfGeneratedReferencePoints);
     247
     248            // Set mutation probability
     249            MutationProbability.Value = 1.0 / Problem.Encoding.Length;
     250        }
     251
    260252        private void InitResults()
    261253        {
    262254            Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", new DoubleMatrix()));
    263             Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix()));
    264255            Results.Add(new Result(CurrentGenerationResultName, "The current generation", new IntValue(1)));
    265256            Results.Add(new Result(GenerationalDistanceResultName, "The generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN)));
    266257            Results.Add(new Result(InvertedGenerationalDistanceResultName, "The inverted generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN)));
    267258            Results.Add(new Result(ScatterPlotResultName, "A scatterplot displaying the evaluated solutions and (if available) the analytically optimal front", new ParetoFrontScatterPlot()));
    268 
    269             var problem = Problem as MultiObjectiveTestFunctionProblem;
    270             if (problem == null) return;
     259            Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix()));
     260
     261            if (!(Problem is MultiObjectiveTestFunctionProblem problem)) return;
    271262            // todo: add BestKnownFront parameter
    272263            ResultsScatterPlot = new ParetoFrontScatterPlot(new double[0][], new double[0][], problem.BestKnownFront.ToJaggedArray(), problem.Objectives, problem.ProblemSize);
     
    276267        {
    277268            random = new MersenneTwister();
    278             InitSolutions();
     269            solutions = GetInitialPopulation();
    279270            InitReferencePoints();
    280271        }
     
    287278        }
    288279
    289         private void InitSolutions()
    290         {
    291             int minBound = 0;
    292             int maxBound = 1;
     280        private List<Solution> GetInitialPopulation()
     281        {
     282            var problem = Problem as MultiObjectiveTestFunctionProblem;
     283            if (problem.Bounds.Rows != 1) throw new Exception();
    293284
    294285            // Initialise solutions
    295             solutions = new List<Solution>(PopulationSize.Value);
     286            List<Solution> solutions = new List<Solution>(PopulationSize.Value);
    296287            for (int i = 0; i < PopulationSize.Value; i++)
    297288            {
     289                double minBound = problem.Bounds[0, 0];
     290                double maxBound = problem.Bounds[0, 1];
    298291                RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);
    299292                var solution = new Solution(randomRealVector);
     
    301294                solutions.Add(solution);
    302295            }
     296
     297            return solutions;
    303298        }
    304299
     
    361356            ResultsSolutions = solutions.Select(s => s.Chromosome.ToArray()).ToMatrix();
    362357
    363             var problem = Problem as MultiObjectiveTestFunctionProblem;
    364             if (problem == null) return;
     358            if (!(Problem is MultiObjectiveTestFunctionProblem problem)) return;
    365359
    366360            ResultsGenerationalDistance = new DoubleValue(problem.BestKnownFront != null ? GenerationalDistance.Calculate(solutions.Select(s => s.Fitness), problem.BestKnownFront.ToJaggedArray(), 1) : double.NaN);
     
    433427
    434428                    double rnd = random.NextDouble();
    435                     var deltaq = 0.0;
     429                    double deltaq;
    436430                    if (rnd <= 0.5)
    437431                    {
     
    447441                    }
    448442
    449                     y = y + deltaq * (ub - lb);
     443                    y += deltaq * (ub - lb);
    450444                    y = Math.Min(ub, Math.Max(lb, y));
    451445
  • branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs

    r17688 r17693  
    44using HeuristicLab.Common;
    55using HeuristicLab.Core;
    6 using HeuristicLab.Optimization;
    76
    87namespace HeuristicLab.Algorithms.NSGA3
     
    109    public class ReferencePoint : IDeepCloneable
    1110    {
    12 
    1311        #region Properties
    1412
     
    109107            int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv);
    110108            return outerPoints + innerPoints;
     109        }
     110
     111        public static int GetPopulationSizeForReferencePoints(int referencePoints)
     112        {
     113            return (referencePoints + 3) / 4 * 4;
    111114        }
    112115
Note: See TracChangeset for help on using the changeset viewer.