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

Last change on this file since 12720 was 12720, checked in by bburlacu, 6 years ago

#2359: Changed the impact calculators so that the quality value necessary for impacts calculation is calculated with a separate method. Refactored the CalculateImpactAndReplacementValues method to return the new quality in an out-parameter (adjusted method signature in interface accordingly). Added Evaluate method to the regression and classification pruning operators that re-evaluates the tree using the problem evaluator after pruning was performed.

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