Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/13/21 10:55:09 (3 years ago)
Author:
gkronber
Message:

#3087: merged r17784:18004 from trunk to branch to prepare for trunk reintegration (fixed a conflict in CrossValidation.cs)

Location:
branches/3087_Ceres_Integration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3087_Ceres_Integration

  • branches/3087_Ceres_Integration/HeuristicLab.Algorithms.DataAnalysis

  • branches/3087_Ceres_Integration/HeuristicLab.Algorithms.DataAnalysis/3.4

  • branches/3087_Ceres_Integration/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs

    r17180 r18006  
    3131
    3232namespace HeuristicLab.Algorithms.DataAnalysis {
    33   [StorableType("9C797DF0-1169-4381-A732-6DAB90802839")]
     33  [StorableType("55412E08-DAD4-4C2E-9181-C142E7EA9474")]
    3434  [Item("RandomForestModelFull", "Represents a random forest for regression and classification.")]
    3535  public sealed class RandomForestModelFull : ClassificationModel, IRandomForestModel {
     
    4242    private double[] classValues;
    4343
     44    public int NumClasses => classValues == null ? 0 : classValues.Length;
     45
    4446    [Storable]
    4547    private string[] inputVariables;
    4648
     49    [Storable]
    4750    public int NumberOfTrees {
    48       get { return RandomForestNTrees; }
     51      get; private set;
    4952    }
    5053
     
    5356
    5457    [Storable]
    55     private int RandomForestBufSize {
    56       get { return randomForest.innerobj.bufsize; }
    57       set { randomForest.innerobj.bufsize = value; }
    58     }
    59     [Storable]
    60     private int RandomForestNClasses {
    61       get { return randomForest.innerobj.nclasses; }
    62       set { randomForest.innerobj.nclasses = value; }
    63     }
    64     [Storable]
    65     private int RandomForestNTrees {
    66       get { return randomForest.innerobj.ntrees; }
    67       set { randomForest.innerobj.ntrees = value; }
    68     }
    69     [Storable]
    70     private int RandomForestNVars {
    71       get { return randomForest.innerobj.nvars; }
    72       set { randomForest.innerobj.nvars = value; }
    73     }
    74     [Storable]
    75     private double[] RandomForestTrees {
    76       get { return randomForest.innerobj.trees; }
    77       set { randomForest.innerobj.trees = value; }
     58    private string RandomForestSerialized {
     59      get { alglib.dfserialize(randomForest, out var serialized); return serialized; }
     60      set { if (value != null) alglib.dfunserialize(value, out randomForest); }
    7861    }
    7962
    8063    [StorableConstructor]
    81     private RandomForestModelFull(StorableConstructorFlag _) : base(_) {
    82       randomForest = new alglib.decisionforest();
    83     }
     64    private RandomForestModelFull(StorableConstructorFlag _) : base(_) { }
    8465
    8566    private RandomForestModelFull(RandomForestModelFull original, Cloner cloner) : base(original, cloner) {
    86       randomForest = new alglib.decisionforest();
    87       randomForest.innerobj.bufsize = original.randomForest.innerobj.bufsize;
    88       randomForest.innerobj.nclasses = original.randomForest.innerobj.nclasses;
    89       randomForest.innerobj.ntrees = original.randomForest.innerobj.ntrees;
    90       randomForest.innerobj.nvars = original.randomForest.innerobj.nvars;
    91       randomForest.innerobj.trees = (double[])original.randomForest.innerobj.trees.Clone();
     67      if (original.randomForest != null)
     68        randomForest = (alglib.decisionforest)original.randomForest.make_copy();
     69      NumberOfTrees = original.NumberOfTrees;
    9270
    9371      // following fields are immutable so we don't need to clone them
     
    9977    }
    10078
    101     public RandomForestModelFull(alglib.decisionforest decisionForest, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<double> classValues = null) : base(targetVariable) {
     79    public RandomForestModelFull(alglib.decisionforest decisionForest, int nTrees, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<double> classValues = null) : base(targetVariable) {
    10280      this.name = ItemName;
    10381      this.description = ItemDescription;
    10482
    105       randomForest = decisionForest;
     83      randomForest = (alglib.decisionforest)decisionForest.make_copy();
     84      NumberOfTrees = nTrees;
    10685
    10786      this.inputVariables = inputVariables.ToArray();
     
    147126      double[] y = new double[1];
    148127
     128      alglib.dfcreatebuffer(randomForest, out var buf);
    149129      for (int row = 0; row < n; row++) {
    150130        for (int column = 0; column < columns; column++) {
    151131          x[column] = inputData[row, column];
    152132        }
    153         alglib.dfprocess(randomForest, x, ref y);
     133        alglib.dftsprocess(randomForest, buf, x, ref y); // thread-safe process (as long as separate buffers are used)
    154134        yield return y[0];
    155135      }
     
    168148          x[column] = inputData[row, column];
    169149        }
    170         alglib.dforest.dfprocessraw(randomForest.innerobj, x, ref ys);
     150        lock (randomForest)
     151          alglib.dforest.dfprocessraw(randomForest.innerobj, x, ref ys, null);
    171152        yield return ys.VariancePop();
    172153      }
     
    180161      int columns = inputData.GetLength(1);
    181162      double[] x = new double[columns];
    182       double[] y = new double[randomForest.innerobj.nclasses];
    183 
     163      double[] y = new double[NumClasses];
     164
     165      alglib.dfcreatebuffer(randomForest, out var buf);
    184166      for (int row = 0; row < n; row++) {
    185167        for (int column = 0; column < columns; column++) {
    186168          x[column] = inputData[row, column];
    187169        }
    188         alglib.dfprocess(randomForest, x, ref y);
     170        alglib.dftsprocess(randomForest, buf, x, ref y);
    189171        // find class for with the largest probability value
    190172        int maxProbClassIndex = 0;
Note: See TracChangeset for help on using the changeset viewer.