Changeset 17688
- Timestamp:
- 07/19/20 20:08:01 (4 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
r17686 r17688 46 46 get 47 47 { 48 if (!(Problem is MultiObjectiveTestFunctionProblem testFunctionProblem)) throw new NotSupportedException("Only test multi objective test function problems are supported");48 if (!(Problem is MultiObjectiveTestFunctionProblem testFunctionProblem)) throw new NotSupportedException("Only Multi Objective Test Function problems are supported"); 49 49 return testFunctionProblem.Objectives; 50 50 } … … 60 60 [Storable] 61 61 private List<Solution> solutions; // maybe todo: rename to nextGeneration (see Run method) 62 63 [Storable] 64 private List<ReferencePoint> referencePoints; 62 65 63 66 #endregion Storable fields … … 149 152 public BoolValue DominateOnEqualQualities => DominateOnEqualQualitiesParameter.Value; 150 153 151 public List<List<Solution>> Fronts { get; private set; }152 153 public List<ReferencePoint> ReferencePoints { get; private set; }154 155 154 // todo: create one property for the Generated Reference Points and one for the current 156 155 // generations reference points … … 224 223 // todo: don't forget to clone storable fields 225 224 random = cloner.Clone(original.random); 226 solutions = new List<Solution>(original.solutions?.Select(cloner.Clone)); 225 solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToList() : null; 226 referencePoints = original.referencePoints != null ? original.referencePoints.Select(r => { 227 var refPoint = new ReferencePoint(random, r.NumberOfDimensions); 228 r.Values.CopyTo(refPoint.Values, 0); 229 return refPoint; 230 }).ToList() : null; 227 231 } 228 232 … … 245 249 InitResults(); 246 250 InitFields(); 247 InitReferencePoints();248 251 Analyze(); 249 }250 251 private void InitReferencePoints()252 {253 // Generate reference points and add them to results254 ReferencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives);255 ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(ReferencePoints);256 }257 258 private void InitFields()259 {260 random = new MersenneTwister();261 InitSolutions();262 }263 264 private void InitSolutions()265 {266 int minBound = 0;267 int maxBound = 1;268 269 // Initialise solutions270 solutions = new List<Solution>(PopulationSize.Value);271 for (int i = 0; i < PopulationSize.Value; i++)272 {273 RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound);274 275 solutions.Add(new Solution(randomRealVector));276 solutions[i].Fitness = Evaluate(solutions[i].Chromosome);277 }278 252 } 279 253 … … 293 267 } 294 268 269 private void InitFields() 270 { 271 random = new MersenneTwister(); 272 InitSolutions(); 273 InitReferencePoints(); 274 } 275 private void InitReferencePoints() 276 { 277 // Generate reference points and add them to results 278 referencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives); 279 ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(referencePoints); 280 } 281 282 283 private void InitSolutions() 284 { 285 int minBound = 0; 286 int maxBound = 1; 287 288 // Initialise solutions 289 solutions = new List<Solution>(PopulationSize.Value); 290 for (int i = 0; i < PopulationSize.Value; i++) 291 { 292 RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound); 293 294 solutions.Add(new Solution(randomRealVector)); 295 solutions[i].Fitness = Evaluate(solutions[i].Chromosome); 296 } 297 } 298 295 299 #endregion Initialization 296 300 … … 301 305 while (ResultsCurrentGeneration.Value < MaximumGenerations.Value) 302 306 { 303 // create copies of generated reference points (to preserve the original ones for304 // the next generation) maybe todo: use cloner?305 307 306 308 try … … 309 311 List<Solution> rt = Utility.Concat(solutions, qt); 310 312 313 // create copies of generated reference points (to preserve the original ones for 314 // the next generation) maybe todo: use cloner? 311 315 solutions = NSGA3Selection.SelectSolutionsForNextGeneration(rt, GetCopyOfReferencePoints(), Problem.Maximization, random); 312 316 … … 327 331 private List<ReferencePoint> GetCopyOfReferencePoints() 328 332 { 329 if ( ReferencePoints == null) return null;330 331 List<ReferencePoint> referencePoints = new List<ReferencePoint>();332 foreach (var referencePoint in ReferencePoints)333 referencePoints .Add(new ReferencePoint(referencePoint));334 335 return referencePoints ;333 if (referencePoints == null) return null; 334 335 List<ReferencePoint> referencePointsCopy = new List<ReferencePoint>(); 336 foreach (var referencePoint in referencePoints) 337 referencePointsCopy.Add(new ReferencePoint(referencePoint)); 338 339 return referencePointsCopy; 336 340 } 337 341 -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs
r17669 r17688 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using HeuristicLab.Common; 4 5 using HeuristicLab.Core; 6 using HeuristicLab.Optimization; 5 7 6 8 namespace HeuristicLab.Algorithms.NSGA3 7 9 { 8 public class ReferencePoint 10 public class ReferencePoint : IDeepCloneable 9 11 { 12 13 #region Properties 14 10 15 // The potentially associated solutions to this reference point and the distance to that solution 11 16 private readonly Dictionary<Solution, double> potentialAssociatedSolutions = new Dictionary<Solution, double>(); 12 17 13 #region Properties14 15 18 private readonly IRandom random; 19 16 20 public double[] Values { get; } 17 21 public int NumberOfDimensions => Values.Length; … … 33 37 Values = new double[other.NumberOfDimensions]; 34 38 other.Values.CopyTo(Values, 0); 39 } 40 41 public ReferencePoint(ReferencePoint other, Cloner cloner) 42 { 43 random = cloner.Clone(other.random); 44 Values = new double[other.NumberOfDimensions]; 45 other.Values.CopyTo(Values, 0); 46 } 47 48 public IDeepCloneable Clone(Cloner cloner) 49 { 50 return new ReferencePoint(this, cloner); 51 } 52 53 public object Clone() 54 { 55 return new Cloner().Clone(this); 35 56 } 36 57
Note: See TracChangeset
for help on using the changeset viewer.