Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/SymbolicExpressionExporter.cs @ 2215

Last change on this file since 2215 was 2212, checked in by gkronber, 15 years ago

GP Refactoring: #713

  • added project GP.Operators
  • moved operators from plugin GP to plugin GP.Operators
  • deleted unused constraints
  • removed dependency of GP plugins on Constraints plugin
  • moved StructID functions into directory Symbols
  • deleted unused class FunView
  • implemented add and remove functionality for the FunctionLibraryView
File size: 7.3 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
22using System.Text;
[2210]23using HeuristicLab.GP.Interfaces;
[645]24
25namespace HeuristicLab.GP.StructureIdentification {
[654]26  public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
[645]27    private StringBuilder builder;
28    private string currentIndent;
[654]29
30    #region IFunctionTreeExporter Members
31
32    public string Name {
33      get { return "Symbolic Expression Exporter"; }
[645]34    }
35
[654]36
37    public string Export(IFunctionTree tree) {
38      builder = new StringBuilder();
[645]39      currentIndent = "";
[654]40      BuildExportString(tree);
41      return builder.ToString();
[645]42    }
43
[654]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      }
[645]52    }
53
[654]54    private void BuildExportString(IFunctionTree tree) {
[645]55      builder.Append(currentIndent);
[654]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);
[645]64    }
65
[654]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    }
[645]95
[654]96
97    #endregion
98
99    #region IFunctionTreeNameGenerator Members
100
101    string IFunctionTreeNameGenerator.Name {
102      get { return "Symbolic Expression"; }
[645]103    }
104
[654]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;
[645]113    }
114
[654]115    #endregion
116  }
117
118  internal static class SchemeExporterExtensions {
119    public static string ExportToScheme(this Addition addition) {
120      return "+";
[645]121    }
122
[654]123    public static string ExportToScheme(this Constant constant, IFunctionTree tree) {
[2210]124      return ((ConstantFunctionTree)tree).Value.ToString("r");
[645]125    }
126
[654]127    public static string ExportToScheme(this Cosinus cosinus) {
128      return "cos";
[645]129    }
130
[654]131    public static string ExportToScheme(this Division division) {
132      return "/";
[645]133    }
134
[654]135    public static string ExportToScheme(this Exponential exponential) {
136      return "exp";
[645]137    }
138
[654]139    public static string ExportToScheme(this Logarithm logarithm) {
140      return "log";
[645]141    }
142
[654]143    public static string ExportToScheme(this Multiplication multiplication) {
144      return "*";
[645]145    }
146
[654]147    public static string ExportToScheme(this Power power) {
148      return "expt";
[645]149    }
150
[654]151    public static string ExportToScheme(this Signum signum) {
152      return "sign";
[645]153    }
154
[654]155    public static string ExportToScheme(this Sinus sinus) {
156      return "sin";
[645]157    }
158
[654]159    public static string ExportToScheme(this Sqrt sqrt) {
160      return "sqrt";
[645]161    }
162
[654]163    public static string ExportToScheme(this Subtraction subtraction) {
164      return "-";
[645]165    }
166
[654]167    public static string ExportToScheme(this Tangens tangens) {
168      return "tan";
[645]169    }
170
[654]171    public static string ExportToScheme(this Variable variable, IFunctionTree tree) {
[2210]172      var varTree = (VariableFunctionTree)tree;
173      return "(variable " + varTree.Weight.ToString("r") + " " +
174        varTree.VariableName + " " + varTree.SampleOffset + ")";
[645]175    }
[654]176    public static string ExportToScheme(this Differential differential, IFunctionTree tree) {
[2210]177      var varTree = (VariableFunctionTree)tree;
178      return "(differential " + varTree.Weight.ToString("r") + " " +
179        varTree.VariableName + " " + varTree.SampleOffset + ")";
[654]180    }
[645]181
[654]182    public static string ExportToScheme(this And and) {
183      return "and";
[645]184    }
185
[654]186    public static string ExportToScheme(this Average average) {
187      return "mean";
[645]188    }
189
[654]190    public static string ExportToScheme(this IfThenElse ifThenElse) {
191      return "if";
[645]192    }
193
[654]194    public static string ExportToScheme(this Not not) {
195      return "not";
[645]196    }
197
[654]198    public static string ExportToScheme(this Or or) {
199      return "or";
[645]200    }
201
[654]202    public static string ExportToScheme(this Xor xor) {
203      return "xor";
[645]204    }
205
[654]206    public static string ExportToScheme(this Equal equal) {
207      return "equ";
[645]208    }
209
[654]210    public static string ExportToScheme(this LessThan lessThan) {
211      return "<";
[645]212    }
213
[654]214    public static string ExportToScheme(this GreaterThan greaterThan) {
215      return ">";
[645]216    }
217  }
218}
Note: See TracBrowser for help on using the repository browser.