Changeset 17434 for branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters
- Timestamp:
- 02/11/20 13:36:02 (5 years ago)
- Location:
- branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Globalization; 25 24 using System.Linq; … … 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;29 using HEAL.Attic; 31 30 32 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 34 33 /// Formats mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3) 35 34 /// </summary> 36 [Storable Class]35 [StorableType("6FE2C83D-A594-4ABF-B101-5AEAEA6D3E3D")] 37 36 [Item("Infix Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to infix expressions.")] 38 37 … … 41 40 42 41 [StorableConstructor] 43 private InfixExpressionFormatter( bool deserializing) : base(deserializing) { }42 private InfixExpressionFormatter(StorableConstructorFlag _) : base(_) { } 44 43 private InfixExpressionFormatter(InfixExpressionFormatter original, Cloner cloner) : base(original, cloner) { } 45 44 public InfixExpressionFormatter() … … 52 51 } 53 52 54 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 53 /// <summary> 54 /// Produces an infix expression for a given expression tree. 55 /// </summary> 56 /// <param name="symbolicExpressionTree">The tree representation of the expression.</param> 57 /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param> 58 /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param> 59 /// <returns>Infix expression</returns> 60 public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") { 55 61 // skip root and start symbols 56 62 StringBuilder strBuilder = new StringBuilder(); 57 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder );63 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); 58 64 return strBuilder.ToString(); 59 65 } 60 66 61 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 67 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 68 return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo); 69 } 70 71 private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) { 62 72 if (node.SubtreeCount > 1) { 63 73 var token = GetToken(node.Symbol); 64 if (token == "+" || token == "-" || token == "OR" || token == "XOR") { 74 // operators 75 if (token == "+" || token == "-" || token == "OR" || token == "XOR" || 76 token == "*" || token == "/" || token == "AND" || 77 token == "^") { 65 78 strBuilder.Append("("); 66 FormatRecursively(node.Subtrees.First(), strBuilder );79 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 67 80 68 81 foreach (var subtree in node.Subtrees.Skip(1)) { 69 82 strBuilder.Append(" ").Append(token).Append(" "); 70 FormatRecursively(subtree, strBuilder); 71 } 72 strBuilder.Append(")"); 73 74 } else if (token == "*" || token == "/" || token == "AND") { 75 strBuilder.Append("("); 76 FormatRecursively(node.Subtrees.First(), strBuilder); 77 78 foreach (var subtree in node.Subtrees.Skip(1)) { 79 strBuilder.Append(" ").Append(token).Append(" "); 80 FormatRecursively(subtree, strBuilder); 83 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 81 84 } 82 85 strBuilder.Append(")"); … … 84 87 // function with multiple arguments 85 88 strBuilder.Append(token).Append("("); 86 FormatRecursively(node.Subtrees.First(), strBuilder );89 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 87 90 foreach (var subtree in node.Subtrees.Skip(1)) { 88 91 strBuilder.Append(", "); 89 FormatRecursively(subtree, strBuilder );92 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 90 93 } 91 94 strBuilder.Append(")"); … … 95 98 if (token == "-" || token == "NOT") { 96 99 strBuilder.Append("(").Append(token).Append("("); 97 FormatRecursively(node.GetSubtree(0), strBuilder );100 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 98 101 strBuilder.Append("))"); 99 102 } else if (token == "/") { 100 103 strBuilder.Append("1/"); 101 FormatRecursively(node.GetSubtree(0), strBuilder );104 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 102 105 } else if (token == "+" || token == "*") { 103 FormatRecursively(node.GetSubtree(0), strBuilder );106 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 104 107 } else { 105 108 // function with only one argument 106 109 strBuilder.Append(token).Append("("); 107 FormatRecursively(node.GetSubtree(0), strBuilder );110 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 108 111 strBuilder.Append(")"); 109 112 } … … 114 117 if (!varNode.Weight.IsAlmost(1.0)) { 115 118 strBuilder.Append("("); 116 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);119 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 117 120 strBuilder.Append("*"); 118 121 } … … 124 127 } 125 128 strBuilder.Append(", ") 126 .AppendFormat( CultureInfo.InvariantCulture, "{0}", varNode.Lag)129 .AppendFormat(numberFormat, "{0}", varNode.Lag) 127 130 .Append(")"); 128 131 } else if (node.Symbol is Variable) { … … 130 133 if (!varNode.Weight.IsAlmost(1.0)) { 131 134 strBuilder.Append("("); 132 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);135 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 133 136 strBuilder.Append("*"); 134 137 } … … 149 152 } 150 153 strBuilder.AppendFormat("[{0}]", 151 string.Join(", ", factorNode.Weights.Select(w => w.ToString( CultureInfo.InvariantCulture))));154 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 152 155 } else if (node.Symbol is BinaryFactorVariable) { 153 156 var factorNode = node as BinaryFactorVariableTreeNode; 154 157 if (!factorNode.Weight.IsAlmost(1.0)) { 155 158 strBuilder.Append("("); 156 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);159 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 157 160 strBuilder.Append("*"); 158 161 } … … 176 179 var constNode = node as ConstantTreeNode; 177 180 if (constNode.Value >= 0.0) 178 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", constNode.Value);181 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 179 182 else 180 strBuilder.Append Format(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1183 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 181 184 } 182 185 } 183 186 } 184 187 185 private st ring GetToken(ISymbol symbol) {186 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol). SingleOrDefault();188 private static string GetToken(ISymbol symbol) { 189 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); 187 190 if (tok == null) 188 191 throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name)); -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 26 26 using System.Text; 27 27 using System.Text.RegularExpressions; 28 using HEAL.Attic; 28 29 using HeuristicLab.Common; 29 30 using HeuristicLab.Core; 30 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 34 34 [Item("C# Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to C# code.")] 35 [Storable Class]35 [StorableType("88298836-6087-405A-9354-D4E6864887EB")] 36 36 public sealed class CSharpSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 37 37 [StorableConstructor] 38 private CSharpSymbolicExpressionTreeStringFormatter( bool deserializing) : base(deserializing) { }38 private CSharpSymbolicExpressionTreeStringFormatter(StorableConstructorFlag _) : base(_) { } 39 39 private CSharpSymbolicExpressionTreeStringFormatter(CSharpSymbolicExpressionTreeStringFormatter original, Cloner cloner) : base(original, cloner) { } 40 40 public CSharpSymbolicExpressionTreeStringFormatter() … … 56 56 } 57 57 58 private string VariableName2Identifier(string name) { 58 private string VariableName2Identifier(string name) { 59 59 /* 60 60 * identifier-start-character: … … 131 131 } else if (node.Symbol is Tangent) { 132 132 FormatFunction(node, "Math.Tan", strBuilder); 133 } else if (node.Symbol is HyperbolicTangent) { 134 FormatFunction(node, "Math.Tanh", strBuilder); 133 135 } else if (node.Symbol is Square) { 134 136 FormatSquare(node, strBuilder); 135 137 } else if (node.Symbol is SquareRoot) { 136 138 FormatFunction(node, "Math.Sqrt", strBuilder); 139 } else if (node.Symbol is Cube) { 140 FormatPower(node, strBuilder, "3"); 141 } else if (node.Symbol is CubeRoot) { 142 strBuilder.Append("Cbrt("); 143 FormatRecursively(node.GetSubtree(0), strBuilder); 144 strBuilder.Append(")"); 137 145 } else if (node.Symbol is Power) { 138 146 FormatFunction(node, "Math.Pow", strBuilder); 139 147 } else if (node.Symbol is Root) { 140 148 FormatRoot(node, strBuilder); 149 } else if (node.Symbol is Absolute) { 150 FormatFunction(node, "Math.Abs", strBuilder); 151 } else if (node.Symbol is AnalyticQuotient) { 152 strBuilder.Append("("); 153 FormatRecursively(node.GetSubtree(0), strBuilder); 154 strBuilder.Append(" / Math.Sqrt(1 + Math.Pow("); 155 FormatRecursively(node.GetSubtree(1), strBuilder); 156 strBuilder.Append(" , 2) )"); 141 157 } else { 142 158 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter."); … … 171 187 172 188 private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 189 FormatPower(node, strBuilder, "2"); 190 } 191 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 173 192 strBuilder.Append("Math.Pow("); 174 193 FormatRecursively(node.GetSubtree(0), strBuilder); 175 strBuilder.Append( ", 2)");194 strBuilder.Append($", {exponent})"); 176 195 } 177 196 … … 235 254 strBuilder.AppendLine("public static class Model {"); 236 255 GenerateAverageSource(strBuilder); 256 GenerateCbrtSource(strBuilder); 237 257 GenerateIfThenElseSource(strBuilder); 238 258 GenerateFactorSource(strBuilder); … … 276 296 strBuilder.AppendLine("private static double Average(params double[] values) {"); 277 297 strBuilder.AppendLine(" return values.Average();"); 298 strBuilder.AppendLine("}"); 299 } 300 private void GenerateCbrtSource(StringBuilder strBuilder) { 301 strBuilder.AppendLine("private static double Cbrt(double x) {"); 302 strBuilder.AppendLine(" return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);"); 278 303 strBuilder.AppendLine("}"); 279 304 } -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 25 25 using System.Linq; 26 26 using System.Text; 27 using HEAL.Attic; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; 29 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 [Item("Excel String Formatter", "String formatter for string representations of symbolic data analysis expressions in Excel syntax.")] 34 [Storable Class]34 [StorableType("46C46897-9C92-4CF1-81C9-700732700DD3")] 35 35 public sealed class SymbolicDataAnalysisExpressionExcelFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 36 36 [StorableConstructor] 37 private SymbolicDataAnalysisExpressionExcelFormatter( bool deserializing) : base(deserializing) { }37 private SymbolicDataAnalysisExpressionExcelFormatter(StorableConstructorFlag _) : base(_) { } 38 38 private SymbolicDataAnalysisExpressionExcelFormatter(SymbolicDataAnalysisExpressionExcelFormatter original, Cloner cloner) : base(original, cloner) { } 39 39 public SymbolicDataAnalysisExpressionExcelFormatter() … … 119 119 } 120 120 stringBuilder.Append(")"); 121 } else if (symbol is Absolute) { 122 stringBuilder.Append($"ABS({FormatRecursively(node.GetSubtree(0))})"); 123 } else if (symbol is AnalyticQuotient) { 124 stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / SQRT(1 + POWER({FormatRecursively(node.GetSubtree(1))}, 2))"); 121 125 } else if (symbol is Average) { 122 stringBuilder.Append("(1/ ");126 stringBuilder.Append("(1/("); 123 127 stringBuilder.Append(node.SubtreeCount); 124 128 stringBuilder.Append(")*("); … … 129 133 stringBuilder.Append(")"); 130 134 } 131 stringBuilder.Append(") ");135 stringBuilder.Append("))"); 132 136 } else if (symbol is Constant) { 133 137 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; … … 137 141 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 138 142 stringBuilder.Append(")"); 143 } else if (symbol is Cube) { 144 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)"); 145 } else if (symbol is CubeRoot) { 146 var arg_expr = FormatRecursively(node.GetSubtree(0)); 147 stringBuilder.Append($"IF({arg_expr} < 0, -POWER(-{arg_expr}, 1/3), POWER({arg_expr}, 1/3))"); 139 148 } else if (symbol is Division) { 140 149 if (node.SubtreeCount == 1) { 141 stringBuilder.Append("1/"); 142 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 150 stringBuilder.Append("1/("); 151 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 152 stringBuilder.Append(")"); 143 153 } else { 144 154 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 192 202 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 193 203 stringBuilder.Append(")"); 194 204 } else if (symbol is HyperbolicTangent) { 205 stringBuilder.Append("TANH("); 206 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 207 stringBuilder.Append(")"); 195 208 } else if (symbol is Variable) { 196 209 VariableTreeNode variableTreeNode = node as VariableTreeNode; -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 32 [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")] 33 [Storable Class]33 [StorableType("D7186DFF-1596-4A58-B27D-974DF0D93E4F")] 34 34 public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 private readonly List<KeyValuePair<string, double>> constants; … … 41 41 42 42 [StorableConstructor] 43 private SymbolicDataAnalysisExpressionLatexFormatter( bool deserializing) : base(deserializing) { }43 private SymbolicDataAnalysisExpressionLatexFormatter(StorableConstructorFlag _) : base(_) { } 44 44 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 45 45 : base(original, cloner) { … … 117 117 strBuilder.Append(@" \cfrac{ "); 118 118 } 119 } else if (node.Symbol is Absolute) { 120 strBuilder.Append(@"\operatorname{abs} \left( "); 121 } else if (node.Symbol is AnalyticQuotient) { 122 strBuilder.Append(@" \frac { "); 119 123 } else if (node.Symbol is Average) { 120 124 // skip output of (1/1) if only one subtree … … 131 135 } else if (node.Symbol is SquareRoot) { 132 136 strBuilder.Append(@"\sqrt{"); 137 } else if (node.Symbol is Cube) { 138 strBuilder.Append(@"\left("); 139 } else if (node.Symbol is CubeRoot) { 140 strBuilder.Append(@"\operatorname{cbrt}\left("); 133 141 } else if (node.Symbol is Sine) { 134 142 strBuilder.Append(@"\sin \left( "); … … 137 145 } else if (node.Symbol is Tangent) { 138 146 strBuilder.Append(@"\tan \left( "); 147 } else if (node.Symbol is HyperbolicTangent) { 148 strBuilder.Append(@"\tanh \left( "); 139 149 } else if (node.Symbol is AiryA) { 140 150 strBuilder.Append(@"\operatorname{airy}_a \left( "); … … 287 297 else 288 298 strBuilder.Append(@" }{ \cfrac{ "); 299 } else if (node.Symbol is Absolute) { 300 throw new InvalidOperationException(); 301 } else if (node.Symbol is AnalyticQuotient) { 302 strBuilder.Append(@"}{\sqrt{1 + \left( "); 289 303 } else if (node.Symbol is Average) { 290 304 strBuilder.Append(@" + "); … … 297 311 } else if (node.Symbol is SquareRoot) { 298 312 throw new InvalidOperationException(); 313 } else if (node.Symbol is Cube) { 314 throw new InvalidOperationException(); 315 } else if (node.Symbol is CubeRoot) { 316 throw new InvalidOperationException(); 299 317 } else if (node.Symbol is Sine) { 300 318 throw new InvalidOperationException(); … … 302 320 throw new InvalidOperationException(); 303 321 } else if (node.Symbol is Tangent) { 322 throw new InvalidOperationException(); 323 } else if (node.Symbol is HyperbolicTangent) { 304 324 throw new InvalidOperationException(); 305 325 } else if (node.Symbol is AiryA) { … … 383 403 for (int i = 2; i < node.SubtreeCount; i++) 384 404 strBuilder.Append(" } "); 405 } else if (node.Symbol is Absolute) { 406 strBuilder.Append(@" \right)"); 407 } else if (node.Symbol is AnalyticQuotient) { 408 strBuilder.Append(@" \right)^2}}"); 385 409 } else if (node.Symbol is Average) { 386 410 strBuilder.Append(@" \right) "); … … 393 417 } else if (node.Symbol is SquareRoot) { 394 418 strBuilder.Append(@"}"); 419 } else if (node.Symbol is Cube) { 420 strBuilder.Append(@"\right)^3"); 421 } else if (node.Symbol is CubeRoot) { 422 strBuilder.Append(@"\right)"); 395 423 } else if (node.Symbol is Sine) { 396 424 strBuilder.Append(@" \right) "); … … 398 426 strBuilder.Append(@" \right) "); 399 427 } else if (node.Symbol is Tangent) { 428 strBuilder.Append(@" \right) "); 429 } else if (node.Symbol is HyperbolicTangent) { 400 430 strBuilder.Append(@" \right) "); 401 431 } else if (node.Symbol is AiryA) { -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 23 23 using System.Linq; 24 24 using System.Text; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;29 29 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 31 31 32 32 [Item("MATLAB String Formatter", "String formatter for string representations of symbolic data analysis expressions in MATLAB syntax.")] 33 [Storable Class]33 [StorableType("ADFB0A37-412D-4DD4-A174-F0103ADD7972")] 34 34 public sealed class SymbolicDataAnalysisExpressionMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 private int currentLag; 36 36 37 37 [StorableConstructor] 38 private SymbolicDataAnalysisExpressionMATLABFormatter( bool deserializing) : base(deserializing) { }38 private SymbolicDataAnalysisExpressionMATLABFormatter(StorableConstructorFlag _) : base(_) { } 39 39 private SymbolicDataAnalysisExpressionMATLABFormatter(SymbolicDataAnalysisExpressionMATLABFormatter original, Cloner cloner) : base(original, cloner) { } 40 40 public SymbolicDataAnalysisExpressionMATLABFormatter() … … 126 126 } 127 127 stringBuilder.Append(")"); 128 } else if (symbol is Absolute) { 129 stringBuilder.Append($"abs({FormatRecursively(node.GetSubtree(0))})"); 130 } else if (symbol is AnalyticQuotient) { 131 stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / sqrt(1 + ({FormatRecursively(node.GetSubtree(1))}).^2)"); 128 132 } else if (symbol is And) { 129 133 stringBuilder.Append("(("); … … 179 183 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 180 184 stringBuilder.Append(")"); 185 } else if (symbol is Cube) { 186 stringBuilder.Append("("); 187 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 188 stringBuilder.Append(").^3"); 189 } else if (symbol is CubeRoot) { 190 stringBuilder.Append("NTHROOT("); 191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(", 3)"); 181 193 } else if (symbol is GreaterThan) { 182 194 stringBuilder.Append("(("); … … 252 264 } else if (symbol is Tangent) { 253 265 stringBuilder.Append("tan("); 266 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 267 stringBuilder.Append(")"); 268 } else if (symbol is HyperbolicTangent) { 269 stringBuilder.Append("tanh("); 254 270 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 255 271 stringBuilder.Append(")"); -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 32 [Item("Mathematica Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to Mathematica expressions.")] 33 [Storable Class]33 [StorableType("818A9294-FA95-41F6-A5F0-D7D050BDD076")] 34 34 public sealed class SymbolicDataAnalysisExpressionMathematicaFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 [StorableConstructor] 36 private SymbolicDataAnalysisExpressionMathematicaFormatter( bool deserializing) : base(deserializing) { }36 private SymbolicDataAnalysisExpressionMathematicaFormatter(StorableConstructorFlag _) : base(_) { } 37 37 private SymbolicDataAnalysisExpressionMathematicaFormatter(SymbolicDataAnalysisExpressionMathematicaFormatter original, Cloner cloner) : base(original, cloner) { } 38 38 public SymbolicDataAnalysisExpressionMathematicaFormatter() … … 56 56 if (node.Symbol is Addition) { 57 57 FormatFunction(node, "Plus", strBuilder); 58 } else if (node.Symbol is Absolute) { 59 FormatFunction(node, "Abs", strBuilder); 60 } else if (node.Symbol is AnalyticQuotient) { 61 strBuilder.Append("["); 62 FormatRecursively(node.GetSubtree(0), strBuilder); 63 strBuilder.Append("]/Sqrt[ 1 + Power["); 64 FormatRecursively(node.GetSubtree(1), strBuilder); 65 strBuilder.Append(", 2]]"); 58 66 } else if (node.Symbol is Average) { 59 67 FormatAverage(node, strBuilder); … … 70 78 } else if (node.Symbol is Tangent) { 71 79 FormatFunction(node, "Tan", strBuilder); 80 } else if (node.Symbol is HyperbolicTangent) { 81 FormatFunction(node, "Tanh", strBuilder); 72 82 } else if (node.Symbol is Exponential) { 73 83 FormatFunction(node, "Exp", strBuilder); … … 102 112 } else if (node.Symbol is SquareRoot) { 103 113 FormatFunction(node, "Sqrt", strBuilder); 114 } else if (node.Symbol is Cube) { 115 FormatPower(node, strBuilder, "3"); 116 } else if (node.Symbol is CubeRoot) { 117 strBuilder.Append("CubeRoot["); 118 FormatRecursively(node.GetSubtree(0), strBuilder); 119 strBuilder.Append("]"); 104 120 } else if (node.Symbol is Power) { 105 121 FormatFunction(node, "Power", strBuilder); … … 203 219 204 220 private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 221 FormatPower(node, strBuilder, "2"); 222 } 223 224 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 205 225 strBuilder.Append("Power["); 206 226 FormatRecursively(node.GetSubtree(0), strBuilder); 207 strBuilder.Append( ", 2]");227 strBuilder.Append($", {exponent}]"); 208 228 } 209 229 -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System; 22 23 using System.Globalization; 23 24 using System.Text; … … 69 70 } 70 71 stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]"); 72 } else if (symbol is Absolute) { 73 stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) abs"); 74 } else if (symbol is AnalyticQuotient) { 75 stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / (1 + ({FormatPower(node.GetSubtree(1), "2")})) sqrt"); 71 76 } else if (symbol is Average) { 72 77 stringBuilder.Append("(1/"); … … 84 89 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 85 90 stringBuilder.Append(" cos"); 91 } else if (symbol is Cube) { 92 stringBuilder.Append(FormatPower(node.GetSubtree(0), "3")); 93 } else if (symbol is CubeRoot) { 94 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 95 stringBuilder.Append(" cbrt"); 86 96 } else if (symbol is Division) { 87 97 if (node.SubtreeCount == 1) { … … 146 156 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 147 157 stringBuilder.Append(" sin"); 158 } else if (symbol is Square) { 159 stringBuilder.Append(FormatPower(node.GetSubtree(0), "2")); 160 } else if (symbol is SquareRoot) { 161 stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/2)")); 148 162 } else if (symbol is Subtraction) { 149 163 if (node.SubtreeCount == 1) { … … 160 174 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 161 175 stringBuilder.Append(" tan"); 176 } else if (symbol is HyperbolicTangent) { 177 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 178 stringBuilder.Append(" tanh"); 162 179 } else if (symbol is Variable) { 163 180 VariableTreeNode variableTreeNode = node as VariableTreeNode; … … 181 198 } 182 199 200 private string FormatPower(ISymbolicExpressionTreeNode node, string exponent) { 201 return $"(({FormatRecursively(node)}) log * {exponent}) exp "; 202 } 203 183 204 public override IDeepCloneable Clone(Cloner cloner) { 184 205 return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner); -
branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/TSQLExpressionFormatter.cs
r16130 r17434 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2018Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;30 using HEAL.Attic; 31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 43 43 44 44 [Item("TSQL String Formatter", "String formatter for string representations of symbolic data analysis expressions in TSQL syntax.")] 45 [Storable Class]45 [StorableType("549808A5-A062-4972-9DDB-E4B5CD392470")] 46 46 public sealed class TSQLExpressionFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 47 47 [StorableConstructor] 48 private TSQLExpressionFormatter( bool deserializing) : base(deserializing) { }48 private TSQLExpressionFormatter(StorableConstructorFlag _) : base(_) { } 49 49 private TSQLExpressionFormatter(TSQLExpressionFormatter original, Cloner cloner) : base(original, cloner) { } 50 50 public TSQLExpressionFormatter()
Note: See TracChangeset
for help on using the changeset viewer.