Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7630


Ignore:
Timestamp:
03/17/12 14:15:15 (12 years ago)
Author:
gkronber
Message:

#1801: worked on MATLAB formatter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r7259 r7630  
    6767      currentLag = 0;
    6868      currentIndexNumber = 0;
    69       return FormatRecursively(symbolicExpressionTree.Root);
     69
     70      var stringBuilder = new StringBuilder();
     71      stringBuilder.AppendLine("rows = ???");
     72      stringBuilder.AppendLine(FormatOnlyExpression(symbolicExpressionTree.Root) + ";");
     73      stringBuilder.AppendLine();
     74      stringBuilder.AppendLine("function y = log_(x)");
     75      stringBuilder.AppendLine("  if(x<=0) y = NaN;");
     76      stringBuilder.AppendLine("  else     y = log(x);");
     77      stringBuilder.AppendLine("  end");
     78      stringBuilder.AppendLine("end");
     79      stringBuilder.AppendLine();
     80      stringBuilder.AppendLine("function y = fivePoint(f0, f1, f3, f4)");
     81      stringBuilder.AppendLine("  y = (f0 + 2*f1 - 2*f3 - f4) / 8;");
     82      stringBuilder.AppendLine("end");
     83      return stringBuilder.ToString();
     84    }
     85
     86    public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode)
     87    {
     88      var stringBuilder = new StringBuilder();
     89      stringBuilder.AppendLine("  for " + CurrentIndexVariable + " = 1:1:rows");
     90      stringBuilder.AppendLine("    estimated(" + CurrentIndexVariable + ") = " + FormatRecursively(expressionNode.GetSubtree(0)) + ";");
     91      stringBuilder.AppendLine("  end;");
     92      return stringBuilder.ToString();
    7093    }
    7194
     
    7598
    7699      if (symbol is ProgramRootSymbol) {
    77         var variableNames = node.IterateNodesPostfix()
    78           .OfType<VariableTreeNode>()
    79           .Select(n => n.VariableName)
    80           .Distinct()
    81           .OrderBy(x => x);
    82         stringBuilder.AppendLine("function test_model");
    83         foreach (string variableName in variableNames)
    84           stringBuilder.AppendLine("  " + variableName + " = Data(:, ???);");
    85         stringBuilder.AppendLine("  for " + CurrentIndexVariable + " = size(Data,1):-1:1");
    86         stringBuilder.AppendLine("    Target_estimated(" + CurrentIndexVariable + ") = " + FormatRecursively(node.GetSubtree(0)) + ";");
    87         stringBuilder.AppendLine("  end");
    88         stringBuilder.AppendLine("end");
    89         stringBuilder.AppendLine();
    90         stringBuilder.AppendLine("function y = log_(x)");
    91         stringBuilder.AppendLine("  if(x<=0) y = NaN;");
    92         stringBuilder.AppendLine("  else     y = log(x);");
    93         stringBuilder.AppendLine("  end");
    94         stringBuilder.AppendLine("end");
    95         stringBuilder.AppendLine();
    96         stringBuilder.AppendLine("function y = fivePoint(f0, f1, f3, f4)");
    97         stringBuilder.AppendLine("  y = (f0 + 2*f1 - 2*f3 - f4) / 8;");
    98         stringBuilder.AppendLine("end");
    99         return stringBuilder.ToString();
    100       }
    101 
    102       if (symbol is StartSymbol)
     100        stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
     101      } else if (symbol is StartSymbol)
    103102        return FormatRecursively(node.GetSubtree(0));
    104 
    105       stringBuilder.Append("(");
    106 
    107       if (symbol is Addition) {
     103      else if (symbol is Addition) {
     104        stringBuilder.Append("(");
    108105        for (int i = 0; i < node.SubtreeCount; i++) {
    109106          if (i > 0) stringBuilder.Append("+");
    110107          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    111108        }
     109        stringBuilder.Append(")");
    112110      } else if (symbol is And) {
    113111        stringBuilder.Append("((");
     
    118116          stringBuilder.Append(")>0)");
    119117        }
    120         stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
     118        stringBuilder.Append(")-0.5)*2");
     119        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    121120      } else if (symbol is Average) {
    122121        stringBuilder.Append("(1/");
     
    159158        stringBuilder.Append(">");
    160159        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    161         stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
     160        stringBuilder.Append(")-0.5)*2");
     161        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    162162      } else if (symbol is IfThenElse) {
    163163        stringBuilder.Append("(");
     
    175175        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    176176        stringBuilder.Append("*");
    177         stringBuilder.Append(laggedVariableTreeNode.VariableName + LagToString(currentLag + laggedVariableTreeNode.Lag));
     177        stringBuilder.Append(laggedVariableTreeNode.VariableName +
     178                             LagToString(currentLag + laggedVariableTreeNode.Lag));
    178179      } else if (symbol is LessThan) {
    179180        stringBuilder.Append("((");
     
    181182        stringBuilder.Append("<");
    182183        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    183         stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
     184        stringBuilder.Append(")-0.5)*2");
     185        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    184186      } else if (symbol is Logarithm) {
    185187        stringBuilder.Append("log_(");
     
    203205          stringBuilder.Append(")>0)");
    204206        }
    205         stringBuilder.Append(")-0.5)*2"); // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
     207        stringBuilder.Append(")-0.5)*2");
     208        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    206209      } else if (symbol is Sine) {
    207210        stringBuilder.Append("sin(");
     
    262265        string prevCounterVariable = CurrentIndexVariable;
    263266        string counterVariable = AllocateIndexVariable();
    264         stringBuilder.AppendLine(" sum (map(@(" + counterVariable + ") " + FormatRecursively(node.GetSubtree(0)) + ", (" + prevCounterVariable + "+" + laggedNode.Lag + "):" + prevCounterVariable + "))");
     267        stringBuilder.AppendLine(" sum (map(@(" + counterVariable + ") " + FormatRecursively(node.GetSubtree(0)) +
     268                                 ", (" + prevCounterVariable + "+" + laggedNode.Lag + "):" + prevCounterVariable +
     269                                 "))");
    265270        ReleaseIndexVariable();
    266271      } else if (symbol is TimeLag) {
     
    272277        stringBuilder.Append("ERROR");
    273278      }
    274 
    275       stringBuilder.Append(")");
    276279      return stringBuilder.ToString();
    277280    }
    278281
    279282
    280     private string LagToString(int lag) {
     283    private string LagToString(int lag)
     284    {
    281285      if (lag < 0) {
    282286        return "(" + CurrentIndexVariable + "" + lag + ")";
Note: See TracChangeset for help on using the changeset viewer.