- Timestamp:
- 04/10/17 15:48:20 (8 years ago)
- Location:
- branches/TSNE/HeuristicLab.Algorithms.DataAnalysis
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
r14319 r14836 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using System.Threading; 24 26 using HeuristicLab.Analysis; 25 27 using HeuristicLab.Common; … … 157 159 158 160 #region nonlinear regression 159 protected override void Run( ) {161 protected override void Run(CancellationToken cancellationToken) { 160 162 IRegressionSolution bestSolution = null; 161 163 if (InitializeParametersRandomly) { … … 207 209 var parser = new InfixExpressionParser(); 208 210 var tree = parser.Parse(modelStructure); 211 // parser handles double and string variables equally by creating a VariableTreeNode 212 // post-process to replace VariableTreeNodes by FactorVariableTreeNodes for all string variables 213 var factorSymbol = new FactorVariable(); 214 factorSymbol.VariableNames = 215 problemData.AllowedInputVariables.Where(name => problemData.Dataset.VariableHasType<string>(name)); 216 factorSymbol.AllVariableNames = factorSymbol.VariableNames; 217 factorSymbol.VariableValues = 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++) { 226 var varChild = parent.GetSubtree(i) as VariableTreeNode; 227 var factorVarChild = parent.GetSubtree(i) as FactorVariableTreeNode; 228 if (varChild != null && factorSymbol.VariableNames.Contains(varChild.VariableName)) { 229 parent.RemoveSubtree(i); 230 var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode(); 231 factorTreeNode.VariableName = varChild.VariableName; 232 factorTreeNode.Weights = 233 factorTreeNode.Symbol.GetVariableValues(factorTreeNode.VariableName).Select(_ => 1.0).ToArray(); 234 // weight = 1.0 for each value 235 parent.InsertSubtree(i, factorTreeNode); 236 } else if (factorVarChild != null && factorSymbol.VariableNames.Contains(factorVarChild.VariableName)) { 237 if (factorSymbol.GetVariableValues(factorVarChild.VariableName).Count() != factorVarChild.Weights.Length) 238 throw new ArgumentException( 239 string.Format("Factor variable {0} needs exactly {1} weights", 240 factorVarChild.VariableName, 241 factorSymbol.GetVariableValues(factorVarChild.VariableName).Count())); 242 parent.RemoveSubtree(i); 243 var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode(); 244 factorTreeNode.VariableName = factorVarChild.VariableName; 245 factorTreeNode.Weights = factorVarChild.Weights; 246 parent.InsertSubtree(i, factorTreeNode); 247 } 248 } 249 } 209 250 210 251 if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) throw new ArgumentException("The optimizer does not support the specified model structure.");
Note: See TracChangeset
for help on using the changeset viewer.