[5017] | 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;
|
---|
[5019] | 23 | using System.Linq;
|
---|
[5017] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Formatters;
|
---|
| 31 | using System.Collections.Generic;
|
---|
| 32 | using System;
|
---|
| 33 |
|
---|
| 34 | namespace 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()
|
---|
[5019] | 41 | : base() {
|
---|
[5017] | 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) {
|
---|
[5019] | 56 | var variableNames = node.IterateNodesPostfix()
|
---|
| 57 | .OfType<VariableTreeNode>()
|
---|
| 58 | .Select(n => n.VariableName)
|
---|
[5020] | 59 | .Distinct()
|
---|
[5019] | 60 | .OrderBy(x => x);
|
---|
[5017] | 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) {
|
---|
[5021] | 79 | stringBuilder.Append("((");
|
---|
[5017] | 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 | }
|
---|
[5021] | 86 | 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.
|
---|
[5017] | 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) {
|
---|
[5021] | 123 | stringBuilder.Append("((");
|
---|
[5017] | 124 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
| 125 | stringBuilder.Append(">");
|
---|
| 126 | stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
|
---|
[5021] | 127 | 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.
|
---|
[5017] | 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]));
|
---|
[5021] | 136 | stringBuilder.Append("<=0)*");
|
---|
[5017] | 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) {
|
---|
[5021] | 144 | stringBuilder.Append("((");
|
---|
[5017] | 145 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
| 146 | stringBuilder.Append("<");
|
---|
| 147 | stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
|
---|
[5021] | 148 | 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.
|
---|
[5017] | 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) {
|
---|
[5021] | 162 | stringBuilder.Append("((");
|
---|
[5017] | 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 | }
|
---|
[5021] | 169 | 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.
|
---|
[5017] | 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)");
|
---|
[5019] | 194 | } else {
|
---|
[5017] | 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 | }
|
---|