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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
29 | internal class SymbolicExpressionImporter {
|
---|
30 | private const string VARSTART = "VAR";
|
---|
31 | private const string LAGGEDVARSTART = "LAGVARIABLE";
|
---|
32 | private const string INTEGRALSTART = "INTEG";
|
---|
33 | private const string DEFUNSTART = "DEFUN";
|
---|
34 | private const string ARGSTART = "ARG";
|
---|
35 | private const string INVOKESTART = "CALL";
|
---|
36 | private const string TIMELAGSTART = "LAG";
|
---|
37 | private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
|
---|
38 | {
|
---|
39 | {"+", new Addition()},
|
---|
40 | {"/", new Division()},
|
---|
41 | {"*", new Multiplication()},
|
---|
42 | {"-", new Subtraction()},
|
---|
43 | {"EXP", new Exponential()},
|
---|
44 | {"LOG", new Logarithm()},
|
---|
45 | {"POW", new Power()},
|
---|
46 | {"ROOT", new Root()},
|
---|
47 | {"SIN",new Sine()},
|
---|
48 | {"COS", new Cosine()},
|
---|
49 | {"TAN", new Tangent()},
|
---|
50 | {"MEAN", new Average()},
|
---|
51 | {"IF", new IfThenElse()},
|
---|
52 | {">", new GreaterThan()},
|
---|
53 | {"<", new LessThan()},
|
---|
54 | {"AND", new And()},
|
---|
55 | {"OR", new Or()},
|
---|
56 | {"NOT", new Not()},
|
---|
57 | {"DIFF", new Derivative()},
|
---|
58 | {"PROG", new ProgramRootSymbol()},
|
---|
59 | {"MAIN", new StartSymbol()},
|
---|
60 | };
|
---|
61 |
|
---|
62 | Constant constant = new Constant();
|
---|
63 | Variable variable = new Variable();
|
---|
64 | LaggedVariable laggedVariable = new LaggedVariable();
|
---|
65 | Defun defun = new Defun();
|
---|
66 | TimeLag timeLag = new TimeLag();
|
---|
67 | Integral integral = new Integral();
|
---|
68 |
|
---|
69 | ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
|
---|
70 | StartSymbol startSymbol = new StartSymbol();
|
---|
71 |
|
---|
72 | public SymbolicExpressionImporter() {
|
---|
73 | }
|
---|
74 |
|
---|
75 | internal ISymbolicExpressionTree Import(string str) {
|
---|
76 | str = str.Replace("(", " ( ").Replace(")", " ) ");
|
---|
77 | ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
|
---|
78 | ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
|
---|
79 | var q = new Queue<Token>(GetTokenStream(str));
|
---|
80 | ISymbolicExpressionTreeNode mainBranch = ParseSexp(q);
|
---|
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 | }
|
---|