Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/17 09:22:48 (7 years ago)
Author:
gkronber
Message:

#2690: merged r14345, r14346, r14368 from trunk to stable

Location:
stable/HeuristicLab.Algorithms.DataAnalysis/3.4
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs

    r15061 r15127  
    270270        } else {
    271271          // otherwise we produce a regression solution
    272           Results.Add(new Result("Solution", new RegressionSolution(model, problemData)));
     272          Results.Add(new Result("Solution", new GradientBoostedTreesSolution(model, problemData)));
    273273        }
    274274      }
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesSolution.cs

    r14186 r15127  
    2020#endregion
    2121
    22 using System.Collections.Generic;
    23 using System.Linq;
    2422using HeuristicLab.Common;
    2523using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeModel.cs

    r14186 r15127  
    2828using HeuristicLab.Common;
    2929using HeuristicLab.Core;
     30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3132using HeuristicLab.Problems.DataAnalysis;
     33using HeuristicLab.Problems.DataAnalysis.Symbolic;
     34using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    3235
    3336namespace HeuristicLab.Algorithms.DataAnalysis {
     
    210213    }
    211214
     215    /// <summary>
     216    /// Transforms the tree model to a symbolic regression solution
     217    /// </summary>
     218    /// <param name="problemData"></param>
     219    /// <returns>A new symbolic regression solution which matches the tree model</returns>
     220    public ISymbolicRegressionSolution CreateSymbolicRegressionSolution(IRegressionProblemData problemData) {
     221      var rootSy = new ProgramRootSymbol();
     222      var startSy = new StartSymbol();
     223      var varCondSy = new VariableCondition() { IgnoreSlope = true };
     224      var constSy = new Constant();
     225
     226      var startNode = startSy.CreateTreeNode();
     227      startNode.AddSubtree(CreateSymbolicRegressionTreeRecursive(tree, 0, varCondSy, constSy));
     228      var rootNode = rootSy.CreateTreeNode();
     229      rootNode.AddSubtree(startNode);
     230      var model = new SymbolicRegressionModel(TargetVariable, new SymbolicExpressionTree(rootNode), new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
     231      return model.CreateRegressionSolution(problemData);
     232    }
     233
     234    private ISymbolicExpressionTreeNode CreateSymbolicRegressionTreeRecursive(TreeNode[] treeNodes, int nodeIdx, VariableCondition varCondSy, Constant constSy) {
     235      var curNode = treeNodes[nodeIdx];
     236      if (curNode.VarName == TreeNode.NO_VARIABLE) {
     237        var node = (ConstantTreeNode)constSy.CreateTreeNode();
     238        node.Value = curNode.Val;
     239        return node;
     240      } else {
     241        var node = (VariableConditionTreeNode)varCondSy.CreateTreeNode();
     242        node.VariableName = curNode.VarName;
     243        node.Threshold = curNode.Val;
     244
     245        var left = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.LeftIdx, varCondSy, constSy);
     246        var right = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.RightIdx, varCondSy, constSy);
     247        node.AddSubtree(left);
     248        node.AddSubtree(right);
     249        return node;
     250      }
     251    }
     252
     253
    212254    private string TreeToString(int idx, string part) {
    213255      var n = tree[idx];
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestClassificationSolution.cs

    r14186 r15127  
    3030  public interface IRandomForestClassificationSolution : IClassificationSolution {
    3131    new IRandomForestModel Model { get; }
     32    int NumberOfTrees { get; }
    3233  }
    3334}
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestModel.cs

    r14822 r15127  
    2020#endregion
    2121
    22 using HeuristicLab.Optimization;
     22using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2323using HeuristicLab.Problems.DataAnalysis;
    24 using HeuristicLab.Core;
    25 using System.Collections.Generic;
     24
    2625
    2726namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3029  /// </summary>
    3130  public interface IRandomForestModel : IConfidenceRegressionModel, IClassificationModel {
     31    int NumberOfTrees { get; }
     32    ISymbolicExpressionTree ExtractTree(int treeIdx); // returns a specific tree from the random forest as a ISymbolicRegressionModel
    3233  }
    3334}
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestRegressionSolution.cs

    r14822 r15127  
    2020#endregion
    2121
    22 using HeuristicLab.Optimization;
    2322using HeuristicLab.Problems.DataAnalysis;
    24 using HeuristicLab.Core;
     23using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2524
    2625namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3029  public interface IRandomForestRegressionSolution : IConfidenceRegressionSolution {
    3130    new IRandomForestModel Model { get; }
     31    int NumberOfTrees { get; }
    3232  }
    3333}
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs

    r14186 r15127  
    3838    }
    3939
     40    public int NumberOfTrees {
     41      get { return Model.NumberOfTrees; }
     42    }
     43
    4044    [StorableConstructor]
    4145    private RandomForestClassificationSolution(bool deserializing) : base(deserializing) { }
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs

    r14822 r15127  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829using HeuristicLab.Problems.DataAnalysis;
     30using HeuristicLab.Problems.DataAnalysis.Symbolic;
    2931
    3032namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4951    }
    5052
     53    public int NumberOfTrees {
     54      get { return nTrees; }
     55    }
    5156
    5257    // instead of storing the data of the model itself
     
    6469    [Storable]
    6570    private double m;
    66 
    6771
    6872    [StorableConstructor]
     
    197201    }
    198202
     203    public ISymbolicExpressionTree ExtractTree(int treeIdx) {
     204      var rf = RandomForest;
     205      // hoping that the internal representation of alglib is stable
     206
     207      // TREE FORMAT
     208      // W[Offs]      -   size of sub-array (for the tree)
     209      //     node info:
     210      // W[K+0]       -   variable number        (-1 for leaf mode)
     211      // W[K+1]       -   threshold              (class/value for leaf node)
     212      // W[K+2]       -   ">=" branch index      (absent for leaf node)
     213
     214      // skip irrelevant trees
     215      int offset = 0;
     216      for (int i = 0; i < treeIdx - 1; i++) {
     217        offset = offset + (int)Math.Round(rf.innerobj.trees[offset]);
     218      }
     219
     220      var constSy = new Constant();
     221      var varCondSy = new VariableCondition() { IgnoreSlope = true };
     222
     223      var node = CreateRegressionTreeRec(rf.innerobj.trees, offset, offset + 1, constSy, varCondSy);
     224
     225      var startNode = new StartSymbol().CreateTreeNode();
     226      startNode.AddSubtree(node);
     227      var root = new ProgramRootSymbol().CreateTreeNode();
     228      root.AddSubtree(startNode);
     229      return new SymbolicExpressionTree(root);
     230    }
     231
     232    private ISymbolicExpressionTreeNode CreateRegressionTreeRec(double[] trees, int offset, int k, Constant constSy, VariableCondition varCondSy) {
     233
     234      // alglib source for evaluation of one tree (dfprocessinternal)
     235      // offs = 0
     236      //
     237      // Set pointer to the root
     238      //
     239      // k = offs + 1;
     240      //
     241      // //
     242      // // Navigate through the tree
     243      // //
     244      // while (true) {
     245      //   if ((double)(df.trees[k]) == (double)(-1)) {
     246      //     if (df.nclasses == 1) {
     247      //       y[0] = y[0] + df.trees[k + 1];
     248      //     } else {
     249      //       idx = (int)Math.Round(df.trees[k + 1]);
     250      //       y[idx] = y[idx] + 1;
     251      //     }
     252      //     break;
     253      //   }
     254      //   if ((double)(x[(int)Math.Round(df.trees[k])]) < (double)(df.trees[k + 1])) {
     255      //     k = k + innernodewidth;
     256      //   } else {
     257      //     k = offs + (int)Math.Round(df.trees[k + 2]);
     258      //   }
     259      // }
     260
     261      if ((double)(trees[k]) == (double)(-1)) {
     262        var constNode = (ConstantTreeNode)constSy.CreateTreeNode();
     263        constNode.Value = trees[k + 1];
     264        return constNode;
     265      } else {
     266        var condNode = (VariableConditionTreeNode)varCondSy.CreateTreeNode();
     267        condNode.VariableName = AllowedInputVariables[(int)Math.Round(trees[k])];
     268        condNode.Threshold = trees[k + 1];
     269        condNode.Slope = double.PositiveInfinity;
     270
     271        var left = CreateRegressionTreeRec(trees, offset, k + 3, constSy, varCondSy);
     272        var right = CreateRegressionTreeRec(trees, offset, offset + (int)Math.Round(trees[k + 2]), constSy, varCondSy);
     273
     274        condNode.AddSubtree(left); // not 100% correct because interpreter uses: if(x <= thres) left() else right() and RF uses if(x < thres) left() else right() (see above)
     275        condNode.AddSubtree(right);
     276        return condNode;
     277      }
     278    }
     279
    199280
    200281    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs

    r14822 r15127  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2526using HeuristicLab.Problems.DataAnalysis;
     27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2628
    2729namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3638      get { return (IRandomForestModel)base.Model; }
    3739      set { base.Model = value; }
     40    }
     41
     42    public int NumberOfTrees {
     43      get { return Model.NumberOfTrees; }
    3844    }
    3945
Note: See TracChangeset for help on using the changeset viewer.