Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionImporter.cs @ 8085

Last change on this file since 8085 was 8085, checked in by gkronber, 12 years ago

#1847: merged trunk changes r7800:HEAD into gp move operators branch

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic_34.Tests {
30  internal class SymbolicExpressionImporter {
31    private const string VARSTART = "VAR";
32    private const string LAGGEDVARSTART = "LAGVARIABLE";
33    private const string INTEGRALSTART = "INTEG";
34    private const string DEFUNSTART = "DEFUN";
35    private const string ARGSTART = "ARG";
36    private const string INVOKESTART = "CALL";
37    private const string TIMELAGSTART = "LAG";
38    private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
39      {
40        {"+", new Addition()},
41        {"/", new Division()},
42        {"*", new Multiplication()},
43        {"-", new Subtraction()},
44        {"EXP", new Exponential()},
45        {"LOG", new Logarithm()},
46        {"POW", new Power()},
47        {"ROOT", new Root()},
48        {"SIN",new Sine()},
49        {"COS", new Cosine()},
50        {"TAN", new Tangent()},
51        {"MEAN", new Average()},
52        {"IF", new IfThenElse()},
53        {">", new GreaterThan()},
54        {"<", new LessThan()},
55        {"AND", new And()},
56        {"OR", new Or()},
57        {"NOT", new Not()},
58        {"DIFF", new Derivative()},
59        {"PROG", new ProgramRootSymbol()},
60        {"MAIN", new StartSymbol()},
61      };
62
63    Constant constant = new Constant();
64    Variable variable = new Variable();
65    LaggedVariable laggedVariable = new LaggedVariable();
66    Defun defun = new Defun();
67    TimeLag timeLag = new TimeLag();
68    Integral integral = new Integral();
69
70    ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
71    StartSymbol startSymbol = new StartSymbol();
72
73    public SymbolicExpressionImporter() {
74    }
75
76    internal ISymbolicExpressionTree Import(string str) {
77      str = str.Replace("(", " ( ").Replace(")", " ) ");
78      ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
79      ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
80      ISymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str)));
81      if (mainBranch.Symbol is ProgramRootSymbol) {
82        // when a root symbol was parsed => use main branch as root
83        root = mainBranch;
84      } else {
85        // only a main branch was given => insert the main branch into the default tree template
86        root.AddSubtree(start);
87        start.AddSubtree(mainBranch);
88      }
89      return new SymbolicExpressionTree(root);
90    }
91
92    private IEnumerable<Token> GetTokenStream(string str) {
93      return
94             from strToken in str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
95             let t = Token.Parse(strToken)
96             where t != null
97             select t;
98    }
99
100    private ISymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) {
101      if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
102        ISymbolicExpressionTreeNode tree;
103        Expect(Token.LPAR, tokens);
104        if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
105          tree = ParseVariable(tokens);
106        } else if (tokens.Peek().StringValue.StartsWith(LAGGEDVARSTART)) {
107          tree = ParseLaggedVariable(tokens);
108        } else if (tokens.Peek().StringValue.StartsWith(TIMELAGSTART)) {
109          tree = ParseTimeLag(tokens);
110          tree.AddSubtree(ParseSexp(tokens));
111        } else if (tokens.Peek().StringValue.StartsWith(INTEGRALSTART)) {
112          tree = ParseIntegral(tokens);
113          tree.AddSubtree(ParseSexp(tokens));
114        } else if (tokens.Peek().StringValue.StartsWith(DEFUNSTART)) {
115          tree = ParseDefun(tokens);
116          while (!tokens.Peek().Equals(Token.RPAR)) {
117            tree.AddSubtree(ParseSexp(tokens));
118          }
119        } else if (tokens.Peek().StringValue.StartsWith(ARGSTART)) {
120          tree = ParseArgument(tokens);
121        } else if (tokens.Peek().StringValue.StartsWith(INVOKESTART)) {
122          tree = ParseInvoke(tokens);
123          while (!tokens.Peek().Equals(Token.RPAR)) {
124            tree.AddSubtree(ParseSexp(tokens));
125          }
126        } else {
127          Token curToken = tokens.Dequeue();
128          tree = CreateTree(curToken);
129          while (!tokens.Peek().Equals(Token.RPAR)) {
130            tree.AddSubtree(ParseSexp(tokens));
131          }
132        }
133        Expect(Token.RPAR, tokens);
134        return tree;
135      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
136        ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
137        t.Value = tokens.Dequeue().DoubleValue;
138        return t;
139      } else throw new FormatException("Expected function or constant symbol");
140    }
141
142    private ISymbolicExpressionTreeNode ParseInvoke(Queue<Token> tokens) {
143      Token invokeTok = tokens.Dequeue();
144      Debug.Assert(invokeTok.StringValue == "CALL");
145      InvokeFunction invokeSym = new InvokeFunction(tokens.Dequeue().StringValue);
146      ISymbolicExpressionTreeNode invokeNode = invokeSym.CreateTreeNode();
147      return invokeNode;
148    }
149
150    private ISymbolicExpressionTreeNode ParseArgument(Queue<Token> tokens) {
151      Token argTok = tokens.Dequeue();
152      Debug.Assert(argTok.StringValue == "ARG");
153      Argument argument = new Argument((int)tokens.Dequeue().DoubleValue);
154      ISymbolicExpressionTreeNode argNode = argument.CreateTreeNode();
155      return argNode;
156    }
157
158    private ISymbolicExpressionTreeNode ParseDefun(Queue<Token> tokens) {
159      Token defTok = tokens.Dequeue();
160      Debug.Assert(defTok.StringValue == "DEFUN");
161      DefunTreeNode t = (DefunTreeNode)defun.CreateTreeNode();
162      t.FunctionName = tokens.Dequeue().StringValue;
163      return t;
164    }
165
166    private ISymbolicExpressionTreeNode ParseTimeLag(Queue<Token> tokens) {
167      Token varTok = tokens.Dequeue();
168      Debug.Assert(varTok.StringValue == "LAG");
169      LaggedTreeNode t = (LaggedTreeNode)timeLag.CreateTreeNode();
170      t.Lag = (int)tokens.Dequeue().DoubleValue;
171      return t;
172    }
173
174    private ISymbolicExpressionTreeNode ParseIntegral(Queue<Token> tokens) {
175      Token varTok = tokens.Dequeue();
176      Debug.Assert(varTok.StringValue == "INTEGRAL");
177      LaggedTreeNode t = (LaggedTreeNode)integral.CreateTreeNode();
178      t.Lag = (int)tokens.Dequeue().DoubleValue;
179      return t;
180    }
181
182    private ISymbolicExpressionTreeNode ParseVariable(Queue<Token> tokens) {
183      Token varTok = tokens.Dequeue();
184      Debug.Assert(varTok.StringValue == "VARIABLE");
185      VariableTreeNode t = (VariableTreeNode)variable.CreateTreeNode();
186      t.Weight = tokens.Dequeue().DoubleValue;
187      t.VariableName = tokens.Dequeue().StringValue;
188      return t;
189    }
190
191    private ISymbolicExpressionTreeNode ParseLaggedVariable(Queue<Token> tokens) {
192      Token varTok = tokens.Dequeue();
193      Debug.Assert(varTok.StringValue == "LAGVARIABLE");
194      LaggedVariableTreeNode t = (LaggedVariableTreeNode)laggedVariable.CreateTreeNode();
195      t.Weight = tokens.Dequeue().DoubleValue;
196      t.VariableName = tokens.Dequeue().StringValue;
197      t.Lag = (int)tokens.Dequeue().DoubleValue;
198      return t;
199    }
200
201    private ISymbolicExpressionTreeNode CreateTree(Token token) {
202      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
203      return knownSymbols[token.StringValue].CreateTreeNode();
204    }
205
206    private void Expect(Token token, Queue<Token> tokens) {
207      Token cur = tokens.Dequeue();
208      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.