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.Text;
|
---|
23 | using HeuristicLab.GP.Interfaces;
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
28 | public class SymbolicExpressionExporter : IFunctionTreeSerializer, IFunctionTreeNameGenerator {
|
---|
29 | private StringBuilder builder;
|
---|
30 | private string currentIndent;
|
---|
31 |
|
---|
32 | #region IFunctionTreeExporter Members
|
---|
33 |
|
---|
34 | public string Name {
|
---|
35 | get { return "Symbolic Expression Exporter"; }
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | public string Export(IFunctionTree tree) {
|
---|
40 | builder = new StringBuilder();
|
---|
41 | currentIndent = "";
|
---|
42 | BuildExportString(tree);
|
---|
43 | return builder.ToString();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public bool TryExport(IFunctionTree tree, out string exported) {
|
---|
47 | try {
|
---|
48 | exported = Export(tree);
|
---|
49 | return true;
|
---|
50 | } catch(UnknownFunctionException) {
|
---|
51 | exported = "";
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | //public IFunctionTree Import(string tree) {
|
---|
57 | // Queue<string> tokens = new Queue<string>(tree.Split(' ', '\n', '\t'));
|
---|
58 | // return ParseTree(tokens);
|
---|
59 | //}
|
---|
60 |
|
---|
61 | //private IFunctionTree ParseTree(Queue<string> tokens) {
|
---|
62 | // Expect("(", tokens);
|
---|
63 | // string funSymb = tokens.Peek();
|
---|
64 | // double constValue;
|
---|
65 | // if (double.TryParse(funSymb, out constValue)) {
|
---|
66 | // throw new UnknownFunctionException(funSymb);
|
---|
67 | // }
|
---|
68 | // switch (funSymb) {
|
---|
69 | // case "variable": return ParseVariable(tokens);
|
---|
70 | // case "differential": return ParseDifferential(tokens);
|
---|
71 | // default:
|
---|
72 | // IFunctionTree fNode = ParseFunction(tokens);
|
---|
73 | // Expect(")", tokens);
|
---|
74 | // return fNode;
|
---|
75 | // }
|
---|
76 |
|
---|
77 | //}
|
---|
78 |
|
---|
79 | //private IFunctionTree ParseFunction(Queue<string> tokens) {
|
---|
80 | // string symb = tokens.Dequeue();
|
---|
81 | // IFunctionTree funTree;
|
---|
82 | // switch (symb) {
|
---|
83 | // case "+": funTree = new Addition().GetTreeNode(); break;
|
---|
84 | // case "-": funTree = new Subtraction().GetTreeNode(); break;
|
---|
85 | // case "*": funTree = new Multiplication().GetTreeNode(); break;
|
---|
86 | // }
|
---|
87 |
|
---|
88 | // while (tokens.Peek() == "(") {
|
---|
89 | // funTree.AddSubTree(ParseTree(tokens));
|
---|
90 | // }
|
---|
91 | // return funTree;
|
---|
92 | //}
|
---|
93 |
|
---|
94 | //private IFunctionTree ParseDifferential(Queue<string> tokens) {
|
---|
95 | // string symb = tokens.Dequeue();
|
---|
96 | // double weight = double.Parse(tokens.Dequeue());
|
---|
97 | // string name = tokens.Dequeue();
|
---|
98 | // int sampleOffset = int.Parse(tokens.Dequeue());
|
---|
99 | // VariableFunctionTree varFunTree = new Differential().GetTreeNode();
|
---|
100 | // varFunTree.Weight = weight;
|
---|
101 | // varFunTree.SampleOffset = sampleOffset;
|
---|
102 | // varFunTree.VariableName = name;
|
---|
103 | // return varFunTree;
|
---|
104 | //}
|
---|
105 |
|
---|
106 | //private IFunctionTree ParseVariable(Queue<string> tokens) {
|
---|
107 | // string symb = tokens.Dequeue();
|
---|
108 | // double weight = double.Parse(tokens.Dequeue());
|
---|
109 | // string name = tokens.Dequeue();
|
---|
110 | // int sampleOffset = int.Parse(tokens.Dequeue());
|
---|
111 | // VariableFunctionTree varFunTree = new Variable().GetTreeNode();
|
---|
112 | // varFunTree.Weight = weight;
|
---|
113 | // varFunTree.SampleOffset = sampleOffset;
|
---|
114 | // varFunTree.VariableName = name;
|
---|
115 | // return varFunTree;
|
---|
116 | //}
|
---|
117 |
|
---|
118 | //private void Expect(string p, Queue<string> tokens) {
|
---|
119 | // var next = tokens.Dequeue();
|
---|
120 | // if (p != next) throw new FormatException("Expected symbol: " + p + ", found symbol: " + next);
|
---|
121 | //}
|
---|
122 |
|
---|
123 | //public bool TryImport(string tree, out IFunctionTree importedTree) {
|
---|
124 | // try {
|
---|
125 | // importedTree = Import(tree);
|
---|
126 | // return true;
|
---|
127 | // }
|
---|
128 | // catch (UnknownFunctionException) {
|
---|
129 | // importedTree = null;
|
---|
130 | // return false;
|
---|
131 | // }
|
---|
132 | //}
|
---|
133 |
|
---|
134 | private void BuildExportString(IFunctionTree tree) {
|
---|
135 | builder.Append(currentIndent);
|
---|
136 | builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
|
---|
137 | currentIndent += " ";
|
---|
138 | foreach(IFunctionTree subTree in tree.SubTrees) {
|
---|
139 | builder.Append("\n");
|
---|
140 | BuildExportString(subTree);
|
---|
141 | }
|
---|
142 | if(tree.SubTrees.Count > 0) builder.Append(")");
|
---|
143 | currentIndent = currentIndent.Remove(0, 2);
|
---|
144 | }
|
---|
145 |
|
---|
146 | private string ExportFunction(IFunction function, IFunctionTree tree) {
|
---|
147 | // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
|
---|
148 | // please let me know! (gkronber 14.10.2008)
|
---|
149 | if(function is Addition) return ((Addition)function).ExportToScheme();
|
---|
150 | if(function is And) return ((And)function).ExportToScheme();
|
---|
151 | if(function is Average) return ((Average)function).ExportToScheme();
|
---|
152 | if(function is Constant) return ((Constant)function).ExportToScheme(tree);
|
---|
153 | if(function is Cosinus) return ((Cosinus)function).ExportToScheme();
|
---|
154 | if(function is Differential) return ((Differential)function).ExportToScheme(tree);
|
---|
155 | if(function is Division) return ((Division)function).ExportToScheme();
|
---|
156 | if(function is Equal) return ((Equal)function).ExportToScheme();
|
---|
157 | if(function is Exponential) return ((Exponential)function).ExportToScheme();
|
---|
158 | if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
|
---|
159 | if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
|
---|
160 | if(function is LessThan) return ((LessThan)function).ExportToScheme();
|
---|
161 | if(function is Logarithm) return ((Logarithm)function).ExportToScheme();
|
---|
162 | if(function is Multiplication) return ((Multiplication)function).ExportToScheme();
|
---|
163 | if(function is Not) return ((Not)function).ExportToScheme();
|
---|
164 | if(function is Or) return ((Or)function).ExportToScheme();
|
---|
165 | if(function is Power) return ((Power)function).ExportToScheme();
|
---|
166 | if(function is Signum) return ((Signum)function).ExportToScheme();
|
---|
167 | if(function is Sinus) return ((Sinus)function).ExportToScheme();
|
---|
168 | if(function is Sqrt) return ((Sqrt)function).ExportToScheme();
|
---|
169 | if(function is Subtraction) return ((Subtraction)function).ExportToScheme();
|
---|
170 | if(function is Tangens) return ((Tangens)function).ExportToScheme();
|
---|
171 | if(function is Variable) return ((Variable)function).ExportToScheme(tree);
|
---|
172 | if(function is Xor) return ((Xor)function).ExportToScheme();
|
---|
173 | throw new UnknownFunctionException(function.Name);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region IFunctionTreeNameGenerator Members
|
---|
180 |
|
---|
181 | string IFunctionTreeNameGenerator.Name {
|
---|
182 | get { return "Symbolic Expression"; }
|
---|
183 | }
|
---|
184 |
|
---|
185 | public string GetName(IFunctionTree tree) {
|
---|
186 | string name = "";
|
---|
187 | try {
|
---|
188 | name = ExportFunction(tree.Function, tree);
|
---|
189 | } catch(UnknownFunctionException) {
|
---|
190 | name = "N/A";
|
---|
191 | }
|
---|
192 | return name;
|
---|
193 | }
|
---|
194 |
|
---|
195 | #endregion
|
---|
196 | }
|
---|
197 |
|
---|
198 | internal static class SchemeExporterExtensions {
|
---|
199 | public static string ExportToScheme(this Addition addition) {
|
---|
200 | return "+";
|
---|
201 | }
|
---|
202 |
|
---|
203 | public static string ExportToScheme(this Constant constant, IFunctionTree tree) {
|
---|
204 | return ((ConstantFunctionTree)tree).Value.ToString("r");
|
---|
205 | }
|
---|
206 |
|
---|
207 | public static string ExportToScheme(this Cosinus cosinus) {
|
---|
208 | return "cos";
|
---|
209 | }
|
---|
210 |
|
---|
211 | public static string ExportToScheme(this Division division) {
|
---|
212 | return "/";
|
---|
213 | }
|
---|
214 |
|
---|
215 | public static string ExportToScheme(this Exponential exponential) {
|
---|
216 | return "exp";
|
---|
217 | }
|
---|
218 |
|
---|
219 | public static string ExportToScheme(this Logarithm logarithm) {
|
---|
220 | return "log";
|
---|
221 | }
|
---|
222 |
|
---|
223 | public static string ExportToScheme(this Multiplication multiplication) {
|
---|
224 | return "*";
|
---|
225 | }
|
---|
226 |
|
---|
227 | public static string ExportToScheme(this Power power) {
|
---|
228 | return "expt";
|
---|
229 | }
|
---|
230 |
|
---|
231 | public static string ExportToScheme(this Signum signum) {
|
---|
232 | return "sign";
|
---|
233 | }
|
---|
234 |
|
---|
235 | public static string ExportToScheme(this Sinus sinus) {
|
---|
236 | return "sin";
|
---|
237 | }
|
---|
238 |
|
---|
239 | public static string ExportToScheme(this Sqrt sqrt) {
|
---|
240 | return "sqrt";
|
---|
241 | }
|
---|
242 |
|
---|
243 | public static string ExportToScheme(this Subtraction subtraction) {
|
---|
244 | return "-";
|
---|
245 | }
|
---|
246 |
|
---|
247 | public static string ExportToScheme(this Tangens tangens) {
|
---|
248 | return "tan";
|
---|
249 | }
|
---|
250 |
|
---|
251 | public static string ExportToScheme(this Variable variable, IFunctionTree tree) {
|
---|
252 | var varTree = (VariableFunctionTree)tree;
|
---|
253 | return "(variable " + varTree.Weight.ToString("r") + " " +
|
---|
254 | varTree.VariableName + " " + varTree.SampleOffset + ")";
|
---|
255 | }
|
---|
256 | public static string ExportToScheme(this Differential differential, IFunctionTree tree) {
|
---|
257 | var varTree = (VariableFunctionTree)tree;
|
---|
258 | return "(differential " + varTree.Weight.ToString("r") + " " +
|
---|
259 | varTree.VariableName + " " + varTree.SampleOffset + ")";
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static string ExportToScheme(this And and) {
|
---|
263 | return "and";
|
---|
264 | }
|
---|
265 |
|
---|
266 | public static string ExportToScheme(this Average average) {
|
---|
267 | return "mean";
|
---|
268 | }
|
---|
269 |
|
---|
270 | public static string ExportToScheme(this IfThenElse ifThenElse) {
|
---|
271 | return "if";
|
---|
272 | }
|
---|
273 |
|
---|
274 | public static string ExportToScheme(this Not not) {
|
---|
275 | return "not";
|
---|
276 | }
|
---|
277 |
|
---|
278 | public static string ExportToScheme(this Or or) {
|
---|
279 | return "or";
|
---|
280 | }
|
---|
281 |
|
---|
282 | public static string ExportToScheme(this Xor xor) {
|
---|
283 | return "xor";
|
---|
284 | }
|
---|
285 |
|
---|
286 | public static string ExportToScheme(this Equal equal) {
|
---|
287 | return "equ";
|
---|
288 | }
|
---|
289 |
|
---|
290 | public static string ExportToScheme(this LessThan lessThan) {
|
---|
291 | return "<";
|
---|
292 | }
|
---|
293 |
|
---|
294 | public static string ExportToScheme(this GreaterThan greaterThan) {
|
---|
295 | return ">";
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|