Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationPruningOperator.cs @ 12461

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

#2398: Skip root and start symbols when calculating impacts and replacement values in the pruning operators.

File size: 5.7 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) : base(original, cloner) { }
45    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationPruningOperator(this, cloner); }
46
47    [StorableConstructor]
48    protected SymbolicClassificationPruningOperator(bool deserializing) : base(deserializing) { }
49
50    public SymbolicClassificationPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator)
51      : base(impactValuesCalculator) {
52      Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName));
53    }
54
55    protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
56      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
57      var classificationProblemData = (IClassificationProblemData)problemData;
58      var rows = classificationProblemData.TrainingIndices;
59      model.RecalculateModelParameters(classificationProblemData, rows);
60      return model;
61    }
62
63    protected override double Evaluate(IDataAnalysisModel model) {
64      var classificationModel = (IClassificationModel)model;
65      var classificationProblemData = (IClassificationProblemData)ProblemDataParameter.ActualValue;
66      var rows = Enumerable.Range(FitnessCalculationPartitionParameter.ActualValue.Start, FitnessCalculationPartitionParameter.ActualValue.Size);
67
68      return Evaluate(classificationModel, classificationProblemData, rows);
69    }
70
71    private static double Evaluate(IClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
72      var estimatedValues = model.GetEstimatedClassValues(problemData.Dataset, rows);
73      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
74      OnlineCalculatorError errorState;
75      var quality = OnlineAccuracyCalculator.Calculate(targetValues, estimatedValues, out errorState);
76      if (errorState != OnlineCalculatorError.None) return double.NaN;
77      return quality;
78    }
79
80    public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicClassificationModelCreator modelCreator,
81      SymbolicClassificationSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
82      IClassificationProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows,
83      double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
84      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
85      var model = modelCreator.CreateSymbolicClassificationModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
86
87      var nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
88      double quality = Evaluate(model, problemData, rows);
89
90      for (int i = 0; i < nodes.Count; ++i) {
91        var node = nodes[i];
92        if (node is ConstantTreeNode) continue;
93
94        double impactValue, replacementValue;
95        impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
96
97        if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
98        if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
99
100        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
101        constantNode.Value = replacementValue;
102
103        ReplaceWithConstant(node, constantNode);
104        i += node.GetLength() - 1; // skip subtrees under the node that was folded
105
106        quality -= impactValue;
107      }
108      return model.SymbolicExpressionTree;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.