Changeset 17617
- Timestamp:
- 06/21/20 16:51:58 (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
r17616 r17617 52 52 private List<Solution> solutions; 53 53 54 [Storable]55 private List<List<Solution>> fronts;56 57 [Storable]58 private List<ReferencePoint> referencePoints;59 60 54 #endregion Storable fields 61 55 … … 134 128 public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value; 135 129 130 public List<List<Solution>> Fronts { get; private set; } 131 132 public List<ReferencePoint> ReferencePoints { get; private set; } 133 136 134 #endregion Properties 137 135 … … 151 149 152 150 #endregion ResultsProperties 151 152 #region Constructors 153 153 154 154 public NSGA3() : base() … … 178 178 } 179 179 180 #region Overriden Methods181 182 180 public override IDeepCloneable Clone(Cloner cloner) 183 181 { 184 182 return new NSGA3(this, cloner); 185 183 } 184 185 #endregion Constructors 186 187 #region Initialization 186 188 187 189 protected override void Initialize(CancellationToken cancellationToken) … … 195 197 } 196 198 197 protected override void Run(CancellationToken cancellationToken)198 {199 referencePoints = new List<ReferencePoint>(); // todo: use existing list of reference points200 for (int iteration = 0; iteration < MaximumGenerations.Value; iteration++)201 ToNextGeneration();202 }203 204 #endregion Overriden Methods205 206 #region Private Methods207 208 199 private void InitFields() 209 200 { … … 235 226 // Generate reference points and add them to results 236 227 int nDiv = 5; // todo: figure out the correct number of divisions 237 referencePoints = ReferencePoint.GenerateReferencePoints(Problem.Encoding.Length, nDiv);238 ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix( referencePoints);228 ReferencePoints = ReferencePoint.GenerateReferencePoints(Problem.Encoding.Length, nDiv); 229 ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(ReferencePoints); 239 230 } 240 231 241 232 private void InitResults() 242 233 { 243 Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", Utility.ConvertToDoubleMatrix( referencePoints)));234 Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", Utility.ConvertToDoubleMatrix(ReferencePoints))); 244 235 Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix())); 245 236 } 237 238 #endregion Initialization 239 240 #region Overriden Methods 241 242 protected override void Run(CancellationToken cancellationToken) 243 { 244 ReferencePoints = new List<ReferencePoint>(); // todo: use existing list of reference points 245 for (int iteration = 0; iteration < MaximumGenerations.Value; iteration++) 246 ToNextGeneration(); 247 } 248 249 #endregion Overriden Methods 250 251 #region Private Methods 246 252 247 253 private void Analyze() … … 278 284 // compute the pareto fronts using the DominationCalculator and discard the qualities 279 285 // part in the inner tuples 280 fronts = DominationCalculator<Solution>.CalculateAllParetoFronts(rt.ToArray(), qualities, Problem.Maximization, out int[] rank, true)286 Fronts = DominationCalculator<Solution>.CalculateAllParetoFronts(rt.ToArray(), qualities, Problem.Maximization, out int[] rank, true) 281 287 .Select(list => new List<Solution>(list.Select(pair => pair.Item1))).ToList(); 282 288 283 289 int i = 0; 284 290 List<Solution> lf = null; // last front to be included 285 while (i < fronts.Count && st.Count < PopulationSize.Value)286 { 287 lf = fronts[i];291 while (i < Fronts.Count && st.Count < PopulationSize.Value) 292 { 293 lf = Fronts[i]; 288 294 st = Utility.Concat(st, lf); 289 295 i++; … … 297 303 nextGeneration = new List<Solution>(); 298 304 for (int f = 0; f < l; f++) 299 nextGeneration = Utility.Concat(nextGeneration, fronts[f]);305 nextGeneration = Utility.Concat(nextGeneration, Fronts[f]); 300 306 Normalize(st); 301 307 Associate(); … … 328 334 for (int i = 0; i < Problem.Encoding.Length; i++) weights[i] = EPSILON; 329 335 weights[j] = 1; 330 Func<Solution, double> func = s=> ASF(s.Fitness, weights);336 double func(Solution s) => ASF(s.Fitness, weights); 331 337 extremePoints[j] = Utility.ArgMin(func, solutions); 332 338 } … … 344 350 private void NormalizeObjectives(List<double> intercepts, double[] idealPoint) 345 351 { 346 for (int f = 0; f < fronts.Count; f++)347 { 348 foreach (var solution in fronts[f])352 for (int f = 0; f < Fronts.Count; f++) 353 { 354 foreach (var solution in Fronts[f]) 349 355 { 350 356 for (int i = 0; i < Problem.Encoding.Length; i++) … … 365 371 private void Associate() 366 372 { 367 for (int f = 0; f < fronts.Count; f++)368 { 369 foreach (var solution in fronts[f])373 for (int f = 0; f < Fronts.Count; f++) 374 { 375 foreach (var solution in Fronts[f]) 370 376 { 371 377 // find reference point for which the perpendicular distance to the current 372 378 // solution is the lowest 373 var rpAndDist = Utility.MinArgMin(rp => GetPerpendicularDistance(rp.Values, solution.Fitness), referencePoints);379 var rpAndDist = Utility.MinArgMin(rp => GetPerpendicularDistance(rp.Values, solution.Fitness), ReferencePoints); 374 380 375 381 //// todo: use ArgMin here … … 385 391 // } 386 392 //} 387 if (f + 1 != fronts.Count)393 if (f + 1 != Fronts.Count) 388 394 { 389 395 // Todo: Add member for reference point on index min_rp … … 422 428 List<int> dimensions = new List<int>(); 423 429 for (int i = 0; i < Problem.Encoding.Length; i++) dimensions.Add(i); 424 Func<int, double> f = dim=> x[dim] / weight[dim];430 double f(int dim) => x[dim] / weight[dim]; 425 431 return Utility.Max(f, dimensions); 426 432 } -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs
r17615 r17617 1 1 using System.Collections.Generic; 2 using HEAL.Attic;3 using HeuristicLab.Common;4 2 5 3 namespace HeuristicLab.Algorithms.NSGA3 … … 11 9 */ 12 10 13 [StorableType("96DCBD85-7C8B-4546-B6F5-FB4AE0DF7158")] 14 internal class ReferencePoint : IDeepCloneable 11 public class ReferencePoint 15 12 { 16 #region StorableProperties13 #region Properties 17 14 18 15 public double[] Values { get; } 19 20 #endregion Storable Properties21 22 #region Properties23 24 16 public int NumberOfDimensions => Values.Length; 25 17 26 18 #endregion Properties 19 20 #region Constructors 27 21 28 22 public ReferencePoint(int numberOfDimensions) … … 38 32 } 39 33 40 public IDeepCloneable Clone(Cloner cloner) 41 { 42 // no cloner is required to copy a Reference Point 43 return new ReferencePoint(this); 44 } 45 46 public object Clone() 47 { 48 return new Cloner().Clone(this); 49 } 34 #endregion Constructors 50 35 51 36 // todo: use this (for optimization)
Note: See TracChangeset
for help on using the changeset viewer.