Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/ModelAnalyzerExporter.cs @ 831

Last change on this file since 831 was 656, checked in by gkronber, 16 years ago

merged changesets r644:647 and r651:655 from the GpPluginsRefactoringBranch back into the trunk (#177)

File size: 8.6 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 ModelAnalyzerExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
30    #region IFunctionTreeExporter Members
31
32    public string Name {
33      get {
34        return "HL2 ModelAnalyzer Exporter";
35      }
[645]36    }
[654]37
38    public bool TryExport(IFunctionTree tree, out string exported) {
39      try {
40        exported = Export(tree);
41        return true;
42      } catch(UnknownFunctionException) {
43        exported = "";
44        return false;
45      }
[645]46    }
47
[654]48    public string Export(IFunctionTree tree) {
49      string result = ExportFunction(tree.Function, tree);
50      result += "(\n";
51      foreach(IFunctionTree subTree in tree.SubTrees) {
52        result += Export(subTree);
53        result += ";\n";
54      }
55      result = result.TrimEnd(';', '\n');
56      if(tree.SubTrees.Count > 0) result += ")";
57      return result;
[645]58    }
59
[654]60    #endregion
[645]61
[654]62    private string ExportFunction(IFunction function, IFunctionTree tree) {
63      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
64      // please let me know! (gkronber 14.10.2008)
65      if(function is Addition) return ((Addition)function).ExportToHL2(tree);
66      if(function is And) return ((And)function).ExportToHL2(tree);
67      if(function is Average) return ((Average)function).ExportToHL2(tree);
68      if(function is Constant) return ((Constant)function).ExportToHL2(tree);
69      if(function is Cosinus) return ((Cosinus)function).ExportToHL2(tree);
70      if(function is Differential) return ((Differential)function).ExportToHL2(tree);
71      if(function is Division) return ((Division)function).ExportToHL2(tree);
72      if(function is Equal) return ((Equal)function).ExportToHL2(tree);
73      if(function is Exponential) return ((Exponential)function).ExportToHL2(tree);
74      if(function is GreaterThan) return ((GreaterThan)function).ExportToHL2(tree);
75      if(function is IfThenElse) return ((IfThenElse)function).ExportToHL2(tree);
76      if(function is LessThan) return ((LessThan)function).ExportToHL2(tree);
77      if(function is Logarithm) return ((Logarithm)function).ExportToHL2(tree);
78      if(function is Multiplication) return ((Multiplication)function).ExportToHL2(tree);
79      if(function is Not) return ((Not)function).ExportToHL2(tree);
80      if(function is Or) return ((Or)function).ExportToHL2(tree);
81      if(function is Power) return ((Power)function).ExportToHL2(tree);
82      if(function is Signum) return ((Signum)function).ExportToHL2(tree);
83      if(function is Sinus) return ((Sinus)function).ExportToHL2(tree);
84      if(function is Sqrt) return ((Sqrt)function).ExportToHL2(tree);
85      if(function is Subtraction) return ((Subtraction)function).ExportToHL2(tree);
86      if(function is Tangens) return ((Tangens)function).ExportToHL2(tree);
87      if(function is Variable) return ((Variable)function).ExportToHL2(tree);
88      if(function is Xor) return ((Xor)function).ExportToHL2(tree);
89      throw new UnknownFunctionException(function.Name);
[645]90    }
91
[654]92    #region IFunctionTreeNameGenerator Members
93
94    string IFunctionTreeNameGenerator.Name {
95      get { return "HL2 Representation"; }
[645]96    }
97
[654]98    public string GetName(IFunctionTree tree) {
99      string name = "";
100      try {
101        name = ExportFunction(tree.Function, tree);
102      } catch(UnknownFunctionException) {
103        name = "N/A";
104      }
105      return name;
[645]106    }
107
[654]108    #endregion
109  }
110
111  internal static class HL2ExporterExtensions {
112    private static string GetHL2FunctionName(string name) {
113      return "[F]" + name ;
[645]114    }
115
[654]116    public static string ExportToHL2(this Addition addition, IFunctionTree tree) {
117      return GetHL2FunctionName("Addition[0]");
118    }
[645]119
[654]120    public static string ExportToHL2(this Constant constant, IFunctionTree tree) {
121      double value = ((ConstrainedDoubleData)tree.GetLocalVariable(Constant.VALUE).Value).Data;
122      return "[T]Constant(" + value.ToString("r") + ";0;0)";
[645]123    }
124
[654]125    public static string ExportToHL2(this Cosinus cosinus, IFunctionTree tree) {
126      return GetHL2FunctionName("Trigonometrics[1]");
[645]127    }
128
[654]129    public static string ExportToHL2(this Differential differential, IFunctionTree tree) {
130      double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Differential.WEIGHT).Value).Data;
131      double index = ((ConstrainedIntData)tree.GetLocalVariable(Differential.INDEX).Value).Data;
132      double offset = ((ConstrainedIntData)tree.GetLocalVariable(Differential.OFFSET).Value).Data;
133
134      return "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
[645]135    }
136
[654]137    public static string ExportToHL2(this Division division, IFunctionTree tree) {
138      return GetHL2FunctionName("Division[0]");
[645]139    }
140
[654]141    public static string ExportToHL2(this Exponential exponential, IFunctionTree tree) {
142      return GetHL2FunctionName("Exponential[0]");
[645]143    }
144
[654]145    public static string ExportToHL2(this Logarithm logarithm, IFunctionTree tree) {
146      return GetHL2FunctionName("Logarithm[0]");
[645]147    }
148
[654]149    public static string ExportToHL2(this Multiplication multiplication, IFunctionTree tree) {
150      return GetHL2FunctionName("Multiplication[0]");
[645]151    }
152
[654]153    public static string ExportToHL2(this Power power, IFunctionTree tree) {
154      return GetHL2FunctionName("Power[0]");
[645]155    }
156
[654]157    public static string ExportToHL2(this Signum signum, IFunctionTree tree) {
158      return GetHL2FunctionName("Signum[0]");
[645]159    }
160
[654]161    public static string ExportToHL2(this Sinus sinus, IFunctionTree tree) {
162      return GetHL2FunctionName("Trigonometrics[0]");
[645]163    }
164
[654]165    public static string ExportToHL2(this Sqrt sqrt, IFunctionTree tree) {
166      return GetHL2FunctionName("Sqrt[0]");
[645]167    }
168
[654]169    public static string ExportToHL2(this Subtraction substraction, IFunctionTree tree) {
170      return GetHL2FunctionName("Subtraction[0]");
171    }
[645]172
[654]173    public static string ExportToHL2(this Tangens tangens, IFunctionTree tree) {
174      return GetHL2FunctionName("Trigonometrics[2]");
[645]175    }
176
[654]177    public static string ExportToHL2(this Variable variable, IFunctionTree tree) {
178      double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Variable.WEIGHT).Value).Data;
179      double index = ((ConstrainedIntData)tree.GetLocalVariable(Variable.INDEX).Value).Data;
180      double offset = ((ConstrainedIntData)tree.GetLocalVariable(Variable.OFFSET).Value).Data;
181
182      return "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
[645]183    }
184
[654]185    public static string ExportToHL2(this And and, IFunctionTree tree) {
186      return GetHL2FunctionName("Logical[0]");
[645]187    }
188
[654]189    public static string ExportToHL2(this Average average, IFunctionTree tree) {
190      return GetHL2FunctionName("Average[0]");
[645]191    }
192
[654]193    public static string ExportToHL2(this IfThenElse ifThenElse, IFunctionTree tree) {
194      return GetHL2FunctionName("Conditional[0]");
[645]195    }
196
[654]197    public static string ExportToHL2(this Not not, IFunctionTree tree) {
198      return GetHL2FunctionName("Logical[2]");
[645]199    }
200
[654]201    public static string ExportToHL2(this Or or, IFunctionTree tree) {
202      return GetHL2FunctionName("Logical[1]");
[645]203    }
204
[654]205    public static string ExportToHL2(this Xor xor, IFunctionTree tree) {
206      return GetHL2FunctionName("Logical[3]");
[645]207    }
208
[654]209    public static string ExportToHL2(this Equal equal, IFunctionTree tree) {
210      return GetHL2FunctionName("Boolean[2]");
[645]211    }
212
[654]213    public static string ExportToHL2(this LessThan lessThan, IFunctionTree tree) {
214      return GetHL2FunctionName("Boolean[0]");
[645]215    }
216
[654]217    public static string ExportToHL2(this GreaterThan greaterThan, IFunctionTree tree) {
218      return GetHL2FunctionName("Boolean[4]");
[645]219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.