Changeset 17707
- Timestamp:
- 08/02/20 11:39:51 (4 years ago)
- Location:
- branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/NSGA3.cs
r17703 r17707 41 41 } 42 42 43 public int NumberOfObjectives43 public int Objectives 44 44 { 45 45 get … … 259 259 { 260 260 // Set population size 261 int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(NumberOfObjectives); 261 int numberOfGeneratedReferencePoints = ReferencePoint.GetNumberOfGeneratedReferencePoints(Objectives); 262 if (numberOfGeneratedReferencePoints == -1) throw new NotSupportedException("The number of objectives is not supported."); 262 263 PopulationSize.Value = ReferencePoint.GetPopulationSizeForReferencePoints(numberOfGeneratedReferencePoints); 263 264 … … 294 295 295 296 solutions = GetInitialPopulation(); 296 referencePoints = ReferencePoint.GenerateReferencePoints(random, NumberOfObjectives);297 referencePoints = ReferencePoint.GenerateReferencePoints(random, Objectives); 297 298 ResultsGeneratedReferencePoints = Utility.ConvertToDoubleMatrix(referencePoints); 298 299 } … … 382 383 ResultsHypervolume = new DoubleValue(Hypervolume.Calculate(front, problem.ReferencePoint.CloneAsArray(), problem.Maximization)); 383 384 ResultsDifferenceToBestKnownHypervolume = new DoubleValue(ResultsBestKnownHypervolume.Value - ResultsHypervolume.Value); 384 385 Problem.Analyze(386 solutions.Select(s => (Individual)new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, s.Chromosome) } })).ToArray(),387 solutions.Select(s => s.Fitness).ToArray(),388 Results,389 random390 );391 385 } 392 386 -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/ReferencePoint.cs
r17700 r17707 17 17 18 18 public double[] Values { get; } 19 public int NumberOfDimensions => Values.Length;19 public int Objectives => Values.Length; 20 20 public int NumberOfAssociatedSolutions { get; set; } = 0; 21 21 … … 24 24 #region Constructors 25 25 26 public ReferencePoint(IRandom random, int numberOfDimensions)26 public ReferencePoint(IRandom random, int obj) 27 27 { 28 28 this.random = random; 29 Values = new double[ numberOfDimensions];29 Values = new double[obj]; 30 30 } 31 31 … … 33 33 { 34 34 random = cloner.Clone(other.random); 35 Values = new double[other. NumberOfDimensions];35 Values = new double[other.Objectives]; 36 36 other.Values.CopyTo(Values, 0); 37 37 } … … 50 50 51 51 #region Static methods 52 53 public static Tuple<int, int> GetDivisions(int obj) 54 { 55 switch (obj) 56 { 57 case 3: 58 return Tuple.Create(12, 0); 59 60 case 5: 61 return Tuple.Create(6, 0); 62 63 case 8: 64 return Tuple.Create(3, 2); 65 66 case 10: 67 return Tuple.Create(3, 2); 68 69 case 15: 70 return Tuple.Create(2, 1); 71 72 default: 73 return null; 74 } 75 } 52 76 53 77 /// <summary> … … 55 79 /// cref="GenerateReferencePoints(IRandom, int)" />. 56 80 /// </summary> 57 /// <param name=" nDim"></param>81 /// <param name="obj"></param> 58 82 /// <returns></returns> 59 public static int GetNumberOfGeneratedReferencePoints(int nDim) 60 { 61 int outerDiv; 62 int innerDiv; 63 switch (nDim) 64 { 65 case 3: 66 outerDiv = 12; 67 innerDiv = 0; 68 break; 69 70 case 5: 71 outerDiv = 6; 72 innerDiv = 0; 73 break; 74 75 case 8: 76 outerDiv = 3; 77 innerDiv = 2; 78 break; 79 80 case 10: 81 outerDiv = 3; 82 innerDiv = 2; 83 break; 84 85 case 15: 86 outerDiv = 2; 87 innerDiv = 1; 88 break; 89 90 default: 91 return -1; 92 } 93 94 return GetNumberOfGeneratedReferencePoints(nDim, outerDiv, innerDiv); 95 } 96 97 public static int GetNumberOfGeneratedReferencePoints(int nDim, int outerDiv, int innerDiv = 0) 98 { 99 int outerPoints = Utility.NCR(nDim + outerDiv - 1, outerDiv); 100 int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(nDim + innerDiv - 1, innerDiv); 83 public static int GetNumberOfGeneratedReferencePoints(int obj) 84 { 85 Tuple<int, int> division = GetDivisions(obj); 86 if (division == null) return -1; 87 88 return GetNumberOfGeneratedReferencePoints(obj, division.Item1, division.Item2); 89 } 90 91 public static int GetNumberOfGeneratedReferencePoints(int obj, int outerDiv, int innerDiv = 0) 92 { 93 int outerPoints = Utility.NCR(obj + outerDiv - 1, outerDiv); 94 int innerPoints = innerDiv == 0 ? 0 : Utility.NCR(obj + innerDiv - 1, innerDiv); 101 95 return outerPoints + innerPoints; 102 96 } … … 109 103 /// <summary> 110 104 /// Generate reference points that are evenly distributed over a hyperplane with dimensions 111 /// <paramref name=" nDim" /> - 1 with the sum of the values in all dimensions being equal to112 /// 1 for each reference point. <paramref name=" nDim" /> may only be one of {3, 5, 8, 10, 15}105 /// <paramref name="obj" /> - 1 with the sum of the values in all dimensions being equal to 106 /// 1 for each reference point. <paramref name="obj" /> may only be one of {3, 5, 8, 10, 15} 113 107 /// -> The number of inner divisions and outer divisions are determined based on the 114 108 /// values provided in the NSGA-III paper, chapter V: Results. If different dimensions are … … 118 112 /// The <see cref="IRandom" /> to give as a parameter to the ReferencePoint constructors. 119 113 /// </param> 120 /// <param name=" nDim">The dimensionality of the Reference Points</param>114 /// <param name="obj">The dimensionality of the Reference Points</param> 121 115 /// <returns></returns> 122 public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim) 123 { 124 List<ReferencePoint> referencePoints; 125 switch (nDim) 126 { 127 case 3: 128 referencePoints = GenerateReferencePoints(random, nDim, 12, 0); 129 break; 130 131 case 5: 132 referencePoints = GenerateReferencePoints(random, nDim, 6, 0); 133 break; 134 135 case 8: 136 referencePoints = GenerateReferencePoints(random, nDim, 3, 2); 137 break; 138 139 case 10: 140 referencePoints = GenerateReferencePoints(random, nDim, 3, 2); 141 break; 142 143 case 15: 144 referencePoints = GenerateReferencePoints(random, nDim, 2, 1); 145 break; 146 147 default: 148 throw new NotSupportedException("Only the reference points for 3, 5, 8, 10 or 15 dimensions can be created."); 149 } 150 151 return referencePoints; 116 public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int obj) 117 { 118 Tuple<int, int> divisions = GetDivisions(obj); 119 120 if (divisions == null) return null; 121 return GenerateReferencePoints(random, obj, divisions.Item1, divisions.Item2); 152 122 } 153 123 … … 159 129 /// <summary> 160 130 /// Generate reference points that are evenly distributed over a hyperplane with dimensions 161 /// <paramref name=" nDim" /> - 1 with the sum of the values in all dimensions being equal to131 /// <paramref name="obj" /> - 1 with the sum of the values in all dimensions being equal to 162 132 /// 1 for each reference point. 163 133 /// </summary> 164 /// <param name=" nDim">The number of dimensions for the reference points.</param>134 /// <param name="obj">The number of dimensions for the reference points.</param> 165 135 /// <param name="outerDiv">The number of divisions for the outer reference points</param> 166 136 /// <returns>The generated reference points (Check Fig. 4 in NSGA-III paper).</returns> … … 170 140 /// not have inner reference points 171 141 /// </returns> 172 public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int nDim, int outerDiv, int innerDiv = 0)173 { 174 if ( nDim <= 0) throw new ArgumentException("nDimmust be greater than 0");142 public static List<ReferencePoint> GenerateReferencePoints(IRandom random, int obj, int outerDiv, int innerDiv = 0) 143 { 144 if (obj <= 0) throw new ArgumentException("obj must be greater than 0"); 175 145 if (outerDiv <= 1) throw new ArgumentException("outerDiv must be greater than 1"); 176 146 177 147 List<ReferencePoint> referencePoints = new List<ReferencePoint>(); 178 ReferencePoint refPoint = new ReferencePoint(random, nDim);179 GenerateRecursive(referencePoints, refPoint, nDim, outerDiv, outerDiv, 0);148 ReferencePoint refPoint = new ReferencePoint(random, obj); 149 GenerateRecursive(referencePoints, refPoint, obj, outerDiv, outerDiv, 0); 180 150 181 151 if (innerDiv > 0) 182 152 { 183 153 List<ReferencePoint> insideReferencePoints = new List<ReferencePoint>(); 184 GenerateRecursive(insideReferencePoints, refPoint, nDim, innerDiv, innerDiv, 0);185 186 double center = 1.0 / nDim;154 GenerateRecursive(insideReferencePoints, refPoint, obj, innerDiv, innerDiv, 0); 155 156 double center = 1.0 / obj; 187 157 188 158 for (int i = 0; i < insideReferencePoints.Count; i++) -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs
r17679 r17707 32 32 { 33 33 if (n < r) throw new InvalidOperationException($"Constraint was not met: n >= r (n = {n}, r = {r})"); 34 if (r < 1) throw new InvalidOperationException($"Constraint was not met: r >= 1(r = {r})");34 if (r < 0) throw new InvalidOperationException($"Constraint was not met: r >= 0 (r = {r})"); 35 35 if (n == r) return 1; 36 return (int)(Factorial(n) / (Factorial(r) * Factorial(n - r))); 37 } 38 39 public static long Factorial(int n) 36 37 r = Math.Max(r, n - r); 38 39 long range1 = RangeMultiplication(r + 1, n - r); 40 return (int)(range1 / Factorial(n - r)); 41 } 42 43 private static long RangeMultiplication(int start, int count) 44 { 45 long s = 1; 46 return LeftFold((long a, int b) => checked(a * b), new List<int>(Enumerable.Range(start, count)), s); 47 } 48 49 private static T2 LeftFold<T1, T2>(Func<T2, T1, T2> func, List<T1> elements, T2 start) 50 { 51 var item = start; 52 foreach (var element in elements) 53 item = func(item, element); 54 55 return item; 56 } 57 58 public static long Factorial(long n) 40 59 { 41 60 if (n < 0 || n > 30) throw new InvalidOperationException($"Constraint for n was not met: 0 <= n <= 30 (n = {n})"); 42 61 long product = 1; 43 for ( inti = 2; i <= n; i++)62 for (long i = 2; i <= n; i++) 44 63 product *= i; 45 64 46 65 return product; 47 66 } 67 48 68 public static double[][] ToJaggedArray(this DoubleMatrix m) 49 69 { … … 94 114 { 95 115 if (referencePoints == null || !referencePoints.Any()) throw new ArgumentException($"{nameof(referencePoints)} is null or empty"); 96 int nDim = referencePoints.First().NumberOfDimensions;116 int obj = referencePoints.First().Objectives; 97 117 int pointCount = referencePoints.Count; 98 double[,] array = new double[ nDim, pointCount];118 double[,] array = new double[obj, pointCount]; 99 119 100 120 for (int p = 0; p < pointCount; p++) 101 for (int d = 0; d < nDim; d++)121 for (int d = 0; d < obj; d++) 102 122 array[d, p] = referencePoints[p].Values[d]; 103 123
Note: See TracChangeset
for help on using the changeset viewer.