Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/04/11 12:38:34 (14 years ago)
Author:
gkronber
Message:

#1314 Merged MATLAB formatter from branch into trunk and implemented additional symbols related to dynamic modeling.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/HeuristicLab.Problems.DataAnalysis-3.3.csproj

    r5428 r5431  
    109109  <ItemGroup>
    110110    <Compile Include="Symbolic\Formatters\SymbolicExpressionTreeLatexFormatter.cs" />
     111    <Compile Include="Symbolic\Formatters\SymbolicExpressionTreeMATLABFormatter.cs" />
    111112    <Compile Include="Symbolic\Symbols\Root.cs" />
    112113    <Compile Include="Symbolic\Symbols\Derivative.cs" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeMATLABFormatter.cs

    r5188 r5431  
    3131using System.Collections.Generic;
    3232using System;
     33using System.Globalization;
    3334
    3435namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
    3536
    3637  [Item("SymbolicExpressionTreeMATLABFormatter", "String formatter for string representations of symbolic expression trees in MATLAB syntax.")]
    37   public class SymbolicExpressionTreeMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    38 
    39     protected SymbolicExpressionTreeMATLABFormatter(SymbolicExpressionTreeMATLABFormatter original, Cloner cloner) : base(original, cloner) { }
     38  [StorableClass]
     39  public sealed class SymbolicExpressionTreeMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
     40    private int currentLag;
     41
     42    [StorableConstructor]
     43    private SymbolicExpressionTreeMATLABFormatter(bool deserializing) : base(deserializing) { }
     44    private SymbolicExpressionTreeMATLABFormatter(SymbolicExpressionTreeMATLABFormatter original, Cloner cloner) : base(original, cloner) { }
    4045    public SymbolicExpressionTreeMATLABFormatter()
    4146      : base() {
    4247      Name = "MATLAB String Formatter";
    4348    }
     49    public override IDeepCloneable Clone(Cloner cloner) {
     50      return new SymbolicExpressionTreeMATLABFormatter(this, cloner);
     51    }
     52    private int currentIndexNumber;
     53    public string CurrentIndexVariable {
     54      get {
     55        return "i" + currentIndexNumber;
     56      }
     57    }
     58    private void ReleaseIndexVariable() {
     59      currentIndexNumber--;
     60    }
     61
     62    private string AllocateIndexVariable() {
     63      currentIndexNumber++;
     64      return CurrentIndexVariable;
     65    }
    4466
    4567    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
     68      currentLag = 0;
     69      currentIndexNumber = 0;
    4670      return FormatRecursively(symbolicExpressionTree.Root);
    4771    }
    4872
    4973    private string FormatRecursively(SymbolicExpressionTreeNode node) {
    50 
    5174      Symbol symbol = node.Symbol;
    52 
    5375      StringBuilder stringBuilder = new StringBuilder();
    5476
     
    6385          stringBuilder.AppendLine("  " + variableName + " = Data(:, ???);");
    6486        stringBuilder.AppendLine();
    65         stringBuilder.AppendLine("  for i = size(Data,1):-1:1");
    66         stringBuilder.AppendLine("    Target_estimated(i) = " + FormatRecursively(node.SubTrees[0]) + ";");
     87        stringBuilder.AppendLine("  for "+CurrentIndexVariable+" = size(Data,1):-1:1");
     88        stringBuilder.AppendLine("    Target_estimated("+CurrentIndexVariable+") = " + FormatRecursively(node.SubTrees[0]) + ";");
    6789        stringBuilder.AppendLine();
    6890        stringBuilder.AppendLine("function y = log_(x)");
    6991        stringBuilder.AppendLine("  if(x<=0) y = NaN;");
    7092        stringBuilder.AppendLine("  else     y = log(x);");
     93        stringBuilder.AppendLine(
     94@"
     95function y = fivePoint(f0, f1, f3, f4)
     96  y = (f0 + 2*f1 - 2*f3 - f4) / 8");
    7197        return stringBuilder.ToString();
    7298      }
     
    104130      } else if (symbol is Constant) {
    105131        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    106         stringBuilder.Append(constantTreeNode.Value.ToString().Replace(",", "."));
     132        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    107133      } else if (symbol is Cosine) {
    108134        stringBuilder.Append("cos(");
     
    143169        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
    144170      } else if (symbol is LaggedVariable) {
     171        // this if must be checked before if(symbol is LaggedVariable)
    145172        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
    146         stringBuilder.Append(laggedVariableTreeNode.Weight.ToString().Replace(",", "."));
     173        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    147174        stringBuilder.Append("*");
    148         stringBuilder.Append(laggedVariableTreeNode.VariableName + "(i-" + laggedVariableTreeNode.Lag + ")");
     175        stringBuilder.Append(laggedVariableTreeNode.VariableName + LagToString(currentLag + laggedVariableTreeNode.Lag));
    149176      } else if (symbol is LessThan) {
    150177        stringBuilder.Append("((");
     
    195222      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
    196223        VariableTreeNode variableTreeNode = node as VariableTreeNode;
    197         stringBuilder.Append(variableTreeNode.Weight.ToString().Replace(",", "."));
     224        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    198225        stringBuilder.Append("*");
    199         stringBuilder.Append(variableTreeNode.VariableName + "(i)");
     226        stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag));
     227      } else if (symbol is Power) {
     228        stringBuilder.Append("(");
     229        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     230        stringBuilder.Append(")^round(");
     231        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
     232        stringBuilder.Append(")");
     233      } else if (symbol is Root) {
     234        stringBuilder.Append("(");
     235        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     236        stringBuilder.Append(")^(1 / round(");
     237        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
     238        stringBuilder.Append("))");
     239      } else if (symbol is Derivative) {
     240        stringBuilder.Append("fivePoint(");
     241        // f0
     242        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     243        stringBuilder.Append(", ");
     244        // f1
     245        currentLag--;
     246        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     247        stringBuilder.Append(", ");
     248        // f3
     249        currentLag -= 2;
     250        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     251        stringBuilder.Append(", ");
     252        currentLag--;
     253        // f4
     254        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     255        stringBuilder.Append(")");
     256        currentLag += 4;
     257      } else if (symbol is Integral) {
     258        var laggedNode = node as LaggedTreeNode;
     259        string prevCounterVariable = CurrentIndexVariable;
     260        string counterVariable = AllocateIndexVariable();
     261        stringBuilder.AppendLine(" sum (map(@(" + counterVariable + ") " + FormatRecursively(node.SubTrees[0]) + ", (" + prevCounterVariable + "+" + laggedNode.Lag + "):" + prevCounterVariable + "))");
     262        ReleaseIndexVariable();
     263      } else if (symbol is TimeLag) {
     264        var laggedNode = node as LaggedTreeNode;
     265        currentLag += laggedNode.Lag;
     266        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
     267        currentLag -= laggedNode.Lag;
    200268      } else {
    201269        stringBuilder.Append("ERROR");
     
    203271
    204272      stringBuilder.Append(")");
    205 
    206273      return stringBuilder.ToString();
    207274    }
    208275
    209     public override IDeepCloneable Clone(Cloner cloner) {
    210       return new SymbolicExpressionTreeMATLABFormatter(this, cloner);
    211     }
     276
     277    private string LagToString(int lag) {
     278      if (lag < 0) {
     279        return "(" + CurrentIndexVariable + "" + lag + ")";
     280      } else if (lag > 0) {
     281        return "(" + CurrentIndexVariable + "+" + lag + ")";
     282      } else return "(" + CurrentIndexVariable + ")";
     283    }
     284
    212285  }
    213286}
Note: See TracChangeset for help on using the changeset viewer.