Changeset 17693 for branches/2825-NSGA3
- Timestamp:
- 07/22/20 20:07:22 (5 years ago)
- 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 26 26 public class NSGA3 : BasicAlgorithm 27 27 { 28 public override bool SupportsPause => false;28 public override bool SupportsPause => true; 29 29 30 30 #region ProblemProperties … … 69 69 // Parameter Names 70 70 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"; 71 77 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";79 78 80 79 // Results Names … … 91 90 #region ParameterProperties 92 91 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 93 122 private IFixedValueParameter<IntValue> SeedParameter 94 123 { … … 96 125 } 97 126 98 private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter99 {100 get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }101 }102 103 private IFixedValueParameter<IntValue> PopulationSizeParameter104 {105 get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }106 }107 108 private IFixedValueParameter<PercentValue> CrossoverProbabilityParameter109 {110 get { return (IFixedValueParameter<PercentValue>)Parameters[CrossoverProbabilityName]; }111 }112 113 private IFixedValueParameter<DoubleValue> CrossoverContiguityParameter114 {115 get { return (IFixedValueParameter<DoubleValue>)Parameters[CrossoverContiguityName]; }116 }117 118 private IFixedValueParameter<PercentValue> MutationProbabilityParameter119 {120 get { return (IFixedValueParameter<PercentValue>)Parameters[MutationProbabilityName]; }121 }122 123 private IFixedValueParameter<IntValue> MaximumGenerationsParameter124 {125 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }126 }127 128 private IFixedValueParameter<BoolValue> DominateOnEqualQualitiesParameter129 {130 get { return (IFixedValueParameter<BoolValue>)Parameters[DominateOnEqualQualitiesName]; }131 }132 133 127 #endregion ParameterProperties 134 128 135 129 #region Properties 136 130 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; 137 142 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;152 143 153 144 // todo: create one property for the Generated Reference Points and one for the current … … 164 155 } 165 156 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 166 181 public DoubleMatrix ResultsSolutions 167 182 { … … 170 185 } 171 186 172 public IntValue ResultsCurrentGeneration173 {174 get { return (IntValue)Results[CurrentGenerationResultName].Value; }175 set { Results[CurrentGenerationResultName].Value = value; }176 }177 178 public DoubleValue ResultsGenerationalDistance179 {180 get { return (DoubleValue)Results[GenerationalDistanceResultName].Value; }181 set { Results[GenerationalDistanceResultName].Value = value; }182 }183 184 public DoubleValue ResultsInvertedGenerationalDistance185 {186 get { return (DoubleValue)Results[InvertedGenerationalDistanceResultName].Value; }187 set { Results[InvertedGenerationalDistanceResultName].Value = value; }188 }189 190 public ParetoFrontScatterPlot ResultsScatterPlot191 {192 get { return (ParetoFrontScatterPlot)Results[ScatterPlotResultName].Value; }193 set { Results[ScatterPlotResultName].Value = value; }194 }195 196 187 #endregion ResultsProperties 197 188 … … 200 191 public NSGA3() : base() 201 192 { 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))); 202 199 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)));210 200 } 211 201 … … 222 212 // todo: don't forget to clone storable fields 223 213 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 => 226 216 { 227 217 var refPoint = new ReferencePoint(random, r.NumberOfDimensions); 228 218 r.Values.CopyTo(refPoint.Values, 0); 229 219 return refPoint; 230 }).ToList() : null;220 }).ToList(); 231 221 } 232 222 … … 244 234 base.Initialize(cancellationToken); 245 235 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(); 255 237 InitResults(); 256 238 InitFields(); … … 258 240 } 259 241 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 260 252 private void InitResults() 261 253 { 262 254 Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", new DoubleMatrix())); 263 Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix()));264 255 Results.Add(new Result(CurrentGenerationResultName, "The current generation", new IntValue(1))); 265 256 Results.Add(new Result(GenerationalDistanceResultName, "The generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN))); 266 257 Results.Add(new Result(InvertedGenerationalDistanceResultName, "The inverted generational distance to an optimal pareto front defined in the Problem", new DoubleValue(double.NaN))); 267 258 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; 271 262 // todo: add BestKnownFront parameter 272 263 ResultsScatterPlot = new ParetoFrontScatterPlot(new double[0][], new double[0][], problem.BestKnownFront.ToJaggedArray(), problem.Objectives, problem.ProblemSize); … … 276 267 { 277 268 random = new MersenneTwister(); 278 InitSolutions();269 solutions = GetInitialPopulation(); 279 270 InitReferencePoints(); 280 271 } … … 287 278 } 288 279 289 private void InitSolutions()290 { 291 int minBound = 0;292 i nt maxBound = 1;280 private List<Solution> GetInitialPopulation() 281 { 282 var problem = Problem as MultiObjectiveTestFunctionProblem; 283 if (problem.Bounds.Rows != 1) throw new Exception(); 293 284 294 285 // Initialise solutions 295 solutions = new List<Solution>(PopulationSize.Value);286 List<Solution> solutions = new List<Solution>(PopulationSize.Value); 296 287 for (int i = 0; i < PopulationSize.Value; i++) 297 288 { 289 double minBound = problem.Bounds[0, 0]; 290 double maxBound = problem.Bounds[0, 1]; 298 291 RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound); 299 292 var solution = new Solution(randomRealVector); … … 301 294 solutions.Add(solution); 302 295 } 296 297 return solutions; 303 298 } 304 299 … … 361 356 ResultsSolutions = solutions.Select(s => s.Chromosome.ToArray()).ToMatrix(); 362 357 363 var problem = Problem as MultiObjectiveTestFunctionProblem; 364 if (problem == null) return; 358 if (!(Problem is MultiObjectiveTestFunctionProblem problem)) return; 365 359 366 360 ResultsGenerationalDistance = new DoubleValue(problem.BestKnownFront != null ? GenerationalDistance.Calculate(solutions.Select(s => s.Fitness), problem.BestKnownFront.ToJaggedArray(), 1) : double.NaN); … … 433 427 434 428 double rnd = random.NextDouble(); 435 var deltaq = 0.0;429 double deltaq; 436 430 if (rnd <= 0.5) 437 431 { … … 447 441 } 448 442 449 y = y +deltaq * (ub - lb);443 y += deltaq * (ub - lb); 450 444 y = Math.Min(ub, Math.Max(lb, y)); 451 445 -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs
r17688 r17693 4 4 using HeuristicLab.Common; 5 5 using HeuristicLab.Core; 6 using HeuristicLab.Optimization;7 6 8 7 namespace HeuristicLab.Algorithms.NSGA3 … … 10 9 public class ReferencePoint : IDeepCloneable 11 10 { 12 13 11 #region Properties 14 12 … … 109 107 int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv); 110 108 return outerPoints + innerPoints; 109 } 110 111 public static int GetPopulationSizeForReferencePoints(int referencePoints) 112 { 113 return (referencePoints + 3) / 4 * 4; 111 114 } 112 115
Note: See TracChangeset
for help on using the changeset viewer.