Free cookie consent management tool by TermsFeed Policy Generator

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

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

Made symbolic expression export of GP trees culture invariant. #778

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