Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeMATLABFormatter.cs @ 9760

Last change on this file since 9760 was 5022, checked in by swinkler, 14 years ago

Worked on MATLAB formatter, added special version of log function that behaves like Math.Log. (#1314)

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Formatters;
31using System.Collections.Generic;
32using System;
33
34namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
35
36  [Item("SymbolicExpressionTreeMATLABFormatter", "String formatter for string representations of symbolic expression trees in MATLAB syntax.")]
37  public class SymbolicExpressionTreeMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
38
39    protected SymbolicExpressionTreeMATLABFormatter(SymbolicExpressionTreeMATLABFormatter original, Cloner cloner) : base(original, cloner) { }
40    public SymbolicExpressionTreeMATLABFormatter()
41      : base() {
42      Name = "MATLAB String Formatter";
43    }
44
45    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
46      return FormatRecursively(symbolicExpressionTree.Root);
47    }
48
49    private string FormatRecursively(SymbolicExpressionTreeNode node) {
50
51      Symbol symbol = node.Symbol;
52
53      StringBuilder stringBuilder = new StringBuilder();
54
55      if (symbol is ProgramRootSymbol) {
56        var variableNames = node.IterateNodesPostfix()
57          .OfType<VariableTreeNode>()
58          .Select(n => n.VariableName)
59          .Distinct()
60          .OrderBy(x => x);
61        stringBuilder.AppendLine("function test_model");
62        foreach (string variableName in variableNames)
63          stringBuilder.AppendLine("  " + variableName + " = Data(:, ???);");
64        stringBuilder.AppendLine();
65        stringBuilder.AppendLine("  for i = size(Data,1):-1:1");
66        stringBuilder.AppendLine("    Target_estimated(i) = " + FormatRecursively(node.SubTrees[0]) + ";");
67        stringBuilder.AppendLine();
68        stringBuilder.AppendLine("function y = log_(x)");
69        stringBuilder.AppendLine("  if(x<=0) y = NaN;");
70        stringBuilder.AppendLine("  else     y = log(x);");
71        return stringBuilder.ToString();
72      }
73
74      if (symbol is StartSymbol)
75        return FormatRecursively(node.SubTrees[0]);
76
77      stringBuilder.Append("(");
78
79      if (symbol is Addition) {
80        for (int i = 0; i < node.SubTrees.Count; i++) {
81          if (i > 0) stringBuilder.Append("+");
82          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
83        }
84      } else if (symbol is And) {
85        stringBuilder.Append("((");
86        for (int i = 0; i < node.SubTrees.Count; i++) {
87          if (i > 0) stringBuilder.Append("&");
88          stringBuilder.Append("((");
89          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
90          stringBuilder.Append(")>0)");
91        }
92        stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
93      } else if (symbol is Average) {
94        stringBuilder.Append("(1/");
95        stringBuilder.Append(node.SubTrees.Count);
96        stringBuilder.Append(")*(");
97        for (int i = 0; i < node.SubTrees.Count; i++) {
98          if (i > 0) stringBuilder.Append("+");
99          stringBuilder.Append("(");
100          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
101          stringBuilder.Append(")");
102        }
103        stringBuilder.Append(")");
104      } else if (symbol is Constant) {
105        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
106        stringBuilder.Append(constantTreeNode.Value.ToString().Replace(",", "."));
107      } else if (symbol is Cosine) {
108        stringBuilder.Append("cos(");
109        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
110        stringBuilder.Append(")");
111      } else if (symbol is Division) {
112        if (node.SubTrees.Count == 1) {
113          stringBuilder.Append("1/");
114          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
115        } else {
116          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
117          stringBuilder.Append("/(");
118          for (int i = 1; i < node.SubTrees.Count; i++) {
119            if (i > 1) stringBuilder.Append("*");
120            stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
121          }
122          stringBuilder.Append(")");
123        }
124      } else if (symbol is Exponential) {
125        stringBuilder.Append("exp(");
126        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
127        stringBuilder.Append(")");
128      } else if (symbol is GreaterThan) {
129        stringBuilder.Append("((");
130        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
131        stringBuilder.Append(">");
132        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
133        stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
134      } else if (symbol is IfThenElse) {
135        stringBuilder.Append("(");
136        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
137        stringBuilder.Append(">0)*");
138        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
139        stringBuilder.Append("+");
140        stringBuilder.Append("(");
141        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
142        stringBuilder.Append("<=0)*");
143        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
144      } else if (symbol is LaggedVariable) {
145        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
146        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString().Replace(",", "."));
147        stringBuilder.Append("*");
148        stringBuilder.Append(laggedVariableTreeNode.VariableName + "(i-" + laggedVariableTreeNode.Lag + ")");
149      } else if (symbol is LessThan) {
150        stringBuilder.Append("((");
151        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
152        stringBuilder.Append("<");
153        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
154        stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
155      } else if (symbol is Logarithm) {
156        stringBuilder.Append("log_(");
157        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
158        stringBuilder.Append(")");
159      } else if (symbol is Multiplication) {
160        for (int i = 0; i < node.SubTrees.Count; i++) {
161          if (i > 0) stringBuilder.Append("*");
162          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
163        }
164      } else if (symbol is Not) {
165        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
166        stringBuilder.Append("*-1");
167      } else if (symbol is Or) {
168        stringBuilder.Append("((");
169        for (int i = 0; i < node.SubTrees.Count; i++) {
170          if (i > 0) stringBuilder.Append("|");
171          stringBuilder.Append("((");
172          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
173          stringBuilder.Append(")>0)");
174        }
175        stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
176      } else if (symbol is Sine) {
177        stringBuilder.Append("sin(");
178        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
179        stringBuilder.Append(")");
180      } else if (symbol is Subtraction) {
181        if (node.SubTrees.Count == 1) {
182          stringBuilder.Append("-1*");
183          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
184        } else {
185          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
186          for (int i = 1; i < node.SubTrees.Count; i++) {
187            stringBuilder.Append("-");
188            stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
189          }
190        }
191      } else if (symbol is Tangent) {
192        stringBuilder.Append("tan(");
193        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
194        stringBuilder.Append(")");
195      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
196        VariableTreeNode variableTreeNode = node as VariableTreeNode;
197        stringBuilder.Append(variableTreeNode.Weight.ToString().Replace(",", "."));
198        stringBuilder.Append("*");
199        stringBuilder.Append(variableTreeNode.VariableName + "(i)");
200      } else {
201        stringBuilder.Append("ERROR");
202      }
203
204      stringBuilder.Append(")");
205
206      return stringBuilder.ToString();
207    }
208
209    public override IDeepCloneable Clone(Cloner cloner) {
210      return new SymbolicExpressionTreeMATLABFormatter(this, cloner);
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.