Changeset 14330 for branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression
- Timestamp:
- 10/13/16 19:47:41 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 3 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/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.