Changeset 16649
 Timestamp:
 03/01/19 12:51:59 (3 months ago)
 Location:
 branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4
 Files:

 1 added
 3 edited
Legend:
 Unmodified
 Added
 Removed

branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/HeuristicLab.Algorithms.MOEAD3.4.csproj
r16630 r16649 171 171 </ItemGroup> 172 172 <ItemGroup> 173 <None Include="app.config" /> 173 174 <None Include="HeuristicLab.snk" /> 174 175 <None Include="packages.config" /> 
branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADAlgorithmBase.cs
r16630 r16649 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Problems.DataAnalysis; 31 using HeuristicLab.Problems.TestFunctions.MultiObjective; 31 32 using HeuristicLab.Random; 32 33 using System; 33 34 using System.Collections.Generic; 35 using System.Drawing; 34 36 using System.Linq; 35 37 using CancellationToken = System.Threading.CancellationToken; … … 40 42 public abstract class MOEADAlgorithmBase : BasicAlgorithm { 41 43 #region data members 44 [StorableType("C04DB21A316F42109CA7A915BE8EBC96")] 42 45 protected enum NeighborType { NEIGHBOR, POPULATION } 46 47 [StorableType("FE35F480E52245E0A22999E61DA7B8BC")] 43 48 // TCHE = Chebyshev (Tchebyshev) 44 49 // PBI = Penaltybased boundary intersection … … 58 63 59 64 [Storable] 60 protected I List<IMOEADSolution>solutions;65 protected IMOEADSolution[] solutions; 61 66 62 67 [Storable] … … 64 69 65 70 [Storable] 66 protected List<IMOEADSolution>population;67 68 [Storable] 69 protected List<IMOEADSolution>offspringPopulation;70 71 [Storable] 72 protected List<IMOEADSolution>jointPopulation;71 protected IMOEADSolution[] population; 72 73 [Storable] 74 protected IMOEADSolution[] offspringPopulation; 75 76 [Storable] 77 protected IMOEADSolution[] jointPopulation; 73 78 74 79 [Storable] … … 232 237 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer())); 233 238 Parameters.Add(new ValueParameter<IntValue>(MaximumEvaluatedSolutionsParameterName, "The maximum number of evaluated solutions (approximately).", new IntValue(100_000))); 234 Parameters.Add(new ValueParameter<IRandom>(RandomParameterName, new MersenneTwister()));239 Parameters.Add(new ValueParameter<IRandom>(RandomParameterName, new FastRandom())); 235 240 Parameters.Add(new FixedValueParameter<IntValue>(NeighbourSizeParameterName, new IntValue(20))); 236 241 Parameters.Add(new FixedValueParameter<IntValue>(MaximumNumberOfReplacedSolutionsParameterName, new IntValue(2))); … … 271 276 272 277 if (original.population != null) { 273 population = original.population.Select(cloner.Clone).To List();278 population = original.population.Select(cloner.Clone).ToArray(); 274 279 } 275 280 276 281 if (original.offspringPopulation != null) { 277 offspringPopulation = original.offspringPopulation.Select(cloner.Clone).To List();282 offspringPopulation = original.offspringPopulation.Select(cloner.Clone).ToArray(); 278 283 } 279 284 280 285 if (original.jointPopulation != null) { 281 jointPopulation = original.jointPopulation.Select(x => cloner.Clone(x)).To List();286 jointPopulation = original.jointPopulation.Select(x => cloner.Clone(x)).ToArray(); 282 287 } 283 288 … … 300 305 301 306 [StorableConstructor] 302 protected MOEADAlgorithmBase(StorableConstructorFlag _) : base(_) { }307 protected MOEADAlgorithmBase(StorableConstructorFlag deserializing) : base(deserializing) { } 303 308 #endregion 304 309 … … 309 314 var dimensions = maximization.Length; 310 315 var populationSize = PopulationSize.Value; 311 population = new List<IMOEADSolution>(populationSize);316 population = new IMOEADSolution[populationSize]; 312 317 313 318 var parentScope = executionContext.Scope; … … 329 334 solution.Qualities[j] = maximization[j] ? 1  qualities[j] : qualities[j]; 330 335 } 331 population .Add(solution);336 population[i] = solution; 332 337 } 333 338 } … … 351 356 IdealPoint.UpdateIdeal(population); 352 357 353 //NadirPoint = Enumerable.Repeat(double.MinValue, dimensions).ToArray();354 NadirPoint = new double[dimensions];358 NadirPoint = Enumerable.Repeat(double.MinValue, dimensions).ToArray(); 359 //NadirPoint = new double[dimensions]; 355 360 NadirPoint.UpdateNadir(population); 356 361 … … 469 474 protected void UpdateNeighbourHood(IRandom random, IMOEADSolution individual, int subProblemId, NeighborType neighbourType) { 470 475 int replacedSolutions = 0; 471 int size = neighbourType == NeighborType.NEIGHBOR ? NeighbourSize : population. Count;476 int size = neighbourType == NeighborType.NEIGHBOR ? NeighbourSize : population.Length; 472 477 473 478 foreach (var i in Enumerable.Range(0, size).Shuffle(random)) { … … 491 496 492 497 private double CalculateFitness(double[] qualities, double[] lambda) { 493 double fitness;494 498 int dim = qualities.Length; 495 499 switch (functionType) { … … 510 514 } 511 515 512 fitness = maxFun; 513 return fitness; 516 return maxFun; 514 517 } 515 518 case FunctionType.AGG: { … … 518 521 sum += lambda[n] * qualities[n]; 519 522 } 520 521 fitness = sum; 522 return fitness; 523 return sum; 523 524 } 524 525 case FunctionType.PBI: { … … 540 541 } 541 542 d2 = Math.Sqrt(d2); 542 543 fitness = (d1 + theta * d2); 544 return fitness; 543 return d1 + theta * d2; 545 544 } 546 545 default: { … … 562 561 563 562 protected void UpdateParetoFronts() { 564 bool dominates(Point2D<double> x, Point2D<double> y) => x.X <= y.X && x.Y <= y.Y; 565 // get all nondominated points 566 var points = population.Select(x => new Point2D<double>(Math.Round(x.Qualities[0], 6), Math.Round(x.Qualities[1], 6))).OrderBy(_ => _.X).ThenBy(_ => _.Y).ToArray(); 567 var dominated = new bool[points.Length]; 568 569 for (int i = 0; i < points.Length; ++i) { 570 if (dominated[i]) { continue; } 571 for (int j = 0; j < points.Length; ++j) { 572 if (i == j) { continue; } 573 if (dominated[j]) { continue; } 574 dominated[j] = dominates(points[i], points[j]); 575 } 576 } 577 578 var pf = Enumerable.Range(0, dominated.Length).Where(x => !dominated[x]).Select(x => points[x]); 563 var qualities = population.Select(x => Enumerable.Range(0, NadirPoint.Length).Select(i => x.Qualities[i] / NadirPoint[i]).ToArray()).ToArray(); 564 var maximization = Enumerable.Repeat(false, IdealPoint.Length).ToArray(); // MOEA/D minimizes everything internally\ 565 var pf = DominationCalculator<IMOEADSolution>.CalculateBestParetoFront(population, qualities, maximization); 566 567 var n = (int)EnumerableExtensions.BinomialCoefficient(IdealPoint.Length, 2); 568 var hypervolumes = new DoubleMatrix(n == 1 ? 1 : n + 1, 2) { ColumnNames = new[] { "PF hypervolume", "PF size" } }; 569 hypervolumes[0, 0] = Hypervolume.Calculate(pf.Select(x => x.Item2), Enumerable.Repeat(1d, NadirPoint.Length).ToArray(), maximization); 570 hypervolumes[0, 1] = pf.Count; 571 var elementNames = new List<string>() { "Pareto Front" }; 572 573 ResultCollection results; 574 if (Results.ContainsKey("Hypervolume Analysis")) { 575 results = (ResultCollection)Results["Hypervolume Analysis"].Value; 576 } else { 577 results = new ResultCollection(); 578 Results.AddOrUpdateResult("Hypervolume Analysis", results); 579 } 579 580 580 581 ScatterPlot sp; 581 if (!Results.ContainsKey("Pareto Front")) { 582 sp = new ScatterPlot(); 583 sp.Rows.Add(new ScatterPlotDataRow("Pareto Front", "", pf) { VisualProperties = { PointSize = 5 } }); 584 Results.AddOrUpdateResult("Pareto Front", sp); 585 } else { 586 sp = (ScatterPlot)Results["Pareto Front"].Value; 587 sp.Rows["Pareto Front"].Points.Replace(pf); 588 } 582 if (IdealPoint.Length == 2) { 583 var points = pf.Select(x => new Point2D<double>(x.Item2[0], x.Item2[1])); 584 var r = OnlinePearsonsRCalculator.Calculate(points.Select(x => x.X), points.Select(x => x.Y), out OnlineCalculatorError error); 585 if (error != OnlineCalculatorError.None) { r = double.NaN; } 586 var resultName = "Pareto Front Analysis "; 587 if (!results.ContainsKey(resultName)) { 588 sp = new ScatterPlot() { 589 VisualProperties = { 590 XAxisMinimumAuto = false, XAxisMinimumFixedValue = 0d, XAxisMaximumAuto = false, XAxisMaximumFixedValue = 1d, 591 YAxisMinimumAuto = false, YAxisMinimumFixedValue = 0d, YAxisMaximumAuto = false, YAxisMaximumFixedValue = 1d 592 } 593 }; 594 sp.Rows.Add(new ScatterPlotDataRow(resultName, "", points) { VisualProperties = { PointSize = 8 } }); 595 results.AddOrUpdateResult(resultName, sp); 596 } else { 597 sp = (ScatterPlot)results[resultName].Value; 598 sp.Rows[resultName].Points.Replace(points); 599 } 600 sp.Name = $"Dimensions [0, 1], correlation: {r.ToString("N2")}"; 601 } else if (IdealPoint.Length > 2) { 602 var indices = Enumerable.Range(0, IdealPoint.Length).ToArray(); 603 var visualProperties = new ScatterPlotDataRowVisualProperties { PointSize = 8, Color = Color.LightGray }; 604 var combinations = indices.Combinations(2).ToArray(); 605 var maximization2d = new[] { false, false }; 606 var solutions2d = pf.Select(x => x.Item1).ToArray(); 607 for (int i = 0; i < combinations.Length; ++i) { 608 var c = combinations[i].ToArray(); 609 610 // calculate the hypervolume in the 2d coordinate space 611 var reference2d = new[] { 1d, 1d }; 612 var qualities2d = pf.Select(x => new[] { x.Item2[c[0]], x.Item2[c[1]] }).ToArray(); 613 var pf2d = DominationCalculator<IMOEADSolution>.CalculateBestParetoFront(solutions2d, qualities2d, maximization2d); 614 615 hypervolumes[i + 1, 0] = pf2d.Count > 0 ? Hypervolume.Calculate(pf2d.Select(x => x.Item2), reference2d, maximization2d) : 0d; 616 hypervolumes[i + 1, 1] = pf2d.Count; 617 618 var resultName = $"Pareto Front Analysis [{c[0]}, {c[1]}]"; 619 elementNames.Add(resultName); 620 621 var points = pf.Select(x => new Point2D<double>(x.Item2[c[0]], x.Item2[c[1]])); 622 var pf2dPoints = pf2d.Select(x => new Point2D<double>(x.Item2[0], x.Item2[1])); 623 624 if (!results.ContainsKey(resultName)) { 625 sp = new ScatterPlot() { 626 VisualProperties = { 627 XAxisMinimumAuto = false, XAxisMinimumFixedValue = 0d, XAxisMaximumAuto = false, XAxisMaximumFixedValue = 1d, 628 YAxisMinimumAuto = false, YAxisMinimumFixedValue = 0d, YAxisMaximumAuto = false, YAxisMaximumFixedValue = 1d 629 } 630 }; 631 sp.Rows.Add(new ScatterPlotDataRow("Pareto Front", "", points) { VisualProperties = visualProperties }); 632 sp.Rows.Add(new ScatterPlotDataRow($"Pareto Front [{c[0]}, {c[1]}]", "", pf2dPoints) { VisualProperties = { PointSize = 10, Color = Color.OrangeRed } }); 633 results.AddOrUpdateResult(resultName, sp); 634 } else { 635 sp = (ScatterPlot)results[resultName].Value; 636 sp.Rows["Pareto Front"].Points.Replace(points); 637 sp.Rows[$"Pareto Front [{c[0]}, {c[1]}]"].Points.Replace(pf2dPoints); 638 } 639 var r = OnlinePearsonsRCalculator.Calculate(points.Select(x => x.X), points.Select(x => x.Y), out OnlineCalculatorError error); 640 if (error != OnlineCalculatorError.None) { r = double.NaN; } 641 sp.Name = $"Pareto Front [{c[0]}, {c[1]}], correlation: {r.ToString("N2")}"; 642 } 643 } 644 hypervolumes.RowNames = elementNames; 645 results.AddOrUpdateResult("Hypervolumes", hypervolumes); 589 646 } 590 647 … … 668 725 669 726 public void ClearState() { 670 if (solutions != null) { 671 solutions.Clear(); 672 } 673 if (population != null) { 674 population.Clear(); 675 } 676 if (offspringPopulation != null) { 677 offspringPopulation.Clear(); 678 } 679 if (jointPopulation != null) { 680 jointPopulation.Clear(); 681 } 727 solutions = null; 728 population = null; 729 offspringPopulation = null; 730 jointPopulation = null; 682 731 lambda = null; 683 732 neighbourhood = null; 
branches/2987_MOEAD_Algorithm/HeuristicLab.Algorithms.MOEAD/3.4/MOEADUtil.cs
r16630 r16649 175 175 public static void UpdateIdeal(this double[] idealPoint, double[] point) { 176 176 for (int i = 0; i < point.Length; ++i) { 177 if (double.IsInfinity(point[i])  double.IsNaN(point[i])) { 178 continue; 179 } 180 177 181 if (idealPoint[i] > point[i]) { 178 182 idealPoint[i] = point[i]; … … 183 187 public static void UpdateNadir(this double[] nadirPoint, double[] point) { 184 188 for (int i = 0; i < point.Length; ++i) { 189 if (double.IsInfinity(point[i])  double.IsNaN(point[i])) { 190 continue; 191 } 192 185 193 if (nadirPoint[i] < point[i]) { 186 194 nadirPoint[i] = point[i];
Note: See TracChangeset
for help on using the changeset viewer.