Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2326: Moved phenotypic diversity analyzers one level up (since they can be applied to both single- and multiobjective problems). Added wiring in the multiobjective problems. Changed base class to SolutionSimilarityCalculator and adjusted analyzers.

File size: 7.6 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.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
34  [Item("SymbolicClassificationPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic distance between trees")]
35  [StorableClass]
36  public class SymbolicClassificationPhenotypicDiversityAnalyzer : PopulationSimilarityAnalyzer {
37    #region parameter names
38    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
39    private const string EvaluatedValuesParameterName = "EstimatedValues";
40    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
41    private const string ProblemDataParameterName = "ProblemData";
42    private const string ModelCreatorParameterName = "ModelCreator";
43    private const string EstimationLimitsParameterName = "EstimationLimits";
44    private const string UseClassValuesParameterName = "UseClassValues";
45    #endregion
46
47    #region parameter properties
48    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
49      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
50    }
51    private IScopeTreeLookupParameter<DoubleArray> EvaluatedValuesParameter {
52      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[EvaluatedValuesParameterName]; }
53    }
54    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
55      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
56    }
57    public ILookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator> ModelCreatorParameter {
58      get { return (ILookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
59    }
60    public IValueLookupParameter<IClassificationProblemData> ProblemDataParameter {
61      get { return (IValueLookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
62    }
63    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
64      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
65    }
66    public IFixedValueParameter<BoolValue> UseClassValuesParameter {
67      get { return (IFixedValueParameter<BoolValue>)Parameters[UseClassValuesParameterName]; }
68    }
69    #endregion
70
71    #region properties
72    public bool UseClassValues {
73      get { return UseClassValuesParameter.Value.Value; }
74      set { UseClassValuesParameter.Value.Value = value; }
75    }
76    #endregion
77
78    public SymbolicClassificationPhenotypicDiversityAnalyzer(IEnumerable<ISolutionSimilarityCalculator> validSimilarityCalculators)
79      : base(validSimilarityCalculators) {
80      #region add parameters
81      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
82      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
83      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
84      Parameters.Add(new ValueLookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
85      Parameters.Add(new LookupParameter<ISymbolicDiscriminantFunctionClassificationModelCreator>(ModelCreatorParameterName, "The model creator for creating discriminant function classification models."));
86      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."));
87      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)));
88      #endregion
89
90      UpdateCounterParameter.ActualName = "PhenotypicDiversityAnalyzerUpdateCounter";
91    }
92
93    [StorableConstructor]
94    protected SymbolicClassificationPhenotypicDiversityAnalyzer(bool deserializing)
95      : base(deserializing) {
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new SymbolicClassificationPhenotypicDiversityAnalyzer(this, cloner);
100    }
101
102    private SymbolicClassificationPhenotypicDiversityAnalyzer(SymbolicClassificationPhenotypicDiversityAnalyzer original, Cloner cloner)
103      : base(original, cloner) {
104    }
105
106    public override IOperation Apply() {
107      int updateInterval = UpdateIntervalParameter.Value.Value;
108      IntValue updateCounter = UpdateCounterParameter.ActualValue;
109
110      if (updateCounter == null) {
111        updateCounter = new IntValue(updateInterval);
112        UpdateCounterParameter.ActualValue = updateCounter;
113      }
114
115      if (updateCounter.Value == updateInterval) {
116        var trees = SymbolicExpressionTreeParameter.ActualValue;
117        var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
118        var problemData = ProblemDataParameter.ActualValue;
119        var ds = ProblemDataParameter.ActualValue.Dataset;
120        var rows = ProblemDataParameter.ActualValue.TrainingIndices;
121        var modelCreator = ModelCreatorParameter.ActualValue;
122        var estimationLimits = EstimationLimitsParameter.ActualValue;
123        var evaluatedValues = new ItemArray<DoubleArray>(trees.Length);
124        for (int i = 0; i < trees.Length; ++i) {
125          var model = (IDiscriminantFunctionClassificationModel)modelCreator.CreateSymbolicDiscriminantFunctionClassificationModel(trees[i], interpreter, estimationLimits.Lower, estimationLimits.Upper);
126          model.RecalculateModelParameters(problemData, rows);
127          var values = UseClassValues ? model.GetEstimatedClassValues(ds, rows) : model.GetEstimatedValues(ds, rows);
128          evaluatedValues[i] = new DoubleArray(values.ToArray());
129        }
130        EvaluatedValuesParameter.ActualValue = evaluatedValues;
131      }
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.