- Timestamp:
- 09/29/16 10:42:52 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r14236 r14315 21 21 #endregion 22 22 23 using System; 23 24 using System.Collections.Generic; 25 using System.Diagnostics.Eventing.Reader; 24 26 using System.Linq; 25 27 using HeuristicLab.Common; … … 36 38 public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel { 37 39 // 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 } 39 45 40 46 [Storable] … … 57 63 58 64 public override IEnumerable<string> VariablesUsedForPrediction { 59 get 60 { 61 lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); } 62 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); 63 67 } 64 68 } 65 69 66 70 [StorableConstructor] 67 private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { } 71 private GradientBoostedTreesModelSurrogate(bool deserializing) 72 : base(deserializing) { 73 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel()); 74 } 68 75 69 76 private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner) 70 77 : base(original, cloner) { 71 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 72 81 73 82 this.trainingProblemData = cloner.Clone(original.trainingProblemData); … … 79 88 this.m = original.m; 80 89 this.nu = original.nu; 90 } 91 92 private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) { 93 return () => { 94 return clonedModel == null ? RecalculateModel() : clonedModel; 95 }; 81 96 } 82 97 … … 100 115 IGradientBoostedTreesModel model) 101 116 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 102 this.actualModel = model;117 actualModel = new Lazy<IGradientBoostedTreesModel>(() => model); 103 118 } 104 119 … … 109 124 // forward message to actual model (recalculate model first if necessary) 110 125 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 111 lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); } 112 return actualModel.GetEstimatedValues(dataset, rows); 126 return ActualModel.GetEstimatedValues(dataset, rows); 113 127 } 114 128 … … 123 137 public IEnumerable<IRegressionModel> Models { 124 138 get { 125 lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();} 126 return actualModel.Models; 139 return ActualModel.Models; 127 140 } 128 141 } … … 130 143 public IEnumerable<double> Weights { 131 144 get { 132 lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();} 133 return actualModel.Weights; 145 return ActualModel.Weights; 134 146 } 135 147 }
Note: See TracChangeset
for help on using the changeset viewer.