Changeset 13756
- Timestamp:
- 04/13/16 11:47:30 (9 years ago)
- Location:
- branches/ichiriac/HeuristicLab.Algorithms.GDE3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ichiriac/HeuristicLab.Algorithms.GDE3/GDE3.cs
r13749 r13756 16 16 using HeuristicLab.Random; 17 17 using System.Threading; 18 using HeuristicLab.Algorithms.GDE3; 18 19 19 20 namespace HeuristicLab.Algoritms.GDE3 … … 23 24 [StorableClass] 24 25 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)] 25 public class GDE3 : BasicAlgorithm 26 public class GDE3 : BasicAlgorithm 26 27 { 27 public Func<IEnumerable<double>, double> Evaluation;28 private enum DominationResult { Dominates, IsDominated, IsNonDominated };29 IComparer<RealVector> dominance;30 public double[] ReferencePoint(int objectives)31 {32 return new double[] { 11, 11 };33 }34 35 28 public override Type ProblemType 36 29 { 37 30 get { return typeof(MultiObjectiveTestFunctionProblem); } 38 31 } 39 32 public new MultiObjectiveTestFunctionProblem Problem 40 33 { 41 34 get { return (MultiObjectiveTestFunctionProblem)base.Problem; } … … 55 48 56 49 #region ParameterNames 57 private const string Maximum EvaluationsParameterName = "Maximum Evaluations";50 private const string MaximumGenerationsParameterName = "Maximum Generations"; 58 51 private const string CrossoverProbabilityParameterName = "CrossoverProbability"; 59 52 private const string PopulationSizeParameterName = "PopulationSize"; … … 62 55 63 56 #region ParameterProperties 64 public IFixedValueParameter<IntValue> Maximum EvaluationsParameter65 { 66 get { return (IFixedValueParameter<IntValue>)Parameters[Maximum EvaluationsParameterName]; }57 public IFixedValueParameter<IntValue> MaximumGenerationsParameter 58 { 59 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; } 67 60 } 68 61 private ValueParameter<IntValue> PopulationSizeParameter … … 83 76 public int MaximumEvaluations 84 77 { 85 get { return Maximum EvaluationsParameter.Value.Value; }86 set { Maximum EvaluationsParameter.Value.Value = value; }78 get { return MaximumGenerationsParameter.Value.Value; } 79 set { MaximumGenerationsParameter.Value.Value = value; } 87 80 } 88 81 … … 111 104 } 112 105 113 private double Results GenerationalDistance106 private double ResultsInvertedGenerationalDistance 114 107 { 115 108 get { return ((DoubleValue)Results["InvertedGenerationalDistance"].Value).Value; } … … 119 112 private double ResultsHypervolume 120 113 { 121 get { return ((DoubleValue)Results["H ipervolumeValue"].Value).Value; }122 set { ((DoubleValue)Results["H ipervolumeValue"].Value).Value = value; }114 get { return ((DoubleValue)Results["HyperVolumeValue"].Value).Value; } 115 set { ((DoubleValue)Results["HyperVolumeValue"].Value).Value = value; } 123 116 } 124 117 … … 134 127 set { ((IntValue)Results["Evaluations"].Value).Value = value; } 135 128 } 129 private int ResultsGenerations 130 { 131 get { return ((IntValue)Results["Generations"].Value).Value; } 132 set { ((IntValue)Results["Generations"].Value).Value = value; } 133 } 134 private double ResultsGenerationalDistance 135 { 136 get { return ((DoubleValue)Results["GenerationalDistance"].Value).Value; } 137 set { ((DoubleValue)Results["GenerationalDistance"].Value).Value = value; } 138 } 136 139 private int ResultsIterations 137 140 { … … 140 143 } 141 144 142 private DataTable ResultsQualities 143 { 144 get { return ((DataTable)Results["Qualities"].Value); } 145 } 146 private DataRow ResultsQualitiesBest 147 { 148 get { return ResultsQualities.Rows["Best Quality"]; } 145 private double ResultsSpacing 146 { 147 get { return ((DoubleValue)Results["Spacing"].Value).Value; } 148 set { ((DoubleValue)Results["Spacing"].Value).Value = value; } 149 } 150 151 private double ResultsCrowding 152 { 153 get { return ((DoubleValue)Results["Crowding"].Value).Value; } 154 set { ((DoubleValue)Results["Crowding"].Value).Value = value; } 149 155 } 150 156 151 157 #endregion 152 153 [Storable]154 private RankBasedParetoFrontAnalyzer paretoFrontAnalyzer;155 158 156 159 [StorableConstructor] … … 160 163 : base(original, cloner) 161 164 { 162 paretoFrontAnalyzer = (RankBasedParetoFrontAnalyzer)cloner.Clone(original.paretoFrontAnalyzer);163 165 } 164 166 … … 170 172 public GDE3() 171 173 { 172 Parameters.Add(new FixedValueParameter<IntValue>(Maximum EvaluationsParameterName, "", new IntValue(Int32.MaxValue)));174 Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsParameterName, "", new IntValue(1000))); 173 175 Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100))); 174 176 Parameters.Add(new ValueParameter<DoubleValue>(CrossoverProbabilityParameterName, "The value for crossover rate", new DoubleValue(0.5))); … … 180 182 { 181 183 // Set up the results display 182 Results.Add(new Result(" Iterations", new IntValue(0)));184 Results.Add(new Result("Generations", new IntValue(0))); 183 185 Results.Add(new Result("Evaluations", new IntValue(0))); 184 186 Results.Add(new Result("Best Front", new DoubleMatrix())); 187 Results.Add(new Result("Crowding", new DoubleValue(0))); 185 188 Results.Add(new Result("InvertedGenerationalDistance", new DoubleValue(0))); 186 Results.Add(new Result("HipervolumeValue", new DoubleValue(0))); 189 Results.Add(new Result("GenerationalDistance", new DoubleValue(0))); 190 Results.Add(new Result("HyperVolumeValue", new DoubleValue(0))); 191 Results.Add(new Result("Spacing", new DoubleValue(0))); 187 192 Results.Add(new Result("Scatterplot", typeof(IMOFrontModel))); 188 193 var table = new DataTable("Qualities"); … … 190 195 Results.Add(new Result("Qualities", table)); 191 196 192 //create initial population 193 var population = new List<SolutionSet>(PopulationSizeParameter.Value.Value); 194 var offspringPopulation = new List<SolutionSet>(PopulationSizeParameter.Value.Value); 195 var matingPopulation = new List<SolutionSet>(); 197 //setup the variables 198 List<SolutionSet> population; 199 List<SolutionSet> offspringPopulation; 200 SolutionSet[] parent; 201 202 //initialize population 203 population = new List<SolutionSet>(PopulationSizeParameter.Value.Value); 196 204 197 205 for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i) … … 199 207 var m = createIndividual(); 200 208 population.Add(m); 201 var n = createEmptyIndividual();202 offspringPopulation.Add(n);203 209 } 204 210 … … 206 212 int iterations = 1; 207 213 208 while (Results Evaluations < MaximumEvaluations214 while (ResultsGenerations < MaximumGenerationsParameter.Value.Value 209 215 && !cancellationToken.IsCancellationRequested) 210 216 { 211 matingPopulation = selection(population); 212 offspringPopulation = reproduction(matingPopulation, population, offspringPopulation); 213 214 population = replacement(population, offspringPopulation); 215 216 double[][] qualitiesFinal = new double[PopulationSizeParameter.Value.Value][]; 217 218 for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i) 219 { 220 qualitiesFinal[i] = new double[Problem.Objectives]; 221 for (int j = 0; j < Problem.Objectives; ++j) 217 var populationSize = PopulationSizeParameter.Value.Value; 218 219 // Create the offSpring solutionSet 220 offspringPopulation = new List<SolutionSet>(PopulationSizeParameter.Value.Value * 2); 221 222 for (int i = 0; i < populationSize; i++) 223 { 224 // Obtain parents. Two parameters are required: the population and the 225 // index of the current individual 226 parent = selection(population, i); 227 228 SolutionSet child; 229 // Crossover. The parameters are the current individual and the index of the array of parents 230 child = reproduction(population[i], parent); 231 232 child.Quality = Problem.Evaluate(child.Population, _random); 233 this.updateProgres(); 234 235 // Dominance test 236 int result; 237 result = compareDomination(population[i].Quality, child.Quality); 238 if (result == -1) 239 { // Solution i dominates child 240 offspringPopulation.Add(population[i]); 241 } 242 else if (result == 1) 243 { // child dominates 244 offspringPopulation.Add(child); 245 } 246 else 247 { // the two solutions are non-dominated 248 offspringPopulation.Add(child); 249 offspringPopulation.Add(population[i]); 250 } 251 } 252 // Ranking the offspring population 253 List<SolutionSet>[] ranking = computeRanking(offspringPopulation); 254 255 int remain = populationSize; 256 int index = 0; 257 population.Clear(); 258 List<SolutionSet> front = null; 259 260 // Obtain the next front 261 front = ranking[index]; 262 263 while ((remain > 0) && (remain >= front.Count)) 264 { 265 //Assign crowding distance to individuals 266 crowdingDistanceAssignment(front, index); 267 //Add the individuals of this front 268 for (int k = 0; k < front.Count; k++) 222 269 { 223 qualitiesFinal[i][j] = population[i].Quality[j]; 224 } 225 } 226 227 double[][] populationFinal = new double[PopulationSizeParameter.Value.Value][]; 228 229 for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i) 230 { 231 populationFinal[i] = new double[Problem.ProblemSize]; 232 for (int j = 0; j < Problem.ProblemSize; ++j) 270 population.Add(front[k]); 271 } // for 272 273 //Decrement remain 274 remain = remain - front.Count; 275 276 //Obtain the next front 277 index++; 278 if (remain > 0) 233 279 { 234 populationFinal[i][j] = population[i].Population[j]; 235 } 236 } 237 238 int objectives = Problem.Objectives; 239 var optimalfront = Problem.TestFunction.OptimalParetoFront(objectives); 240 241 IEnumerable<double[]> en = qualitiesFinal; 242 243 double[][] opf = new double[0][]; 244 if (optimalfront != null) 245 { 246 opf = optimalfront.Select(s => s.ToArray()).ToArray(); 247 } 248 249 iterations = iterations + 1; 250 251 IEnumerable<double[]> front = NonDominatedSelect.selectNonDominatedVectors(qualitiesFinal, Problem.TestFunction.Maximization(objectives), true); 252 //update the results 253 ResultsEvaluations = evals; 254 ResultsIterations = iterations; 255 ResultsBestFront = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualitiesFinal)); 256 ResultsGenerationalDistance = InvertedGenerationalDistance.Calculate(en, optimalfront, 1); 257 ResultsHypervolume = Hypervolume.Calculate(front, ReferencePoint(objectives), Problem.TestFunction.Maximization(objectives)); 258 259 Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives); 260 } 261 } 262 280 front = ranking[index]; 281 } 282 } 283 284 // remain is less than front(index).size, insert only the best one 285 if (remain > 0) 286 { // front contains individuals to insert 287 while (front.Count > remain) 288 { 289 crowdingDistanceAssignment(front, index); 290 int indx = getWorstIndex(front); 291 front.RemoveAt(indx); 292 } 293 for (int k = 0; k < front.Count; k++) 294 { 295 population.Add(front[k]); 296 } 297 298 remain = 0; 299 } 300 iterations++; 301 ResultsGenerations = iterations; 302 displayResults(front); 303 } 304 } 305 306 private void displayResults(List<SolutionSet> population) 307 { 308 //compute the 0 front 309 // Return the first non-dominated front 310 List<SolutionSet>[] rankingFinal = computeRanking(population); 311 312 int objectives = Problem.Objectives; 313 var optimalfront = Problem.TestFunction.OptimalParetoFront(objectives); 314 315 double[][] opf = new double[0][]; 316 if (optimalfront != null) 317 { 318 opf = optimalfront.Select(s => s.ToArray()).ToArray(); 319 } 320 321 double[][] qualitiesFinal = new double[rankingFinal[0].Count][]; 322 323 for (int i = 0; i < rankingFinal[0].Count; ++i) 324 { 325 qualitiesFinal[i] = new double[Problem.Objectives]; 326 for (int j = 0; j < Problem.Objectives; ++j) 327 { 328 qualitiesFinal[i][j] = rankingFinal[0][i].Quality[j]; 329 } 330 } 331 332 double[][] populationFinal = new double[rankingFinal[0].Count][]; 333 334 for (int i = 0; i < rankingFinal[0].Count; ++i) 335 { 336 populationFinal[i] = new double[Problem.ProblemSize]; 337 for (int j = 0; j < Problem.ProblemSize; ++j) 338 { 339 populationFinal[i][j] = rankingFinal[0][i].Population[j]; 340 } 341 } 342 IEnumerable<double[]> en = qualitiesFinal; 343 IEnumerable<double[]> frontVectors = NonDominatedSelect.selectNonDominatedVectors(qualitiesFinal, Problem.TestFunction.Maximization(objectives), true); 344 //update the results 345 346 ResultsEvaluations = this.evals; 347 ResultsBestFront = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualitiesFinal)); 348 ResultsCrowding = Crowding.Calculate(qualitiesFinal, Problem.TestFunction.Bounds(objectives)); 349 ResultsInvertedGenerationalDistance = InvertedGenerationalDistance.Calculate(en, optimalfront, 1); 350 ResultsHypervolume = Hypervolume.Calculate(frontVectors, Problem.TestFunction.ReferencePoint(objectives), Problem.TestFunction.Maximization(objectives)); 351 ResultsGenerationalDistance = GenerationalDistance.Calculate(qualitiesFinal, optimalfront, 1); 352 Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives); 353 ResultsSpacing = Spacing.Calculate(qualitiesFinal); 354 } 355 356 private int getWorstIndex(List<SolutionSet> SolutionsList) 357 { 358 int result = 0; 359 360 if ((SolutionsList == null) || SolutionsList.Count == 0) 361 { 362 result = 0; 363 } 364 else 365 { 366 SolutionSet worstKnown = SolutionsList[0], 367 candidateSolution; 368 int flag; 369 for (int i = 1; i < SolutionsList.Count; i++) 370 { 371 candidateSolution = SolutionsList[i]; 372 flag = compareDomination(worstKnown.Quality, candidateSolution.Quality); 373 if (flag == -1) 374 { 375 result = i; 376 worstKnown = candidateSolution; 377 } 378 } 379 } 380 return result; 381 } 382 263 383 protected SolutionSet createIndividual() 264 384 { … … 296 416 protected void updateProgres() 297 417 { 298 this.evals += PopulationSizeParameter.Value.Value; 299 } 300 301 protected List<SolutionSet> selection(List<SolutionSet> population) 302 { 303 List<SolutionSet> parents = new List<SolutionSet>(); 304 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 305 { 306 int r0, r1, r2; 307 //assure the selected vectors r0, r1 and r2 are different 308 do 309 { 310 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 311 } while (r0 == i); 312 do 313 { 314 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 315 } while (r1 == i || r1 == r0); 316 do 317 { 318 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 319 } while (r2 == i || r2 == r0 || r2 == r1); 320 321 parents.Add(population[r0]); 322 parents.Add(population[r1]); 323 parents.Add(population[r2]); 324 } 418 this.evals++; 419 } 420 421 protected SolutionSet[] selection(List<SolutionSet> population, int i) 422 { 423 SolutionSet[] parents = new SolutionSet[3]; 424 int r0, r1, r2; 425 //assure the selected vectors r0, r1 and r2 are different 426 do 427 { 428 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 429 } while (r0 == i); 430 do 431 { 432 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 433 } while (r1 == i || r1 == r0); 434 do 435 { 436 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 437 } while (r2 == i || r2 == r0 || r2 == r1); 438 439 parents[0] = population[r0]; 440 parents[1] = population[r1]; 441 parents[2] = population[r2]; 442 325 443 return parents; 326 444 } 327 445 328 protected List<SolutionSet> reproduction(List<SolutionSet> matingPopulation, List<SolutionSet> parentPopulation, List<SolutionSet> offspringPopulation) 329 { 330 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 331 { 332 List<SolutionSet> parents = new List<SolutionSet>(3); 333 for (int j = 0; j < 3; j++) { 334 parents.Add(matingPopulation[0]); 335 matingPopulation.RemoveAt(0); 336 } 337 338 double rnbr = _random.Next(0, Problem.ProblemSize); 339 for (int m = 0; m < Problem.ProblemSize; m++) 340 { 341 if (_random.NextDouble() < CrossoverProbabilityParameter.Value.Value || m == rnbr) 342 { 343 offspringPopulation[i].Population[m] = parents[2].Population[m] + 344 ScalingFactorParameter.Value.Value * (parents[1].Population[m] - parents[2].Population[0]); 345 //check the problem upper and lower bounds 346 if (offspringPopulation[i].Population[m] > Problem.Bounds[0, 1]) offspringPopulation[i].Population[m] = Problem.Bounds[0, 1]; 347 if (offspringPopulation[i].Population[m] < Problem.Bounds[0, 0]) offspringPopulation[i].Population[m] = Problem.Bounds[0, 0]; 348 } 349 else 350 { 351 offspringPopulation[i].Population[m] = parentPopulation[i].Population[m]; // check again 352 } 353 } 354 offspringPopulation[i].Quality = Problem.Evaluate(offspringPopulation[i].Population, _random); 355 } 356 this.updateProgres(); 357 return offspringPopulation; 446 protected SolutionSet reproduction(SolutionSet parent, SolutionSet[] parentsSolutions) 447 { 448 var individual = createEmptyIndividual(); 449 double rnbr = _random.Next(0, Problem.ProblemSize); 450 for (int m = 0; m < Problem.ProblemSize; m++) 451 { 452 if (_random.NextDouble() < CrossoverProbabilityParameter.Value.Value || m == rnbr) 453 { 454 double value; 455 value = parentsSolutions[2].Population[m] + 456 ScalingFactorParameter.Value.Value * (parentsSolutions[0].Population[m] - parentsSolutions[1].Population[m]); 457 //check the problem upper and lower bounds 458 if (value > Problem.Bounds[0, 1]) value = Problem.Bounds[0, 1]; 459 if (value < Problem.Bounds[0, 0]) value = Problem.Bounds[0, 0]; 460 individual.Population[m] = value; 461 } 462 else 463 { 464 double value; 465 value = parent.Population[m]; 466 individual.Population[m] = value; 467 } 468 } 469 return individual; 358 470 } 359 471 … … 380 492 } 381 493 382 List<SolutionSet>[] ranking = computeRanking(tmpList);494 List<SolutionSet>[] ranking = computeRanking(tmpList); 383 495 List<SolutionSet> finalPopulation = crowdingDistanceSelection(ranking); 384 496 return finalPopulation; … … 391 503 while (populationIsNotFull(population)) 392 504 { 393 if (subFrontFillsIntoThePopulation(ranking, rankingIndex, population)){ 505 if (subFrontFillsIntoThePopulation(ranking, rankingIndex, population)) 506 { 394 507 addRankedSolutionToPopulation(ranking, rankingIndex, population); 395 508 rankingIndex++; 396 } else { 509 } 510 else { 397 511 crowdingDistanceAssignment(ranking[rankingIndex], rankingIndex); 398 512 addLastRankedSolutionToPopulation(ranking, rankingIndex, population); … … 448 562 double distance; 449 563 450 for (int i = 0; i < Problem.Objectives 564 for (int i = 0; i < Problem.Objectives; i++) 451 565 { 452 566 // Sort the population by Obj n … … 456 570 457 571 //Set de crowding distance 458 front[0].CrowdingDistance = double.PositiveInfinity; 572 front[0].CrowdingDistance = double.PositiveInfinity; 459 573 front[size - 1].CrowdingDistance = double.PositiveInfinity; 460 574 … … 471 585 private void addRankedSolutionToPopulation(List<SolutionSet>[] ranking, int rankingIndex, List<SolutionSet> population) 472 586 { 473 foreach (SolutionSet solution in ranking[rankingIndex])587 foreach (SolutionSet solution in ranking[rankingIndex]) 474 588 { 475 589 population.Add(solution); … … 497 611 // front[i] contains the list of individuals belonging to the front i 498 612 List<int>[] front = new List<int>[tmpList.Count + 1]; 499 int[] solutionsRanks = new int[tmpList.Count];500 613 501 614 // flagDominate is an auxiliar encodings.variable -
branches/ichiriac/HeuristicLab.Algorithms.GDE3/HeuristicLab.Algorithms.GDE3.csproj
r13749 r13756 114 114 <ItemGroup> 115 115 <Compile Include="GDE3.cs" /> 116 <Compile Include="GenerationalDistanceCalculator.cs" /> 116 117 <Compile Include="Plugin.cs" /> 117 118 <Compile Include="Properties\AssemblyInfo.cs" />
Note: See TracChangeset
for help on using the changeset viewer.