Free cookie consent management tool by TermsFeed Policy Generator

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

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