[14024] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14024] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Globalization;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Text;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Formats mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3)
|
---|
| 35 | /// </summary>
|
---|
| 36 | [StorableClass]
|
---|
| 37 | [Item("Infix Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to infix expressions.")]
|
---|
| 38 |
|
---|
[14026] | 39 | public sealed class InfixExpressionFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
[14024] | 40 |
|
---|
| 41 |
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | private InfixExpressionFormatter(bool deserializing) : base(deserializing) { }
|
---|
| 44 | private InfixExpressionFormatter(InfixExpressionFormatter original, Cloner cloner) : base(original, cloner) { }
|
---|
| 45 | public InfixExpressionFormatter()
|
---|
| 46 | : base() {
|
---|
| 47 | Name = ItemName;
|
---|
| 48 | Description = ItemDescription;
|
---|
| 49 | }
|
---|
| 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 51 | return new InfixExpressionFormatter(this, cloner);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
| 55 | // skip root and start symbols
|
---|
| 56 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 57 | FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
|
---|
| 58 | return strBuilder.ToString();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
|
---|
| 62 | if (node.SubtreeCount > 1) {
|
---|
| 63 | var token = GetToken(node.Symbol);
|
---|
[16359] | 64 | // operators
|
---|
| 65 | if (token == "+" || token == "-" || token == "OR" || token == "XOR" ||
|
---|
| 66 | token == "*" || token == "/" || token == "AND" ||
|
---|
| 67 | token == "^") {
|
---|
[14024] | 68 | strBuilder.Append("(");
|
---|
| 69 | FormatRecursively(node.Subtrees.First(), strBuilder);
|
---|
| 70 |
|
---|
| 71 | foreach (var subtree in node.Subtrees.Skip(1)) {
|
---|
| 72 | strBuilder.Append(" ").Append(token).Append(" ");
|
---|
| 73 | FormatRecursively(subtree, strBuilder);
|
---|
| 74 | }
|
---|
| 75 | strBuilder.Append(")");
|
---|
[14347] | 76 | } else {
|
---|
| 77 | // function with multiple arguments
|
---|
| 78 | strBuilder.Append(token).Append("(");
|
---|
| 79 | FormatRecursively(node.Subtrees.First(), strBuilder);
|
---|
| 80 | foreach (var subtree in node.Subtrees.Skip(1)) {
|
---|
| 81 | strBuilder.Append(", ");
|
---|
| 82 | FormatRecursively(subtree, strBuilder);
|
---|
| 83 | }
|
---|
| 84 | strBuilder.Append(")");
|
---|
[14024] | 85 | }
|
---|
| 86 | } else if (node.SubtreeCount == 1) {
|
---|
| 87 | var token = GetToken(node.Symbol);
|
---|
| 88 | if (token == "-" || token == "NOT") {
|
---|
| 89 | strBuilder.Append("(").Append(token).Append("(");
|
---|
| 90 | FormatRecursively(node.GetSubtree(0), strBuilder);
|
---|
| 91 | strBuilder.Append("))");
|
---|
| 92 | } else if (token == "/") {
|
---|
| 93 | strBuilder.Append("1/");
|
---|
| 94 | FormatRecursively(node.GetSubtree(0), strBuilder);
|
---|
| 95 | } else if (token == "+" || token == "*") {
|
---|
| 96 | FormatRecursively(node.GetSubtree(0), strBuilder);
|
---|
| 97 | } else {
|
---|
[14347] | 98 | // function with only one argument
|
---|
[14024] | 99 | strBuilder.Append(token).Append("(");
|
---|
| 100 | FormatRecursively(node.GetSubtree(0), strBuilder);
|
---|
| 101 | strBuilder.Append(")");
|
---|
| 102 | }
|
---|
| 103 | } else {
|
---|
| 104 | // no subtrees
|
---|
[14350] | 105 | if (node.Symbol is LaggedVariable) {
|
---|
| 106 | var varNode = node as LaggedVariableTreeNode;
|
---|
| 107 | if (!varNode.Weight.IsAlmost(1.0)) {
|
---|
| 108 | strBuilder.Append("(");
|
---|
| 109 | strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
|
---|
| 110 | strBuilder.Append("*");
|
---|
| 111 | }
|
---|
| 112 | strBuilder.Append("LAG(");
|
---|
| 113 | if (varNode.VariableName.Contains("'")) {
|
---|
| 114 | strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
|
---|
| 115 | } else {
|
---|
| 116 | strBuilder.AppendFormat("'{0}'", varNode.VariableName);
|
---|
| 117 | }
|
---|
| 118 | strBuilder.Append(", ")
|
---|
| 119 | .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag)
|
---|
| 120 | .Append(")");
|
---|
| 121 | } else if (node.Symbol is Variable) {
|
---|
[14024] | 122 | var varNode = node as VariableTreeNode;
|
---|
| 123 | if (!varNode.Weight.IsAlmost(1.0)) {
|
---|
| 124 | strBuilder.Append("(");
|
---|
| 125 | strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
|
---|
| 126 | strBuilder.Append("*");
|
---|
| 127 | }
|
---|
| 128 | if (varNode.VariableName.Contains("'")) {
|
---|
| 129 | strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
|
---|
| 130 | } else {
|
---|
| 131 | strBuilder.AppendFormat("'{0}'", varNode.VariableName);
|
---|
| 132 | }
|
---|
| 133 | if (!varNode.Weight.IsAlmost(1.0)) {
|
---|
| 134 | strBuilder.Append(")");
|
---|
| 135 | }
|
---|
[14826] | 136 | } else if (node.Symbol is FactorVariable) {
|
---|
| 137 | var factorNode = node as FactorVariableTreeNode;
|
---|
| 138 | if (factorNode.VariableName.Contains("'")) {
|
---|
| 139 | strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName);
|
---|
| 140 | } else {
|
---|
| 141 | strBuilder.AppendFormat("'{0}'", factorNode.VariableName);
|
---|
| 142 | }
|
---|
| 143 | strBuilder.AppendFormat("[{0}]",
|
---|
| 144 | string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture))));
|
---|
| 145 | } else if (node.Symbol is BinaryFactorVariable) {
|
---|
| 146 | var factorNode = node as BinaryFactorVariableTreeNode;
|
---|
| 147 | if (!factorNode.Weight.IsAlmost(1.0)) {
|
---|
| 148 | strBuilder.Append("(");
|
---|
| 149 | strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);
|
---|
| 150 | strBuilder.Append("*");
|
---|
| 151 | }
|
---|
| 152 | if (factorNode.VariableName.Contains("'")) {
|
---|
| 153 | strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName);
|
---|
| 154 | } else {
|
---|
| 155 | strBuilder.AppendFormat("'{0}'", factorNode.VariableName);
|
---|
| 156 | }
|
---|
| 157 | strBuilder.Append(" = ");
|
---|
| 158 | if (factorNode.VariableValue.Contains("'")) {
|
---|
| 159 | strBuilder.AppendFormat("\"{0}\"", factorNode.VariableValue);
|
---|
| 160 | } else {
|
---|
| 161 | strBuilder.AppendFormat("'{0}'", factorNode.VariableValue);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | if (!factorNode.Weight.IsAlmost(1.0)) {
|
---|
| 165 | strBuilder.Append(")");
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[14024] | 168 | } else if (node.Symbol is Constant) {
|
---|
| 169 | var constNode = node as ConstantTreeNode;
|
---|
| 170 | if (constNode.Value >= 0.0)
|
---|
| 171 | strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
|
---|
| 172 | else
|
---|
[14350] | 173 | strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1
|
---|
[14024] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | private string GetToken(ISymbol symbol) {
|
---|
[16359] | 179 | var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
|
---|
[14024] | 180 | if (tok == null)
|
---|
| 181 | throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name));
|
---|
| 182 | return tok;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|