Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/21 11:50:57 (3 years ago)
Author:
gkronber
Message:

#3140: merged r18091:18131 from branch to trunk

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic

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

    r17902 r18132  
    5757  /// </summary>
    5858  public sealed class InfixExpressionParser {
    59     private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, LeftBracket, RightBracket, Comma, Eq, End, NA };
     59    private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, LeftBracket, RightBracket, LeftAngleBracket, RightAngleBracket, Comma, Eq, End, NA };
    6060    private class Token {
    6161      internal double doubleVal;
     
    8282      knownSymbols = new BidirectionalLookup<string, ISymbol>(StringComparer.InvariantCulture, new SymbolComparer());
    8383
     84    private Number number = new Number();
    8485    private Constant constant = new Constant();
    8586    private Variable variable = new Variable();
     
    180181            && str[pos] != ']'
    181182            && str[pos] != '}'
    182             && str[pos] != ',') {
     183            && str[pos] != ','
     184            && str[pos] != '>') {
    183185            sb.Append(str[pos]);
    184186            pos++;
     
    265267          pos++;
    266268          yield return new Token { TokenType = TokenType.Comma, strVal = "," };
     269        } else if (str[pos] == '<') {
     270          pos++;
     271          yield return new Token {TokenType = TokenType.LeftAngleBracket, strVal = "<"};
     272        } else if (str[pos] == '>') {
     273          pos++;
     274          yield return new Token {TokenType = TokenType.RightAngleBracket, strVal = ">"};
    267275        } else {
    268276          throw new ArgumentException("Invalid character: " + str[pos]);
     
    326334          foreach (var negTerm in negTerms) sumNeg.AddSubtree(negTerm);
    327335
    328           var constNode = (ConstantTreeNode)constant.CreateTreeNode();
     336          var constNode = (NumberTreeNode)number.CreateTreeNode();
    329337          constNode.Value = -1.0;
    330338          var prod = GetSymbol("*").CreateTreeNode();
     
    524532          }
    525533        }
     534      } else if (next.TokenType == TokenType.LeftAngleBracket) {
     535        Token numberTok = null;
     536        var leftAngleBracket = tokens.Dequeue();
     537        if (leftAngleBracket.TokenType != TokenType.LeftAngleBracket)
     538          throw new ArgumentException("opening bracket < expected");
     539
     540        var idTok = tokens.Dequeue();
     541        if (idTok.TokenType != TokenType.Identifier || idTok.strVal.ToLower() != "num")
     542          throw new ArgumentException("string 'num' expected");
     543
     544        if (tokens.Peek().TokenType == TokenType.Eq) {
     545          var equalTok = tokens.Dequeue();
     546          if (tokens.Peek().TokenType != TokenType.Number)
     547            throw new ArgumentException("No value for number specified.");
     548
     549          numberTok = tokens.Dequeue();
     550        }
     551
     552        var rightAngleBracket = tokens.Dequeue();
     553        if (rightAngleBracket.TokenType != TokenType.RightAngleBracket)
     554          throw new ArgumentException("closing bracket > expected");
     555        var numNode = (NumberTreeNode)number.CreateTreeNode();
     556        if (numberTok != null) numNode.Value = numberTok.doubleVal;
     557        return numNode;
    526558      } else if (next.TokenType == TokenType.Number) {
    527559        var numTok = tokens.Dequeue();
    528         var constNode = (ConstantTreeNode)constant.CreateTreeNode();
     560        var constSy = new Constant {Value = numTok.doubleVal};
     561        return constSy.CreateTreeNode();
     562        /*var constNode = (ConstantTreeNode)constant.CreateTreeNode();
    529563        constNode.Value = numTok.doubleVal;
    530         return constNode;
     564        return constNode;*/
    531565      } else {
    532566        throw new ArgumentException(string.Format("unexpected token in expression {0}", next.strVal));
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r17180 r18132  
    8585      };
    8686
    87     Constant constant = new Constant();
     87    Number number = new Number();
    8888    Variable variable = new Variable();
    8989    LaggedVariable laggedVariable = new LaggedVariable();
     
    9191    TimeLag timeLag = new TimeLag();
    9292    Integral integral = new Integral();
    93     FactorVariable factorVar = new FactorVariable();
    9493    BinaryFactorVariable binFactorVar = new BinaryFactorVariable();
    9594
     
    9897
    9998    public ISymbolicExpressionTree Import(string str) {
    100       str = str.Replace("(", " ( ").Replace(")", " ) ");
     99      str = str.Replace("(", " ( ").Replace(")", " ) ")
     100        .Replace("<", " < ").Replace(">", " > ")
     101        .Replace("=", " = ");
    101102      ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
    102103      ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
     
    160161        Expect(Token.RPAR, tokens);
    161162        return tree;
    162       } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
    163         ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
     163      } else if (tokens.Peek().Symbol == TokenSymbol.CONSTANT) {
     164        var t = (INumericTreeNode)number.CreateTreeNode();
    164165        t.Value = tokens.Dequeue().DoubleValue;
    165166        return t;
    166       } else throw new FormatException("Expected function or constant symbol");
     167      } else if (tokens.Peek().Symbol == TokenSymbol.LBRACKET) {
     168        Expect(Token.LBRACKET, tokens);
     169        Expect(Token.NUM, tokens);
     170        var t = (INumericTreeNode)number.CreateTreeNode();
     171        if (tokens.Peek().Symbol == TokenSymbol.EQ) {
     172          Expect(Token.EQ, tokens);
     173          var initValToken = tokens.Dequeue();
     174          if(initValToken.Symbol == TokenSymbol.CONSTANT) {
     175            t.Value = initValToken.DoubleValue;
     176          } else {
     177            throw new FormatException("Expected a real value");
     178          }
     179        }
     180        Expect(Token.RBRACKET, tokens);
     181        return t;
     182      } else throw new FormatException("Expected function or number symbol");
    167183    }
    168184
     
    225241
    226242      var weights = new List<double>();
    227       while (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     243      while (tokens.Peek().Symbol == TokenSymbol.CONSTANT) {
    228244        weights.Add(tokens.Dequeue().DoubleValue);
    229245      }
     
    253269
    254270      var weightTok = tokens.Dequeue();
    255       Debug.Assert(weightTok.Symbol == TokenSymbol.NUMBER);
     271      Debug.Assert(weightTok.Symbol == TokenSymbol.CONSTANT);
    256272      t.Weight = weightTok.DoubleValue;
    257273
     
    271287
    272288    private ISymbolicExpressionTreeNode CreateTree(Token token) {
    273       if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
     289      if (token.Symbol != TokenSymbol.SYMB &&
     290          token.Symbol != TokenSymbol.LBRACKET &&  // LBRACKET and RBRACKET are used for <num=..> and as LT, GT operators
     291          token.Symbol != TokenSymbol.RBRACKET 
     292          ) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
    274293      return knownSymbols[token.StringValue].CreateTreeNode();
    275294    }
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs

    r17180 r18132  
    2323
    2424namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    25   internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
     25  internal enum TokenSymbol {
     26    LPAR, RPAR, SYMB, CONSTANT,
     27    NUM, // num
     28    EQ,  // =
     29    LBRACKET, // <
     30    RBRACKET  // >
     31  };
    2632  internal class Token {
    2733    public static readonly Token LPAR = Token.Parse("(");
    2834    public static readonly Token RPAR = Token.Parse(")");
     35    public static readonly Token LBRACKET = Token.Parse("<");
     36    public static readonly Token RBRACKET = Token.Parse(">");
     37    public static readonly Token EQ = Token.Parse("=");
     38    public static readonly Token NUM = Token.Parse("num");
    2939
    3040    public TokenSymbol Symbol { get; set; }
     
    4656    public static Token Parse(string strToken) {
    4757      strToken = strToken.Trim();
    48       Token t = new Token();
     58      var t = new Token();
    4959      t.StringValue = strToken.Trim();
    50       double temp;
    5160      if (strToken == "") {
    5261        t = null;
     
    5564      } else if (strToken == ")") {
    5665        t.Symbol = TokenSymbol.RPAR;
    57       } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) {
    58         t.Symbol = TokenSymbol.NUMBER;
    59         t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat);
     66      } else if (strToken == "<") {
     67        t.Symbol = TokenSymbol.LBRACKET;
     68      } else if (strToken == ">") {
     69        t.Symbol = TokenSymbol.RBRACKET;
     70      } else if (strToken == "=") {
     71        t.Symbol = TokenSymbol.EQ;
     72      } else if (strToken.ToLower() == "num") {
     73        t.Symbol = TokenSymbol.NUM;
     74      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out var val)) {
     75        t.Symbol = TokenSymbol.CONSTANT;
     76        t.DoubleValue = val;
    6077      } else {
    6178        t.Symbol = TokenSymbol.SYMB;
Note: See TracChangeset for help on using the changeset viewer.