Changeset 18123
- Timestamp:
- 12/15/21 10:34:24 (3 years ago)
- Location:
- branches/3140_NumberSymbol
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
r18114 r18123 97 97 98 98 public ISymbolicExpressionTree Import(string str) { 99 str = str.Replace("(", " ( ").Replace(")", " ) "); 99 str = str.Replace("(", " ( ").Replace(")", " ) ") 100 .Replace("<", " < ").Replace(">", " > ") 101 .Replace("=", " = "); 100 102 ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode(); 101 103 ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode(); … … 159 161 Expect(Token.RPAR, tokens); 160 162 return tree; 161 } else if (tokens.Peek().Symbol == TokenSymbol. NUMBER) {163 } else if (tokens.Peek().Symbol == TokenSymbol.CONSTANT) { 162 164 var t = (INumericTreeNode)number.CreateTreeNode(); 163 165 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); 164 181 return t; 165 182 } else throw new FormatException("Expected function or number symbol"); … … 224 241 225 242 var weights = new List<double>(); 226 while (tokens.Peek().Symbol == TokenSymbol. NUMBER) {243 while (tokens.Peek().Symbol == TokenSymbol.CONSTANT) { 227 244 weights.Add(tokens.Dequeue().DoubleValue); 228 245 } … … 252 269 253 270 var weightTok = tokens.Dequeue(); 254 Debug.Assert(weightTok.Symbol == TokenSymbol. NUMBER);271 Debug.Assert(weightTok.Symbol == TokenSymbol.CONSTANT); 255 272 t.Weight = weightTok.DoubleValue; 256 273 … … 270 287 271 288 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); 273 293 return knownSymbols[token.StringValue].CreateTreeNode(); 274 294 } -
branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs
r17180 r18123 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; -
branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs
r17820 r18123 230 230 231 231 #region abs 232 AssertEqualAfterSimplification("(abs 2.0)", "2.0");233 AssertEqualAfterSimplification("(abs -2.0)", "2.0"); // constant folding232 AssertEqualAfterSimplification("(abs <num=2.0>)", "2.0"); 233 AssertEqualAfterSimplification("(abs <num=-2.0>)", "2.0"); // constant folding 234 234 AssertEqualAfterSimplification("(abs (exp (variable 2.0 x)))", "(exp (variable 2.0 x)))"); // exp is always positive 235 235 AssertEqualAfterSimplification("(abs (exp (variable 2.0 x)))", "(exp (variable 2.0 x)))"); // exp is always positive … … 238 238 AssertEqualAfterSimplification("(abs (cuberoot (variable 2.0 a)))", "(cuberoot (variable 2.0 a))"); // cuberoot is always positive (for our cases) 239 239 240 AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) 2.0)", "(abs (variable 4.0 x))"); // can multiply positive constants into abs241 AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) -2.0)", "(* (abs (variable 4.0 x)) -1.0)"); // for negative constants keep the sign240 AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) <num=2.0>)", "(abs (variable 4.0 x))"); // can multiply positive constants into abs 241 AssertEqualAfterSimplification("(* (abs (variable 2.0 x)) <num=-2.0>)", "(* (abs (variable 4.0 x)) -1.0)"); // for negative constants keep the sign 242 242 243 243 AssertEqualAfterSimplification("(abs (* (variable 1.0 a) (variable 2.0 b)))", "(* (abs (variable 1.0 a)) (abs (variable 1.0 b)) 2.0))"); … … 265 265 266 266 #region AQ 267 AssertEqualAfterSimplification("(* (aq (variable 1.0 x) (variable 1.0 y)) 2.0)", "(aq (variable 2.0 x) (variable 1.0 y))");268 AssertEqualAfterSimplification("(/ (aq (variable 1.0 x) (variable 1.0 y)) 2.0)", "(aq (variable 0.5 x) (variable 1.0 y))");267 AssertEqualAfterSimplification("(* (aq (variable 1.0 x) (variable 1.0 y)) <num=2.0>)", "(aq (variable 2.0 x) (variable 1.0 y))"); 268 AssertEqualAfterSimplification("(/ (aq (variable 1.0 x) (variable 1.0 y)) <num=2.0>)", "(aq (variable 0.5 x) (variable 1.0 y))"); 269 269 270 270 #endregion 271 271 272 272 #region do not drop subtrees with small weights 273 AssertEqualAfterSimplification("(* 1e-14(variable 1.0 a))", "(variable 1e-14 a)");274 AssertEqualAfterSimplification("(+ (variable 1.0 a) 1e-14)",275 "(+ (variable 1.0 a) 1e-14)");273 AssertEqualAfterSimplification("(* <num=1e-14> (variable 1.0 a))", "(variable 1e-14 a)"); 274 AssertEqualAfterSimplification("(+ (variable 1.0 a) <num=1e-14>)", 275 "(+ (variable 1.0 a) <num=1e-14>)"); 276 276 // a scenario where a term with small weight can have large effect 277 AssertEqualAfterSimplification("(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)",278 "(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)");277 AssertEqualAfterSimplification("(+ (* (pow (variable 1.0 a) <num=10>) <num=1e-14>) 1.0)", 278 "(+ (* (pow (variable 1.0 a) <num=10>) <num=1e-14>) 1.0)"); 279 279 // a test case (from ticket #2985) 280 AssertEqualAfterSimplification("(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)",281 "(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)");280 AssertEqualAfterSimplification("(+ (* (exp (variable 3.5861E+001 a)) <num=5.5606E-016>) <num=5.9323E-002>)", 281 "(+ (* (exp (variable 3.5861E+001 a)) <num=5.5606E-016>) <num=5.9323E-002>)"); 282 282 #endregion 283 283 }
Note: See TracChangeset
for help on using the changeset viewer.