Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/CedmaExporter/SymbolicExpressionExporter.cs @ 3024

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

Fixed a bug in the s-expression exporter. Improved exporter UI. #715 & #719

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