Changeset 14044 for branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Analyzers/NormalizedHypervolumeAnalyzer.cs
- Timestamp:
- 07/12/16 18:20:50 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Analyzers/NormalizedHypervolumeAnalyzer.cs
r14030 r14044 32 32 namespace HeuristicLab.Problems.MultiObjectiveTestFunctions { 33 33 [StorableClass] 34 [Item(" GenerationalDistanceAnalyzer", "Computes the enclosed Hypervolume between the current front and a given reference Point")]34 [Item("NormalizedHypervolumeAnalyzer", "Computes the enclosed Hypervolume between the current front and a given reference Point")] 35 35 public class NormalizedHypervolumeAnalyzer : MOTFAnalyzer { 36 [StorableHook(HookType.AfterDeserialization)]37 private void AfterDeserialization() {38 }39 [StorableConstructor]40 protected NormalizedHypervolumeAnalyzer(bool deserializing) : base(deserializing) { }41 public NormalizedHypervolumeAnalyzer(NormalizedHypervolumeAnalyzer original, Cloner cloner) : base(original, cloner) { }42 public override IDeepCloneable Clone(Cloner cloner) {43 return new NormalizedHypervolumeAnalyzer(this, cloner);44 }45 46 47 #region Names48 36 private const string bestKnownFront = "BestKnownFront Zitzler"; 49 37 private const string resultsHV = "NormalizedHypervolume"; … … 51 39 52 40 private const string bestknownHV = "NormalizedBestKnownHyperVolume"; 53 #endregion54 41 55 #region parameters56 42 public IValueParameter<DoubleMatrix> OptimalFrontParameter { 57 get { 58 return (IValueParameter<DoubleMatrix>)Parameters[bestKnownFront]; 59 } 43 get { return (IValueParameter<DoubleMatrix>)Parameters[bestKnownFront]; } 60 44 } 61 45 62 46 public IValueParameter<DoubleValue> BestKnownHyperVolumeParameter { 63 get { 64 return (IValueParameter<DoubleValue>)Parameters[bestknownHV]; 65 } 66 set { 67 Parameters[bestknownHV].ActualValue = value; 68 } 47 get { return (IValueParameter<DoubleValue>)Parameters[bestknownHV]; } 69 48 } 70 #endregion71 49 72 public NormalizedHypervolumeAnalyzer() { 73 if (!Parameters.ContainsKey(bestKnownFront)) Parameters.Add(new ValueParameter<DoubleMatrix>(bestKnownFront, "The true / best known pareto front")); 74 if (!Parameters.ContainsKey(bestknownHV)) Parameters.Add(new ValueParameter<DoubleValue>(bestknownHV, "The currently best known hypervolume")); 50 51 public NormalizedHypervolumeAnalyzer() 52 : base() { 53 Parameters.Add(new ValueParameter<DoubleMatrix>(bestKnownFront, "The true / best known pareto front")); 54 Parameters.Add(new ValueParameter<DoubleValue>(bestknownHV, "The currently best known hypervolume")); 75 55 } 56 57 [StorableConstructor] 58 protected NormalizedHypervolumeAnalyzer(bool deserializing) : base(deserializing) { } 59 protected NormalizedHypervolumeAnalyzer(NormalizedHypervolumeAnalyzer original, Cloner cloner) : base(original, cloner) { } 60 public override IDeepCloneable Clone(Cloner cloner) { 61 return new NormalizedHypervolumeAnalyzer(this, cloner); 62 } 63 76 64 77 65 private void RegisterEventHandlers() { … … 83 71 } 84 72 85 public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) { 86 if (qualities == null || qualities.Length < 1) return; 73 public override IOperation Apply() { 74 var results = ResultsParameter.ActualValue; 75 var qualities = QualitiesParameter.ActualValue; 76 var testFunction = TestFunctionParameter.ActualValue; 87 77 int objectives = qualities[0].Length; 78 88 79 double best = BestKnownHyperVolumeParameter.Value.Value; 89 80 if (OptimalFrontParameter.Value == null || OptimalFrontParameter.Value.Rows < 1 || OptimalFrontParameter.Value.Columns != qualities[0].Length) { 90 return ; // too pareto front nonexistant or with wrong number of dimensions81 return base.Apply(); // too pareto front nonexistant or with wrong number of dimensions 91 82 } 92 83 93 IEnumerable<double[]> front = NonDominatedSelect.selectNonDominatedVectors(qualities , TestFunctionParameter.ActualValue.Maximization(objectives), true);84 IEnumerable<double[]> front = NonDominatedSelect.selectNonDominatedVectors(qualities.Select(q => q.ToArray()), testFunction.Maximization(objectives), true); 94 85 95 86 if (!results.ContainsKey(resultsHV)) results.Add(new Result(resultsHV, typeof(DoubleValue))); … … 101 92 } 102 93 103 bool[] maximization = TestFunctionParameter.ActualValue.Maximization(objectives);94 bool[] maximization = testFunction.Maximization(objectives); 104 95 double[] invPoint = GetBestPoint(OptimalFrontParameter.Value, maximization); 105 96 double[] refPoint = GetWorstPoint(OptimalFrontParameter.Value, maximization); … … 107 98 double hv = front.Any() ? Hypervolume.Calculate(front, refPoint, maximization) / normalization : 0; 108 99 109 if (Double.IsNaN(best)) best = hv; else best = Math.Max(best, hv); 110 double diff; 111 diff = best - hv; 112 if (diff == 0) { 113 BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities)); 100 if (double.IsNaN(best) || best < hv) { 101 best = hv; 102 BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities.Select(q => q.ToArray()).ToArray())); 114 103 } 115 104 116 105 results[resultsHV].Value = new DoubleValue(hv); 117 results[resultsDist].Value = new DoubleValue(diff);118 106 results[bestknownHV].Value = new DoubleValue(best); 107 results[resultsDist].Value = new DoubleValue(best - hv); 119 108 109 return base.Apply(); 120 110 } 121 111 122 private double[] GetWorstPoint(DoubleMatrix value, bool[] maximization) { 112 113 private static double[] GetWorstPoint(DoubleMatrix value, bool[] maximization) { 123 114 bool[] invMax = new bool[maximization.Length]; 124 115 int i = 0; … … 129 120 } 130 121 131 private double[] GetBestPoint(DoubleMatrix value, bool[] maximization) {122 private static double[] GetBestPoint(DoubleMatrix value, bool[] maximization) { 132 123 double[] res = new double[maximization.Length]; 133 124 for (int i = 0; i < maximization.Length; i++) {
Note: See TracChangeset
for help on using the changeset viewer.