1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
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 | #endregion
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
34 | [StorableClass]
|
---|
35 | [Item("RegressionTreeModel", "Represents a decision tree for regression.")]
|
---|
36 | public sealed class RegressionTreeModel : NamedItem, IRegressionModel {
|
---|
37 |
|
---|
38 | // trees are represented as a flat array
|
---|
39 | internal struct TreeNode {
|
---|
40 | public readonly static string NO_VARIABLE = string.Empty;
|
---|
41 |
|
---|
42 | public TreeNode(string varName, double val, int leftIdx = -1, int rightIdx = -1)
|
---|
43 | : this() {
|
---|
44 | VarName = varName;
|
---|
45 | Val = val;
|
---|
46 | LeftIdx = leftIdx;
|
---|
47 | RightIdx = rightIdx;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public string VarName { get; private set; } // name of the variable for splitting or NO_VARIABLE if terminal node
|
---|
51 | public double Val { get; private set; } // threshold
|
---|
52 | public int LeftIdx { get; private set; }
|
---|
53 | public int RightIdx { get; private set; }
|
---|
54 |
|
---|
55 | // necessary because the default implementation of GetHashCode for structs in .NET would only return the hashcode of val here
|
---|
56 | public override int GetHashCode() {
|
---|
57 | return LeftIdx ^ RightIdx ^ Val.GetHashCode();
|
---|
58 | }
|
---|
59 | // necessary because of GetHashCode override
|
---|
60 | public override bool Equals(object obj) {
|
---|
61 | if (obj is TreeNode) {
|
---|
62 | var other = (TreeNode)obj;
|
---|
63 | return Val.Equals(other.Val) &&
|
---|
64 | VarName.Equals(other.VarName) &&
|
---|
65 | LeftIdx.Equals(other.LeftIdx) &&
|
---|
66 | RightIdx.Equals(other.RightIdx);
|
---|
67 | } else {
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | private readonly TreeNode[] tree;
|
---|
75 |
|
---|
76 | [StorableConstructor]
|
---|
77 | private RegressionTreeModel(bool serializing) : base(serializing) { }
|
---|
78 | // cloning ctor
|
---|
79 | private RegressionTreeModel(RegressionTreeModel original, Cloner cloner)
|
---|
80 | : base(original, cloner) {
|
---|
81 | this.tree = original.tree; // shallow clone, tree must be readonly
|
---|
82 | }
|
---|
83 |
|
---|
84 | internal RegressionTreeModel(TreeNode[] tree)
|
---|
85 | : base("RegressionTreeModel", "Represents a decision tree for regression.") {
|
---|
86 | this.tree = tree;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private static double GetPredictionForRow(TreeNode[] t, int nodeIdx, IDataset ds, int row) {
|
---|
90 | var node = t[nodeIdx];
|
---|
91 | if (node.VarName == TreeNode.NO_VARIABLE)
|
---|
92 | return node.Val;
|
---|
93 | // TODO: many calls to GetDoubleValue are slow because of the dictionary lookup in Dataset (see ticket #2417)
|
---|
94 | else if (ds.GetDoubleValue(node.VarName, row) <= node.Val)
|
---|
95 | return GetPredictionForRow(t, node.LeftIdx, ds, row);
|
---|
96 | else
|
---|
97 | return GetPredictionForRow(t, node.RightIdx, ds, row);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
101 | return new RegressionTreeModel(this, cloner);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public IEnumerable<double> GetEstimatedValues(IDataset ds, IEnumerable<int> rows) {
|
---|
105 | return rows.Select(r => GetPredictionForRow(tree, 0, ds, r));
|
---|
106 | }
|
---|
107 |
|
---|
108 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
109 | return new RegressionSolution(this, new RegressionProblemData(problemData));
|
---|
110 | }
|
---|
111 |
|
---|
112 | // mainly for debugging
|
---|
113 | public override string ToString() {
|
---|
114 | return TreeToString(0, "");
|
---|
115 | }
|
---|
116 |
|
---|
117 | private string TreeToString(int idx, string part) {
|
---|
118 | var n = tree[idx];
|
---|
119 | if (n.VarName == TreeNode.NO_VARIABLE) {
|
---|
120 | return string.Format(CultureInfo.InvariantCulture, "{0} -> {1:F}{2}", part, n.Val, Environment.NewLine);
|
---|
121 | } else {
|
---|
122 | return
|
---|
123 | TreeToString(n.LeftIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} <= {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val))
|
---|
124 | + TreeToString(n.RightIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} > {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val));
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|