Changeset 9126 for branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
- Timestamp:
- 01/08/13 15:36:13 (12 years ago)
- Location:
- branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Property svn:ignore
-
old new 1 *.user 2 Plugin.cs 1 3 bin 2 *.user3 HeuristicLabProblemsDataAnalysisSymbolicPlugin.cs4 4 obj 5 *.vs10x6 Plugin.cs
-
- Property svn:ignore
-
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r7696 r9126 52 52 private GreaterThan gtSymbol = new GreaterThan(); 53 53 private LessThan ltSymbol = new LessThan(); 54 private Integral integralSymbol = new Integral(); 55 private LaggedVariable laggedVariableSymbol = new LaggedVariable(); 56 private TimeLag timeLagSymbol = new TimeLag(); 54 57 55 58 public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) { … … 182 185 private bool IsConstant(ISymbolicExpressionTreeNode node) { 183 186 return node.Symbol is Constant; 187 } 188 189 // dynamic 190 private bool IsTimeLag(ISymbolicExpressionTreeNode node) { 191 return node.Symbol is TimeLag; 192 } 193 private bool IsIntegral(ISymbolicExpressionTreeNode node) { 194 return node.Symbol is Integral; 184 195 } 185 196 … … 234 245 } else if (IsNot(original)) { 235 246 return SimplifyNot(original); 247 } else if (IsTimeLag(original)) { 248 return SimplifyTimeLag(original); 249 } else if (IsIntegral(original)) { 250 return SimplifyIntegral(original); 236 251 } else { 237 252 return SimplifyAny(original); … … 376 391 return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 377 392 } 393 private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) { 394 var laggedTreeNode = original as ILaggedTreeNode; 395 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); 396 if (!ContainsVariableCondition(simplifiedSubtree)) { 397 return AddLagToDynamicNodes(simplifiedSubtree, laggedTreeNode.Lag); 398 } else { 399 return MakeTimeLag(simplifiedSubtree, laggedTreeNode.Lag); 400 } 401 } 402 private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) { 403 var laggedTreeNode = original as ILaggedTreeNode; 404 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); 405 if (IsConstant(simplifiedSubtree)) { 406 return GetSimplifiedTree(MakeProduct(simplifiedSubtree, MakeConstant(-laggedTreeNode.Lag))); 407 } else { 408 return MakeIntegral(simplifiedSubtree, laggedTreeNode.Lag); 409 } 410 } 411 378 412 #endregion 379 413 380 414 #region low level tree restructuring 415 private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) { 416 if (lag == 0) return subtree; 417 if (IsConstant(subtree)) return subtree; 418 var lagNode = (LaggedTreeNode)timeLagSymbol.CreateTreeNode(); 419 lagNode.Lag = lag; 420 lagNode.AddSubtree(subtree); 421 return lagNode; 422 } 423 424 private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) { 425 if (lag == 0) return subtree; 426 else if (lag == -1 || lag == 1) { 427 return MakeSum(subtree, AddLagToDynamicNodes((ISymbolicExpressionTreeNode)subtree.Clone(), lag)); 428 } else { 429 var node = (LaggedTreeNode)integralSymbol.CreateTreeNode(); 430 node.Lag = lag; 431 node.AddSubtree(subtree); 432 return node; 433 } 434 } 435 381 436 private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) { 382 437 if (IsConstant(t)) { … … 847 902 848 903 #region helper functions 904 private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) { 905 if (node.Symbol is VariableCondition) return true; 906 foreach (var subtree in node.Subtrees) 907 if (ContainsVariableCondition(subtree)) return true; 908 return false; 909 } 910 911 private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) { 912 var laggedTreeNode = node as ILaggedTreeNode; 913 var variableNode = node as VariableTreeNode; 914 var variableConditionNode = node as VariableConditionTreeNode; 915 if (laggedTreeNode != null) 916 laggedTreeNode.Lag += lag; 917 else if (variableNode != null) { 918 var laggedVariableNode = (LaggedVariableTreeNode)laggedVariableSymbol.CreateTreeNode(); 919 laggedVariableNode.Lag = lag; 920 laggedVariableNode.VariableName = variableNode.VariableName; 921 return laggedVariableNode; 922 } else if (variableConditionNode != null) { 923 throw new NotSupportedException("Removal of time lags around variable condition symbols is not allowed."); 924 } 925 var subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees); 926 while (node.SubtreeCount > 0) node.RemoveSubtree(0); 927 foreach (var subtree in subtrees) { 928 node.AddSubtree(AddLagToDynamicNodes(subtree, lag)); 929 } 930 return node; 931 } 849 932 850 933 private bool AreSameVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
Note: See TracChangeset
for help on using the changeset viewer.