- Timestamp:
- 12/30/16 19:35:15 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
r14185 r14534 35 35 private const string INVOKESTART = "CALL"; 36 36 private const string TIMELAGSTART = "LAG"; 37 private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>() 37 private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>() 38 38 { 39 39 {"+", new Addition()}, … … 45 45 {"POW", new Power()}, 46 46 {"ROOT", new Root()}, 47 {"SQR", new Square()}, 48 {"SQRT", new SquareRoot()}, 47 49 {"SIN",new Sine()}, 48 50 {"COS", new Cosine()}, … … 74 76 {"PROG", new ProgramRootSymbol()}, 75 77 {"MAIN", new StartSymbol()}, 78 {"FACTOR", new FactorVariable() }, 79 {"BINARYFACTOR", new BinaryFactorVariable()} 76 80 }; 77 81 … … 82 86 TimeLag timeLag = new TimeLag(); 83 87 Integral integral = new Integral(); 88 FactorVariable factorVar = new FactorVariable(); 89 BinaryFactorVariable binFactorVar = new BinaryFactorVariable(); 84 90 85 91 ProgramRootSymbol programRootSymbol = new ProgramRootSymbol(); … … 91 97 ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode(); 92 98 ISymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str))); 93 if 99 if(mainBranch.Symbol is ProgramRootSymbol) { 94 100 // when a root symbol was parsed => use main branch as root 95 101 root = mainBranch; … … 111 117 112 118 private ISymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) { 113 if 119 if(tokens.Peek().Symbol == TokenSymbol.LPAR) { 114 120 ISymbolicExpressionTreeNode tree; 115 121 Expect(Token.LPAR, tokens); 116 if 122 if(tokens.Peek().StringValue.StartsWith(VARSTART)) { 117 123 tree = ParseVariable(tokens); 118 } else if 124 } else if(tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) { 119 125 tree = ParseLaggedVariable(tokens); 120 } else if 126 } else if(tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) { 121 127 tree = ParseTimeLag(tokens); 122 128 tree.AddSubtree(ParseSexp(tokens)); 123 } else if 129 } else if(tokens.Peek().StringValue.StartsWith(INTEGRALSTART)) { 124 130 tree = ParseIntegral(tokens); 125 131 tree.AddSubtree(ParseSexp(tokens)); 126 } else if 132 } else if(tokens.Peek().StringValue.StartsWith(DEFUNSTART)) { 127 133 tree = ParseDefun(tokens); 128 while 134 while(!tokens.Peek().Equals(Token.RPAR)) { 129 135 tree.AddSubtree(ParseSexp(tokens)); 130 136 } 131 } else if 137 } else if(tokens.Peek().StringValue.StartsWith(ARGSTART)) { 132 138 tree = ParseArgument(tokens); 133 } else if 139 } else if(tokens.Peek().StringValue.StartsWith(INVOKESTART)) { 134 140 tree = ParseInvoke(tokens); 135 while 141 while(!tokens.Peek().Equals(Token.RPAR)) { 136 142 tree.AddSubtree(ParseSexp(tokens)); 137 143 } 144 } else if(tokens.Peek().StringValue.StartsWith("FACTOR")) { 145 tree = ParseFactor(tokens); 146 } else if(tokens.Peek().StringValue.StartsWith("BINARYFACTOR")) { 147 tree = ParseBinaryFactor(tokens); 138 148 } else { 139 149 Token curToken = tokens.Dequeue(); 140 150 tree = CreateTree(curToken); 141 while 151 while(!tokens.Peek().Equals(Token.RPAR)) { 142 152 tree.AddSubtree(ParseSexp(tokens)); 143 153 } … … 145 155 Expect(Token.RPAR, tokens); 146 156 return tree; 147 } else if 157 } else if(tokens.Peek().Symbol == TokenSymbol.NUMBER) { 148 158 ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode(); 149 159 t.Value = tokens.Dequeue().DoubleValue; … … 201 211 } 202 212 213 private ISymbolicExpressionTreeNode ParseFactor(Queue<Token> tokens) { 214 Token tok = tokens.Dequeue(); 215 Debug.Assert(tok.StringValue == "FACTOR"); 216 FactorVariableTreeNode t = (FactorVariableTreeNode)(new FactorVariable()).CreateTreeNode(); // create a new symbol each time on purpose 217 var varNameTok = tokens.Dequeue(); 218 Debug.Assert(tok.Symbol == TokenSymbol.SYMB); 219 t.VariableName = varNameTok.StringValue; 220 221 var weights = new List<double>(); 222 while(tokens.Peek().Symbol == TokenSymbol.NUMBER) { 223 weights.Add(tokens.Dequeue().DoubleValue); 224 } 225 226 t.Weights = weights.ToArray(); 227 228 // create a set of (virtual) values to match the number of weights 229 t.Symbol.VariableNames = new string[] { t.VariableName }; 230 t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "x" + i).ToList()) }; 231 return t; 232 } 233 234 private ISymbolicExpressionTreeNode ParseBinaryFactor(Queue<Token> tokens) { 235 Token tok = tokens.Dequeue(); 236 Debug.Assert(tok.StringValue == "BINFACTOR"); 237 var t = (BinaryFactorVariableTreeNode)binFactorVar.CreateTreeNode(); 238 var varNameTok = tokens.Dequeue(); 239 Debug.Assert(tok.Symbol == TokenSymbol.SYMB); 240 t.VariableName = varNameTok.StringValue; 241 242 var varValTok = tokens.Dequeue(); 243 Debug.Assert(tok.Symbol == TokenSymbol.SYMB); 244 t.VariableValue = varNameTok.StringValue; 245 246 var weightTok = tokens.Dequeue(); 247 Debug.Assert(tok.Symbol == TokenSymbol.NUMBER); 248 t.Weight = weightTok.DoubleValue; 249 250 return t; 251 } 252 253 203 254 private ISymbolicExpressionTreeNode ParseLaggedVariable(Queue<Token> tokens) { 204 255 Token varTok = tokens.Dequeue(); … … 212 263 213 264 private ISymbolicExpressionTreeNode CreateTree(Token token) { 214 if 265 if(token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue); 215 266 return knownSymbols[token.StringValue].CreateTreeNode(); 216 267 } … … 218 269 private void Expect(Token token, Queue<Token> tokens) { 219 270 Token cur = tokens.Dequeue(); 220 if 271 if(!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue); 221 272 } 222 273 }
Note: See TracChangeset
for help on using the changeset viewer.