Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/17 14:39:32 (8 years ago)
Author:
gkronber
Message:

#2650 added support for negative weights for parsing expressions with factors

Location:
branches/symbreg-factors-2650
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r14761 r14765  
    4848  /// ArgList       = Expr { ',' Expr }
    4949  /// VarExpr       = varId OptFactorPart
    50   /// OptFactorPart = [ ('=' varVal | '[' number {',' number } ']' ) ]
     50  /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ]
    5151  /// varId         =  ident | ' ident ' | " ident "
    5252  /// varVal        =  ident | ' ident ' | " ident "
     
    366366    /// ArgList       = Expr { ',' Expr }
    367367    /// VarExpr       = varId OptFactorPart
    368     /// OptFactorPart = [ ('=' varVal | '[' number {',' number } ']' ) ]
     368    /// OptFactorPart = [ ('=' varVal | '[' ['+' | '-' ] number {',' ['+' | '-' ] number } ']' ) ]
    369369    /// varId         =  ident | ' ident ' | " ident "
    370370    /// varVal        =  ident | ' ident ' | " ident "
     
    440440          } else if(tokens.Peek().TokenType == TokenType.LeftBracket) {
    441441            // factor variable
    442             var factorVariableNode = (FactorVariableTreeNode) factorVar.CreateTreeNode();
     442            var factorVariableNode = (FactorVariableTreeNode)factorVar.CreateTreeNode();
    443443            factorVariableNode.VariableName = idTok.strVal;
    444444
     
    446446            var weights = new List<double>();
    447447            // at least one weight is necessary
     448            var sign = 1.0;
     449            if(tokens.Peek().TokenType == TokenType.Operator) {
     450              var opToken = tokens.Dequeue();
     451              if(opToken.strVal == "+") sign = 1.0;
     452              else if(opToken.strVal == "-") sign = -1.0;
     453              else throw new ArgumentException();
     454            }
    448455            if(tokens.Peek().TokenType != TokenType.Number) throw new ArgumentException("number expected");
    449456            var weightTok = tokens.Dequeue();
    450             weights.Add(weightTok.doubleVal);
     457            weights.Add(sign * weightTok.doubleVal);
    451458            while(tokens.Peek().TokenType == TokenType.Comma) {
    452459              // skip comma
    453460              tokens.Dequeue();
     461              if(tokens.Peek().TokenType == TokenType.Operator) {
     462                var opToken = tokens.Dequeue();
     463                if(opToken.strVal == "+") sign = 1.0;
     464                else if(opToken.strVal == "-") sign = -1.0;
     465                else throw new ArgumentException();
     466              }
    454467              weightTok = tokens.Dequeue();
    455468              if(weightTok.TokenType != TokenType.Number) throw new ArgumentException("number expected");
    456               weights.Add(weightTok.doubleVal);
     469              weights.Add(sign * weightTok.doubleVal);
    457470            }
    458471            var rightBracketToken = tokens.Dequeue();
  • branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs

    r14761 r14765  
    111111      Console.WriteLine(formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));
    112112
     113      Console.WriteLine(formatter.Format(parser.Parse("x [+1.0] * y")));
     114      Console.WriteLine(formatter.Format(parser.Parse("x [-1.0] * y")));
     115      Console.WriteLine(formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));
     116
    113117      Console.WriteLine(formatter.Format(parser.Parse("x='bla' * y")));
    114118      Console.WriteLine(formatter.Format(parser.Parse("x = 'bla'")));
    115119      Console.WriteLine(formatter.Format(parser.Parse("x = \"bla\"")));
    116120      Console.WriteLine(formatter.Format(parser.Parse("1.0 * x = bla")));
    117       Console.WriteLine(formatter.Format(parser.Parse("1.0 * \"x\" = bla + y = \"bla2\"")));
     121      Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla")));
     122      Console.WriteLine(formatter.Format(parser.Parse("+1.0 * \"x\" = bla + y = \"bla2\"")));
    118123    }
    119124  }
Note: See TracChangeset for help on using the changeset viewer.