Changeset 13849
- Timestamp:
- 05/18/16 11:27:21 (9 years ago)
- Location:
- branches/ichiriac/HeuristicLab.Algorithms.GDE3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ichiriac/HeuristicLab.Algorithms.GDE3/GDE3.cs
r13756 r13849 46 46 private readonly IRandom _random = new MersenneTwister(); 47 47 private int evals; 48 private double IGDSumm; 48 49 49 50 #region ParameterNames … … 52 53 private const string PopulationSizeParameterName = "PopulationSize"; 53 54 private const string ScalingFactorParameterName = "ScalingFactor"; 55 54 56 #endregion 55 57 … … 104 106 } 105 107 108 private double ResultsIGDMean 109 { 110 get { return ((DoubleValue)Results["IGDMeanValue"].Value).Value; } 111 set { ((DoubleValue)Results["IGDMeanValue"].Value).Value = value; } 112 } 113 114 private double ResultsIGDBest 115 { 116 get { return ((DoubleValue)Results["IGDBestValue"].Value).Value; } 117 set { ((DoubleValue)Results["IGDBestValue"].Value).Value = value; } 118 } 119 120 private double ResultsIGDWorst 121 { 122 get { return ((DoubleValue)Results["IGDWorstValue"].Value).Value; } 123 set { ((DoubleValue)Results["IGDWorstValue"].Value).Value = value; } 124 } 125 106 126 private double ResultsInvertedGenerationalDistance 107 127 { … … 136 156 get { return ((DoubleValue)Results["GenerationalDistance"].Value).Value; } 137 157 set { ((DoubleValue)Results["GenerationalDistance"].Value).Value = value; } 138 }139 private int ResultsIterations140 {141 get { return ((IntValue)Results["Iterations"].Value).Value; }142 set { ((IntValue)Results["Iterations"].Value).Value = value; }143 158 } 144 159 … … 189 204 Results.Add(new Result("GenerationalDistance", new DoubleValue(0))); 190 205 Results.Add(new Result("HyperVolumeValue", new DoubleValue(0))); 206 Results.Add(new Result("IGDMeanValue", new DoubleValue(0))); 207 Results.Add(new Result("IGDBestValue", new DoubleValue(Int32.MaxValue))); 208 Results.Add(new Result("IGDWorstValue", new DoubleValue(0))); 209 191 210 Results.Add(new Result("Spacing", new DoubleValue(0))); 192 211 Results.Add(new Result("Scatterplot", typeof(IMOFrontModel))); … … 199 218 List<SolutionSet> offspringPopulation; 200 219 SolutionSet[] parent; 220 double IGDSumm = 0; 201 221 202 222 //initialize population … … 206 226 { 207 227 var m = createIndividual(); 228 m.Quality = Problem.Evaluate(m.Population, _random); 229 //the test function is constrained 230 if (m.Quality.Length > Problem.Objectives) 231 { 232 m.OverallConstrainViolation = m.Quality[Problem.Objectives]; 233 } else { 234 m.OverallConstrainViolation = 0; 235 } 208 236 population.Add(m); 209 237 } 210 238 211 239 this.initProgress(); 212 int iterations = 1;240 int generations = 1; 213 241 214 242 while (ResultsGenerations < MaximumGenerationsParameter.Value.Value … … 231 259 232 260 child.Quality = Problem.Evaluate(child.Population, _random); 261 233 262 this.updateProgres(); 263 264 //the test function is constrained 265 if (child.Quality.Length > Problem.Objectives) 266 { 267 child.OverallConstrainViolation = child.Quality[Problem.Objectives]; 268 } else { 269 child.OverallConstrainViolation = 0; 270 } 234 271 235 272 // Dominance test 236 273 int result; 237 result = compareDomination(population[i].Quality, child.Quality); 274 result = compareDomination(population[i], child); 275 238 276 if (result == -1) 239 277 { // Solution i dominates child … … 250 288 } 251 289 } 290 252 291 // Ranking the offspring population 253 292 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++) 269 { 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) 279 { 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); 293 population = crowdingDistanceSelection(ranking); 294 generations++; 295 ResultsGenerations = generations; 296 displayResults(population); 303 297 } 304 298 } … … 306 300 private void displayResults(List<SolutionSet> population) 307 301 { 308 //compute the 0 front309 // Return the first non-dominated front310 302 List<SolutionSet>[] rankingFinal = computeRanking(population); 311 303 … … 319 311 } 320 312 313 //compute the final qualities and population 321 314 double[][] qualitiesFinal = new double[rankingFinal[0].Count][]; 315 double[][] populationFinal = new double[rankingFinal[0].Count][]; 322 316 323 317 for (int i = 0; i < rankingFinal[0].Count; ++i) 324 318 { 325 319 qualitiesFinal[i] = new double[Problem.Objectives]; 320 populationFinal[i] = new double[Problem.Objectives]; 326 321 for (int j = 0; j < Problem.Objectives; ++j) 327 322 { 323 populationFinal[i][j] = rankingFinal[0][i].Population[j]; 328 324 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 325 } 341 326 } … … 352 337 Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives); 353 338 ResultsSpacing = Spacing.Calculate(qualitiesFinal); 339 340 if (ResultsIGDBest > ResultsInvertedGenerationalDistance) { 341 ResultsIGDBest = ResultsInvertedGenerationalDistance; 342 } 343 if (ResultsIGDWorst < ResultsInvertedGenerationalDistance) 344 { 345 ResultsIGDWorst = ResultsInvertedGenerationalDistance; 346 } 347 this.IGDSumm += ResultsInvertedGenerationalDistance; 348 ResultsIGDMean = this.IGDSumm / ResultsGenerations; 354 349 } 355 350 … … 370 365 { 371 366 candidateSolution = SolutionsList[i]; 372 flag = compareDomination(worstKnown .Quality, candidateSolution.Quality);367 flag = compareDomination(worstKnown, candidateSolution); 373 368 if (flag == -1) 374 369 { … … 395 390 396 391 } 397 var m = new RealVector(v); 398 solutionObject.Population = m; 399 solutionObject.Quality = Problem.Evaluate(m, _random); 392 solutionObject.createSolution(v); 400 393 return solutionObject; 401 394 } … … 470 463 } 471 464 472 private List<SolutionSet> replacement(List<SolutionSet> population, List<SolutionSet> offspringPopulation)473 {474 List<SolutionSet> tmpList = new List<SolutionSet>();475 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)476 {477 int result;478 result = compareDomination(population[i].Quality, offspringPopulation[i].Quality);479 if (result == -1)480 { // Solution i dominates child481 tmpList.Add(population[i]);482 }483 else if (result == 1)484 { // child dominates485 tmpList.Add(offspringPopulation[i]);486 }487 else488 { // the two solutions are non-dominated489 tmpList.Add(offspringPopulation[i]);490 tmpList.Add(population[i]);491 }492 }493 494 List<SolutionSet>[] ranking = computeRanking(tmpList);495 List<SolutionSet> finalPopulation = crowdingDistanceSelection(ranking);496 return finalPopulation;497 }498 499 465 private List<SolutionSet> crowdingDistanceSelection(List<SolutionSet>[] ranking) 500 466 { … … 509 475 } 510 476 else { 511 crowdingDistanceAssignment(ranking[rankingIndex] , rankingIndex);477 crowdingDistanceAssignment(ranking[rankingIndex]); 512 478 addLastRankedSolutionToPopulation(ranking, rankingIndex, population); 513 479 } … … 519 485 { 520 486 List<SolutionSet> currentRankedFront = ranking[rankingIndex]; 521 currentRankedFront.Sort((x, y) => x.CrowdingDistance.CompareTo(y.CrowdingDistance)); 487 //descending sort and add the front with highest crowding distance to the population 488 currentRankedFront.Sort((x, y) => -x.CrowdingDistance.CompareTo(y.CrowdingDistance)); 522 489 int i = 0; 523 490 while (population.Count < PopulationSizeParameter.Value.Value) … … 528 495 } 529 496 530 public void crowdingDistanceAssignment(List<SolutionSet> rankingSubfront , int index)497 public void crowdingDistanceAssignment(List<SolutionSet> rankingSubfront) 531 498 { 532 499 int size = rankingSubfront.Count; … … 556 523 557 524 for (int i = 0; i < size; i++) 558 rankingSubfront[i].CrowdingDistance = 0.0;525 front[i].CrowdingDistance = 0.0; 559 526 560 527 double objetiveMaxn; … … 564 531 for (int i = 0; i < Problem.Objectives; i++) 565 532 { 566 // Sort the population by Obj n533 // Sort the front population by the objective i 567 534 front.Sort((x, y) => x.Quality[i].CompareTo(y.Quality[i])); 568 535 objetiveMinn = front[0].Quality[i]; 569 536 objetiveMaxn = front[front.Count - 1].Quality[i]; 570 537 571 //Set de crowding distance538 //Set crowding distance for the current front 572 539 front[0].CrowdingDistance = double.PositiveInfinity; 573 540 front[size - 1].CrowdingDistance = double.PositiveInfinity; … … 627 594 // Initialize the list of individuals that i dominate and the number 628 595 // of individuals that dominate me 629 iDominate[p] = new List<int>(); 596 iDominate[p] = new List<int>(); 630 597 dominateMe[p] = 0; 631 598 } … … 635 602 for (int q = p + 1; q < tmpList.Count; q++) 636 603 { 637 flagDominate = compareDomination(tmpList[p].Quality, tmpList[q].Quality); 604 flagDominate = compareConstraintsViolation(tmpList[p], tmpList[q]); 605 if (flagDominate == 0) { 606 flagDominate = compareDomination(tmpList[p], tmpList[q]); 607 } 638 608 if (flagDominate == -1) 639 609 { … … 693 663 } 694 664 695 private int compareDomination( double[] solution1, double[]solution2)665 private int compareDomination(SolutionSet solution1, SolutionSet solution2) 696 666 { 697 667 int dominate1; // dominate1 indicates if some objective of solution1 … … 704 674 int flag; //stores the result of the comparison 705 675 676 // Test to determine whether at least a solution violates some constraint 677 if (needToCompareViolations(solution1, solution2)) 678 { 679 return compareConstraintsViolation(solution1, solution2); 680 } 681 706 682 // Equal number of violated constraints. Applying a dominance Test then 707 683 double value1, value2; 708 684 for (int i = 0; i < Problem.Objectives; i++) 709 685 { 710 value1 = solution1 [i];711 value2 = solution2 [i];686 value1 = solution1.Quality[i]; 687 value2 = solution2.Quality[i]; 712 688 if (value1 < value2) 713 689 { 714 690 flag = -1; 715 691 } 716 else if (value 1 > value2)692 else if (value2 < value1) 717 693 { 718 694 flag = 1; … … 743 719 } 744 720 return 1; // solution2 dominate 721 } 722 723 private bool needToCompareViolations(SolutionSet solution1, SolutionSet solution2) 724 { 725 bool needToCompare; 726 needToCompare = (solution1.OverallConstrainViolation < 0) || (solution2.OverallConstrainViolation < 0); 727 728 return needToCompare; 729 } 730 731 private int compareConstraintsViolation(SolutionSet solution1, SolutionSet solution2) 732 { 733 int result; 734 double overall1, overall2; 735 overall1 = solution1.OverallConstrainViolation; 736 overall2 = solution2.OverallConstrainViolation; 737 738 if ((overall1 < 0) && (overall2 < 0)) 739 { 740 if (overall1 > overall2) 741 { 742 result = -1; 743 } 744 else if (overall2 > overall1) 745 { 746 result = 1; 747 } 748 else 749 { 750 result = 0; 751 } 752 } 753 else if ((overall1 == 0) && (overall2 < 0)) 754 { 755 result = -1; 756 } 757 else if ((overall1 < 0) && (overall2 == 0)) 758 { 759 result = 1; 760 } 761 else 762 { 763 result = 0; 764 } 765 return result; 745 766 } 746 767 } -
branches/ichiriac/HeuristicLab.Algorithms.GDE3/SolutionSet.cs
r13749 r13849 12 12 private double crowdingDistance; 13 13 private double rank; 14 private double overallConstrainViolation; 15 public int populationSize; 16 17 //constructor set size equal with population size 18 public SolutionSet(int populationSize) 19 { 20 this.populationSize = populationSize; 21 } 14 22 15 23 public double CrowdingDistance … … 37 45 } 38 46 39 40 public int populationSize;41 public SolutionSet(int populationSize) {42 this.populationSize = populationSize;47 public double OverallConstrainViolation 48 { 49 get { return overallConstrainViolation; } 50 set { overallConstrainViolation = value; } 43 51 } 44 52
Note: See TracChangeset
for help on using the changeset viewer.