Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs @ 9779

Last change on this file since 9779 was 9708, checked in by mkommend, 11 years ago

#1837: Merged trunk changes into sliding window gp branch.

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