Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
- Timestamp:
- 03/18/19 17:24:30 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
/stable/HeuristicLab.Algorithms.DataAnalysis/3.4 merged eligible /branches/1721-RandomForestPersistence/HeuristicLab.Algorithms.DataAnalysis/3.4 10321-10322 /branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4 13329-15286 /branches/Benchmarking/sources/HeuristicLab.Algorithms.DataAnalysis/3.4 6917-7005 /branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4 9070-13099 /branches/CloningRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4 4656-4721 /branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4 5471-5808 /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Algorithms.DataAnalysis/3.4 5815-6180 /branches/DataAnalysis/HeuristicLab.Algorithms.DataAnalysis/3.4 4458-4459,4462,4464 /branches/DataPreprocessing/HeuristicLab.Algorithms.DataAnalysis/3.4 10085-11101 /branches/GP.Grammar.Editor/HeuristicLab.Algorithms.DataAnalysis/3.4 6284-6795 /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Algorithms.DataAnalysis/3.4 5060 /branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Algorithms.DataAnalysis/3.4 11570-12508 /branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Algorithms.DataAnalysis/3.4 11130-12721 /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Algorithms.DataAnalysis/3.4 13819-14091 /branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4 8116-8789 /branches/LogResidualEvaluator/HeuristicLab.Algorithms.DataAnalysis/3.4 10202-10483 /branches/NET40/sources/HeuristicLab.Algorithms.DataAnalysis/3.4 5138-5162 /branches/ParallelEngine/HeuristicLab.Algorithms.DataAnalysis/3.4 5175-5192 /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Algorithms.DataAnalysis/3.4 7773-7810 /branches/QAPAlgorithms/HeuristicLab.Algorithms.DataAnalysis/3.4 6350-6627 /branches/Restructure trunk solution/HeuristicLab.Algorithms.DataAnalysis/3.4 6828 /branches/SpectralKernelForGaussianProcesses/HeuristicLab.Algorithms.DataAnalysis/3.4 10204-10479 /branches/SuccessProgressAnalysis/HeuristicLab.Algorithms.DataAnalysis/3.4 5370-5682 /branches/Trunk/HeuristicLab.Algorithms.DataAnalysis/3.4 6829-6865 /branches/VNS/HeuristicLab.Algorithms.DataAnalysis/3.4 5594-5752 /branches/Weighted TSNE/3.4 15451-15531 /branches/histogram/HeuristicLab.Algorithms.DataAnalysis/3.4 5959-6341 /branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4 14232-14825 /trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4 13331-15681
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r13157 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * and the BEACON Center for the Study of Evolution in Action. 5 5 * … … 21 21 #endregion 22 22 23 using System; 23 24 using System.Collections.Generic; 25 using System.Linq; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; … … 33 35 // recalculate the actual GBT model on demand 34 36 [Item("Gradient boosted tree model", "")] 35 public sealed class GradientBoostedTreesModelSurrogate : NamedItem, IGradientBoostedTreesModel {37 public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel { 36 38 // don't store the actual model! 37 private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary 39 // the actual model is only recalculated when necessary 40 private readonly Lazy<IGradientBoostedTreesModel> actualModel; 41 private IGradientBoostedTreesModel ActualModel { 42 get { return actualModel.Value; } 43 } 38 44 39 45 [Storable] … … 55 61 56 62 63 public override IEnumerable<string> VariablesUsedForPrediction { 64 get { 65 return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); 66 } 67 } 68 57 69 [StorableConstructor] 58 private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { } 70 private GradientBoostedTreesModelSurrogate(bool deserializing) 71 : base(deserializing) { 72 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel()); 73 } 59 74 60 75 private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner) 61 76 : base(original, cloner) { 62 if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel); 77 IGradientBoostedTreesModel clonedModel = null; 78 if (original.ActualModel != null) clonedModel = cloner.Clone(original.ActualModel); 79 actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure 63 80 64 81 this.trainingProblemData = cloner.Clone(original.trainingProblemData); … … 72 89 } 73 90 91 private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) { 92 return () => { 93 return clonedModel == null ? RecalculateModel() : clonedModel; 94 }; 95 } 96 74 97 // create only the surrogate model without an actual model 75 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu) 76 : base("Gradient boosted tree model", string.Empty) { 98 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, 99 ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu) 100 : base(trainingProblemData.TargetVariable, "Gradient boosted tree model", string.Empty) { 77 101 this.trainingProblemData = trainingProblemData; 78 102 this.seed = seed; … … 86 110 87 111 // wrap an actual model in a surrograte 88 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu, IGradientBoostedTreesModel model) 112 public GradientBoostedTreesModelSurrogate(IRegressionProblemData trainingProblemData, uint seed, 113 ILossFunction lossFunction, int iterations, int maxSize, double r, double m, double nu, 114 IGradientBoostedTreesModel model) 89 115 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 90 this.actualModel = model;116 actualModel = new Lazy<IGradientBoostedTreesModel>(() => model); 91 117 } 92 118 … … 96 122 97 123 // forward message to actual model (recalculate model first if necessary) 98 public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 99 if (actualModel == null) actualModel = RecalculateModel(); 100 return actualModel.GetEstimatedValues(dataset, rows); 124 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 125 return ActualModel.GetEstimatedValues(dataset, rows); 101 126 } 102 127 103 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {128 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 104 129 return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone()); 105 130 } 106 107 131 108 132 private IGradientBoostedTreesModel RecalculateModel() { … … 112 136 public IEnumerable<IRegressionModel> Models { 113 137 get { 114 if (actualModel == null) actualModel = RecalculateModel(); 115 return actualModel.Models; 138 return ActualModel.Models; 116 139 } 117 140 } … … 119 142 public IEnumerable<double> Weights { 120 143 get { 121 if (actualModel == null) actualModel = RecalculateModel(); 122 return actualModel.Weights; 144 return ActualModel.Weights; 123 145 } 124 146 }
Note: See TracChangeset
for help on using the changeset viewer.