Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5020 was 5020, checked in by swinkler, 13 years ago

Update variables retrieval in MATLAB formatter; now only distinct variables are selected. (#1314)

File size: 9.0 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        foreach (string variableName in variableNames)
62          stringBuilder.AppendLine(variableName + " = Data(:, ???);");
63        stringBuilder.AppendLine();
64        stringBuilder.AppendLine("Target_estimated(i) = " + FormatRecursively(node.SubTrees[0]));
65        return stringBuilder.ToString();
66      }
67
68      if (symbol is StartSymbol)
69        return FormatRecursively(node.SubTrees[0]);
70
71      stringBuilder.Append("(");
72
73      if (symbol is Addition) {
74        for (int i = 0; i < node.SubTrees.Count; i++) {
75          if (i > 0) stringBuilder.Append("+");
76          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
77        }
78      } else if (symbol is And) {
79        stringBuilder.Append("(");
80        for (int i = 0; i < node.SubTrees.Count; i++) {
81          if (i > 0) stringBuilder.Append("&");
82          stringBuilder.Append("((");
83          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
84          stringBuilder.Append(")>0)");
85        }
86        stringBuilder.Append(")-0.5"); // MATLAB maps false and true to 0 and 1, resp., so we map this result to -0.5 and +0.5, resp.
87      } else if (symbol is Average) {
88        stringBuilder.Append("(1/");
89        stringBuilder.Append(node.SubTrees.Count);
90        stringBuilder.Append(")*(");
91        for (int i = 0; i < node.SubTrees.Count; i++) {
92          if (i > 0) stringBuilder.Append("+");
93          stringBuilder.Append("(");
94          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
95          stringBuilder.Append(")");
96        }
97        stringBuilder.Append(")");
98      } else if (symbol is Constant) {
99        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
100        stringBuilder.Append(constantTreeNode.Value.ToString().Replace(",", "."));
101      } else if (symbol is Cosine) {
102        stringBuilder.Append("cos(");
103        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
104        stringBuilder.Append(")");
105      } else if (symbol is Division) {
106        if (node.SubTrees.Count == 1) {
107          stringBuilder.Append("1/");
108          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
109        } else {
110          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
111          stringBuilder.Append("/(");
112          for (int i = 1; i < node.SubTrees.Count; i++) {
113            if (i > 1) stringBuilder.Append("*");
114            stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
115          }
116          stringBuilder.Append(")");
117        }
118      } else if (symbol is Exponential) {
119        stringBuilder.Append("exp(");
120        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
121        stringBuilder.Append(")");
122      } else if (symbol is GreaterThan) {
123        stringBuilder.Append("(");
124        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
125        stringBuilder.Append(">");
126        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
127        stringBuilder.Append(")-0.5"); // MATLAB maps false and true to 0 and 1, resp., so we map this result to -0.5 and +0.5, resp.
128      } else if (symbol is IfThenElse) {
129        stringBuilder.Append("(");
130        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
131        stringBuilder.Append(">0)*");
132        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
133        stringBuilder.Append("+");
134        stringBuilder.Append("(");
135        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
136        stringBuilder.Append("<0)*");
137        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
138      } else if (symbol is LaggedVariable) {
139        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
140        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString().Replace(",", "."));
141        stringBuilder.Append("*");
142        stringBuilder.Append(laggedVariableTreeNode.VariableName + "(i-" + laggedVariableTreeNode.Lag + ")");
143      } else if (symbol is LessThan) {
144        stringBuilder.Append("(");
145        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
146        stringBuilder.Append("<");
147        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
148        stringBuilder.Append(")-0.5");
149      } else if (symbol is Logarithm) {
150        stringBuilder.Append("log(");
151        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
152        stringBuilder.Append(")");
153      } else if (symbol is Multiplication) {
154        for (int i = 0; i < node.SubTrees.Count; i++) {
155          if (i > 0) stringBuilder.Append("*");
156          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
157        }
158      } else if (symbol is Not) {
159        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
160        stringBuilder.Append("*-1");
161      } else if (symbol is Or) {
162        stringBuilder.Append("(");
163        for (int i = 0; i < node.SubTrees.Count; i++) {
164          if (i > 0) stringBuilder.Append("|");
165          stringBuilder.Append("((");
166          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
167          stringBuilder.Append(")>0)");
168        }
169        stringBuilder.Append(")-0.5"); // MATLAB maps false and true to 0 and 1, resp., so we map this result to -0.5 and +0.5, resp.
170      } else if (symbol is Sine) {
171        stringBuilder.Append("sin(");
172        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
173        stringBuilder.Append(")");
174      } else if (symbol is Subtraction) {
175        if (node.SubTrees.Count == 1) {
176          stringBuilder.Append("-1*");
177          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
178        } else {
179          stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
180          for (int i = 1; i < node.SubTrees.Count; i++) {
181            stringBuilder.Append("-");
182            stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
183          }
184        }
185      } else if (symbol is Tangent) {
186        stringBuilder.Append("tan(");
187        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
188        stringBuilder.Append(")");
189      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
190        VariableTreeNode variableTreeNode = node as VariableTreeNode;
191        stringBuilder.Append(variableTreeNode.Weight.ToString().Replace(",", "."));
192        stringBuilder.Append("*");
193        stringBuilder.Append(variableTreeNode.VariableName + "(i)");
194      } else {
195        stringBuilder.Append("ERROR");
196      }
197
198      stringBuilder.Append(")");
199
200      return stringBuilder.ToString();
201    }
202
203    public override IDeepCloneable Clone(Cloner cloner) {
204      return new SymbolicExpressionTreeMATLABFormatter(this, cloner);
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.