Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/21 11:50:57 (3 years ago)
Author:
gkronber
Message:

#3140: merged r18091:18131 from branch to trunk

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r17811 r18132  
    3333  public static class BaseInfixExpressionFormatter {
    3434    public static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder,
    35                                           NumberFormatInfo numberFormat, string formatString, List<KeyValuePair<string, double>> constants = null) {
     35                                          NumberFormatInfo numberFormat, string formatString, List<KeyValuePair<string, double>> parameters = null) {
    3636      if (node.SubtreeCount > 1) {
    3737        var token = GetToken(node.Symbol);
     
    4040            token == "*" || token == "/" || token == "AND") {
    4141          strBuilder.Append("(");
    42           FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);
     42          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters);
    4343
    4444          foreach (var subtree in node.Subtrees.Skip(1)) {
    4545            strBuilder.Append(" ").Append(token).Append(" ");
    46             FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);
     46            FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters);
    4747          }
    4848
     
    5151          // handle integer powers directly
    5252          strBuilder.Append("(");
    53           FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);
     53          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters);
    5454
    5555          var power = node.GetSubtree(1);
    56           if(power is ConstantTreeNode constNode && Math.Truncate(constNode.Value) == constNode.Value) {
    57             strBuilder.Append(" ").Append(token).Append(" ").Append(constNode.Value.ToString(formatString, numberFormat));
     56          if(power is INumericTreeNode numNode && Math.Truncate(numNode.Value) == numNode.Value) {
     57            strBuilder.Append(" ").Append(token).Append(" ").Append(numNode.Value.ToString(formatString, numberFormat));
    5858          } else {
    5959            strBuilder.Append(" ").Append(token).Append(" ");
    60             FormatRecursively(power, strBuilder, numberFormat, formatString, constants);
     60            FormatRecursively(power, strBuilder, numberFormat, formatString, parameters);
    6161          }
    6262
     
    6565          // function with multiple arguments
    6666          strBuilder.Append(token).Append("(");
    67           FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);
     67          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters);
    6868          foreach (var subtree in node.Subtrees.Skip(1)) {
    6969            strBuilder.Append(", ");
    70             FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);
     70            FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters);
    7171          }
    7272
     
    7777        if (token == "-" || token == "NOT") {
    7878          strBuilder.Append("(").Append(token).Append("(");
    79           FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
     79          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters);
    8080          strBuilder.Append("))");
    8181        } else if (token == "/") {
    8282          strBuilder.Append("1/");
    83           FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
     83          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters);
    8484        } else if (token == "+" || token == "*") {
    85           FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
     85          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters);
    8686        } else {
    8787          // function with only one argument
    8888          strBuilder.Append(token).Append("(");
    89           FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
     89          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters);
    9090          strBuilder.Append(")");
    9191        }
     
    9696          if (!varNode.Weight.IsAlmost(1.0)) {
    9797            strBuilder.Append("(");
    98             AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat);
     98            AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat);
    9999            strBuilder.Append("*");
    100100          }
     
    110110          if (!varNode.Weight.IsAlmost(1.0)) {
    111111            strBuilder.Append("(");
    112             AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat);
     112            AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat);
    113113            strBuilder.Append("*");
    114114          }
     
    124124          for (int i = 0; i < factorNode.Weights.Length; i++) {
    125125            if (i > 0) strBuilder.Append(", ");
    126             AppendConstant(strBuilder, constants, factorNode.Weights[i], formatString, numberFormat);
     126            AppendNumber(strBuilder, parameters, factorNode.Weights[i], formatString, numberFormat);
    127127          }
    128128          strBuilder.Append("]");
     
    131131          if (!factorNode.Weight.IsAlmost(1.0)) {
    132132            strBuilder.Append("(");
    133             AppendConstant(strBuilder, constants, factorNode.Weight, formatString, numberFormat);
     133            AppendNumber(strBuilder, parameters, factorNode.Weight, formatString, numberFormat);
    134134
    135135            strBuilder.Append("*");
     
    141141
    142142          if (!factorNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");
    143         } else if (node.Symbol is Constant) {
    144           var constNode = node as ConstantTreeNode;
    145           if (constants == null && constNode.Value < 0) {
    146             strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat))
    147                       .Append(")"); // (-1
     143        } else if (node is INumericTreeNode numNode) {
     144          if (parameters == null && numNode.Value < 0) {
     145            // negative value
     146            strBuilder.Append("(").Append(numNode.Value.ToString(formatString, numberFormat))
     147                      .Append(")");
    148148          } else {
    149             AppendConstant(strBuilder, constants, constNode.Value, formatString, numberFormat);
     149            AppendNumber(strBuilder, parameters, numNode.Value, formatString, numberFormat);
    150150          }
    151151        }
     
    153153    }
    154154
    155     private static void AppendConstant(StringBuilder strBuilder, List<KeyValuePair<string, double>> constants, double value, string formatString, NumberFormatInfo numberFormat) {
    156       if (constants != null) {
    157         string constantKey = $"c_{constants.Count}";
    158         strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constantKey);
    159         constants.Add(new KeyValuePair<string, double>(constantKey, value));
     155    private static void AppendNumber(StringBuilder strBuilder, List<KeyValuePair<string, double>> parameters, double value, string formatString, NumberFormatInfo numberFormat) {
     156      if (parameters != null) {
     157        string paramKey = $"c_{parameters.Count}";
     158        strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", paramKey);
     159        parameters.Add(new KeyValuePair<string, double>(paramKey, value));
    160160      } else {
    161161        strBuilder.Append(value.ToString(formatString, numberFormat));
     
    204204    /// </summary>
    205205    /// <param name="symbolicExpressionTree">The tree representation of the expression.</param>
    206     /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>
    207     /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
     206    /// <param name="numberFormat">Number format that should be used for parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>
     207    /// <param name="formatString">The format string for parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
    208208    /// <returns>Infix expression</returns>
    209209    public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat,
     
    241241    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
    242242      StringBuilder strBuilder = new StringBuilder();
    243       var constants = new List<KeyValuePair<string, double>>();
     243      var parameters = new List<KeyValuePair<string, double>>();
    244244      BaseInfixExpressionFormatter.FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0),
    245         strBuilder, NumberFormatInfo.InvariantInfo, "G", constants);
     245        strBuilder, NumberFormatInfo.InvariantInfo, "G", parameters);
    246246      strBuilder.Append($"{Environment.NewLine}{Environment.NewLine}");
    247247
    248       int maxDigits = GetDigits(constants.Count);
    249       int padding = constants.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length);
    250       foreach (var constant in constants) {
    251         int digits = GetDigits(Int32.Parse(constant.Key.Substring(2)));
    252         strBuilder.Append($"{constant.Key}{new String(' ', maxDigits - digits)} = " +
    253                           string.Format($"{{0,{padding}:F12}}", constant.Value, CultureInfo.InvariantCulture) +
     248      int maxDigits = GetDigits(parameters.Count);
     249      int padding = parameters.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length);
     250      foreach (var param in parameters) {
     251        int digits = GetDigits(int.Parse(param.Key.Substring(2)));
     252        strBuilder.Append($"{param.Key}{new string(' ', maxDigits - digits)} = " +
     253                          string.Format($"{{0,{padding}:F12}}", param.Value, CultureInfo.InvariantCulture) +
    254254                          Environment.NewLine);
    255255      }
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r17648 r18132  
    162162          var varNode = node as VariableTreeNode;
    163163          strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
    164         } else if (node is ConstantTreeNode) {
    165           var constNode = node as ConstantTreeNode;
    166           strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
     164        } else if (node is INumericTreeNode numNode) {
     165          strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture));
    167166        } else if (node.Symbol is FactorVariable) {
    168167          var factorNode = node as FactorVariableTreeNode;
     
    311310
    312311    private void GenerateFactorSource(StringBuilder strBuilder) {
    313       strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] constants) {");
     312      strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] parameters) {");
    314313      strBuilder.AppendLine("   for(int i=0;i<factorValues.Length;i++) " +
    315                             "      if(factorValues[i] == factorValue) return constants[i];" +
     314                            "      if(factorValues[i] == factorValue) return parameters[i];" +
    316315                            "   throw new ArgumentException();");
    317316      strBuilder.AppendLine("}");
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r17180 r18132  
    4747    private string GetExcelColumnName(int columnNumber) {
    4848      int dividend = columnNumber;
    49       string columnName = String.Empty;
     49      string columnName = string.Empty;
    5050
    5151      while (dividend > 0) {
    5252        int modulo = (dividend - 1) % 26;
    5353        columnName = System.Convert.ToChar(65 + modulo) + columnName;
    54         dividend = (int)((dividend - modulo) / 26);
     54        dividend = (dividend - modulo) / 26;
    5555      }
    5656
     
    134134        }
    135135        stringBuilder.Append("))");
    136       } else if (symbol is Constant) {
    137         ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    138         stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
     136      } else if (symbol is INumericSymbol numSy) {
     137        var numTreeNode = node as INumericTreeNode;
     138        stringBuilder.Append(numTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    139139      } else if (symbol is Cosine) {
    140140        stringBuilder.Append("COS(");
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r17826 r18132  
    3333  [StorableType("D7186DFF-1596-4A58-B27D-974DF0D93E4F")]
    3434  public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    35     private readonly List<KeyValuePair<string, double>> constants;
    36     private int constIndex;
     35    private readonly List<KeyValuePair<string, double>> parameters;
     36    private int paramIdx;
    3737    private int targetCount;
    3838    private int currentLag;
     
    4444    private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner)
    4545      : base(original, cloner) {
    46       constants = new List<KeyValuePair<string, double>>(original.constants);
    47       constIndex = original.constIndex;
     46      parameters = new List<KeyValuePair<string, double>>(original.parameters);
     47      paramIdx = original.paramIdx;
    4848      currentLag = original.currentLag;
    4949      targetCount = original.targetCount;
     
    5353      Name = ItemName;
    5454      Description = ItemDescription;
    55       constants = new List<KeyValuePair<string, double>>();
     55      parameters = new List<KeyValuePair<string, double>>();
    5656    }
    5757
     
    6666      try {
    6767        StringBuilder strBuilder = new StringBuilder();
    68         constants.Clear();
    69         constIndex = 0;
     68        parameters.Clear();
     69        paramIdx = 0;
    7070        this.targetVariable = targetVariable;
    7171        containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol));
     
    189189      } else if (node.Symbol is IfThenElse) {
    190190        strBuilder.Append(@" \operatorname{if}  \left( ");
    191       } else if (node.Symbol is Constant) {
    192         var constName = "c_{" + constIndex + "}";
    193         constIndex++;
    194         var constNode = node as ConstantTreeNode;
    195         if (constNode.Value.IsAlmost(1.0)) {
     191      } else if (node.Symbol is INumericSymbol numSy) {
     192        var numName = "c_{" + paramIdx + "}";
     193        paramIdx++;
     194        var numericNode = node as INumericTreeNode;
     195        if (numericNode.Value.IsAlmost(1.0)) {
    196196          strBuilder.Append("1 ");
    197197        } else {
    198           strBuilder.Append(constName);
    199           constants.Add(new KeyValuePair<string, double>(constName, constNode.Value));
    200         }
    201 
     198          strBuilder.Append(numName);
     199          parameters.Add(new KeyValuePair<string, double>(numName, numericNode.Value));
     200        }
    202201      } else if (node.Symbol is FactorVariable) {
    203202        var factorNode = node as FactorVariableTreeNode;
    204         var constName = "c_{" + constIndex + "}";
    205         strBuilder.Append(constName + " ");
     203        var paramName = "c_{" + paramIdx + "}";
     204        strBuilder.Append(paramName + " ");
    206205        foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName)
    207206          .Zip(factorNode.Weights, Tuple.Create)) {
    208           constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2));
    209         }
    210         constIndex++;
     207          parameters.Add(new KeyValuePair<string, double>("c_{" + paramIdx + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2));
     208        }
     209        paramIdx++;
    211210      } else if (node.Symbol is BinaryFactorVariable) {
    212211        var binFactorNode = node as BinaryFactorVariableTreeNode;
    213212        if (!binFactorNode.Weight.IsAlmost((1.0))) {
    214           var constName = "c_{" + constIndex + "}";
    215           strBuilder.Append(constName + "  \\cdot");
    216           constants.Add(new KeyValuePair<string, double>(constName, binFactorNode.Weight));
    217           constIndex++;
     213          var paramName = "c_{" + paramIdx + "}";
     214          strBuilder.Append(paramName + "  \\cdot");
     215          parameters.Add(new KeyValuePair<string, double>(paramName, binFactorNode.Weight));
     216          paramIdx++;
    218217        }
    219218        strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName));
     
    223222        var laggedVarNode = node as LaggedVariableTreeNode;
    224223        if (!laggedVarNode.Weight.IsAlmost(1.0)) {
    225           var constName = "c_{" + constIndex + "}";
    226           strBuilder.Append(constName + "  \\cdot");
    227           constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight));
    228           constIndex++;
     224          var paramName = "c_{" + paramIdx + "}";
     225          strBuilder.Append(paramName + "  \\cdot");
     226          parameters.Add(new KeyValuePair<string, double>(paramName, laggedVarNode.Weight));
     227          paramIdx++;
    229228        }
    230229        strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
     
    234233        var varNode = node as VariableTreeNode;
    235234        if (!varNode.Weight.IsAlmost((1.0))) {
    236           var constName = "c_{" + constIndex + "}";
    237           strBuilder.Append(constName + "  \\cdot");
    238           constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight));
    239           constIndex++;
     235          var paramName = "c_{" + paramIdx + "}";
     236          strBuilder.Append(paramName + "  \\cdot");
     237          parameters.Add(new KeyValuePair<string, double>(paramName, varNode.Weight));
     238          paramIdx++;
    240239        }
    241240        strBuilder.Append(EscapeLatexString(varNode.VariableName));
     
    271270      } else if (node.Symbol is VariableCondition) {
    272271        var conditionTreeNode = node as VariableConditionTreeNode;
    273         var constName = "c_{" + constants.Count + "}";
    274         string p = @"1 /  1 + \exp  - " + constName + " ";
    275         constants.Add(new KeyValuePair<string, double>(constName, conditionTreeNode.Slope));
    276         constIndex++;
    277         var const2Name = "c_{" + constants.Count + @"}";
     272        var paramName = "c_{" + parameters.Count + "}";
     273        string p = @"1 /  1 + \exp  - " + paramName + " ";
     274        parameters.Add(new KeyValuePair<string, double>(paramName, conditionTreeNode.Slope));
     275        paramIdx++;
     276        var const2Name = "c_{" + parameters.Count + @"}";
    278277        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + "   ";
    279         constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
    280         constIndex++;
     278        parameters.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
     279        paramIdx++;
    281280        strBuilder.Append(@" \left( " + p + @"\cdot ");
    282281      } else {
     
    379378      } else if (node.Symbol is VariableCondition) {
    380379        var conditionTreeNode = node as VariableConditionTreeNode;
    381         var const1Name = "c_{" + constants.Count + "}";
     380        var const1Name = "c_{" + parameters.Count + "}";
    382381        string p = @"1 / \left( 1 + \exp \left( - " + const1Name + " ";
    383         constants.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope));
    384         constIndex++;
    385         var const2Name = "c_{" + constants.Count + "}";
     382        parameters.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope));
     383        paramIdx++;
     384        var const2Name = "c_{" + parameters.Count + "}";
    386385        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " \right) \right) \right)   ";
    387         constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
    388         constIndex++;
     386        parameters.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
     387        paramIdx++;
    389388        strBuilder.Append(@" +  \left( 1 - " + p + @" \right) \cdot ");
    390389      } else {
     
    471470      } else if (node.Symbol is IfThenElse) {
    472471        strBuilder.Append(@" \right) ");
     472      } else if (node.Symbol is Number) {
    473473      } else if (node.Symbol is Constant) {
    474474      } else if (node.Symbol is LaggedVariable) {
     
    481481          .AppendLine("\\begin{align*}")
    482482          .AppendLine("\\nonumber");
    483         // output all constant values
    484         if (constants.Count > 0) {
    485           foreach (var constant in constants) {
     483        // output all parameter values
     484        if (parameters.Count > 0) {
     485          foreach (var param in parameters) {
    486486            // replace "." with ".&" to align decimal points
    487             var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);
    488             if (!constStr.Contains(".")) constStr = constStr + ".0";
    489             constStr = constStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
    490             strBuilder.Append(constant.Key + "& = & " + constStr);
     487            var paramStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", param.Value);
     488            if (!paramStr.Contains(".")) paramStr = paramStr + ".0";
     489            paramStr = paramStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
     490            strBuilder.Append(param.Key + "& = & " + paramStr);
    491491            strBuilder.Append(@"\\");
    492492          }
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r17180 r18132  
    151151        }
    152152        stringBuilder.Append(")");
    153       } else if (symbol is Constant) {
    154         ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    155         stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
     153      } else if (symbol is INumericSymbol) {
     154        var numberTreeNode = node as INumericTreeNode;
     155        stringBuilder.Append(numberTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    156156      } else if (symbol is Cosine) {
    157157        stringBuilder.Append("cos(");
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r17180 r18132  
    130130          var varNode = node as VariableTreeNode;
    131131          strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture));
    132         } else if (node.Symbol is Constant) {
    133           var constNode = node as ConstantTreeNode;
    134           strBuilder.Append(constNode.Value.ToString("G17", CultureInfo.InvariantCulture));
     132        } else if (node.Symbol is INumericSymbol) {
     133          var numNode = node as INumericTreeNode;
     134          strBuilder.Append(numNode.Value.ToString("G17", CultureInfo.InvariantCulture));
    135135        } else if (node.Symbol is FactorVariable) {
    136136          var factorNode = node as FactorVariableTreeNode;
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionPythonFormatter.cs

    r17929 r18132  
    202202      else if (node is VariableTreeNode)
    203203        FormatVariableTreeNode(node, strBuilder);
    204       else if (node is ConstantTreeNode)
    205         FormatConstantTreeNode(node, strBuilder);
     204      else if (node is INumericTreeNode)
     205        FormatNumericTreeNode(node, strBuilder);
    206206      else
    207207        throw new NotSupportedException("Formatting of symbol: " + symbol + " not supported for Python symbolic expression tree formatter.");
     
    227227    }
    228228
    229     private static void FormatConstantTreeNode(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    230       var constNode = node as ConstantTreeNode;
    231       strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
     229    private static void FormatNumericTreeNode(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     230      var symbol = node.Symbol;
     231      if (node is INumericTreeNode numNode) {
     232        strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture));
     233      } else {
     234        throw new NotSupportedException("Formatting of symbol: " + symbol + " not supported for Python symbolic expression tree formatter.");
     235      }
    232236    }
    233237
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r17180 r18132  
    8383        }
    8484        stringBuilder.Append(")");
    85       } else if (symbol is Constant) {
    86         ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    87         stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
     85      } else if (symbol is INumericSymbol) {
     86        var numberTreeNode = node as INumericTreeNode;
     87        stringBuilder.Append(numberTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    8888      } else if (symbol is Cosine) {
    8989        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/TSQLExpressionFormatter.cs

    r17180 r18132  
    9393      //Generate function header
    9494      strBuilder.Append("CREATE FUNCTION dbo.REGRESSIONMODEL(");
    95       strBuilder.Append(string.Join(", ", sortedVarcharIdentifiers.Select(n => string.Format("{0} NVARCHAR(max)",n))));
     95      strBuilder.Append(string.Join(", ", sortedVarcharIdentifiers.Select(n => string.Format("{0} NVARCHAR(max)", n))));
    9696      if (varcharVarNames.Any() && floatVarNames.Any())
    9797        strBuilder.Append(",");
    98       strBuilder.Append(string.Join(", ", sortedFloatIdentifiers.Select(n => string.Format("{0} FLOAT",n))));
     98      strBuilder.Append(string.Join(", ", sortedFloatIdentifiers.Select(n => string.Format("{0} FLOAT", n))));
    9999      strBuilder.AppendLine(")");
    100100
     
    164164          var varNode = node as VariableTreeNode;
    165165          strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
    166         } else if (node is ConstantTreeNode) {
    167           var constNode = node as ConstantTreeNode;
    168           strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
     166        } else if (node is INumericTreeNode numNode) {
     167          strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture));
    169168        } else if (node.Symbol is FactorVariable) {
    170169          var factorNode = node as FactorVariableTreeNode;
     
    181180    private void FormatIfThenElse(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    182181      strBuilder.Append("CASE ISNULL((SELECT 1 WHERE");
    183       FormatRecursively(level,node.GetSubtree(0), strBuilder);
     182      FormatRecursively(level, node.GetSubtree(0), strBuilder);
    184183      strBuilder.AppendLine("),0)");
    185       strBuilder.AppendIndented(level,"WHEN 1 THEN ");
     184      strBuilder.AppendIndented(level, "WHEN 1 THEN ");
    186185      FormatRecursively(level, node.GetSubtree(1), strBuilder);
    187186      strBuilder.AppendLine();
     
    203202
    204203    private string VariableName2Identifier(string variableName) {
    205       return "@"+variableName.Replace(' ', '_');
     204      return "@" + variableName.Replace(' ', '_');
    206205    }
    207206
Note: See TracChangeset for help on using the changeset viewer.