Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/20 12:14:21 (5 years ago)
Author:
gkronber
Message:

#2925: merged r17255:17402 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs

    r17246 r17409  
    3535    #region parameters for recalculation of the model
    3636    [Storable]
    37     private int seed;
     37    private readonly int seed;
    3838    [Storable]
    39     private IDataAnalysisProblemData originalTrainingData;
     39    private readonly IDataAnalysisProblemData originalTrainingData;
    4040    [Storable]
    41     private double[] classValues;
     41    private readonly double[] classValues;
    4242    [Storable]
    43     private int nTrees;
     43    private readonly int nTrees;
    4444    [Storable]
    45     private double r;
     45    private readonly double r;
    4646    [Storable]
    47     private double m;
     47    private readonly double m;
    4848    #endregion
     49
    4950
    5051    // don't store the actual model!
    5152    // the actual model is only recalculated when necessary
     53    private IRandomForestModel fullModel = null;
    5254    private readonly Lazy<IRandomForestModel> actualModel;
     55
    5356    private IRandomForestModel ActualModel {
    5457      get { return actualModel.Value; }
     
    7477      this.m = m;
    7578
    76       actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());
     79      actualModel = CreateLazyInitFunc();
    7780    }
    7881
    79     // wrap an actual model in a surrograte
     82    // wrap an actual model in a surrogate
    8083    public RandomForestModelSurrogate(IRandomForestModel model, string targetVariable, IDataAnalysisProblemData originalTrainingData,
    81       int seed, int nTrees, double r, double m, double[] classValues = null) : this(targetVariable, originalTrainingData, seed, nTrees, r, m, classValues) {
    82       actualModel = new Lazy<IRandomForestModel>(() => model);
     84      int seed, int nTrees, double r, double m, double[] classValues = null)
     85      : this(targetVariable, originalTrainingData, seed, nTrees, r, m, classValues) {
     86      fullModel = model;
    8387    }
    8488
    8589    [StorableConstructor]
    8690    private RandomForestModelSurrogate(StorableConstructorFlag _) : base(_) {
    87       actualModel = new Lazy<IRandomForestModel>(() => RecalculateModel());
     91      actualModel = CreateLazyInitFunc();
    8892    }
    8993
    9094    private RandomForestModelSurrogate(RandomForestModelSurrogate original, Cloner cloner) : base(original, cloner) {
    91       IRandomForestModel clonedModel = null;
    92       if (original.actualModel.IsValueCreated) clonedModel = cloner.Clone(original.ActualModel);
    93       actualModel = new Lazy<IRandomForestModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
    94 
    9595      // clone data which is necessary to rebuild the model
    9696      this.originalTrainingData = cloner.Clone(original.originalTrainingData);
     
    100100      this.r = original.r;
    101101      this.m = original.m;
    102     }
    103102
    104     private Func<IRandomForestModel> CreateLazyInitFunc(IRandomForestModel clonedModel) {
    105       return () => {
    106         return clonedModel ?? RecalculateModel();
    107       };
     103      // clone full model if it has already been created
     104      if (original.fullModel != null) this.fullModel = cloner.Clone(original.fullModel);
     105      actualModel = CreateLazyInitFunc();
    108106    }
    109107
    110108    public override IDeepCloneable Clone(Cloner cloner) {
    111109      return new RandomForestModelSurrogate(this, cloner);
     110    }
     111
     112    private Lazy<IRandomForestModel> CreateLazyInitFunc() {
     113      return new Lazy<IRandomForestModel>(() => {
     114        if (fullModel == null) fullModel = RecalculateModel();
     115        return fullModel;
     116      });
    112117    }
    113118
Note: See TracChangeset for help on using the changeset viewer.