- Timestamp:
- 10/13/16 19:47:41 (7 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650
- Property svn:mergeinfo changed
/trunk/sources merged: 14282,14284-14300,14307,14314-14316,14319,14322
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 14314-14316,14319,14322
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r14239 r14330 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 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r14239 r14330 36 36 public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel { 37 37 38 private readonly object kdTreeLockObject = new object(); 38 39 private alglib.nearestneighbor.kdtree kdTree; 39 40 public alglib.nearestneighbor.kdtree KDTree { … … 47 48 } 48 49 } 50 49 51 50 52 public override IEnumerable<string> VariablesUsedForPrediction { … … 201 203 } 202 204 int numNeighbours; 203 lock (kdTree ) { // gkronber: the following calls change the kdTree data structure205 lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure 204 206 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 205 207 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); … … 237 239 } 238 240 int numNeighbours; 239 lock (kdTree ) {241 lock (kdTreeLockObject) { 240 242 // gkronber: the following calls change the kdTree data structure 241 243 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
r14277 r14330 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 24 using HeuristicLab.Analysis; 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Optimization; 28 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Optimization; … … 48 49 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 49 50 private const string SeedParameterName = "Seed"; 51 private const string InitParamsRandomlyParameterName = "InitializeParametersRandomly"; 50 52 51 53 public IFixedValueParameter<StringValue> ModelStructureParameter { … … 66 68 public IFixedValueParameter<IntValue> RestartsParameter { 67 69 get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; } 70 } 71 72 public IFixedValueParameter<BoolValue> InitParametersRandomlyParameter { 73 get { return (IFixedValueParameter<BoolValue>)Parameters[InitParamsRandomlyParameterName]; } 68 74 } 69 75 … … 91 97 get { return SetSeedRandomlyParameter.Value.Value; } 92 98 set { SetSeedRandomlyParameter.Value.Value = value; } 99 } 100 101 public bool InitializeParametersRandomly { 102 get { return InitParametersRandomlyParameter.Value.Value; } 103 set { InitParametersRandomlyParameter.Value.Value = value; } 93 104 } 94 105 … … 103 114 Parameters.Add(new FixedValueParameter<StringValue>(ModelStructureParameterName, "The function for which the parameters must be fit (only numeric constants are tuned).", new StringValue("1.0 * x*x + 0.0"))); 104 115 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "The maximum number of iterations for constants optimization.", new IntValue(200))); 105 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts ", new IntValue(10)));116 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts (>0)", new IntValue(10))); 106 117 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The PRNG seed value.", new IntValue())); 107 118 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 119 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the real-valued model parameters should be initialized randomly in each restart.", new BoolValue(false))); 120 121 SetParameterHiddenState(); 122 123 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 124 SetParameterHiddenState(); 125 }; 126 } 127 128 private void SetParameterHiddenState() { 129 var hide = !InitializeParametersRandomly; 130 RestartsParameter.Hidden = hide; 131 SeedParameter.Hidden = hide; 132 SetSeedRandomlyParameter.Hidden = hide; 108 133 } 109 134 … … 118 143 if (!Parameters.ContainsKey(SetSeedRandomlyParameterName)) 119 144 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 145 if (!Parameters.ContainsKey(InitParamsRandomlyParameterName)) 146 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the numeric parameters of the model should be initialized randomly.", new BoolValue(false))); 147 148 SetParameterHiddenState(); 149 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 150 SetParameterHiddenState(); 151 }; 120 152 #endregion 121 153 } … … 127 159 #region nonlinear regression 128 160 protected override void Run() { 129 if (SetSeedRandomly) Seed = (new System.Random()).Next();130 var rand = new MersenneTwister((uint)Seed);131 161 IRegressionSolution bestSolution = null; 132 for (int r = 0; r < Restarts; r++) { 133 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 134 if (bestSolution == null || solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 135 bestSolution = solution; 162 if (InitializeParametersRandomly) { 163 var qualityTable = new DataTable("RMSE table"); 164 qualityTable.VisualProperties.YAxisLogScale = true; 165 var trainRMSERow = new DataRow("RMSE (train)"); 166 trainRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 167 var testRMSERow = new DataRow("RMSE test"); 168 testRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 169 170 qualityTable.Rows.Add(trainRMSERow); 171 qualityTable.Rows.Add(testRMSERow); 172 Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable)); 173 if (SetSeedRandomly) Seed = (new System.Random()).Next(); 174 var rand = new MersenneTwister((uint)Seed); 175 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 176 trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError); 177 testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError); 178 for (int r = 0; r < Restarts; r++) { 179 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 180 trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError); 181 testRMSERow.Values.Add(solution.TestRootMeanSquaredError); 182 if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 183 bestSolution = solution; 184 } 136 185 } 186 } else { 187 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations); 137 188 } 138 189 … … 148 199 /// The starting point for the numeric constants is initialized randomly if a random number generator is specified (~N(0,1)). Otherwise the user specified constants are 149 200 /// used as a starting point. 150 /// </summary> 201 /// </summary>- 151 202 /// <param name="problemData">Training and test data</param> 152 203 /// <param name="modelStructure">The function as infix expression</param> … … 154 205 /// <param name="random">Optional random number generator for random initialization of numeric constants.</param> 155 206 /// <returns></returns> 156 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand om= null) {207 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand = null) { 157 208 var parser = new InfixExpressionParser(); 158 209 var tree = parser.Parse(modelStructure); … … 183 234 184 235 // initialize constants randomly 185 if (rand om!= null) {236 if (rand != null) { 186 237 foreach (var node in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) { 187 node.Value = NormalDistributedRandom.NextDouble(random, 0, 1); 238 double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1)); 239 double s = rand.NextDouble() < 0.5 ? -1 : 1; 240 node.Value = s * node.Value * f; 188 241 } 189 242 }
Note: See TracChangeset
for help on using the changeset viewer.