Changeset 14719
- Timestamp:
- 03/06/17 17:10:14 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
r14542 r14719 138 138 // BackwardsCompatibility3.3 139 139 #region Backwards compatible code, remove with 3.4 140 if 140 if(!Parameters.ContainsKey(RestartsParameterName)) 141 141 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts", new IntValue(1))); 142 if 142 if(!Parameters.ContainsKey(SeedParameterName)) 143 143 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The PRNG seed value.", new IntValue())); 144 if 144 if(!Parameters.ContainsKey(SetSeedRandomlyParameterName)) 145 145 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 146 if 146 if(!Parameters.ContainsKey(InitParamsRandomlyParameterName)) 147 147 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the numeric parameters of the model should be initialized randomly.", new BoolValue(false))); 148 148 … … 161 161 protected override void Run(CancellationToken cancellationToken) { 162 162 IRegressionSolution bestSolution = null; 163 if 163 if(InitializeParametersRandomly) { 164 164 var qualityTable = new DataTable("RMSE table"); 165 165 qualityTable.VisualProperties.YAxisLogScale = true; … … 172 172 qualityTable.Rows.Add(testRMSERow); 173 173 Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable)); 174 if 174 if(SetSeedRandomly) Seed = (new System.Random()).Next(); 175 175 var rand = new MersenneTwister((uint)Seed); 176 176 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 177 177 trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError); 178 178 testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError); 179 for 179 for(int r = 0; r < Restarts; r++) { 180 180 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 181 181 trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError); 182 182 testRMSERow.Values.Add(solution.TestRootMeanSquaredError); 183 if 183 if(solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 184 184 bestSolution = solution; 185 185 } … … 216 216 factorSymbol.AllVariableNames = factorSymbol.VariableNames; 217 217 factorSymbol.VariableValues = 218 factorSymbol.VariableNames.Select(name => new KeyValuePair<string, List<string>>(name, problemData.Dataset.GetReadOnlyStringValues(name).Distinct().ToList())); 219 220 foreach (var parent in tree.IterateNodesPrefix().ToArray()) { 221 for (int i = 0; i < parent.SubtreeCount; i++) { 218 factorSymbol.VariableNames.Select(name => 219 new KeyValuePair<string, Dictionary<string, int>>(name, 220 problemData.Dataset.GetReadOnlyStringValues(name).Distinct() 221 .Select((n, i) => Tuple.Create(n, i)) 222 .ToDictionary(tup => tup.Item1, tup => tup.Item2))); 223 224 foreach(var parent in tree.IterateNodesPrefix().ToArray()) { 225 for(int i = 0; i < parent.SubtreeCount; i++) { 222 226 var child = parent.GetSubtree(i) as VariableTreeNode; 223 if 227 if(child != null && factorSymbol.VariableNames.Contains(child.VariableName)) { 224 228 parent.RemoveSubtree(i); 225 229 var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode(); … … 232 236 } 233 237 234 if 238 if(!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) throw new ArgumentException("The optimizer does not support the specified model structure."); 235 239 236 240 // initialize constants randomly 237 if 238 foreach 241 if(rand != null) { 242 foreach(var node in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) { 239 243 double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1)); 240 244 double s = rand.NextDouble() < 0.5 ? -1 : 1;
Note: See TracChangeset
for help on using the changeset viewer.