source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs @ 12359

Last change on this file since 12359 was 12359, checked in by mkommend, 6 years ago

#2359: Removed commented code from pruning analyzer.

File size: 9.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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  [StorableClass]
35  [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")]
36  public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator, ISymbolicExpressionTreeOperator {
37    #region parameter names
38    private const string ProblemDataParameterName = "ProblemData";
39    private const string SymbolicDataAnalysisModelParameterName = "SymbolicDataAnalysisModel";
40    private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator";
41    private const string PrunedSubtreesParameterName = "PrunedSubtrees";
42    private const string PrunedTreesParameterName = "PrunedTrees";
43    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
44    private const string NodeImpactThresholdParameterName = "ImpactThreshold";
45    private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
46    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; // the tree to be pruned
47    private const string QualityParameterName = "Quality"; // the quality
48    private const string EstimationLimitsParameterName = "EstimationLimits";
49    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
50    #endregion
51
52    #region parameter properties
53    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
54      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
55    }
56    public ILookupParameter<DoubleValue> QualityParameter {
57      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
58    }
59    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
60      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
61    }
62    public IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator> ImpactValuesCalculatorParameter {
63      get { return (IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>)Parameters[ImpactValuesCalculatorParameterName]; }
64    }
65    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
66      get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
67    }
68    public ILookupParameter<IntValue> PrunedSubtreesParameter {
69      get { return (ILookupParameter<IntValue>)Parameters[PrunedSubtreesParameterName]; }
70    }
71    public ILookupParameter<IntValue> PrunedTreesParameter {
72      get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; }
73    }
74    public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {
75      get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
76    }
77    public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {
78      get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }
79    }
80    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
81      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
82    }
83    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
84      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
85    }
86    #endregion
87
88    #region properties
89    public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator {
90      get { return ImpactValuesCalculatorParameter.Value; }
91      set { ImpactValuesCalculatorParameter.Value = value; }
92    }
93    public bool PruneOnlyZeroImpactNodes {
94      get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
95      set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
96    }
97    public double NodeImpactThreshold {
98      get { return NodeImpactThresholdParameter.Value.Value; }
99      set { NodeImpactThresholdParameter.Value.Value = value; }
100    }
101    #endregion
102
103    [StorableConstructor]
104    protected SymbolicDataAnalysisExpressionPruningOperator(bool deserializing) : base(deserializing) { }
105    protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner)
106      : base(original, cloner) { }
107
108    protected SymbolicDataAnalysisExpressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator) {
109      #region add parameters
110      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
111      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName));
112      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName));
113      Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced."));
114      Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned."));
115      Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Specify whether or not only zero impact nodes should be pruned."));
116      Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "Specifies an impact value threshold below which nodes should be pruned."));
117      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
118      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
119      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
120      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName));
121      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, impactValuesCalculator));
122      #endregion
123    }
124
125    protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits);
126
127    protected abstract double Evaluate(IDataAnalysisModel model);
128
129    public override IOperation Apply() {
130      var tree = SymbolicExpressionTreeParameter.ActualValue;
131      var problemData = ProblemDataParameter.ActualValue;
132      var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue;
133      var estimationLimits = EstimationLimitsParameter.ActualValue;
134      var interpreter = InterpreterParameter.ActualValue;
135
136      var model = CreateModel(tree, interpreter, problemData, estimationLimits);
137      var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
138      var rows = Enumerable.Range(fitnessCalculationPartition.Start, fitnessCalculationPartition.Size);
139      var prunedSubtrees = 0;
140      var prunedTrees = 0;
141
142      double quality = Evaluate(model);
143
144      for (int i = 0; i < nodes.Count; ++i) {
145        var node = nodes[i];
146        if (node is ConstantTreeNode) continue;
147
148        double impactValue, replacementValue;
149        ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
150
151        if (PruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
152        if (!PruneOnlyZeroImpactNodes && impactValue > NodeImpactThreshold) continue;
153
154        var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
155        constantNode.Value = replacementValue;
156
157        ReplaceWithConstant(node, constantNode);
158        i += node.GetLength() - 1; // skip subtrees under the node that was folded
159
160        quality -= impactValue;
161        prunedSubtrees++;
162      }
163
164      if (prunedSubtrees > 0) prunedTrees = 1;
165      PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees);
166      PrunedTreesParameter.ActualValue = new IntValue(prunedTrees);
167      QualityParameter.ActualValue.Value = quality;
168
169      return base.Apply();
170    }
171
172    protected static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
173      var parent = original.Parent;
174      var i = parent.IndexOfSubtree(original);
175      parent.RemoveSubtree(i);
176      parent.InsertSubtree(i, replacement);
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.