Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/14/17 19:11:27 (8 years ago)
Author:
gkronber
Message:

#2687: merged r14350 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r14564 r14565  
    2626using System.Text;
    2727using HeuristicLab.Collections;
     28using HeuristicLab.Common;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2930
     
    109110        { "XOR", new Xor()},
    110111        { "DIFF", new Derivative()},
     112        { "LAG", new LaggedVariable() },
    111113      };
    112114
     
    333335    }
    334336
    335     // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number
     337    // Fact = '(' Expr ')' | 'LAG' '(' varId ',' ['+' | '-'] number ')' | funcId '(' ArgList ')' | varId | number
    336338    private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) {
    337339      var next = tokens.Peek();
     
    353355          if (lPar.TokenType != TokenType.LeftPar)
    354356            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          }
    362388
    363389          var rPar = tokens.Dequeue();
Note: See TracChangeset for help on using the changeset viewer.