Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicExpressionImporter.cs @ 6089

Last change on this file since 6089 was 5465, checked in by gkronber, 14 years ago

#1227 implemented test cases and transformation rules for root and power symbols.

File size: 6.8 KB
RevLine 
[2447]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2447]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;
[4068]24using System.Diagnostics;
[2447]25using System.Linq;
[4068]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[3733]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
[2447]29
[3733]30namespace HeuristicLab.Problems.DataAnalysis.Tests {
31  internal class SymbolicExpressionImporter {
[3746]32    private const string VARSTART = "VAR";
33    private const string DEFUNSTART = "DEFUN";
34    private const string ARGSTART = "ARG";
35    private const string INVOKESTART = "CALL";
[3733]36    private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
[2447]37      {
38        {"+", new Addition()},
39        {"/", new Division()},
40        {"*", new Multiplication()},
41        {"-", new Subtraction()},
[3841]42        {"EXP", new Exponential()},
43        {"LOG", new Logarithm()},
[5465]44        {"POW", new Power()},
45        {"ROOT", new Root()},
[3841]46        {"SIN",new Sine()},
47        {"COS", new Cosine()},
48        {"TAN", new Tangent()},
49        {"MEAN", new Average()},
50        {"IF", new IfThenElse()},
51        {">", new GreaterThan()},
52        {"<", new LessThan()},
53        {"AND", new And()},
54        {"OR", new Or()},
55        {"NOT", new Not()},
[3746]56        {"PROG", new ProgramRootSymbol()},
[3841]57        {"MAIN", new StartSymbol()},
[3746]58      };
[2622]59
[2447]60    Constant constant = new Constant();
[3733]61    Variable variable = new Variable();
[3746]62    Defun defun = new Defun();
63
[3733]64    ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
65    StartSymbol startSymbol = new StartSymbol();
66
[2447]67    public SymbolicExpressionImporter() {
68    }
69
[3733]70    internal SymbolicExpressionTree Import(string str) {
[2447]71      str = str.Replace("(", " ( ").Replace(")", " ) ");
[3733]72      SymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
73      SymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
74      SymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str)));
[3746]75      if (mainBranch.Symbol is ProgramRootSymbol) {
76        // when a root symbol was parsed => use main branch as root
77        root = mainBranch;
78      } else {
79        // only a main branch was given => insert the main branch into the default tree template
80        root.AddSubTree(start);
81        start.AddSubTree(mainBranch);
82      }
[3733]83      return new SymbolicExpressionTree(root);
[2447]84    }
85
86    private IEnumerable<Token> GetTokenStream(string str) {
87      return
88             from strToken in str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
89             let t = Token.Parse(strToken)
90             where t != null
91             select t;
92    }
93
[3733]94    private SymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) {
[2447]95      if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
[3733]96        SymbolicExpressionTreeNode tree;
[2447]97        Expect(Token.LPAR, tokens);
98        if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
99          tree = ParseVariable(tokens);
[3746]100        } else if (tokens.Peek().StringValue.StartsWith(DEFUNSTART)) {
101          tree = ParseDefun(tokens);
102          while (!tokens.Peek().Equals(Token.RPAR)) {
103            tree.AddSubTree(ParseSexp(tokens));
104          }
105        } else if (tokens.Peek().StringValue.StartsWith(ARGSTART)) {
106          tree = ParseArgument(tokens);
107        } else if (tokens.Peek().StringValue.StartsWith(INVOKESTART)) {
108          tree = ParseInvoke(tokens);
109          while (!tokens.Peek().Equals(Token.RPAR)) {
110            tree.AddSubTree(ParseSexp(tokens));
111          }
[2447]112        } else {
113          Token curToken = tokens.Dequeue();
114          tree = CreateTree(curToken);
115          while (!tokens.Peek().Equals(Token.RPAR)) {
116            tree.AddSubTree(ParseSexp(tokens));
117          }
118        }
119        Expect(Token.RPAR, tokens);
120        return tree;
121      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
[3733]122        ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
[2447]123        t.Value = tokens.Dequeue().DoubleValue;
124        return t;
125      } else throw new FormatException("Expected function or constant symbol");
126    }
127
[3746]128    private SymbolicExpressionTreeNode ParseInvoke(Queue<Token> tokens) {
129      Token invokeTok = tokens.Dequeue();
130      Debug.Assert(invokeTok.StringValue == "CALL");
131      InvokeFunction invokeSym = new InvokeFunction(tokens.Dequeue().StringValue);
132      SymbolicExpressionTreeNode invokeNode = invokeSym.CreateTreeNode();
133      return invokeNode;
134    }
135
136    private SymbolicExpressionTreeNode ParseArgument(Queue<Token> tokens) {
137      Token argTok = tokens.Dequeue();
138      Debug.Assert(argTok.StringValue == "ARG");
139      Argument argument = new Argument((int)tokens.Dequeue().DoubleValue);
140      SymbolicExpressionTreeNode argNode = argument.CreateTreeNode();
141      return argNode;
142    }
143
144    private SymbolicExpressionTreeNode ParseDefun(Queue<Token> tokens) {
145      Token defTok = tokens.Dequeue();
146      Debug.Assert(defTok.StringValue == "DEFUN");
147      DefunTreeNode t = (DefunTreeNode)defun.CreateTreeNode();
148      t.FunctionName = tokens.Dequeue().StringValue;
149      return t;
150    }
151
[3733]152    private SymbolicExpressionTreeNode ParseVariable(Queue<Token> tokens) {
[2447]153      Token varTok = tokens.Dequeue();
[3746]154      Debug.Assert(varTok.StringValue == "VARIABLE");
[3733]155      VariableTreeNode t = (VariableTreeNode)variable.CreateTreeNode();
[2447]156      t.Weight = tokens.Dequeue().DoubleValue;
157      t.VariableName = tokens.Dequeue().StringValue;
158      return t;
159    }
160
[3733]161    private SymbolicExpressionTreeNode CreateTree(Token token) {
[2447]162      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
[3733]163      return knownSymbols[token.StringValue].CreateTreeNode();
[2447]164    }
165
166    private void Expect(Token token, Queue<Token> tokens) {
167      Token cur = tokens.Dequeue();
[3733]168      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
[2447]169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.