Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2326: Addressed the issues found by the reviewer.

File size: 7.1 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.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
32  [Item("SymbolicClassificationPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
33  [StorableClass]
34  public class SymbolicClassificationPhenotypicDiversityAnalyzer : SingleObjectivePopulationDiversityAnalyzer {
35    #region parameter names
36    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
37    private const string EvaluatedValuesParameterName = "EstimatedValues";
38    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
39    private const string ProblemDataParameterName = "ProblemData";
40    private const string ModelCreatorParameterName = "ModelCreator";
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 ILookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator> ModelCreatorParameter {
56      get { return (ILookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
57    }
58    public IValueLookupParameter<IClassificationProblemData> ProblemDataParameter {
59      get { return (IValueLookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
60    }
61    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
62      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
63    }
64    public IFixedValueParameter<BoolValue> UseClassValuesParameter {
65      get { return (IFixedValueParameter<BoolValue>)Parameters[UseClassValuesParameterName]; }
66    }
67    #endregion
68
69    #region properties
70    private bool UseClassValues {
71      get { return UseClassValuesParameter.Value.Value; }
72      set { UseClassValuesParameter.Value.Value = value; }
73    }
74    #endregion
75
76    public SymbolicClassificationPhenotypicDiversityAnalyzer() {
77      SimilarityCalculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator { SolutionVariableName = "SymbolicExpressionTree", QualityVariableName = "Quality" };
78      #region add parameters
79      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
81      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
82      Parameters.Add(new ValueLookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
83      Parameters.Add(new LookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator>(ModelCreatorParameterName, "The model creator for creating discriminant function classification models."));
84      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."));
85      Parameters.Add(new FixedValueParameter<BoolValue>(UseClassValuesParameterName, "Specifies whether the raw estimated values of the tree or the corresponding class values should be used for similarity calculation.", new BoolValue(false)));
86      #endregion
87    }
88
89    [StorableConstructor]
90    protected SymbolicClassificationPhenotypicDiversityAnalyzer(bool deserializing)
91      : base(deserializing) {
92    }
93
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new SymbolicClassificationPhenotypicDiversityAnalyzer(this, cloner);
96    }
97
98    private SymbolicClassificationPhenotypicDiversityAnalyzer(SymbolicClassificationPhenotypicDiversityAnalyzer original, Cloner cloner)
99      : base(original, cloner) {
100    }
101
102    public override IOperation Apply() {
103      var trees = SymbolicExpressionTreeParameter.ActualValue;
104      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
105      var problemData = ProblemDataParameter.ActualValue;
106      var ds = ProblemDataParameter.ActualValue.Dataset;
107      var rows = ProblemDataParameter.ActualValue.TrainingIndices;
108      var modelCreator = ModelCreatorParameter.ActualValue;
109      var estimationLimits = EstimationLimitsParameter.ActualValue;
110      var evaluatedValues = new ItemArray<DoubleArray>(trees.Length);
111      for (int i = 0; i < trees.Length; ++i) {
112        var model = (IDiscriminantFunctionClassificationModel)modelCreator.CreateSymbolicDiscriminantFunctionClassificationModel(trees[i], interpreter, estimationLimits.Lower, estimationLimits.Upper);
113        model.RecalculateModelParameters(problemData, rows);
114        var values = UseClassValues ? model.GetEstimatedClassValues(ds, rows) : model.GetEstimatedValues(ds, rows);
115        evaluatedValues[i] = new DoubleArray(values.ToArray());
116      }
117      EvaluatedValuesParameter.ActualValue = evaluatedValues;
118      return base.Apply();
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.