[13368] | 1 | #region License Information
|
---|
[12590] | 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 |
|
---|
[12658] | 23 | using System;
|
---|
[12372] | 24 | using System.Collections.Generic;
|
---|
[13030] | 25 | using System.Collections.ObjectModel;
|
---|
[12658] | 26 | using System.Globalization;
|
---|
[12332] | 27 | using System.Linq;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
[12590] | 33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[13368] | 34 | [StorableClass("2B90568C-D6C1-48EE-86F4-62389FB60E0F")]
|
---|
[12332] | 35 | [Item("RegressionTreeModel", "Represents a decision tree for regression.")]
|
---|
[12658] | 36 | public sealed class RegressionTreeModel : NamedItem, IRegressionModel {
|
---|
[12332] | 37 |
|
---|
[12699] | 38 | // trees are represented as a flat array
|
---|
[12658] | 39 | internal struct TreeNode {
|
---|
[12699] | 40 | public readonly static string NO_VARIABLE = null;
|
---|
[12332] | 41 |
|
---|
[12658] | 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
|
---|
[12590] | 56 | public override int GetHashCode() {
|
---|
[12632] | 57 | return LeftIdx ^ RightIdx ^ Val.GetHashCode();
|
---|
[12372] | 58 | }
|
---|
[12658] | 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 | LeftIdx.Equals(other.LeftIdx) &&
|
---|
[12663] | 65 | RightIdx.Equals(other.RightIdx) &&
|
---|
| 66 | EqualStrings(VarName, other.VarName);
|
---|
[12658] | 67 | } else {
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[12663] | 71 |
|
---|
| 72 | private bool EqualStrings(string a, string b) {
|
---|
| 73 | return (a == null && b == null) ||
|
---|
| 74 | (a != null && b != null && a.Equals(b));
|
---|
| 75 | }
|
---|
[12332] | 76 | }
|
---|
| 77 |
|
---|
[12699] | 78 | // not storable!
|
---|
| 79 | private TreeNode[] tree;
|
---|
| 80 |
|
---|
[12332] | 81 | [Storable]
|
---|
[12699] | 82 | // to prevent storing the references to data caches in nodes
|
---|
[13030] | 83 | // TODO seemingly it is bad (performance-wise) to persist tuples (tuples are used as keys in a dictionary)
|
---|
[12699] | 84 | private Tuple<string, double, int, int>[] SerializedTree {
|
---|
| 85 | get { return tree.Select(t => Tuple.Create(t.VarName, t.Val, t.LeftIdx, t.RightIdx)).ToArray(); }
|
---|
| 86 | set { this.tree = value.Select(t => new TreeNode(t.Item1, t.Item2, t.Item3, t.Item4)).ToArray(); }
|
---|
| 87 | }
|
---|
[12332] | 88 |
|
---|
| 89 | [StorableConstructor]
|
---|
| 90 | private RegressionTreeModel(bool serializing) : base(serializing) { }
|
---|
| 91 | // cloning ctor
|
---|
[12658] | 92 | private RegressionTreeModel(RegressionTreeModel original, Cloner cloner)
|
---|
[12332] | 93 | : base(original, cloner) {
|
---|
[12699] | 94 | if (original.tree != null) {
|
---|
| 95 | this.tree = new TreeNode[original.tree.Length];
|
---|
| 96 | Array.Copy(original.tree, this.tree, this.tree.Length);
|
---|
| 97 | }
|
---|
[12332] | 98 | }
|
---|
| 99 |
|
---|
[12658] | 100 | internal RegressionTreeModel(TreeNode[] tree)
|
---|
[12372] | 101 | : base("RegressionTreeModel", "Represents a decision tree for regression.") {
|
---|
[12332] | 102 | this.tree = tree;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[13030] | 105 | private static double GetPredictionForRow(TreeNode[] t, ReadOnlyCollection<double>[] columnCache, int nodeIdx, int row) {
|
---|
[12699] | 106 | while (nodeIdx != -1) {
|
---|
| 107 | var node = t[nodeIdx];
|
---|
| 108 | if (node.VarName == TreeNode.NO_VARIABLE)
|
---|
| 109 | return node.Val;
|
---|
| 110 |
|
---|
[13030] | 111 | if (columnCache[nodeIdx][row] <= node.Val)
|
---|
[12699] | 112 | nodeIdx = node.LeftIdx;
|
---|
| 113 | else
|
---|
| 114 | nodeIdx = node.RightIdx;
|
---|
| 115 | }
|
---|
| 116 | throw new InvalidOperationException("Invalid tree in RegressionTreeModel");
|
---|
[12332] | 117 | }
|
---|
| 118 |
|
---|
| 119 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 120 | return new RegressionTreeModel(this, cloner);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[12589] | 123 | public IEnumerable<double> GetEstimatedValues(IDataset ds, IEnumerable<int> rows) {
|
---|
[12699] | 124 | // lookup columns for variableNames in one pass over the tree to speed up evaluation later on
|
---|
[13030] | 125 | ReadOnlyCollection<double>[] columnCache = new ReadOnlyCollection<double>[tree.Length];
|
---|
| 126 |
|
---|
[12699] | 127 | for (int i = 0; i < tree.Length; i++) {
|
---|
| 128 | if (tree[i].VarName != TreeNode.NO_VARIABLE) {
|
---|
[13030] | 129 | columnCache[i] = ds.GetReadOnlyDoubleValues(tree[i].VarName);
|
---|
[12699] | 130 | }
|
---|
| 131 | }
|
---|
[13030] | 132 | return rows.Select(r => GetPredictionForRow(tree, columnCache, 0, r));
|
---|
[12332] | 133 | }
|
---|
| 134 |
|
---|
| 135 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 136 | return new RegressionSolution(this, new RegressionProblemData(problemData));
|
---|
| 137 | }
|
---|
[12658] | 138 |
|
---|
| 139 | // mainly for debugging
|
---|
| 140 | public override string ToString() {
|
---|
| 141 | return TreeToString(0, "");
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private string TreeToString(int idx, string part) {
|
---|
| 145 | var n = tree[idx];
|
---|
| 146 | if (n.VarName == TreeNode.NO_VARIABLE) {
|
---|
| 147 | return string.Format(CultureInfo.InvariantCulture, "{0} -> {1:F}{2}", part, n.Val, Environment.NewLine);
|
---|
| 148 | } else {
|
---|
| 149 | return
|
---|
| 150 | TreeToString(n.LeftIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} <= {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val))
|
---|
| 151 | + TreeToString(n.RightIdx, string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} > {3:F}", part, string.IsNullOrEmpty(part) ? "" : " and ", n.VarName, n.Val));
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[12332] | 154 | }
|
---|
| 155 | }
|
---|