[4861] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9794] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4861] | 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 |
|
---|
[9794] | 22 | using System.Globalization;
|
---|
[4861] | 23 | using System.Text;
|
---|
[9821] | 24 | using HeuristicLab.Common;
|
---|
[4861] | 25 | using HeuristicLab.Core;
|
---|
[9821] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[4861] | 27 |
|
---|
[9821] | 28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[4861] | 29 |
|
---|
[9649] | 30 | [Item("Smalltalk String Formatter", "String formatter for string representations of symbolic expression trees in Smalltalk syntax.")]
|
---|
[9821] | 31 | public class SymbolicDataAnalysisExpressionSmalltalkFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
[4861] | 32 |
|
---|
[9821] | 33 | protected SymbolicDataAnalysisExpressionSmalltalkFormatter(SymbolicDataAnalysisExpressionSmalltalkFormatter original, Cloner cloner) : base(original, cloner) { }
|
---|
| 34 | public SymbolicDataAnalysisExpressionSmalltalkFormatter()
|
---|
[9649] | 35 | : base() {
|
---|
[9821] | 36 | Name = ItemName;
|
---|
| 37 | Description = ItemDescription;
|
---|
[4861] | 38 | }
|
---|
| 39 |
|
---|
[9649] | 40 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
[4861] | 41 | return FormatRecursively(symbolicExpressionTree.Root);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[9794] | 44 | // returns the smalltalk expression corresponding to the node
|
---|
| 45 | // smalltalk expressions are always surrounded by parantheses "(<expr>)"
|
---|
[9649] | 46 | private string FormatRecursively(ISymbolicExpressionTreeNode node) {
|
---|
[4868] | 47 |
|
---|
[9649] | 48 | ISymbol symbol = node.Symbol;
|
---|
[4868] | 49 |
|
---|
| 50 | if (symbol is ProgramRootSymbol || symbol is StartSymbol)
|
---|
[9649] | 51 | return FormatRecursively(node.GetSubtree(0));
|
---|
[4868] | 52 |
|
---|
[9794] | 53 | StringBuilder stringBuilder = new StringBuilder(20);
|
---|
[4861] | 54 |
|
---|
[4868] | 55 | stringBuilder.Append("(");
|
---|
| 56 |
|
---|
[4861] | 57 | if (symbol is Addition) {
|
---|
[9649] | 58 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
[4861] | 59 | if (i > 0) stringBuilder.Append("+");
|
---|
[9649] | 60 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[4861] | 61 | }
|
---|
| 62 | } else if (symbol is And) {
|
---|
[4868] | 63 | stringBuilder.Append("(");
|
---|
[9649] | 64 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
[4861] | 65 | if (i > 0) stringBuilder.Append("&");
|
---|
[9794] | 66 | stringBuilder.Append("(");
|
---|
[9649] | 67 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[9794] | 68 | stringBuilder.Append(" > 0)");
|
---|
[4861] | 69 | }
|
---|
[4868] | 70 | stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
|
---|
[4861] | 71 | } else if (symbol is Average) {
|
---|
| 72 | stringBuilder.Append("(1/");
|
---|
[9649] | 73 | stringBuilder.Append(node.SubtreeCount);
|
---|
[4861] | 74 | stringBuilder.Append(")*(");
|
---|
[9649] | 75 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
[4861] | 76 | if (i > 0) stringBuilder.Append("+");
|
---|
[9649] | 77 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[4861] | 78 | }
|
---|
| 79 | stringBuilder.Append(")");
|
---|
| 80 | } else if (symbol is Constant) {
|
---|
| 81 | ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
|
---|
[9794] | 82 | stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
|
---|
[4861] | 83 | } else if (symbol is Cosine) {
|
---|
[9649] | 84 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 85 | stringBuilder.Append(" cos");
|
---|
[4861] | 86 | } else if (symbol is Division) {
|
---|
[9649] | 87 | if (node.SubtreeCount == 1) {
|
---|
[4868] | 88 | stringBuilder.Append("1/");
|
---|
[9649] | 89 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 90 | } else {
|
---|
[9649] | 91 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 92 | stringBuilder.Append("/(");
|
---|
[9649] | 93 | for (int i = 1; i < node.SubtreeCount; i++) {
|
---|
[4868] | 94 | if (i > 1) stringBuilder.Append("*");
|
---|
[9649] | 95 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[4868] | 96 | }
|
---|
[4861] | 97 | stringBuilder.Append(")");
|
---|
| 98 | }
|
---|
| 99 | } else if (symbol is Exponential) {
|
---|
[9649] | 100 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 101 | stringBuilder.Append(" exp");
|
---|
[4861] | 102 | } else if (symbol is GreaterThan) {
|
---|
| 103 | stringBuilder.Append("(");
|
---|
[9649] | 104 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 105 | stringBuilder.Append(" > ");
|
---|
[9649] | 106 | stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
|
---|
[4868] | 107 | stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
|
---|
[4861] | 108 | } else if (symbol is IfThenElse) {
|
---|
| 109 | stringBuilder.Append("(");
|
---|
[9649] | 110 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 111 | stringBuilder.Append(" > 0) ifTrue: [");
|
---|
[9649] | 112 | stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
|
---|
[4861] | 113 | stringBuilder.Append("] ifFalse: [");
|
---|
[9649] | 114 | stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
|
---|
[4869] | 115 | stringBuilder.Append("]");
|
---|
[4861] | 116 | } else if (symbol is LaggedVariable) {
|
---|
[9794] | 117 | stringBuilder.Append("lagged variable not implemented");
|
---|
[4861] | 118 | } else if (symbol is LessThan) {
|
---|
| 119 | stringBuilder.Append("(");
|
---|
[9649] | 120 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 121 | stringBuilder.Append(" < ");
|
---|
[9649] | 122 | stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
|
---|
[4868] | 123 | stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
|
---|
[4861] | 124 | } else if (symbol is Logarithm) {
|
---|
[9649] | 125 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 126 | stringBuilder.Append("ln");
|
---|
[4861] | 127 | } else if (symbol is Multiplication) {
|
---|
[9649] | 128 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
[4861] | 129 | if (i > 0) stringBuilder.Append("*");
|
---|
[9649] | 130 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[4861] | 131 | }
|
---|
| 132 | } else if (symbol is Not) {
|
---|
[9794] | 133 | stringBuilder.Append("(");
|
---|
[9649] | 134 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[9794] | 135 | stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]");
|
---|
[4861] | 136 | } else if (symbol is Or) {
|
---|
[4868] | 137 | stringBuilder.Append("(");
|
---|
[9649] | 138 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
[4861] | 139 | if (i > 0) stringBuilder.Append("|");
|
---|
[9794] | 140 | stringBuilder.Append("(");
|
---|
[9649] | 141 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[9794] | 142 | stringBuilder.Append(">0)");
|
---|
[4861] | 143 | }
|
---|
[4868] | 144 | stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
|
---|
[4861] | 145 | } else if (symbol is Sine) {
|
---|
[9649] | 146 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 147 | stringBuilder.Append(" sin");
|
---|
[4861] | 148 | } else if (symbol is Subtraction) {
|
---|
[9649] | 149 | if (node.SubtreeCount == 1) {
|
---|
[4869] | 150 | stringBuilder.Append("-1*");
|
---|
[9649] | 151 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[4868] | 152 | } else {
|
---|
[9649] | 153 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
| 154 | for (int i = 1; i < node.SubtreeCount; i++) {
|
---|
[9794] | 155 | stringBuilder.Append(" - ");
|
---|
[9649] | 156 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
[4868] | 157 | }
|
---|
[4861] | 158 | }
|
---|
| 159 | } else if (symbol is Tangent) {
|
---|
[9649] | 160 | stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
|
---|
[9794] | 161 | stringBuilder.Append(" tan");
|
---|
[9649] | 162 | } else if (symbol is Variable) {
|
---|
[4868] | 163 | VariableTreeNode variableTreeNode = node as VariableTreeNode;
|
---|
[9794] | 164 | stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
|
---|
[4868] | 165 | stringBuilder.Append("*");
|
---|
| 166 | stringBuilder.Append(variableTreeNode.VariableName);
|
---|
[9649] | 167 | } else {
|
---|
[9794] | 168 | stringBuilder.Append("(");
|
---|
| 169 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
| 170 | if (i > 0) stringBuilder.Append(", ");
|
---|
| 171 | stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
|
---|
| 172 | }
|
---|
| 173 | stringBuilder.AppendFormat(" {0} [Not Supported] )", node.Symbol.Name);
|
---|
[4861] | 174 | }
|
---|
| 175 |
|
---|
[4868] | 176 | stringBuilder.Append(")");
|
---|
| 177 |
|
---|
[4861] | 178 | return stringBuilder.ToString();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9821] | 182 | return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
|
---|
[4861] | 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|