Changeset 17559
- Timestamp:
- 05/25/20 19:33:28 (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
r17558 r17559 10 10 using HeuristicLab.Optimization; 11 11 using HeuristicLab.Parameters; 12 using HeuristicLab.Random; 12 13 13 14 namespace HeuristicLab.Algorithms.NSGA3 … … 66 67 67 68 private const string GeneratedReferencePointsResultName = "Generated Reference Points"; 69 private const string CurrentFrontResultName = "Pareto Front"; // Do not touch this 68 70 69 71 #endregion ParameterAndResultsNames … … 132 134 get { return (DoubleMatrix)Results[GeneratedReferencePointsResultName].Value; } 133 135 set { Results[GeneratedReferencePointsResultName].Value = value; } 136 } 137 138 public DoubleMatrix ResultsSolutions 139 { 140 get { return (DoubleMatrix)Results[CurrentFrontResultName].Value; } 141 set { Results[CurrentFrontResultName].Value = value; } 134 142 } 135 143 … … 173 181 base.Initialize(cancellationToken); 174 182 183 InitFields(); 175 184 InitResults(); 176 185 InitReferencePoints(); 186 Analyze(); 177 187 } 178 188 … … 186 196 #region Private Methods 187 197 198 private void InitFields() 199 { 200 random = new MersenneTwister(); 201 InitSolutions(); 202 } 203 188 204 private void InitResults() 189 205 { 190 206 Results.Add(new Result(GeneratedReferencePointsResultName, "The initially generated reference points", new DoubleMatrix())); 191 }192 193 // Generates the structured reference points 207 Results.Add(new Result(CurrentFrontResultName, "The Pareto Front", new DoubleMatrix())); 208 } 209 194 210 private void InitReferencePoints() 195 211 { … … 200 216 } 201 217 218 private void InitSolutions() 219 { 220 int minBound = 0; // todo: find min inside Problem.Encoding 221 int maxBound = 1; // todo: find max inside Problem.Encoding 222 223 // Initialise solutions 224 solutions = new Solution[PopulationSize.Value]; 225 for (int i = 0; i < PopulationSize.Value; i++) 226 { 227 RealVector randomRealVector = new RealVector(Problem.Encoding.Length, random, minBound, maxBound); 228 229 solutions[i] = new Solution(StorableConstructorFlag.Default) 230 { 231 Chromosome = randomRealVector 232 }; 233 solutions[i].Fitness = Evaluate(solutions[i].Chromosome); 234 } 235 } 236 237 /// <summary> 238 /// Returns the fitness of the given <paramref name="chromosome" /> by applying the Evaluate 239 /// method of the Problem. 240 /// </summary> 241 /// <param name="chromosome"></param> 242 /// <returns></returns> 243 private double[] Evaluate(RealVector chromosome) 244 { 245 return Problem.Evaluate(new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, chromosome) } }), random); 246 } 247 248 private void Analyze() 249 { 250 ResultsSolutions = solutions.Select(s => s.Chromosome.ToArray()).ToMatrix(); 251 Problem.Analyze( 252 solutions.Select(s => (Individual)new SingleEncodingIndividual(Problem.Encoding, new Scope { Variables = { new Variable(Problem.Encoding.Name, s.Chromosome) } })).ToArray(), 253 solutions.Select(s => s.Fitness).ToArray(), 254 Results, 255 random 256 ); 257 } 258 202 259 #endregion Private Methods 203 260 } -
branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/Utility.cs
r17558 r17559 55 55 return array; 56 56 } 57 58 internal static DoubleMatrix ToMatrix(this IEnumerable<IReadOnlyList<double>> data) 59 { 60 var d2 = data.ToArray(); 61 var mat = new DoubleMatrix(d2.Length, d2[0].Count); 62 for (var i = 0; i < mat.Rows; i++) 63 for (var j = 0; j < mat.Columns; j++) 64 mat[i, j] = d2[i][j]; 65 return mat; 66 } 57 67 } 58 68 }
Note: See TracChangeset
for help on using the changeset viewer.