Changeset 12658 for branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees
- Timestamp:
- 07/07/15 17:42:24 (9 years ago)
- Location:
- branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeBuilder.cs
r12632 r12658 207 207 Debug.Assert(endIdx < internalIdx.Length); 208 208 209 // transform the leaf node into an internal node210 tree[f.ParentNodeIdx].VarName = f.SplittingVariable;211 tree[f.ParentNodeIdx].Val = f.SplittingThreshold;212 213 209 // split partition into left and right 214 210 int splitIdx; … … 218 214 219 215 // create two leaf nodes (and enqueue best splits for both) 220 tree[f.ParentNodeIdx].LeftIdx = CreateLeafNode(startIdx, splitIdx, lineSearch); 221 tree[f.ParentNodeIdx].RightIdx = CreateLeafNode(splitIdx + 1, endIdx, lineSearch); 216 var leftTreeIdx = CreateLeafNode(startIdx, splitIdx, lineSearch); 217 var rightTreeIdx = CreateLeafNode(splitIdx + 1, endIdx, lineSearch); 218 219 // overwrite existing leaf node with an internal node 220 tree[f.ParentNodeIdx] = new RegressionTreeModel.TreeNode(f.SplittingVariable, f.SplittingThreshold, leftTreeIdx, rightTreeIdx); 222 221 } 223 222 } … … 226 225 // returns the index of the newly created tree node 227 226 private int CreateLeafNode(int startIdx, int endIdx, LineSearchFunc lineSearch) { 228 tree[curTreeNodeIdx].VarName = RegressionTreeModel.TreeNode.NO_VARIABLE; 229 tree[curTreeNodeIdx].Val = lineSearch(internalIdx, startIdx, endIdx); 227 // write a leaf node 228 var val = lineSearch(internalIdx, startIdx, endIdx); 229 tree[curTreeNodeIdx] = new RegressionTreeModel.TreeNode(RegressionTreeModel.TreeNode.NO_VARIABLE, val); 230 230 231 231 EnqueuePartitionSplit(curTreeNodeIdx, startIdx, endIdx); -
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeModel.cs
r12635 r12658 21 21 #endregion 22 22 23 using System; 23 24 using System.Collections.Generic; 25 using System.Globalization; 24 26 using System.Linq; 27 using System.Text; 25 28 using HeuristicLab.Common; 26 29 using HeuristicLab.Core; … … 31 34 [StorableClass] 32 35 [Item("RegressionTreeModel", "Represents a decision tree for regression.")] 33 public class RegressionTreeModel : NamedItem, IRegressionModel {36 public sealed class RegressionTreeModel : NamedItem, IRegressionModel { 34 37 35 38 // trees are represented as a flat array 36 publicstruct TreeNode {39 internal struct TreeNode { 37 40 public readonly static string NO_VARIABLE = string.Empty; 38 public string VarName { get; set; } // name of the variable for splitting or NO_VARIABLE if terminal node39 public double Val { get; set; } // threshold40 public int LeftIdx { get; set; }41 public int RightIdx { get; set; }42 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 43 56 public override int GetHashCode() { 44 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 } 45 70 } 46 71 } 47 72 48 73 [Storable] 49 p ublicreadonly TreeNode[] tree;74 private readonly TreeNode[] tree; 50 75 51 76 [StorableConstructor] 52 77 private RegressionTreeModel(bool serializing) : base(serializing) { } 53 78 // cloning ctor 54 p ublicRegressionTreeModel(RegressionTreeModel original, Cloner cloner)79 private RegressionTreeModel(RegressionTreeModel original, Cloner cloner) 55 80 : base(original, cloner) { 56 81 this.tree = original.tree; // shallow clone, tree must be readonly 57 82 } 58 83 59 publicRegressionTreeModel(TreeNode[] tree)84 internal RegressionTreeModel(TreeNode[] tree) 60 85 : base("RegressionTreeModel", "Represents a decision tree for regression.") { 61 86 this.tree = tree; … … 84 109 return new RegressionSolution(this, new RegressionProblemData(problemData)); 85 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 86 128 } 87 129
Note: See TracChangeset
for help on using the changeset viewer.