Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Functions/ModelAnalyzerExporter.cs @ 3508

Last change on this file since 3508 was 518, checked in by gkronber, 16 years ago

fixed a typo (#258)

File size: 5.4 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;
27
28namespace HeuristicLab.Functions {
29  public class ModelAnalyzerExporter : IFunctionVisitor {
30    private string prefix;
31    private string currentIndent = "";
32    private IFunctionTree currentBranch;
33    public string ModelAnalyzerPrefix {
34      get { return prefix; }
35    }
36    public void Reset() {
37      prefix = "";
38    }
39
40    private void VisitFunction(string name, IFunction f) {
41      prefix += currentIndent + "[F]" + name + "(\n";
42    }
43
44    #region IFunctionVisitor Members
45
46    public void Visit(IFunction function) {
47      prefix += function.Name;
48    }
49
50    public void Visit(Addition addition) {
51      VisitFunction("Addition[0]", addition);
52    }
53
54    public void Visit(Constant constant) {
55      double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value).Data;
56      prefix += currentIndent + "[T]Constant(" + value.ToString("r") + ";0;0)";
57    }
58
59    public void Visit(Cosinus cosinus) {
60      VisitFunction("Trigonometrics[1]", cosinus);
61    }
62
63    public void Visit(Differential differential) {
64      double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.WEIGHT).Value).Data;
65      double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.INDEX).Value).Data;
66      double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.OFFSET).Value).Data;
67
68      prefix += currentIndent + "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
69    }
70
71    public void Visit(Division division) {
72      VisitFunction("Division[0]", division);
73    }
74
75    public void Visit(Exponential exponential) {
76      VisitFunction("Exponential[0]", exponential);
77    }
78
79    public void Visit(Logarithm logarithm) {
80      VisitFunction("Logarithm[0]", logarithm);
81    }
82
83    public void Visit(Multiplication multiplication) {
84      VisitFunction("Multiplication[0]", multiplication);
85    }
86
87    public void Visit(Power power) {
88      VisitFunction("Power[0]", power);
89    }
90
91    public void Visit(Signum signum) {
92      VisitFunction("Signum[0]", signum);
93    }
94
95    public void Visit(Sinus sinus) {
96      VisitFunction("Trigonometrics[0]", sinus);
97    }
98
99    public void Visit(Sqrt sqrt) {
100      VisitFunction("Sqrt[0]", sqrt);
101    }
102
103    public void Visit(Subtraction substraction) {
104      VisitFunction("Subtraction[0]", substraction);
105    }
106
107    public void Visit(Tangens tangens) {
108      VisitFunction("Trigonometrics[2]", tangens);
109    }
110
111    public void Visit(HeuristicLab.Functions.Variable variable) {
112      double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
113      double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
114      double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
115
116      prefix += currentIndent + "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")";
117    }
118
119    public void Visit(And and) {
120      VisitFunction("Logical[0]", and);
121    }
122
123    public void Visit(Average average) {
124      VisitFunction("N/A (average)", average);
125    }
126
127    public void Visit(IfThenElse ifThenElse) {
128      VisitFunction("Conditional[0]", ifThenElse);
129    }
130
131    public void Visit(Not not) {
132      VisitFunction("Logical[2]", not);
133    }
134
135    public void Visit(Or or) {
136      VisitFunction("Logical[1]", or);
137    }
138
139    public void Visit(Xor xor) {
140      VisitFunction("N/A (xor)", xor);
141    }
142
143    public void Visit(Equal equal) {
144      VisitFunction("Boolean[2]", equal);
145    }
146
147    public void Visit(LessThan lessThan) {
148      VisitFunction("Boolean[0]", lessThan);
149    }
150
151    public void Visit(GreaterThan greaterThan) {
152      VisitFunction("Boolean[4]", greaterThan);
153    }
154    #endregion
155
156    public void Visit(IFunctionTree functionTree) {
157      currentBranch = functionTree;
158      functionTree.Function.Accept(this);
159      currentIndent += "  ";
160      foreach(IFunctionTree subTree in functionTree.SubTrees) {
161        Visit(subTree);
162        prefix += ";\n";
163      }
164      prefix = prefix.TrimEnd(';', '\n');
165      if(functionTree.SubTrees.Count>0) prefix += ")";
166      currentIndent = currentIndent.Remove(0, 2);
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.