Ticket #2944: CustomCAnalyzer.cs

File CustomCAnalyzer.cs, 4.0 KB (added by abeham, 2 years ago)

Crowding Analyzer - contributed by David Schmaranzer

Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.TestFunctions.MultiObjective;
31
32
33namespace HeuristicLab.Analysis
34{
35    [StorableClass]
36  [Item("CustomCAnalyzer", "The mean crowding distance for each point of the Front (see Multi-Objective Performance Metrics - Shodhganga for more information)")]
37  public class CustomCAnalyzer : SingleSuccessorOperator, IAnalyzer, IMultiObjectiveOperator
38    {
39        public virtual bool EnabledByDefault { get { return true; } }
40   
41    //parameters from MOTFAnalyzer
42    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
43        get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
44    }
45
46    public ILookupParameter<ResultCollection> ResultsParameter {
47        get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
48    }
49   
50    public ILookupParameter<DoubleMatrix> BestKnownFrontParameter {
51        get { return (ILookupParameter<DoubleMatrix>)Parameters["BestKnownFront"]; }
52    }
53   
54    //old ones
55    public ILookupParameter<DoubleMatrix> BoundsParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
57    }
58
59    public IResultParameter<DoubleValue> CrowdingResultParameter {
60      get { return (IResultParameter<DoubleValue>)Parameters["Crowding"]; }
61    }
62
63    [StorableConstructor]
64    protected CustomCAnalyzer(bool deserializing) : base(deserializing) { }
65    public CustomCAnalyzer(CustomCAnalyzer original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new CustomCAnalyzer(this, cloner);
70    }
71
72    public CustomCAnalyzer() {
73     
74      //add parameters from MOTFAnalyzer
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
76      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
77      Parameters.Add(new LookupParameter<DoubleMatrix>("BestKnownFront", "The currently best known Pareto front"));
78
79      //old ones
80      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds",
81        "The bounds of the solution given as either one line for all variables or a line for each variable. The first column specifies lower bound, the second upper bound."));
82      Parameters.Add(new ResultParameter<DoubleValue>("Crowding", "The average corwding value of all points (excluding infinities)"));
83      CrowdingResultParameter.DefaultValue = new DoubleValue(double.NaN);
84
85    }
86
87    public override IOperation Apply() {
88      var qualities = QualitiesParameter.ActualValue;
89      var bounds = BoundsParameter.ActualValue;
90
91      var crowdingDistance = Crowding.Calculate(qualities.Select(x => x.ToArray()), bounds.CloneAsMatrix());
92      CrowdingResultParameter.ActualValue.Value = crowdingDistance;
93
94      return base.Apply();
95    }
96  }
97}