Changeset 14350
- Timestamp:
- 10/23/16 09:22:49 (8 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14347 r14350 110 110 } else { 111 111 // no subtrees 112 if (node.Symbol is Variable) { 112 if (node.Symbol is LaggedVariable) { 113 var varNode = node as LaggedVariableTreeNode; 114 if (!varNode.Weight.IsAlmost(1.0)) { 115 strBuilder.Append("("); 116 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); 117 strBuilder.Append("*"); 118 } 119 strBuilder.Append("LAG("); 120 if (varNode.VariableName.Contains("'")) { 121 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 122 } else { 123 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 124 } 125 strBuilder.Append(", ") 126 .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag) 127 .Append(")"); 128 } else if (node.Symbol is Variable) { 113 129 var varNode = node as VariableTreeNode; 114 130 if (!varNode.Weight.IsAlmost(1.0)) { … … 130 146 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value); 131 147 else 132 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1)148 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1 133 149 } 134 150 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14347 r14350 26 26 using System.Text; 27 27 using HeuristicLab.Collections; 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 30 … … 109 110 { "XOR", new Xor()}, 110 111 { "DIFF", new Derivative()}, 112 { "LAG", new LaggedVariable() }, 111 113 }; 112 114 … … 333 335 } 334 336 335 // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number337 // Fact = '(' Expr ')' | 'LAG' '(' varId ',' ['+' | '-'] number ')' | funcId '(' ArgList ')' | varId | number 336 338 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 337 339 var next = tokens.Peek(); … … 353 355 if (lPar.TokenType != TokenType.LeftPar) 354 356 throw new ArgumentException("expected ("); 355 var args = ParseArgList(tokens); 356 357 // check semantic constraints 358 if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) 359 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId, 360 funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity)); 361 foreach (var arg in args) funcNode.AddSubtree(arg); 357 358 // handle 'lag' specifically 359 if (funcNode.Symbol is LaggedVariable) { 360 var varId = tokens.Dequeue(); 361 if (varId.TokenType != TokenType.Identifier) throw new ArgumentException("Identifier expected. Format for lagged variables: \"lag(x, -1)\""); 362 var comma = tokens.Dequeue(); 363 if (comma.TokenType != TokenType.Comma) throw new ArgumentException("',' expected, Format for lagged variables: \"lag(x, -1)\""); 364 double sign = 1.0; 365 if (tokens.Peek().strVal == "+" || tokens.Peek().strVal == "-") { 366 // read sign 367 var signTok = tokens.Dequeue(); 368 if (signTok.strVal == "-") sign = -1.0; 369 } 370 var lagToken = tokens.Dequeue(); 371 if (lagToken.TokenType != TokenType.Number) throw new ArgumentException("Number expected, Format for lagged variables: \"lag(x, -1)\""); 372 if (!lagToken.doubleVal.IsAlmost(Math.Round(lagToken.doubleVal))) 373 throw new ArgumentException("Time lags must be integer values"); 374 var laggedVarNode = funcNode as LaggedVariableTreeNode; 375 laggedVarNode.VariableName = varId.strVal; 376 laggedVarNode.Lag = (int)Math.Round(sign * lagToken.doubleVal); 377 laggedVarNode.Weight = 1.0; 378 } else { 379 // functions 380 var args = ParseArgList(tokens); 381 // check number of arguments 382 if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) { 383 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId, 384 funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity)); 385 } 386 foreach (var arg in args) funcNode.AddSubtree(arg); 387 } 362 388 363 389 var rPar = tokens.Dequeue(); -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r14347 r14350 101 101 Console.WriteLine(formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)"))); 102 102 103 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, 1)"))); 104 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, -1)"))); 105 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, +1)"))); 106 Console.WriteLine(formatter.Format(parser.Parse("x * LAG('x', +1)"))); 107 103 108 } 104 109 }
Note: See TracChangeset
for help on using the changeset viewer.