Changeset 14319 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
- Timestamp:
- 10/02/16 13:02:03 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
r14316 r14319 22 22 using System; 23 23 using System.Linq; 24 using HeuristicLab.Analysis; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 47 48 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 48 49 private const string SeedParameterName = "Seed"; 50 private const string InitParamsRandomlyParameterName = "InitializeParametersRandomly"; 49 51 50 52 public IFixedValueParameter<StringValue> ModelStructureParameter { … … 65 67 public IFixedValueParameter<IntValue> RestartsParameter { 66 68 get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; } 69 } 70 71 public IFixedValueParameter<BoolValue> InitParametersRandomlyParameter { 72 get { return (IFixedValueParameter<BoolValue>)Parameters[InitParamsRandomlyParameterName]; } 67 73 } 68 74 … … 90 96 get { return SetSeedRandomlyParameter.Value.Value; } 91 97 set { SetSeedRandomlyParameter.Value.Value = value; } 98 } 99 100 public bool InitializeParametersRandomly { 101 get { return InitParametersRandomlyParameter.Value.Value; } 102 set { InitParametersRandomlyParameter.Value.Value = value; } 92 103 } 93 104 … … 102 113 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"))); 103 114 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "The maximum number of iterations for constants optimization.", new IntValue(200))); 104 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts ", new IntValue(10)));115 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts (>0)", new IntValue(10))); 105 116 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The PRNG seed value.", new IntValue())); 106 117 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 118 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))); 119 120 SetParameterHiddenState(); 121 122 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 123 SetParameterHiddenState(); 124 }; 125 } 126 127 private void SetParameterHiddenState() { 128 var hide = !InitializeParametersRandomly; 129 RestartsParameter.Hidden = hide; 130 SeedParameter.Hidden = hide; 131 SetSeedRandomlyParameter.Hidden = hide; 107 132 } 108 133 … … 117 142 if (!Parameters.ContainsKey(SetSeedRandomlyParameterName)) 118 143 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 144 if (!Parameters.ContainsKey(InitParamsRandomlyParameterName)) 145 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the numeric parameters of the model should be initialized randomly.", new BoolValue(false))); 146 147 SetParameterHiddenState(); 148 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 149 SetParameterHiddenState(); 150 }; 119 151 #endregion 120 152 } … … 126 158 #region nonlinear regression 127 159 protected override void Run() { 128 if (SetSeedRandomly) Seed = (new System.Random()).Next(); 129 var rand = new MersenneTwister((uint)Seed); 130 131 var bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 132 for (int r = 0; r < Restarts; r++) { 133 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 134 if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 135 bestSolution = solution; 160 IRegressionSolution bestSolution = null; 161 if (InitializeParametersRandomly) { 162 var qualityTable = new DataTable("RMSE table"); 163 qualityTable.VisualProperties.YAxisLogScale = true; 164 var trainRMSERow = new DataRow("RMSE (train)"); 165 trainRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 166 var testRMSERow = new DataRow("RMSE test"); 167 testRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 168 169 qualityTable.Rows.Add(trainRMSERow); 170 qualityTable.Rows.Add(testRMSERow); 171 Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable)); 172 if (SetSeedRandomly) Seed = (new System.Random()).Next(); 173 var rand = new MersenneTwister((uint)Seed); 174 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 175 trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError); 176 testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError); 177 for (int r = 0; r < Restarts; r++) { 178 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 179 trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError); 180 testRMSERow.Values.Add(solution.TestRootMeanSquaredError); 181 if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 182 bestSolution = solution; 183 } 136 184 } 185 } else { 186 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations); 137 187 } 138 188 … … 154 204 /// <param name="random">Optional random number generator for random initialization of numeric constants.</param> 155 205 /// <returns></returns> 156 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand om= null) {206 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand = null) { 157 207 var parser = new InfixExpressionParser(); 158 208 var tree = parser.Parse(modelStructure); … … 161 211 162 212 // initialize constants randomly 163 if (rand om!= null) {213 if (rand != null) { 164 214 foreach (var node in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) { 165 node.Value = NormalDistributedRandom.NextDouble(random, 0, 1); 215 double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1)); 216 double s = rand.NextDouble() < 0.5 ? -1 : 1; 217 node.Value = s * node.Value * f; 166 218 } 167 219 }
Note: See TracChangeset
for help on using the changeset viewer.