Ticket #2944: CustomSAnalyzer.cs

File CustomSAnalyzer.cs, 3.4 KB (added by abeham, 2 years ago)

Spacing 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
32namespace HeuristicLab.Analysis {
33  [StorableClass]
34  [Item("CustomSAnalyzer", "The spacing of the current front (see Multi-Objective Performance Metrics - Shodhganga for more information)")]
35  public class CustomSAnalyzer : SingleSuccessorOperator, IAnalyzer, IMultiObjectiveOperator
36  {
37    public virtual bool EnabledByDefault { get { return true; } }
38
39    //parameters from MOTFAnalyzer
40    public IScopeTreeLookupParameter<DoubleArray> QualitiesParameter {
41        get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters["Qualities"]; }
42    }
43
44    public ILookupParameter<ResultCollection> ResultsParameter {
45        get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
46    }
47   
48    public ILookupParameter<DoubleMatrix> BestKnownFrontParameter {
49        get { return (ILookupParameter<DoubleMatrix>)Parameters["BestKnownFront"]; }
50    }
51   
52    //old ones
53    public IResultParameter<DoubleValue> SpacingResultParameter {
54      get { return (IResultParameter<DoubleValue>)Parameters["Spacing"]; }
55    }
56    [StorableConstructor]
57    protected CustomSAnalyzer(bool deserializing) : base(deserializing) { }
58
59
60    protected CustomSAnalyzer(CustomSAnalyzer original, Cloner cloner) : base(original, cloner) { }
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new CustomSAnalyzer(this, cloner);
63    }
64
65    public CustomSAnalyzer() {
66      //add parameters from MOTFAnalyzer
67      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector."));
68      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
69      Parameters.Add(new LookupParameter<DoubleMatrix>("BestKnownFront", "The currently best known Pareto front"));
70   
71      //old one(s)
72      Parameters.Add(new ResultParameter<DoubleValue>("Spacing", "The spacing of the current front"));
73      SpacingResultParameter.DefaultValue = new DoubleValue(double.NaN);
74    }
75
76    public override IOperation Apply() {
77      var qualities = QualitiesParameter.ActualValue;
78      var spacing = Spacing.Calculate(qualities.Select(q => q.ToArray()));
79      SpacingResultParameter.ActualValue.Value = spacing;
80
81      return base.Apply();
82    }
83  }
84}