Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/16 11:00:55 (8 years ago)
Author:
gkronber
Message:

#2653: merged r14236,r14314:14315 and r14322 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs

    r14186 r14327  
    2121#endregion
    2222
     23using System;
    2324using System.Collections.Generic;
     25using System.Diagnostics.Eventing.Reader;
    2426using System.Linq;
    2527using HeuristicLab.Common;
     
    3638  public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
    3739    // don't store the actual model!
    38     private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary
     40    // the actual model is only recalculated when necessary
     41    private readonly Lazy<IGradientBoostedTreesModel> actualModel;
     42    private IGradientBoostedTreesModel ActualModel {
     43      get { return actualModel.Value; }
     44    }
    3945
    4046    [Storable]
     
    5763
    5864    public override IEnumerable<string> VariablesUsedForPrediction {
    59       get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
     65      get {
     66        return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x);
     67      }
    6068    }
    6169
    6270    [StorableConstructor]
    63     private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { }
     71    private GradientBoostedTreesModelSurrogate(bool deserializing)
     72      : base(deserializing) {
     73      actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
     74    }
    6475
    6576    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
    6677      : base(original, cloner) {
    67       if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel);
     78      IGradientBoostedTreesModel clonedModel = null;
     79      if (original.ActualModel != null) clonedModel = cloner.Clone(original.ActualModel);
     80      actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
    6881
    6982      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
     
    7588      this.m = original.m;
    7689      this.nu = original.nu;
     90    }
     91
     92    private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) {
     93      return () => {
     94        return clonedModel == null ? RecalculateModel() : clonedModel;
     95      };
    7796    }
    7897
     
    96115      IGradientBoostedTreesModel model)
    97116      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
    98       this.actualModel = model;
     117      actualModel = new Lazy<IGradientBoostedTreesModel>(() => model);
    99118    }
    100119
     
    105124    // forward message to actual model (recalculate model first if necessary)
    106125    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    107       if (actualModel == null) actualModel = RecalculateModel();
    108       return actualModel.GetEstimatedValues(dataset, rows);
     126      return ActualModel.GetEstimatedValues(dataset, rows);
    109127    }
    110128
     
    119137    public IEnumerable<IRegressionModel> Models {
    120138      get {
    121         if (actualModel == null) actualModel = RecalculateModel();
    122         return actualModel.Models;
     139        return ActualModel.Models;
    123140      }
    124141    }
     
    126143    public IEnumerable<double> Weights {
    127144      get {
    128         if (actualModel == null) actualModel = RecalculateModel();
    129         return actualModel.Weights;
     145        return ActualModel.Weights;
    130146      }
    131147    }
Note: See TracChangeset for help on using the changeset viewer.