[2447] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.GP.Interfaces;
|
---|
| 27 | using System.IO;
|
---|
| 28 | using System.Diagnostics;
|
---|
| 29 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.GP.Test {
|
---|
| 32 | class SymbolicExpressionImporter {
|
---|
| 33 | private const string DIFFSTART = "dif";
|
---|
| 34 | private const string VARSTART = "var";
|
---|
| 35 | private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>()
|
---|
| 36 | {
|
---|
| 37 | {"+", new Addition()},
|
---|
| 38 | {"and", new And()},
|
---|
| 39 | {"mean", new Average()},
|
---|
| 40 | {"cos", new Cosinus()},
|
---|
| 41 | {"/", new Division()},
|
---|
| 42 | {"equ", new Equal()},
|
---|
| 43 | {"exp", new Exponential()},
|
---|
| 44 | {">", new GreaterThan()},
|
---|
| 45 | {"if", new IfThenElse()},
|
---|
| 46 | {"<", new LessThan()},
|
---|
| 47 | {"log", new Logarithm()},
|
---|
| 48 | {"*", new Multiplication()},
|
---|
| 49 | {"not", new Not()},
|
---|
| 50 | {"or", new Or()},
|
---|
| 51 | {"expt", new Power()},
|
---|
| 52 | {"sign", new Signum()},
|
---|
| 53 | {"sin",new Sinus()},
|
---|
| 54 | {"sqrt", new Sqrt()},
|
---|
| 55 | {"-", new Subtraction()},
|
---|
| 56 | {"tan", new Tangens()},
|
---|
| 57 | {"xor", new Xor()}
|
---|
| 58 | };
|
---|
| 59 | Constant constant = new Constant();
|
---|
| 60 | HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable();
|
---|
| 61 | Differential differential = new Differential();
|
---|
| 62 |
|
---|
| 63 | public SymbolicExpressionImporter() {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | internal IFunctionTree Import(string str) {
|
---|
| 67 | str = str.Replace("(", " ( ").Replace(")", " ) ");
|
---|
| 68 | return ParseSexp(new Queue<Token>(GetTokenStream(str)));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private IEnumerable<Token> GetTokenStream(string str) {
|
---|
| 72 | return
|
---|
| 73 | from strToken in str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
|
---|
| 74 | let t = Token.Parse(strToken)
|
---|
| 75 | where t != null
|
---|
| 76 | select t;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {
|
---|
| 80 | if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
|
---|
| 81 | IFunctionTree tree;
|
---|
| 82 | Expect(Token.LPAR, tokens);
|
---|
| 83 | if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
|
---|
| 84 | tree = ParseVariable(tokens);
|
---|
| 85 | } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) {
|
---|
| 86 | tree = ParseDifferential(tokens);
|
---|
| 87 | } else {
|
---|
| 88 | Token curToken = tokens.Dequeue();
|
---|
| 89 | tree = CreateTree(curToken);
|
---|
| 90 | while (!tokens.Peek().Equals(Token.RPAR)) {
|
---|
| 91 | tree.AddSubTree(ParseSexp(tokens));
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | Expect(Token.RPAR, tokens);
|
---|
| 95 | return tree;
|
---|
| 96 | } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
|
---|
| 97 | ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
|
---|
| 98 | t.Value = tokens.Dequeue().DoubleValue;
|
---|
| 99 | return t;
|
---|
| 100 | } else throw new FormatException("Expected function or constant symbol");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | private IFunctionTree ParseDifferential(Queue<Token> tokens) {
|
---|
| 104 | Token diffTok = tokens.Dequeue();
|
---|
| 105 | Debug.Assert(diffTok.StringValue == "differential");
|
---|
| 106 | VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode();
|
---|
| 107 | t.Weight = tokens.Dequeue().DoubleValue;
|
---|
| 108 | t.VariableName = tokens.Dequeue().StringValue;
|
---|
| 109 | t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
|
---|
| 110 | return t;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private IFunctionTree ParseVariable(Queue<Token> tokens) {
|
---|
| 114 | Token varTok = tokens.Dequeue();
|
---|
| 115 | Debug.Assert(varTok.StringValue == "variable");
|
---|
| 116 | VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode();
|
---|
| 117 | t.Weight = tokens.Dequeue().DoubleValue;
|
---|
| 118 | t.VariableName = tokens.Dequeue().StringValue;
|
---|
| 119 | t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
|
---|
| 120 | return t;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private IFunctionTree CreateTree(Token token) {
|
---|
| 124 | if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
|
---|
| 125 | return knownFunctions[token.StringValue].GetTreeNode();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private void Expect(Token token, Queue<Token> tokens) {
|
---|
| 129 | Token cur = tokens.Dequeue();
|
---|
| 130 | if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|