Changeset 7630
- Timestamp:
- 03/17/12 14:15:15 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r7259 r7630 67 67 currentLag = 0; 68 68 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(); 70 93 } 71 94 … … 75 98 76 99 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) 103 102 return FormatRecursively(node.GetSubtree(0)); 104 105 stringBuilder.Append("("); 106 107 if (symbol is Addition) { 103 else if (symbol is Addition) { 104 stringBuilder.Append("("); 108 105 for (int i = 0; i < node.SubtreeCount; i++) { 109 106 if (i > 0) stringBuilder.Append("+"); 110 107 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 111 108 } 109 stringBuilder.Append(")"); 112 110 } else if (symbol is And) { 113 111 stringBuilder.Append("(("); … … 118 116 stringBuilder.Append(")>0)"); 119 117 } 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. 121 120 } else if (symbol is Average) { 122 121 stringBuilder.Append("(1/"); … … 159 158 stringBuilder.Append(">"); 160 159 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. 162 162 } else if (symbol is IfThenElse) { 163 163 stringBuilder.Append("("); … … 175 175 stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 176 176 stringBuilder.Append("*"); 177 stringBuilder.Append(laggedVariableTreeNode.VariableName + LagToString(currentLag + laggedVariableTreeNode.Lag)); 177 stringBuilder.Append(laggedVariableTreeNode.VariableName + 178 LagToString(currentLag + laggedVariableTreeNode.Lag)); 178 179 } else if (symbol is LessThan) { 179 180 stringBuilder.Append("(("); … … 181 182 stringBuilder.Append("<"); 182 183 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. 184 186 } else if (symbol is Logarithm) { 185 187 stringBuilder.Append("log_("); … … 203 205 stringBuilder.Append(")>0)"); 204 206 } 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. 206 209 } else if (symbol is Sine) { 207 210 stringBuilder.Append("sin("); … … 262 265 string prevCounterVariable = CurrentIndexVariable; 263 266 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 "))"); 265 270 ReleaseIndexVariable(); 266 271 } else if (symbol is TimeLag) { … … 272 277 stringBuilder.Append("ERROR"); 273 278 } 274 275 stringBuilder.Append(")");276 279 return stringBuilder.ToString(); 277 280 } 278 281 279 282 280 private string LagToString(int lag) { 283 private string LagToString(int lag) 284 { 281 285 if (lag < 0) { 282 286 return "(" + CurrentIndexVariable + "" + lag + ")";
Note: See TracChangeset
for help on using the changeset viewer.