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