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