Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/tools/CedmaExporter/SymbolicExpressionExporter.cs @ 2262

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

Fixed bug in s-exp export format of terminal nodes. #715

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