- Timestamp:
- 02/04/11 12:38:34 (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeMATLABFormatter.cs
r5188 r5431 31 31 using System.Collections.Generic; 32 32 using System; 33 using System.Globalization; 33 34 34 35 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters { 35 36 36 37 [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) { } 40 45 public SymbolicExpressionTreeMATLABFormatter() 41 46 : base() { 42 47 Name = "MATLAB String Formatter"; 43 48 } 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 } 44 66 45 67 public string Format(SymbolicExpressionTree symbolicExpressionTree) { 68 currentLag = 0; 69 currentIndexNumber = 0; 46 70 return FormatRecursively(symbolicExpressionTree.Root); 47 71 } 48 72 49 73 private string FormatRecursively(SymbolicExpressionTreeNode node) { 50 51 74 Symbol symbol = node.Symbol; 52 53 75 StringBuilder stringBuilder = new StringBuilder(); 54 76 … … 63 85 stringBuilder.AppendLine(" " + variableName + " = Data(:, ???);"); 64 86 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]) + ";"); 67 89 stringBuilder.AppendLine(); 68 90 stringBuilder.AppendLine("function y = log_(x)"); 69 91 stringBuilder.AppendLine(" if(x<=0) y = NaN;"); 70 92 stringBuilder.AppendLine(" else y = log(x);"); 93 stringBuilder.AppendLine( 94 @" 95 function y = fivePoint(f0, f1, f3, f4) 96 y = (f0 + 2*f1 - 2*f3 - f4) / 8"); 71 97 return stringBuilder.ToString(); 72 98 } … … 104 130 } else if (symbol is Constant) { 105 131 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 106 stringBuilder.Append(constantTreeNode.Value.ToString( ).Replace(",", "."));132 stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 107 133 } else if (symbol is Cosine) { 108 134 stringBuilder.Append("cos("); … … 143 169 stringBuilder.Append(FormatRecursively(node.SubTrees[2])); 144 170 } else if (symbol is LaggedVariable) { 171 // this if must be checked before if(symbol is LaggedVariable) 145 172 LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode; 146 stringBuilder.Append(laggedVariableTreeNode.Weight.ToString( ).Replace(",", "."));173 stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 147 174 stringBuilder.Append("*"); 148 stringBuilder.Append(laggedVariableTreeNode.VariableName + "(i-" + laggedVariableTreeNode.Lag + ")");175 stringBuilder.Append(laggedVariableTreeNode.VariableName + LagToString(currentLag + laggedVariableTreeNode.Lag)); 149 176 } else if (symbol is LessThan) { 150 177 stringBuilder.Append("(("); … … 195 222 } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 196 223 VariableTreeNode variableTreeNode = node as VariableTreeNode; 197 stringBuilder.Append(variableTreeNode.Weight.ToString( ).Replace(",", "."));224 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 198 225 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; 200 268 } else { 201 269 stringBuilder.Append("ERROR"); … … 203 271 204 272 stringBuilder.Append(")"); 205 206 273 return stringBuilder.ToString(); 207 274 } 208 275 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 212 285 } 213 286 }
Note: See TracChangeset
for help on using the changeset viewer.