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 |
|
---|
25 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
26 | public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
|
---|
27 | private StringBuilder builder;
|
---|
28 | private string currentIndent;
|
---|
29 |
|
---|
30 | #region IFunctionTreeExporter Members
|
---|
31 |
|
---|
32 | public string Name {
|
---|
33 | get { return "Symbolic Expression Exporter"; }
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | public string Export(IFunctionTree tree) {
|
---|
38 | builder = new StringBuilder();
|
---|
39 | currentIndent = "";
|
---|
40 | BuildExportString(tree);
|
---|
41 | return builder.ToString();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public bool TryExport(IFunctionTree tree, out string exported) {
|
---|
45 | try {
|
---|
46 | exported = Export(tree);
|
---|
47 | return true;
|
---|
48 | } catch(UnknownFunctionException) {
|
---|
49 | exported = "";
|
---|
50 | return false;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void BuildExportString(IFunctionTree tree) {
|
---|
55 | builder.Append(currentIndent);
|
---|
56 | builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
|
---|
57 | currentIndent += " ";
|
---|
58 | foreach(IFunctionTree subTree in tree.SubTrees) {
|
---|
59 | builder.Append("\n");
|
---|
60 | BuildExportString(subTree);
|
---|
61 | }
|
---|
62 | if(tree.SubTrees.Count > 0) builder.Append(")");
|
---|
63 | currentIndent = currentIndent.Remove(0, 2);
|
---|
64 | }
|
---|
65 |
|
---|
66 | private string ExportFunction(IFunction function, IFunctionTree tree) {
|
---|
67 | // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
|
---|
68 | // please let me know! (gkronber 14.10.2008)
|
---|
69 | if(function is Addition) return ((Addition)function).ExportToScheme();
|
---|
70 | if(function is And) return ((And)function).ExportToScheme();
|
---|
71 | if(function is Average) return ((Average)function).ExportToScheme();
|
---|
72 | if(function is Constant) return ((Constant)function).ExportToScheme(tree);
|
---|
73 | if(function is Cosinus) return ((Cosinus)function).ExportToScheme();
|
---|
74 | if(function is Differential) return ((Differential)function).ExportToScheme(tree);
|
---|
75 | if(function is Division) return ((Division)function).ExportToScheme();
|
---|
76 | if(function is Equal) return ((Equal)function).ExportToScheme();
|
---|
77 | if(function is Exponential) return ((Exponential)function).ExportToScheme();
|
---|
78 | if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
|
---|
79 | if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
|
---|
80 | if(function is LessThan) return ((LessThan)function).ExportToScheme();
|
---|
81 | if(function is Logarithm) return ((Logarithm)function).ExportToScheme();
|
---|
82 | if(function is Multiplication) return ((Multiplication)function).ExportToScheme();
|
---|
83 | if(function is Not) return ((Not)function).ExportToScheme();
|
---|
84 | if(function is Or) return ((Or)function).ExportToScheme();
|
---|
85 | if(function is Power) return ((Power)function).ExportToScheme();
|
---|
86 | if(function is Signum) return ((Signum)function).ExportToScheme();
|
---|
87 | if(function is Sinus) return ((Sinus)function).ExportToScheme();
|
---|
88 | if(function is Sqrt) return ((Sqrt)function).ExportToScheme();
|
---|
89 | if(function is Subtraction) return ((Subtraction)function).ExportToScheme();
|
---|
90 | if(function is Tangens) return ((Tangens)function).ExportToScheme();
|
---|
91 | if(function is Variable) return ((Variable)function).ExportToScheme(tree);
|
---|
92 | if(function is Xor) return ((Xor)function).ExportToScheme();
|
---|
93 | throw new UnknownFunctionException(function.Name);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | #endregion
|
---|
98 |
|
---|
99 | #region IFunctionTreeNameGenerator Members
|
---|
100 |
|
---|
101 | string IFunctionTreeNameGenerator.Name {
|
---|
102 | get { return "Symbolic Expression"; }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public string GetName(IFunctionTree tree) {
|
---|
106 | string name = "";
|
---|
107 | try {
|
---|
108 | name = ExportFunction(tree.Function, tree);
|
---|
109 | } catch(UnknownFunctionException) {
|
---|
110 | name = "N/A";
|
---|
111 | }
|
---|
112 | return name;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #endregion
|
---|
116 | }
|
---|
117 |
|
---|
118 | internal static class SchemeExporterExtensions {
|
---|
119 | public static string ExportToScheme(this Addition addition) {
|
---|
120 | return "+";
|
---|
121 | }
|
---|
122 |
|
---|
123 | public static string ExportToScheme(this Constant constant, IFunctionTree tree) {
|
---|
124 | return ((ConstantFunctionTree)tree).Value.ToString("r");
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static string ExportToScheme(this Cosinus cosinus) {
|
---|
128 | return "cos";
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static string ExportToScheme(this Division division) {
|
---|
132 | return "/";
|
---|
133 | }
|
---|
134 |
|
---|
135 | public static string ExportToScheme(this Exponential exponential) {
|
---|
136 | return "exp";
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static string ExportToScheme(this Logarithm logarithm) {
|
---|
140 | return "log";
|
---|
141 | }
|
---|
142 |
|
---|
143 | public static string ExportToScheme(this Multiplication multiplication) {
|
---|
144 | return "*";
|
---|
145 | }
|
---|
146 |
|
---|
147 | public static string ExportToScheme(this Power power) {
|
---|
148 | return "expt";
|
---|
149 | }
|
---|
150 |
|
---|
151 | public static string ExportToScheme(this Signum signum) {
|
---|
152 | return "sign";
|
---|
153 | }
|
---|
154 |
|
---|
155 | public static string ExportToScheme(this Sinus sinus) {
|
---|
156 | return "sin";
|
---|
157 | }
|
---|
158 |
|
---|
159 | public static string ExportToScheme(this Sqrt sqrt) {
|
---|
160 | return "sqrt";
|
---|
161 | }
|
---|
162 |
|
---|
163 | public static string ExportToScheme(this Subtraction subtraction) {
|
---|
164 | return "-";
|
---|
165 | }
|
---|
166 |
|
---|
167 | public static string ExportToScheme(this Tangens tangens) {
|
---|
168 | return "tan";
|
---|
169 | }
|
---|
170 |
|
---|
171 | public static string ExportToScheme(this Variable variable, IFunctionTree tree) {
|
---|
172 | var varTree = (VariableFunctionTree)tree;
|
---|
173 | return "(variable " + varTree.Weight.ToString("r") + " " +
|
---|
174 | varTree.VariableName + " " + varTree.SampleOffset + ")";
|
---|
175 | }
|
---|
176 | public static string ExportToScheme(this Differential differential, IFunctionTree tree) {
|
---|
177 | var varTree = (VariableFunctionTree)tree;
|
---|
178 | return "(differential " + varTree.Weight.ToString("r") + " " +
|
---|
179 | varTree.VariableName + " " + varTree.SampleOffset + ")";
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static string ExportToScheme(this And and) {
|
---|
183 | return "and";
|
---|
184 | }
|
---|
185 |
|
---|
186 | public static string ExportToScheme(this Average average) {
|
---|
187 | return "mean";
|
---|
188 | }
|
---|
189 |
|
---|
190 | public static string ExportToScheme(this IfThenElse ifThenElse) {
|
---|
191 | return "if";
|
---|
192 | }
|
---|
193 |
|
---|
194 | public static string ExportToScheme(this Not not) {
|
---|
195 | return "not";
|
---|
196 | }
|
---|
197 |
|
---|
198 | public static string ExportToScheme(this Or or) {
|
---|
199 | return "or";
|
---|
200 | }
|
---|
201 |
|
---|
202 | public static string ExportToScheme(this Xor xor) {
|
---|
203 | return "xor";
|
---|
204 | }
|
---|
205 |
|
---|
206 | public static string ExportToScheme(this Equal equal) {
|
---|
207 | return "equ";
|
---|
208 | }
|
---|
209 |
|
---|
210 | public static string ExportToScheme(this LessThan lessThan) {
|
---|
211 | return "<";
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static string ExportToScheme(this GreaterThan greaterThan) {
|
---|
215 | return ">";
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|