Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeDiversityAnalyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationPhenotypicDiversityAnalyzer.cs @ 12029

Last change on this file since 12029 was 12029, checked in by bburlacu, 9 years ago

#2326: Implemented analyzers

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
33  [Item("SymbolicDataAnalysisPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
34  [StorableClass]
35  public class SymbolicClassificationPhenotypicDiversityAnalyzer : SingleObjectivePopulationDiversityAnalyzer {
36    #region parameter names
37    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
38    private const string EvaluatedValuesParameterName = "EstimatedValues";
39    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
40    private const string ProblemDataParameterName = "ProblemData";
41    private const string EstimationLimitsParameterName = "EstimationLimits";
42    private const string UseClassValuesParameterName = "UseClassValues";
43    #endregion
44
45    #region parameter properties
46    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
47      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
48    }
49    private IScopeTreeLookupParameter<DoubleArray> EvaluatedValuesParameter {
50      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[EvaluatedValuesParameterName]; }
51    }
52    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
53      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
54    }
55    public IValueLookupParameter<IClassificationProblemData> ProblemDataParameter {
56      get { return (IValueLookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
57    }
58    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
59      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
60    }
61    public IFixedValueParameter<BoolValue> UseClassValuesParameter {
62      get { return (IFixedValueParameter<BoolValue>)Parameters[UseClassValuesParameterName]; }
63    }
64    #endregion
65
66    #region properties
67    bool UseClassValues {
68      get { return UseClassValuesParameter.Value.Value; }
69      set { UseClassValuesParameter.Value.Value = value; }
70    }
71    #endregion
72
73    public SymbolicClassificationPhenotypicDiversityAnalyzer() {
74      SimilarityCalculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator { SolutionVariableName = "SymbolicExpressionTree", QualityVariableName = "Quality" };
75      #region add parameters
76      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
77      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
78      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
79      Parameters.Add(new ValueLookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
80      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
81      Parameters.Add(new FixedValueParameter<BoolValue>(UseClassValuesParameterName,
82        "Specifies whether the raw estimated values of the tree or the corresponding class values should be used for similarity calculation.", new BoolValue(false)));
83      #endregion
84    }
85
86    [StorableConstructor]
87    protected SymbolicClassificationPhenotypicDiversityAnalyzer(bool deserializing)
88      : base(deserializing) {
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new SymbolicClassificationPhenotypicDiversityAnalyzer(this, cloner);
93    }
94
95    private SymbolicClassificationPhenotypicDiversityAnalyzer(SymbolicClassificationPhenotypicDiversityAnalyzer original, Cloner cloner)
96      : base(original, cloner) {
97    }
98
99    public override IOperation Apply() {
100      var trees = SymbolicExpressionTreeParameter.ActualValue;
101      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
102      var ds = ProblemDataParameter.ActualValue.Dataset;
103      var rows = ProblemDataParameter.ActualValue.TrainingIndices;
104
105      if (UseClassValues) {
106        var problemData = ProblemDataParameter.ActualValue;
107        var evaluatedValues = new ItemArray<DoubleArray>();
108        for (int i = 0; i < trees.Length; ++i) {
109          var t = trees[i];
110          double[] classValues, thresholds;
111          var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray();
112          AccuracyMaximizationThresholdCalculator.CalculateThresholds(problemData, estimatedValues,
113            problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices),
114            out classValues, out thresholds);
115          var estimatedClassValues = new List<double>();
116          foreach (var x in estimatedValues) {
117            int classIndex = 0;
118            // find first threshold value which is larger than x => class index = threshold index + 1
119            for (int j = 0; j < thresholds.Length; j++) {
120              if (x > thresholds[j]) classIndex++;
121              else break;
122            }
123            estimatedClassValues.Add(classValues.ElementAt(classIndex - 1));
124          }
125          evaluatedValues[i] = new DoubleArray(estimatedClassValues.ToArray());
126        }
127        EvaluatedValuesParameter.ActualValue = evaluatedValues;
128      } else {
129        var evaluatedValues = new ItemArray<DoubleArray>(trees.Select(t => new DoubleArray(interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray())));
130        EvaluatedValuesParameter.ActualValue = evaluatedValues;
131      }
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.