# Changeset 16892 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic

Ignore:
Timestamp:
05/04/19 08:22:42 (13 months ago)
Message:

#2925 merged r16661:16890 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
19 edited

Unmodified
Added
Removed

• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

 r16662 if (branch.SubtreeCount >= 2) { var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone(); var fprime = Derive(f, variableName); var gprime = Derive(g, variableName); var fgPrime = Sum(Product(f, gprime), Product(fprime, g)); for (int i = 2; i < branch.SubtreeCount; i++) { for (int i = 1; i < branch.SubtreeCount; i++) { var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); var fg = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone()); var h = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); var hPrime = Derive(h, variableName); fgPrime = Sum(Product(fgPrime, h), Product(fg, hPrime)); var gPrime = Derive(g, variableName); var fgPrime = Sum(Product(fprime, g), Product(gPrime, f)); // prepare for next iteration f = fg; fprime = fgPrime; } return fgPrime; return fprime; } else // multiplication with only one argument has no effect -> derive the argument
• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs

 r16662 double @const = 0) { if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException(); if (factorCoefficients.Length == 0 && coefficients.Length == 0 && @const==0) throw new ArgumentException(); // Combine both trees ISymbolicExpressionTreeNode add = (new Addition()).CreateTreeNode(); // Create tree for double variables ISymbolicExpressionTree tree = null; if (coefficients.Length > 0) { tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const); if (factorCoefficients.Length == 0) return tree; var varTree = CreateTree(variableNames, new int[variableNames.Length], coefficients); foreach (var varNode in varTree.IterateNodesPrefix().OfType()) add.AddSubtree(varNode); } // Create tree for string variables ISymbolicExpressionTree factorTree = null; if (factorCoefficients.Length > 0) { factorTree = CreateTree(factors, factorCoefficients, @const); if (tree == null) return factorTree; var factorTree = CreateTree(factors, factorCoefficients); foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType()) add.AddSubtree(binFactorNode); } // Combine both trees ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0); foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType()) add.InsertSubtree(add.SubtreeCount - 1, binFactorNode); if (@const!=0.0) { ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); cNode.Value = @const; add.AddSubtree(cNode); } ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode()); ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode(); tree.Root.AddSubtree(startNode); startNode.AddSubtree(add); return tree; throw new ArgumentException(); }
• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

 r16662 } public string Format(ISymbolicExpressionTree symbolicExpressionTree) { /// /// Produces an infix expression for a given expression tree. /// /// The tree representation of the expression. /// Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)). /// The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\") /// Infix expression public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") { // skip root and start symbols StringBuilder strBuilder = new StringBuilder(); FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder); FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); return strBuilder.ToString(); } private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { public string Format(ISymbolicExpressionTree symbolicExpressionTree) { return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo); } private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) { if (node.SubtreeCount > 1) { var token = GetToken(node.Symbol); token == "^") { strBuilder.Append("("); FormatRecursively(node.Subtrees.First(), strBuilder); FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); foreach (var subtree in node.Subtrees.Skip(1)) { strBuilder.Append(" ").Append(token).Append(" "); FormatRecursively(subtree, strBuilder); FormatRecursively(subtree, strBuilder, numberFormat, formatString); } strBuilder.Append(")"); // function with multiple arguments strBuilder.Append(token).Append("("); FormatRecursively(node.Subtrees.First(), strBuilder); FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); foreach (var subtree in node.Subtrees.Skip(1)) { strBuilder.Append(", "); FormatRecursively(subtree, strBuilder); FormatRecursively(subtree, strBuilder, numberFormat, formatString); } strBuilder.Append(")"); if (token == "-" || token == "NOT") { strBuilder.Append("(").Append(token).Append("("); FormatRecursively(node.GetSubtree(0), strBuilder); FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); strBuilder.Append("))"); } else if (token == "/") { strBuilder.Append("1/"); FormatRecursively(node.GetSubtree(0), strBuilder); FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); } else if (token == "+" || token == "*") { FormatRecursively(node.GetSubtree(0), strBuilder); FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); } else { // function with only one argument strBuilder.Append(token).Append("("); FormatRecursively(node.GetSubtree(0), strBuilder); FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); strBuilder.Append(")"); } if (!varNode.Weight.IsAlmost(1.0)) { strBuilder.Append("("); strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); strBuilder.Append("*"); } } strBuilder.Append(", ") .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag) .AppendFormat(numberFormat, "{0}", varNode.Lag) .Append(")"); } else if (node.Symbol is Variable) { if (!varNode.Weight.IsAlmost(1.0)) { strBuilder.Append("("); strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); strBuilder.Append("*"); } } strBuilder.AppendFormat("[{0}]", string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture)))); string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); } else if (node.Symbol is BinaryFactorVariable) { var factorNode = node as BinaryFactorVariableTreeNode; if (!factorNode.Weight.IsAlmost(1.0)) { strBuilder.Append("("); strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight); strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); strBuilder.Append("*"); } var constNode = node as ConstantTreeNode; if (constNode.Value >= 0.0) strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value); strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); else strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 } } } private string GetToken(ISymbol symbol) { private static string GetToken(ISymbol symbol) { var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); if (tok == null)

• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

 r16662 private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }); private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) }); private static MethodInfo tanh = typeof(Math).GetMethod("Tanh", new Type[] { typeof(double) }); private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) }); private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) }); CompileInstructions(il, state, ds); il.Emit(System.Reflection.Emit.OpCodes.Call, tan); return; } case OpCodes.Tanh: { CompileInstructions(il, state, ds); il.Emit(System.Reflection.Emit.OpCodes.Call, tanh); return; }
• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs

 r16662 private IDataset dataset; private static readonly HashSet supportedOpCodes = new HashSet() { (byte)OpCode.Constant, (byte)OpCode.Variable, (byte)OpCode.Add, (byte)OpCode.Sub, (byte)OpCode.Mul, (byte)OpCode.Div, (byte)OpCode.Exp, (byte)OpCode.Log, (byte)OpCode.Sin, (byte)OpCode.Cos, (byte)OpCode.Tan, (byte)OpCode.Tanh, (byte)OpCode.Power, (byte)OpCode.Root, (byte)OpCode.SquareRoot, (byte)OpCode.Square, (byte)OpCode.CubeRoot, (byte)OpCode.Cube, (byte)OpCode.Absolute, (byte)OpCode.AnalyticQuotient }; public IEnumerable GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable rows) { if (!rows.Any()) return Enumerable.Empty(); } var code = Compile(tree, OpCodes.MapSymbolToOpCode); byte mapSupportedSymbols(ISymbolicExpressionTreeNode node) { var opCode = OpCodes.MapSymbolToOpCode(node); if (supportedOpCodes.Contains(opCode)) return opCode; else throw new NotSupportedException(\$"The native interpreter does not support {node.Symbol.Name}"); }; var code = Compile(tree, mapSupportedSymbols); var rowsArray = rows.ToArray();
• ## branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Selectors/DiversitySelector.cs

 r16623 #region Events private void RegisterParameterEventHandlers() { SelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged); CopySelected.ValueChanged += new EventHandler(CopySelected_ValueChanged); SelectorParameter.ValueChanged += SelectorParameter_ValueChanged; CopySelectedParameter.ValueChanged += CopySelectedParameter_ValueChanged; CopySelected.ValueChanged += CopySelected_ValueChanged; } private void CopySelectedParameter_ValueChanged(object sender, EventArgs e) { if (CopySelected.Value != true) { CopySelected.Value = true; } CopySelected.ValueChanged += CopySelected_ValueChanged; }
Note: See TracChangeset for help on using the changeset viewer.