[11025] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12012] | 4 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11025] | 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 |
|
---|
[12189] | 24 | using System.Collections.Generic;
|
---|
[11025] | 25 | using System.Linq;
|
---|
[10469] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[12189] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[10469] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
| 32 | [StorableClass]
|
---|
| 33 | [Item("SymbolicRegressionPruningOperator", "An operator which prunes symbolic regression trees.")]
|
---|
| 34 | public class SymbolicRegressionPruningOperator : SymbolicDataAnalysisExpressionPruningOperator {
|
---|
| 35 | protected SymbolicRegressionPruningOperator(SymbolicRegressionPruningOperator original, Cloner cloner)
|
---|
| 36 | : base(original, cloner) {
|
---|
| 37 | }
|
---|
| 38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 39 | return new SymbolicRegressionPruningOperator(this, cloner);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | protected SymbolicRegressionPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
| 44 |
|
---|
[12189] | 45 | public SymbolicRegressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator)
|
---|
| 46 | : base(impactValuesCalculator) {
|
---|
[10469] | 47 | }
|
---|
| 48 |
|
---|
[12189] | 49 | protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
|
---|
| 50 | return new SymbolicRegressionModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
[10469] | 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override double Evaluate(IDataAnalysisModel model) {
|
---|
| 54 | var regressionModel = (IRegressionModel)model;
|
---|
[12358] | 55 | var regressionProblemData = (IRegressionProblemData)ProblemDataParameter.ActualValue;
|
---|
| 56 | var rows = Enumerable.Range(FitnessCalculationPartitionParameter.ActualValue.Start, FitnessCalculationPartitionParameter.ActualValue.Size);
|
---|
[12189] | 57 | return Evaluate(regressionModel, regressionProblemData, rows);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | private static double Evaluate(IRegressionModel model, IRegressionProblemData problemData,
|
---|
| 61 | IEnumerable<int> rows) {
|
---|
| 62 | var estimatedValues = model.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values
|
---|
| 63 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
[10469] | 64 | OnlineCalculatorError errorState;
|
---|
| 65 | var quality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);
|
---|
| 66 | if (errorState != OnlineCalculatorError.None) return double.NaN;
|
---|
| 67 | return quality;
|
---|
| 68 | }
|
---|
[12189] | 69 |
|
---|
| 70 | public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, SymbolicRegressionSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IRegressionProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows, double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
|
---|
| 71 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
| 72 | var model = new SymbolicRegressionModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
[12461] | 73 | var nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); // skip the nodes corresponding to the ProgramRootSymbol and the StartSymbol
|
---|
[12189] | 74 | double quality = Evaluate(model, problemData, rows);
|
---|
| 75 |
|
---|
| 76 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
| 77 | var node = nodes[i];
|
---|
| 78 | if (node is ConstantTreeNode) continue;
|
---|
| 79 |
|
---|
| 80 | double impactValue, replacementValue;
|
---|
| 81 | impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
|
---|
| 82 |
|
---|
[12358] | 83 | if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
|
---|
| 84 | if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
|
---|
[12189] | 85 |
|
---|
| 86 | var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
|
---|
| 87 | constantNode.Value = replacementValue;
|
---|
| 88 |
|
---|
| 89 | ReplaceWithConstant(node, constantNode);
|
---|
| 90 | i += node.GetLength() - 1; // skip subtrees under the node that was folded
|
---|
| 91 |
|
---|
| 92 | quality -= impactValue;
|
---|
| 93 | }
|
---|
| 94 | return model.SymbolicExpressionTree;
|
---|
| 95 | }
|
---|
[10469] | 96 | }
|
---|
| 97 | }
|
---|