1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
33 | public abstract class BottomUpPruningBase : ParameterizedNamedItem, IPruning {
|
---|
34 | private const string PruningStrengthParameterName = "PruningStrength";
|
---|
35 |
|
---|
36 | public IFixedValueParameter<DoubleValue> PruningStrengthParameter {
|
---|
37 | get { return (IFixedValueParameter<DoubleValue>)Parameters[PruningStrengthParameterName]; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public double PruningStrength {
|
---|
41 | get { return PruningStrengthParameter.Value.Value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region Constructors & Cloning
|
---|
45 | [StorableConstructor]
|
---|
46 | protected BottomUpPruningBase(bool deserializing) : base(deserializing) { }
|
---|
47 | protected BottomUpPruningBase(BottomUpPruningBase original, Cloner cloner) : base(original, cloner) { }
|
---|
48 | protected BottomUpPruningBase() {
|
---|
49 | Parameters.Add(new FixedValueParameter<DoubleValue>(PruningStrengthParameterName, "The strength of the pruning. Higher values force the algorithm to create simpler models", new DoubleValue(4.0)));
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | public abstract ILeafModel PruningLeafModel(ILeafModel leafModel);
|
---|
54 |
|
---|
55 | #region IPruning
|
---|
56 | public int MinLeafSize(IRegressionProblemData pd, ILeafModel leafModel) {
|
---|
57 | return PruningLeafModel(leafModel).MinLeafSize(pd);
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | internal void Prune(M5TreeModel treeModel, IReadOnlyList<int> trainingRows, IReadOnlyList<int> pruningRows, M5Parameters m5Params, CancellationToken cancellationToken) {
|
---|
62 | var globalStdDev = m5Params.Data.GetDoubleValues(m5Params.TargetVariable, trainingRows).StandardDeviationPop();
|
---|
63 |
|
---|
64 | Prune(treeModel.Root, trainingRows, pruningRows, m5Params, new Dictionary<M5NodeModel, int>(), new Dictionary<M5NodeModel, int>(), cancellationToken, globalStdDev);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private bool Prune(M5NodeModel node, IReadOnlyList<int> trainingRows, IReadOnlyList<int> pruningRows, M5Parameters m5Params,
|
---|
68 | Dictionary<M5NodeModel, int> modelComplexities, Dictionary<M5NodeModel, int> nodeComplexities,
|
---|
69 | CancellationToken cancellationToken, double globalStdDev) {
|
---|
70 | //build pruning model
|
---|
71 | int numModelParams;
|
---|
72 | var pruningModel = M5StaticUtilities.BuildModel(trainingRows, m5Params, PruningLeafModel(m5Params.LeafModel), cancellationToken, out numModelParams);
|
---|
73 | node.Model = pruningModel;
|
---|
74 | modelComplexities.Add(node, numModelParams);
|
---|
75 |
|
---|
76 | if (node.IsLeaf) {
|
---|
77 | nodeComplexities.Add(node, numModelParams);
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | //split training & pruning data
|
---|
82 | IReadOnlyList<int> leftTest, rightTest;
|
---|
83 | M5StaticUtilities.SplitRows(pruningRows, m5Params.Data, node.SplitAttribute, node.SplitValue, out leftTest, out rightTest);
|
---|
84 | IReadOnlyList<int> leftTraining, rightTraining;
|
---|
85 | M5StaticUtilities.SplitRows(trainingRows, m5Params.Data, node.SplitAttribute, node.SplitValue, out leftTraining, out rightTraining);
|
---|
86 |
|
---|
87 | //prune children frist
|
---|
88 | var lpruned = Prune(node.Left, leftTraining, leftTest, m5Params, modelComplexities, nodeComplexities, cancellationToken, globalStdDev);
|
---|
89 | var rpruned = Prune(node.Right, rightTraining, rightTest, m5Params, modelComplexities, nodeComplexities, cancellationToken, globalStdDev);
|
---|
90 | nodeComplexities.Add(node, nodeComplexities[node.Left] + nodeComplexities[node.Right] + 1);
|
---|
91 |
|
---|
92 | //TODO check if this reduces quality. It reduces training effort (consideraby for some pruningTypes)
|
---|
93 | if (!lpruned && !rpruned) return false;
|
---|
94 |
|
---|
95 | //check if pruning will happen on this node
|
---|
96 | if (!DecidePruneNode(node, m5Params, pruningRows, modelComplexities, nodeComplexities, globalStdDev)) return false;
|
---|
97 |
|
---|
98 | //convert to leafNode
|
---|
99 | ((IntValue)m5Params.Results[M5TreeModel.NumCurrentLeafsResultName].Value).Value -= node.EnumerateNodes().Count(x => x.IsLeaf) - 1;
|
---|
100 |
|
---|
101 | //TODO chack wether removal is beneficial
|
---|
102 | nodeComplexities.Remove(node.Left);
|
---|
103 | nodeComplexities.Remove(node.Right);
|
---|
104 | modelComplexities.Remove(node.Left);
|
---|
105 | modelComplexities.Remove(node.Right);
|
---|
106 |
|
---|
107 | node.ToLeaf();
|
---|
108 |
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private bool DecidePruneNode(M5NodeModel node, M5Parameters m5Params, IReadOnlyCollection<int> testRows,
|
---|
113 | IReadOnlyDictionary<M5NodeModel, int> modelComplexities, IReadOnlyDictionary<M5NodeModel, int> nodeComplexities,
|
---|
114 | double globalStdDev) {
|
---|
115 | if (testRows.Count == 0) return true;
|
---|
116 |
|
---|
117 | //create regressionProblemdata from pruning data
|
---|
118 | var vars = m5Params.AllowedInputVariables.Concat(new[] {m5Params.TargetVariable}).ToArray();
|
---|
119 | var reducedData = new Dataset(vars, vars.Select(x => m5Params.Data.GetDoubleValues(x, testRows).ToList()));
|
---|
120 | var pd = new RegressionProblemData(reducedData, m5Params.AllowedInputVariables, m5Params.TargetVariable);
|
---|
121 | pd.TrainingPartition.Start = pd.TrainingPartition.End = pd.TestPartition.Start = 0;
|
---|
122 | pd.TestPartition.End = reducedData.Rows;
|
---|
123 |
|
---|
124 | //evaluate combined sub nodes and pruning model
|
---|
125 | var rmsModel = node.Model.CreateRegressionSolution(pd).TestRootMeanSquaredError;
|
---|
126 | var rmsSubTree = node.CreateRegressionSolution(pd).TestRootMeanSquaredError;
|
---|
127 |
|
---|
128 | //weigh, compare and decide
|
---|
129 | var adjustedRmsModel = rmsModel * PruningFactor(pd.Dataset.Rows, modelComplexities[node]);
|
---|
130 | var adjustedRmsTree = rmsSubTree * PruningFactor(pd.Dataset.Rows, nodeComplexities[node.Left] + nodeComplexities[node.Right] + 1);
|
---|
131 | return adjustedRmsModel <= adjustedRmsTree;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private double PruningFactor(int noInstances, int noParams) {
|
---|
135 | return noInstances <= noParams ? 10.0 : (noInstances + PruningStrength * noParams) / (noInstances - PruningStrength * noParams);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | } |
---|