Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/SymbolicExpressionExporter.cs @ 2879

Last change on this file since 2879 was 2629, checked in by gkronber, 15 years ago

Fixed display of unknown functions and removed UnknownFunctionException. #845 (In the FunctionTreeView only the string N/A is displayed for unknown functions)

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