Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2057 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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