Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/28/19 11:22:24 (6 years ago)
Author:
msemenki
Message:

#2988: Add first version of GP for Evolvment models of models.

Location:
branches/2988_ModelsOfModels2
Files:
1 added
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r16565 r16722  
    5151    }
    5252
    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") {
    5461      // skip root and start symbols
    5562      StringBuilder strBuilder = new StringBuilder();
    56       FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
     63      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString);
    5764      return strBuilder.ToString();
    5865    }
    5966
    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) {
    6172      if (node.SubtreeCount > 1) {
    6273        var token = GetToken(node.Symbol);
     
    6677            token == "^") {
    6778          strBuilder.Append("(");
    68           FormatRecursively(node.Subtrees.First(), strBuilder);
     79          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    6980
    7081          foreach (var subtree in node.Subtrees.Skip(1)) {
    7182            strBuilder.Append(" ").Append(token).Append(" ");
    72             FormatRecursively(subtree, strBuilder);
     83            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    7384          }
    7485          strBuilder.Append(")");
     
    7687          // function with multiple arguments
    7788          strBuilder.Append(token).Append("(");
    78           FormatRecursively(node.Subtrees.First(), strBuilder);
     89          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    7990          foreach (var subtree in node.Subtrees.Skip(1)) {
    8091            strBuilder.Append(", ");
    81             FormatRecursively(subtree, strBuilder);
     92            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    8293          }
    8394          strBuilder.Append(")");
     
    8798        if (token == "-" || token == "NOT") {
    8899          strBuilder.Append("(").Append(token).Append("(");
    89           FormatRecursively(node.GetSubtree(0), strBuilder);
     100          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    90101          strBuilder.Append("))");
    91102        } else if (token == "/") {
    92103          strBuilder.Append("1/");
    93           FormatRecursively(node.GetSubtree(0), strBuilder);
     104          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    94105        } else if (token == "+" || token == "*") {
    95           FormatRecursively(node.GetSubtree(0), strBuilder);
     106          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    96107        } else {
    97108          // function with only one argument
    98109          strBuilder.Append(token).Append("(");
    99           FormatRecursively(node.GetSubtree(0), strBuilder);
     110          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    100111          strBuilder.Append(")");
    101112        }
     
    106117          if (!varNode.Weight.IsAlmost(1.0)) {
    107118            strBuilder.Append("(");
    108             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     119            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    109120            strBuilder.Append("*");
    110121          }
     
    116127          }
    117128          strBuilder.Append(", ")
    118             .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag)
     129            .AppendFormat(numberFormat, "{0}", varNode.Lag)
    119130            .Append(")");
    120131        } else if (node.Symbol is Variable) {
     
    122133          if (!varNode.Weight.IsAlmost(1.0)) {
    123134            strBuilder.Append("(");
    124             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     135            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    125136            strBuilder.Append("*");
    126137          }
     
    141152          }
    142153          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))));
    144155        } else if (node.Symbol is BinaryFactorVariable) {
    145156          var factorNode = node as BinaryFactorVariableTreeNode;
    146157          if (!factorNode.Weight.IsAlmost(1.0)) {
    147158            strBuilder.Append("(");
    148             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);
     159            strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat));
    149160            strBuilder.Append("*");
    150161          }
     
    168179          var constNode = node as ConstantTreeNode;
    169180          if (constNode.Value >= 0.0)
    170             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
     181            strBuilder.Append(constNode.Value.ToString(formatString, numberFormat));
    171182          else
    172             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1
     183            strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1
    173184        }
    174185      }
    175186    }
    176187
    177     private string GetToken(ISymbol symbol) {
     188    private static string GetToken(ISymbol symbol) {
    178189      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
    179190      if (tok == null)
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r16565 r16722  
    2626using System.Text;
    2727using System.Text.RegularExpressions;
     28using HEAL.Attic;
    2829using HeuristicLab.Common;
    2930using HeuristicLab.Core;
    3031using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    5656    }
    5757
    58     private string VariableName2Identifier(string name) {     
     58    private string VariableName2Identifier(string name) {
    5959      /*
    6060       * identifier-start-character:
     
    131131        } else if (node.Symbol is Tangent) {
    132132          FormatFunction(node, "Math.Tan", strBuilder);
     133        } else if (node.Symbol is HyperbolicTangent) {
     134          FormatFunction(node, "Math.Tanh", strBuilder);
    133135        } else if (node.Symbol is Square) {
    134136          FormatSquare(node, strBuilder);
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r16565 r16722  
    2525using System.Linq;
    2626using System.Text;
     27using HEAL.Attic;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Core;
    2930using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    192192        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    193193        stringBuilder.Append(")");
    194 
     194      } else if (symbol is HyperbolicTangent) {
     195        stringBuilder.Append("TANH(");
     196        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     197        stringBuilder.Append(")");
    195198      } else if (symbol is Variable) {
    196199        VariableTreeNode variableTreeNode = node as VariableTreeNode;
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r16565 r16722  
    2424using System.Linq;
    2525using System.Text;
     26using HEAL.Attic;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    137137      } else if (node.Symbol is Tangent) {
    138138        strBuilder.Append(@"\tan \left( ");
     139      } else if (node.Symbol is HyperbolicTangent) {
     140        strBuilder.Append(@"\tanh \left( ");
    139141      } else if (node.Symbol is AiryA) {
    140142        strBuilder.Append(@"\operatorname{airy}_a \left( ");
     
    303305      } else if (node.Symbol is Tangent) {
    304306        throw new InvalidOperationException();
     307      } else if (node.Symbol is HyperbolicTangent) {
     308        throw new InvalidOperationException();
    305309      } else if (node.Symbol is AiryA) {
    306310        throw new InvalidOperationException();
     
    399403      } else if (node.Symbol is Tangent) {
    400404        strBuilder.Append(@" \right) ");
     405      } else if (node.Symbol is HyperbolicTangent) {
     406        strBuilder.Append(@" \right) ");
    401407      } else if (node.Symbol is AiryA) {
    402408        strBuilder.Append(@" \right) ");
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r16565 r16722  
    2323using System.Linq;
    2424using System.Text;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    28 using HEAL.Attic;
    2929
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    252252      } else if (symbol is Tangent) {
    253253        stringBuilder.Append("tan(");
     254        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     255        stringBuilder.Append(")");
     256      } else if (symbol is HyperbolicTangent) {
     257        stringBuilder.Append("tanh(");
    254258        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    255259        stringBuilder.Append(")");
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r16565 r16722  
    2424using System.Linq;
    2525using System.Text;
     26using HEAL.Attic;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    7070        } else if (node.Symbol is Tangent) {
    7171          FormatFunction(node, "Tan", strBuilder);
     72        } else if (node.Symbol is HyperbolicTangent) {
     73          FormatFunction(node, "Tanh", strBuilder);
    7274        } else if (node.Symbol is Exponential) {
    7375          FormatFunction(node, "Exp", strBuilder);
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r16565 r16722  
    160160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    161161        stringBuilder.Append(" tan");
     162      } else if (symbol is HyperbolicTangent) {
     163        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     164        stringBuilder.Append(" tanh");
    162165      } else if (symbol is Variable) {
    163166        VariableTreeNode variableTreeNode = node as VariableTreeNode;
Note: See TracChangeset for help on using the changeset viewer.