Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeEncoding/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationPruningOperator.cs @ 12417

Last change on this file since 12417 was 12189, checked in by bburlacu, 10 years ago

#2359: Implemented improvements

File size: 5.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
33  [StorableClass]
34  [Item("SymbolicClassificationPruningOperator", "An operator which prunes symbolic classificaton trees.")]
35  public class SymbolicClassificationPruningOperator : SymbolicDataAnalysisExpressionPruningOperator {
36    private const string ModelCreatorParameterName = "ModelCreator";
37
38    #region parameter properties
39    public ILookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
40      get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
41    }
42    #endregion
43
44    protected SymbolicClassificationPruningOperator(SymbolicClassificationPruningOperator original, Cloner cloner)
45      : base(original, cloner) {
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new SymbolicClassificationPruningOperator(this, cloner);
50    }
51
52    [StorableConstructor]
53    protected SymbolicClassificationPruningOperator(bool deserializing) : base(deserializing) { }
54
55    public SymbolicClassificationPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator)
56      : base(impactValuesCalculator) {
57      Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName));
58    }
59
60    protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
61      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
62      var classificationProblemData = (IClassificationProblemData)problemData;
63      var rows = classificationProblemData.TrainingIndices;
64      model.RecalculateModelParameters(classificationProblemData, rows);
65      return model;
66    }
67
68    protected override double Evaluate(IDataAnalysisModel model) {
69      var classificationModel = (IClassificationModel)model;
70      var classificationProblemData = (IClassificationProblemData)ProblemData;
71      var trainingIndices = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
72
73      return Evaluate(classificationModel, classificationProblemData, trainingIndices);
74    }
75
76    private static double Evaluate(IClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
77      var estimatedValues = model.GetEstimatedClassValues(problemData.Dataset, rows);
78      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
79      OnlineCalculatorError errorState;
80      var quality = OnlineAccuracyCalculator.Calculate(targetValues, estimatedValues, out errorState);
81      if (errorState != OnlineCalculatorError.None) return double.NaN;
82      return quality;
83    }
84
85    public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicClassificationModelCreator modelCreator,
86      SymbolicClassificationSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
87      IClassificationProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows,
88      double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
89      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
90      var model = modelCreator.CreateSymbolicClassificationModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
91
92      var nodes = clonedTree.IterateNodesPrefix().ToList();
93      double quality = Evaluate(model, problemData, rows);
94
95      for (int i = 0; i < nodes.Count; ++i) {
96        var node = nodes[i];
97        if (node is ConstantTreeNode) continue;
98
99        double impactValue, replacementValue;
100        impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
101
102        if (pruneOnlyZeroImpactNodes) {
103          if (!impactValue.IsAlmost(0.0)) continue;
104        } else if (nodeImpactThreshold < impactValue) {
105          continue;
106        }
107
108        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
109        constantNode.Value = replacementValue;
110
111        ReplaceWithConstant(node, constantNode);
112        i += node.GetLength() - 1; // skip subtrees under the node that was folded
113
114        quality -= impactValue;
115      }
116      return model.SymbolicExpressionTree;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.