Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/InOrderExpressionExporter.cs

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

Added in-order exporter for function trees. Not every function is implemented yet. #928 (Structure tree exporter for in order format)

File size: 6.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.Linq;
26using System.Collections.Generic;
27using System.Globalization;
28
29namespace HeuristicLab.GP.StructureIdentification {
30  public class InOrderExpressionExporter : IFunctionTreeSerializer {
31    private StringBuilder builder;
32    private string currentIndent;
33
34    #region IFunctionTreeExporter Members
35
36    public string Name {
37      get { return "InOrder Expression Exporter"; }
38    }
39
40
41    public string Export(IFunctionTree tree) {
42      builder = new StringBuilder();
43      currentIndent = "";
44      BuildExportString(tree);
45      return builder.ToString();
46    }
47
48    public bool TryExport(IFunctionTree tree, out string exported) {
49      foreach (IFunctionTree t in FunctionTreeIterator.IteratePrefix(tree))
50        if (!functionExporter.ContainsKey(t.Function.GetType())) {
51          exported = null;
52          return false;
53        }
54      exported = Export(tree);
55      return true;
56    }
57
58    private void BuildExportString(IFunctionTree tree) {
59      //builder.Append(currentIndent);
60      builder.Append("( ");
61      //currentIndent += "  ";
62      if (tree.SubTrees.Count > 0) {
63        if (tree.SubTrees.Count == 1) {
64          builder.Append(ExportFunction(tree.Function, tree));
65          builder.Append("(");
66          BuildExportString(tree.SubTrees[0]);
67          builder.Append(")");
68        } else {
69          BuildExportString(tree.SubTrees[0]);
70          foreach (IFunctionTree subTree in tree.SubTrees.Skip(1)) {
71            builder.Append(ExportFunction(tree.Function, tree));
72            BuildExportString(subTree);
73          }
74        }
75      } else {
76        builder.Append(ExportFunction(tree.Function, tree));
77      }
78      builder.Append(")");
79      //currentIndent = currentIndent.Remove(0, 2);
80    }
81
82    // try-export checks the keys of this dictionary
83    private static Dictionary<Type, Func<IFunction, IFunctionTree, string>> functionExporter = new Dictionary<Type, Func<IFunction, IFunctionTree, string>>() {
84      { typeof(Addition), (function, tree) => ((Addition)function).ExportToExcel()},
85      { typeof(Constant), (function, tree) => ((Constant)function).ExportToExcel(tree)},
86      { typeof(Differential), (function, tree) => ((Differential)function).ExportToExcel(tree)},
87      { typeof(Division), (function, tree) => ((Division)function).ExportToExcel()},
88      { typeof(Multiplication), (function, tree) => ((Multiplication)function).ExportToExcel()},
89      { typeof(Subtraction), (function, tree) => ((Subtraction)function).ExportToExcel()},
90      { typeof(Variable), (function, tree) => ((Variable)function).ExportToExcel(tree)},
91      { typeof(Logarithm), (function, tree) => ((Logarithm)function).ExportToExcel()},
92      { typeof(Exponential), (function, tree) => ((Exponential)function).ExportToExcel()},
93      { typeof(Cosinus), (function, tree) => ((Cosinus)function).ExportToExcel()},
94      { typeof(Sinus), (function, tree) => ((Sinus)function).ExportToExcel()},
95      { typeof(Tangens), (function, tree) => ((Tangens)function).ExportToExcel()},
96      { typeof(Sqrt), (function, tree) => ((Sqrt)function).ExportToExcel()},
97    };
98    private static string ExportFunction(IFunction function, IFunctionTree tree) {
99      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
100      // please let me know! (gkronber 14.10.2008)
101      if (functionExporter.ContainsKey(function.GetType())) return functionExporter[function.GetType()](function, tree);
102      else return function.Name;
103    }
104
105    #endregion
106
107    public static string GetName(IFunctionTree tree) {
108      return ExportFunction(tree.Function, tree);
109    }
110  }
111
112  internal static class ExcelExporterExtensions {
113    public static string ExportToExcel(this Addition addition) {
114      return "+";
115    }
116
117    public static string ExportToExcel(this Constant constant, IFunctionTree tree) {
118      return ((ConstantFunctionTree)tree).Value.ToString("r", CultureInfo.InvariantCulture.NumberFormat);
119    }
120
121    public static string ExportToExcel(this Division division) {
122      return "/";
123    }
124
125    public static string ExportToExcel(this Multiplication multiplication) {
126      return "*";
127    }
128
129    public static string ExportToExcel(this Subtraction subtraction) {
130      return "-";
131    }
132
133    public static string ExportToExcel(this Exponential exp) {
134      return "Exp";
135    }
136
137    public static string ExportToExcel(this Logarithm log) {
138      return "Log";
139    }
140    public static string ExportToExcel(this Cosinus cos) {
141      return "Cos";
142    }
143    public static string ExportToExcel(this Sinus sin) {
144      return "Sin";
145    }
146    public static string ExportToExcel(this Tangens tan) {
147      return "Tan";
148    }
149    public static string ExportToExcel(this Sqrt sqrt) {
150      return "Sqrt";
151    }
152    public static string ExportToExcel(this Variable variable, IFunctionTree tree) {
153      var varTree = (VariableFunctionTree)tree;
154      return " " + varTree.Weight.ToString("r", CultureInfo.InvariantCulture.NumberFormat) + " * " +
155        varTree.VariableName + " " + varTree.SampleOffset;
156    }
157    public static string ExportToExcel(this Differential differential, IFunctionTree tree) {
158      var varTree = (VariableFunctionTree)tree;
159      return varTree.Weight.ToString("r", CultureInfo.InvariantCulture.NumberFormat) + " * d(" +
160        varTree.VariableName + ") " + varTree.SampleOffset;
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.