Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/21 10:34:24 (3 years ago)
Author:
gkronber
Message:

#3140: fixed parsing of Numbers in SymbolicExpressionImporter

Location:
branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r18114 r18123  
    9797
    9898    public ISymbolicExpressionTree Import(string str) {
    99       str = str.Replace("(", " ( ").Replace(")", " ) ");
     99      str = str.Replace("(", " ( ").Replace(")", " ) ")
     100        .Replace("<", " < ").Replace(">", " > ")
     101        .Replace("=", " = ");
    100102      ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
    101103      ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
     
    159161        Expect(Token.RPAR, tokens);
    160162        return tree;
    161       } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     163      } else if (tokens.Peek().Symbol == TokenSymbol.CONSTANT) {
    162164        var t = (INumericTreeNode)number.CreateTreeNode();
    163165        t.Value = tokens.Dequeue().DoubleValue;
     166        return t;
     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);
    164181        return t;
    165182      } else throw new FormatException("Expected function or number symbol");
     
    224241
    225242      var weights = new List<double>();
    226       while (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
     243      while (tokens.Peek().Symbol == TokenSymbol.CONSTANT) {
    227244        weights.Add(tokens.Dequeue().DoubleValue);
    228245      }
     
    252269
    253270      var weightTok = tokens.Dequeue();
    254       Debug.Assert(weightTok.Symbol == TokenSymbol.NUMBER);
     271      Debug.Assert(weightTok.Symbol == TokenSymbol.CONSTANT);
    255272      t.Weight = weightTok.DoubleValue;
    256273
     
    270287
    271288    private ISymbolicExpressionTreeNode CreateTree(Token token) {
    272       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);
    273293      return knownSymbols[token.StringValue].CreateTreeNode();
    274294    }
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs

    r17180 r18123  
    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.