Changeset 16892 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 05/04/19 08:22:42 (6 years ago)
- Location:
- branches/2925_AutoDiffForDynamicalModels
- Files:
-
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels
- Property svn:mergeinfo changed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
r16662 r16892 83 83 if (branch.SubtreeCount >= 2) { 84 84 var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 85 var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();86 85 var fprime = Derive(f, variableName); 87 var gprime = Derive(g, variableName); 88 var fgPrime = Sum(Product(f, gprime), Product(fprime, g)); 89 for (int i = 2; i < branch.SubtreeCount; i++) { 86 for (int i = 1; i < branch.SubtreeCount; i++) { 87 var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); 90 88 var fg = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone()); 91 var h = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); 92 var hPrime = Derive(h, variableName); 93 fgPrime = Sum(Product(fgPrime, h), Product(fg, hPrime)); 89 var gPrime = Derive(g, variableName); 90 var fgPrime = Sum(Product(fprime, g), Product(gPrime, f)); 91 // prepare for next iteration 92 f = fg; 93 fprime = fgPrime; 94 94 } 95 return f gPrime;95 return fprime; 96 96 } else 97 97 // multiplication with only one argument has no effect -> derive the argument -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs
r16662 r16892 38 38 double @const = 0) { 39 39 40 if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException(); 40 if (factorCoefficients.Length == 0 && coefficients.Length == 0 && @const==0) throw new ArgumentException(); 41 42 // Combine both trees 43 ISymbolicExpressionTreeNode add = (new Addition()).CreateTreeNode(); 41 44 42 45 // Create tree for double variables 43 ISymbolicExpressionTree tree = null;44 46 if (coefficients.Length > 0) { 45 tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const); 46 if (factorCoefficients.Length == 0) return tree; 47 var varTree = CreateTree(variableNames, new int[variableNames.Length], coefficients); 48 foreach (var varNode in varTree.IterateNodesPrefix().OfType<VariableTreeNode>()) 49 add.AddSubtree(varNode); 47 50 } 48 51 49 52 // Create tree for string variables 50 ISymbolicExpressionTree factorTree = null;51 53 if (factorCoefficients.Length > 0) { 52 factorTree = CreateTree(factors, factorCoefficients, @const); 53 if (tree == null) return factorTree; 54 var factorTree = CreateTree(factors, factorCoefficients); 55 foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>()) 56 add.AddSubtree(binFactorNode); 54 57 } 55 58 56 // Combine both trees 57 ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0); 58 foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>()) 59 add.InsertSubtree(add.SubtreeCount - 1, binFactorNode); 59 if (@const!=0.0) { 60 ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); 61 cNode.Value = @const; 62 add.AddSubtree(cNode); 63 } 64 65 ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode()); 66 ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode(); 67 tree.Root.AddSubtree(startNode); 68 startNode.AddSubtree(add); 60 69 return tree; 61 62 throw new ArgumentException();63 70 } 64 71 -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16662 r16892 51 51 } 52 52 53 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") { 54 61 // skip root and start symbols 55 62 StringBuilder strBuilder = new StringBuilder(); 56 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder );63 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); 57 64 return strBuilder.ToString(); 58 65 } 59 66 60 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) { 61 72 if (node.SubtreeCount > 1) { 62 73 var token = GetToken(node.Symbol); … … 66 77 token == "^") { 67 78 strBuilder.Append("("); 68 FormatRecursively(node.Subtrees.First(), strBuilder );79 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 69 80 70 81 foreach (var subtree in node.Subtrees.Skip(1)) { 71 82 strBuilder.Append(" ").Append(token).Append(" "); 72 FormatRecursively(subtree, strBuilder );83 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 73 84 } 74 85 strBuilder.Append(")"); … … 76 87 // function with multiple arguments 77 88 strBuilder.Append(token).Append("("); 78 FormatRecursively(node.Subtrees.First(), strBuilder );89 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 79 90 foreach (var subtree in node.Subtrees.Skip(1)) { 80 91 strBuilder.Append(", "); 81 FormatRecursively(subtree, strBuilder );92 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 82 93 } 83 94 strBuilder.Append(")"); … … 87 98 if (token == "-" || token == "NOT") { 88 99 strBuilder.Append("(").Append(token).Append("("); 89 FormatRecursively(node.GetSubtree(0), strBuilder );100 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 90 101 strBuilder.Append("))"); 91 102 } else if (token == "/") { 92 103 strBuilder.Append("1/"); 93 FormatRecursively(node.GetSubtree(0), strBuilder );104 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 94 105 } else if (token == "+" || token == "*") { 95 FormatRecursively(node.GetSubtree(0), strBuilder );106 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 96 107 } else { 97 108 // function with only one argument 98 109 strBuilder.Append(token).Append("("); 99 FormatRecursively(node.GetSubtree(0), strBuilder );110 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 100 111 strBuilder.Append(")"); 101 112 } … … 106 117 if (!varNode.Weight.IsAlmost(1.0)) { 107 118 strBuilder.Append("("); 108 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);119 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 109 120 strBuilder.Append("*"); 110 121 } … … 116 127 } 117 128 strBuilder.Append(", ") 118 .AppendFormat( CultureInfo.InvariantCulture, "{0}", varNode.Lag)129 .AppendFormat(numberFormat, "{0}", varNode.Lag) 119 130 .Append(")"); 120 131 } else if (node.Symbol is Variable) { … … 122 133 if (!varNode.Weight.IsAlmost(1.0)) { 123 134 strBuilder.Append("("); 124 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);135 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 125 136 strBuilder.Append("*"); 126 137 } … … 141 152 } 142 153 strBuilder.AppendFormat("[{0}]", 143 string.Join(", ", factorNode.Weights.Select(w => w.ToString( CultureInfo.InvariantCulture))));154 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 144 155 } else if (node.Symbol is BinaryFactorVariable) { 145 156 var factorNode = node as BinaryFactorVariableTreeNode; 146 157 if (!factorNode.Weight.IsAlmost(1.0)) { 147 158 strBuilder.Append("("); 148 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);159 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 149 160 strBuilder.Append("*"); 150 161 } … … 168 179 var constNode = node as ConstantTreeNode; 169 180 if (constNode.Value >= 0.0) 170 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", constNode.Value);181 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 171 182 else 172 strBuilder.Append Format(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1183 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 173 184 } 174 185 } 175 186 } 176 187 177 private st ring GetToken(ISymbol symbol) {188 private static string GetToken(ISymbol symbol) { 178 189 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); 179 190 if (tok == null) -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16662 r16892 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 HEAL.Attic;32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 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 FormatPower(node, strBuilder, "1.0/3"); 137 143 } else if (node.Symbol is Power) { 138 144 FormatFunction(node, "Math.Pow", strBuilder); 139 145 } else if (node.Symbol is Root) { 140 146 FormatRoot(node, strBuilder); 147 } else if (node.Symbol is Absolute) { 148 FormatFunction(node, "Math.Abs", strBuilder); 149 } else if (node.Symbol is AnalyticQuotient) { 150 strBuilder.Append("("); 151 FormatRecursively(node.GetSubtree(0), strBuilder); 152 strBuilder.Append(" / Math.Sqrt(1 + Math.Pow("); 153 FormatRecursively(node.GetSubtree(1), strBuilder); 154 strBuilder.Append(" , 2) )"); 141 155 } else { 142 156 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter."); … … 171 185 172 186 private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 187 FormatPower(node, strBuilder, "2"); 188 } 189 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 173 190 strBuilder.Append("Math.Pow("); 174 191 FormatRecursively(node.GetSubtree(0), strBuilder); 175 strBuilder.Append( ", 2)");192 strBuilder.Append($", {exponent})"); 176 193 } 177 194 -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16662 r16892 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 HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 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 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 1/3)"); 139 147 } else if (symbol is Division) { 140 148 if (node.SubtreeCount == 1) { 141 stringBuilder.Append("1/"); 142 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 149 stringBuilder.Append("1/("); 150 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 151 stringBuilder.Append(")"); 143 152 } else { 144 153 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 192 201 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 193 202 stringBuilder.Append(")"); 194 203 } else if (symbol is HyperbolicTangent) { 204 stringBuilder.Append("TANH("); 205 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 206 stringBuilder.Append(")"); 195 207 } else if (symbol is Variable) { 196 208 VariableTreeNode variableTreeNode = node as VariableTreeNode; -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16662 r16892 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 HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 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(@"\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)^\frac{1}{3}"); 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/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16662 r16892 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 HEAL.Attic;29 29 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 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("("); 191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(").^(1/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/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16662 r16892 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 HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 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 FormatPower(node, strBuilder, "1/3"); 104 118 } else if (node.Symbol is Power) { 105 119 FormatFunction(node, "Power", strBuilder); … … 203 217 204 218 private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 219 FormatPower(node, strBuilder, "2"); 220 } 221 222 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 205 223 strBuilder.Append("Power["); 206 224 FormatRecursively(node.GetSubtree(0), strBuilder); 207 strBuilder.Append( ", 2]");225 strBuilder.Append($", {exponent}]"); 208 226 } 209 227 -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16662 r16892 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(FormatPower(node.GetSubtree(0), "(1/3)")); 86 95 } else if (symbol is Division) { 87 96 if (node.SubtreeCount == 1) { … … 146 155 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 147 156 stringBuilder.Append(" sin"); 157 } else if (symbol is Square) { 158 stringBuilder.Append(FormatPower(node.GetSubtree(0), "2")); 159 } else if (symbol is SquareRoot) { 160 stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/2)")); 148 161 } else if (symbol is Subtraction) { 149 162 if (node.SubtreeCount == 1) { … … 160 173 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 161 174 stringBuilder.Append(" tan"); 175 } else if (symbol is HyperbolicTangent) { 176 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 177 stringBuilder.Append(" tanh"); 162 178 } else if (symbol is Variable) { 163 179 VariableTreeNode variableTreeNode = node as VariableTreeNode; … … 181 197 } 182 198 199 private string FormatPower(ISymbolicExpressionTreeNode node, string exponent) { 200 return $"(({FormatRecursively(node)}) log * {exponent}) exp "; 201 } 202 183 203 public override IDeepCloneable Clone(Cloner cloner) { 184 204 return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16662 r16892 106 106 <HintPath>..\..\bin\AutoDiff-1.0.dll</HintPath> 107 107 <Private>False</Private> 108 </Reference> 109 <Reference Include="HEAL.Attic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 110 <SpecificVersion>False</SpecificVersion> 111 <HintPath>..\..\bin\HEAL.Attic.dll</HintPath> 108 112 </Reference> 109 113 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.NativeInterpreter-0.1, Version=0.0.0.1, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> … … 404 408 </ProjectReference> 405 409 </ItemGroup> 406 <ItemGroup>407 <PackageReference Include="HEAL.Attic">408 <Version>1.0.0-pre03</Version>409 </PackageReference>410 <PackageReference Include="System.Numerics.Vectors">411 <Version>4.5.0</Version>412 </PackageReference>413 </ItemGroup>414 410 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 415 411 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r16662 r16892 69 69 #endregion 70 70 71 public Interval GetSymbolicEx ressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {71 public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) { 72 72 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 73 return GetSymbolicEx ressionTreeInterval(tree, variableRanges);74 } 75 76 public Interval GetSymbolicEx ressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,77 out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {73 return GetSymbolicExpressionTreeInterval(tree, variableRanges); 74 } 75 76 public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 77 out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) { 78 78 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 79 return GetSymbolicEx ressionTreeIntervals(tree, variableRanges, out nodeIntervals);80 } 81 82 public Interval GetSymbolicEx ressionTreeInterval(ISymbolicExpressionTree tree,Dictionary<string, Interval> variableRanges) {79 return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals); 80 } 81 82 public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) { 83 83 lock (syncRoot) { 84 84 EvaluatedSolutions++; … … 96 96 97 97 98 public Interval GetSymbolicEx ressionTreeIntervals(ISymbolicExpressionTree tree,99 Dictionary<string, Interval> variableRanges, outDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {98 public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, 99 IDictionary<string, Interval> variableRanges, out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) { 100 100 lock (syncRoot) { 101 101 EvaluatedSolutions++; … … 108 108 // fix incorrect intervals if necessary (could occur because of numerical errors) 109 109 nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 110 foreach (var kvp in intervals) {110 foreach (var kvp in intervals) { 111 111 var interval = kvp.Value; 112 112 if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound) … … 124 124 125 125 126 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) {126 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) { 127 127 if (variableRanges == null) 128 128 throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges)); … … 141 141 } 142 142 143 private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {143 private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) { 144 144 Instruction currentInstr = instructions[instructionCounter]; 145 145 //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side … … 216 216 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 217 217 result = Interval.Tangens(argumentInterval); 218 break; 219 } 220 case OpCodes.Tanh: { 221 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 222 result = Interval.HyperbolicTangent(argumentInterval); 218 223 break; 219 224 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
r16662 r16892 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 26 26 27 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 28 public enum OpCode : byte { 29 Add = 1, 30 Sub = 2, 31 Mul = 3, 32 Div = 4, 33 Sin = 5, 34 Cos = 6, 35 Tan = 7, 36 Log = 8, 37 Exp = 9, 38 IfThenElse = 10, 39 GT = 11, 40 LT = 12, 41 AND = 13, 42 OR = 14, 43 NOT = 15, 44 Average = 16, 45 Call = 17, 46 Variable = 18, 47 LagVariable = 19, 48 Constant = 20, 49 Arg = 21, 50 Power = 22, 51 Root = 23, 52 TimeLag = 24, 53 Integral = 25, 54 Derivative = 26, 55 VariableCondition = 27, 56 Square = 28, 57 SquareRoot = 29, 58 Gamma = 30, 59 Psi = 31, 60 Dawson = 32, 61 ExponentialIntegralEi = 33, 62 CosineIntegral = 34, 63 SineIntegral = 35, 64 HyperbolicCosineIntegral = 36, 65 HyperbolicSineIntegral = 37, 66 FresnelCosineIntegral = 38, 67 FresnelSineIntegral = 39, 68 AiryA = 40, 69 AiryB = 41, 70 Norm = 42, 71 Erf = 43, 72 Bessel = 44, 73 XOR = 45, 74 FactorVariable = 46, 75 BinaryFactorVariable = 47, 76 Absolute = 48, 77 AnalyticQuotient = 49, 78 Cube = 50, 79 CubeRoot = 51, 80 Tanh = 52, 81 }; 27 82 public static class OpCodes { 28 public const byte Add = 1; 29 public const byte Sub = 2; 30 public const byte Mul = 3; 31 public const byte Div = 4; 32 33 public const byte Sin = 5; 34 public const byte Cos = 6; 35 public const byte Tan = 7; 36 37 public const byte Log = 8; 38 public const byte Exp = 9; 39 40 public const byte IfThenElse = 10; 41 42 public const byte GT = 11; 43 public const byte LT = 12; 44 45 public const byte AND = 13; 46 public const byte OR = 14; 47 public const byte NOT = 15; 48 49 50 public const byte Average = 16; 51 52 public const byte Call = 17; 53 54 public const byte Variable = 18; 55 public const byte LagVariable = 19; 56 public const byte Constant = 20; 57 public const byte Arg = 21; 58 59 public const byte Power = 22; 60 public const byte Root = 23; 61 public const byte TimeLag = 24; 62 public const byte Integral = 25; 63 public const byte Derivative = 26; 64 65 public const byte VariableCondition = 27; 66 67 public const byte Square = 28; 68 public const byte SquareRoot = 29; 69 public const byte Gamma = 30; 70 public const byte Psi = 31; 71 public const byte Dawson = 32; 72 public const byte ExponentialIntegralEi = 33; 73 public const byte CosineIntegral = 34; 74 public const byte SineIntegral = 35; 75 public const byte HyperbolicCosineIntegral = 36; 76 public const byte HyperbolicSineIntegral = 37; 77 public const byte FresnelCosineIntegral = 38; 78 public const byte FresnelSineIntegral = 39; 79 public const byte AiryA = 40; 80 public const byte AiryB = 41; 81 public const byte Norm = 42; 82 public const byte Erf = 43; 83 public const byte Bessel = 44; 84 public const byte XOR = 45; 85 public const byte FactorVariable = 46; 86 public const byte BinaryFactorVariable = 47; 87 public const byte Absolute = 48; 88 public const byte AnalyticQuotient = 49; 89 public const byte Cube = 50; 90 public const byte CubeRoot = 51; 91 92 public const byte Tanh = 52; 83 // constants for API compatibility only 84 public const byte Add = (byte)OpCode.Add; 85 public const byte Sub =(byte)OpCode.Sub; 86 public const byte Mul =(byte)OpCode.Mul; 87 public const byte Div =(byte)OpCode.Div; 88 public const byte Sin =(byte)OpCode.Sin; 89 public const byte Cos =(byte)OpCode.Cos; 90 public const byte Tan =(byte)OpCode.Tan; 91 public const byte Log =(byte)OpCode.Log; 92 public const byte Exp = (byte)OpCode.Exp; 93 public const byte IfThenElse = (byte)OpCode.IfThenElse; 94 public const byte GT = (byte)OpCode.GT; 95 public const byte LT = (byte)OpCode.LT; 96 public const byte AND = (byte)OpCode.AND; 97 public const byte OR = (byte)OpCode.OR; 98 public const byte NOT = (byte)OpCode.NOT; 99 public const byte Average = (byte)OpCode.Average; 100 public const byte Call = (byte)OpCode.Call; 101 public const byte Variable = (byte)OpCode.Variable; 102 public const byte LagVariable = (byte)OpCode.LagVariable; 103 public const byte Constant = (byte)OpCode.Constant; 104 public const byte Arg = (byte)OpCode.Arg; 105 public const byte Power = (byte)OpCode.Power; 106 public const byte Root = (byte)OpCode.Root; 107 public const byte TimeLag = (byte)OpCode.TimeLag; 108 public const byte Integral = (byte)OpCode.Integral; 109 public const byte Derivative = (byte)OpCode.Derivative; 110 public const byte VariableCondition = (byte)OpCode.VariableCondition; 111 public const byte Square = (byte)OpCode.Square; 112 public const byte SquareRoot = (byte)OpCode.SquareRoot; 113 public const byte Gamma = (byte)OpCode.Gamma; 114 public const byte Psi = (byte)OpCode.Psi; 115 public const byte Dawson = (byte)OpCode.Dawson; 116 public const byte ExponentialIntegralEi = (byte)OpCode.ExponentialIntegralEi; 117 public const byte CosineIntegral = (byte)OpCode.CosineIntegral; 118 public const byte SineIntegral = (byte)OpCode.SineIntegral; 119 public const byte HyperbolicCosineIntegral = (byte)OpCode.HyperbolicCosineIntegral; 120 public const byte HyperbolicSineIntegral = (byte)OpCode.HyperbolicSineIntegral; 121 public const byte FresnelCosineIntegral = (byte)OpCode.FresnelCosineIntegral; 122 public const byte FresnelSineIntegral = (byte)OpCode.FresnelSineIntegral; 123 public const byte AiryA = (byte)OpCode.AiryA; 124 public const byte AiryB = (byte)OpCode.AiryB; 125 public const byte Norm = (byte)OpCode.Norm; 126 public const byte Erf = (byte)OpCode.Erf; 127 public const byte Bessel = (byte)OpCode.Bessel; 128 public const byte XOR = (byte)OpCode.XOR; 129 public const byte FactorVariable = (byte)OpCode.FactorVariable; 130 public const byte BinaryFactorVariable = (byte)OpCode.BinaryFactorVariable; 131 public const byte Absolute = (byte)OpCode.Absolute; 132 public const byte AnalyticQuotient = (byte)OpCode.AnalyticQuotient; 133 public const byte Cube = (byte)OpCode.Cube; 134 public const byte CubeRoot = (byte)OpCode.CubeRoot; 135 public const byte Tanh = (byte)OpCode.Tanh; 93 136 94 137 … … 150 193 151 194 public static byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) { 152 byte opCode; 153 if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode)) return opCode; 195 if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out byte opCode)) return opCode; 154 196 else throw new NotSupportedException("Symbol: " + treeNode.Symbol); 155 197 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r16662 r16892 45 45 private static readonly MethodInfo Cos = typeof(Math).GetMethod("Cos", new[] { typeof(double) }); 46 46 private static readonly MethodInfo Tan = typeof(Math).GetMethod("Tan", new[] { typeof(double) }); 47 private static readonly MethodInfo Tanh = typeof(Math).GetMethod("Tanh", new[] { typeof(double) }); 47 48 private static readonly MethodInfo Sqrt = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) }); 48 49 private static readonly MethodInfo Floor = typeof(Math).GetMethod("Floor", new[] { typeof(double) }); … … 223 224 return Expression.Call(Tan, arg); 224 225 } 226 case OpCodes.Tanh: { 227 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 228 return Expression.Call(Tanh, arg); 229 } 225 230 case OpCodes.Square: { 226 231 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs
r16662 r16892 63 63 break; 64 64 } 65 65 case OpCodes.Constant: break; // nothing to do here, don't remove because we want to prevent falling into the default case here. 66 66 case OpCodes.Add: { 67 67 Load(instr.buf, code[c].buf); … … 173 173 break; 174 174 } 175 default: throw new NotSupportedException($"This interpreter does not support {(OpCode)instr.opcode}"); 175 176 } 176 177 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r16662 r16892 45 45 private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }); 46 46 private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) }); 47 private static MethodInfo tanh = typeof(Math).GetMethod("Tanh", new Type[] { typeof(double) }); 47 48 private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) }); 48 49 private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) }); … … 283 284 CompileInstructions(il, state, ds); 284 285 il.Emit(System.Reflection.Emit.OpCodes.Call, tan); 286 return; 287 } 288 case OpCodes.Tanh: { 289 CompileInstructions(il, state, ds); 290 il.Emit(System.Reflection.Emit.OpCodes.Call, tanh); 285 291 return; 286 292 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs
r16662 r16892 101 101 private IDataset dataset; 102 102 103 private static readonly HashSet<byte> supportedOpCodes = new HashSet<byte>() { 104 (byte)OpCode.Constant, 105 (byte)OpCode.Variable, 106 (byte)OpCode.Add, 107 (byte)OpCode.Sub, 108 (byte)OpCode.Mul, 109 (byte)OpCode.Div, 110 (byte)OpCode.Exp, 111 (byte)OpCode.Log, 112 (byte)OpCode.Sin, 113 (byte)OpCode.Cos, 114 (byte)OpCode.Tan, 115 (byte)OpCode.Tanh, 116 (byte)OpCode.Power, 117 (byte)OpCode.Root, 118 (byte)OpCode.SquareRoot, 119 (byte)OpCode.Square, 120 (byte)OpCode.CubeRoot, 121 (byte)OpCode.Cube, 122 (byte)OpCode.Absolute, 123 (byte)OpCode.AnalyticQuotient 124 }; 125 103 126 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 104 127 if (!rows.Any()) return Enumerable.Empty<double>(); … … 108 131 } 109 132 110 var code = Compile(tree, OpCodes.MapSymbolToOpCode); 133 byte mapSupportedSymbols(ISymbolicExpressionTreeNode node) { 134 var opCode = OpCodes.MapSymbolToOpCode(node); 135 if (supportedOpCodes.Contains(opCode)) return opCode; 136 else throw new NotSupportedException($"The native interpreter does not support {node.Symbol.Name}"); 137 }; 138 var code = Compile(tree, mapSupportedSymbols); 111 139 112 140 var rowsArray = rows.ToArray(); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Selectors/DiversitySelector.cs
r16623 r16892 100 100 #region Events 101 101 private void RegisterParameterEventHandlers() { 102 SelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged); 103 CopySelected.ValueChanged += new EventHandler(CopySelected_ValueChanged); 102 SelectorParameter.ValueChanged += SelectorParameter_ValueChanged; 103 CopySelectedParameter.ValueChanged += CopySelectedParameter_ValueChanged; 104 CopySelected.ValueChanged += CopySelected_ValueChanged; 105 } 106 107 private void CopySelectedParameter_ValueChanged(object sender, EventArgs e) { 108 if (CopySelected.Value != true) { 109 CopySelected.Value = true; 110 } 111 CopySelected.ValueChanged += CopySelected_ValueChanged; 104 112 } 105 113
Note: See TracChangeset
for help on using the changeset viewer.