- Timestamp:
- 12/15/21 11:50:57 (3 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol (added) merged: 18091,18093,18100,18112-18121,18123-18131
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 18093,18100,18112-18116,18118,18121,18123-18124,18129-18130
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r17902 r18132 57 57 /// </summary> 58 58 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 }; 60 60 private class Token { 61 61 internal double doubleVal; … … 82 82 knownSymbols = new BidirectionalLookup<string, ISymbol>(StringComparer.InvariantCulture, new SymbolComparer()); 83 83 84 private Number number = new Number(); 84 85 private Constant constant = new Constant(); 85 86 private Variable variable = new Variable(); … … 180 181 && str[pos] != ']' 181 182 && str[pos] != '}' 182 && str[pos] != ',') { 183 && str[pos] != ',' 184 && str[pos] != '>') { 183 185 sb.Append(str[pos]); 184 186 pos++; … … 265 267 pos++; 266 268 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 = ">"}; 267 275 } else { 268 276 throw new ArgumentException("Invalid character: " + str[pos]); … … 326 334 foreach (var negTerm in negTerms) sumNeg.AddSubtree(negTerm); 327 335 328 var constNode = ( ConstantTreeNode)constant.CreateTreeNode();336 var constNode = (NumberTreeNode)number.CreateTreeNode(); 329 337 constNode.Value = -1.0; 330 338 var prod = GetSymbol("*").CreateTreeNode(); … … 524 532 } 525 533 } 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; 526 558 } else if (next.TokenType == TokenType.Number) { 527 559 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(); 529 563 constNode.Value = numTok.doubleVal; 530 return constNode; 564 return constNode;*/ 531 565 } else { 532 566 throw new ArgumentException(string.Format("unexpected token in expression {0}", next.strVal)); -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
r17180 r18132 85 85 }; 86 86 87 Constant constant = new Constant();87 Number number = new Number(); 88 88 Variable variable = new Variable(); 89 89 LaggedVariable laggedVariable = new LaggedVariable(); … … 91 91 TimeLag timeLag = new TimeLag(); 92 92 Integral integral = new Integral(); 93 FactorVariable factorVar = new FactorVariable();94 93 BinaryFactorVariable binFactorVar = new BinaryFactorVariable(); 95 94 … … 98 97 99 98 public ISymbolicExpressionTree Import(string str) { 100 str = str.Replace("(", " ( ").Replace(")", " ) "); 99 str = str.Replace("(", " ( ").Replace(")", " ) ") 100 .Replace("<", " < ").Replace(">", " > ") 101 .Replace("=", " = "); 101 102 ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode(); 102 103 ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode(); … … 160 161 Expect(Token.RPAR, tokens); 161 162 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(); 164 165 t.Value = tokens.Dequeue().DoubleValue; 165 166 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"); 167 183 } 168 184 … … 225 241 226 242 var weights = new List<double>(); 227 while (tokens.Peek().Symbol == TokenSymbol. NUMBER) {243 while (tokens.Peek().Symbol == TokenSymbol.CONSTANT) { 228 244 weights.Add(tokens.Dequeue().DoubleValue); 229 245 } … … 253 269 254 270 var weightTok = tokens.Dequeue(); 255 Debug.Assert(weightTok.Symbol == TokenSymbol. NUMBER);271 Debug.Assert(weightTok.Symbol == TokenSymbol.CONSTANT); 256 272 t.Weight = weightTok.DoubleValue; 257 273 … … 271 287 272 288 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); 274 293 return knownSymbols[token.StringValue].CreateTreeNode(); 275 294 } -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs
r17180 r18132 23 23 24 24 namespace 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 }; 26 32 internal class Token { 27 33 public static readonly Token LPAR = Token.Parse("("); 28 34 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"); 29 39 30 40 public TokenSymbol Symbol { get; set; } … … 46 56 public static Token Parse(string strToken) { 47 57 strToken = strToken.Trim(); 48 Tokent = new Token();58 var t = new Token(); 49 59 t.StringValue = strToken.Trim(); 50 double temp;51 60 if (strToken == "") { 52 61 t = null; … … 55 64 } else if (strToken == ")") { 56 65 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; 60 77 } else { 61 78 t.Symbol = TokenSymbol.SYMB;
Note: See TracChangeset
for help on using the changeset viewer.