Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed #323 (The default representation of FunctionTreeView should show coefficients and time-offsets of variables and values of constants).

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