Changeset 4868 for branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters
- Timestamp:
- 11/20/10 01:58:43 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs
r4861 r4868 46 46 47 47 private string FormatRecursively(SymbolicExpressionTreeNode node) { 48 49 Symbol symbol = node.Symbol; 50 51 if (symbol is ProgramRootSymbol || symbol is StartSymbol) 52 return FormatRecursively(node.SubTrees[0]); 53 48 54 StringBuilder stringBuilder = new StringBuilder(); 49 55 50 Symbol symbol = node.Symbol; 56 stringBuilder.Append("("); 57 51 58 if (symbol is Addition) { 52 59 for (int i = 0; i < node.SubTrees.Count; i++) { … … 57 64 } 58 65 } else if (symbol is And) { 66 stringBuilder.Append("("); 59 67 for (int i = 0; i < node.SubTrees.Count; i++) { 60 68 if (i > 0) stringBuilder.Append("&"); 61 stringBuilder.Append("( ");69 stringBuilder.Append("(("); 62 70 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 63 stringBuilder.Append(") ");71 stringBuilder.Append(")>=0)"); 64 72 } 73 stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]"); 65 74 } else if (symbol is Average) { 66 75 stringBuilder.Append("(1/"); … … 79 88 } else if (symbol is Cosine) { 80 89 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 81 stringBuilder.Append(" cos");90 stringBuilder.Append(" cos"); 82 91 } else if (symbol is Division) { 83 for (int i = 0; i < node.SubTrees.Count; i++) { 84 if (i > 0) stringBuilder.Append("/"); 85 stringBuilder.Append("("); 86 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 92 if (node.SubTrees.Count == 1) { 93 stringBuilder.Append("1/"); 94 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 95 } else { 96 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 97 stringBuilder.Append("/("); 98 for (int i = 1; i < node.SubTrees.Count; i++) { 99 if (i > 1) stringBuilder.Append("*"); 100 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 101 } 87 102 stringBuilder.Append(")"); 88 103 } 89 104 } else if (symbol is Exponential) { 90 stringBuilder.Append("not yet implemented"); 105 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 106 stringBuilder.Append(" exp"); 91 107 } else if (symbol is GreaterThan) { 92 108 stringBuilder.Append("("); 93 109 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 94 stringBuilder.Append(" >");110 stringBuilder.Append(" > "); 95 111 stringBuilder.Append(FormatRecursively(node.SubTrees[1])); 96 stringBuilder.Append(") ");112 stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]"); 97 113 } else if (symbol is IfThenElse) { 98 114 stringBuilder.Append("("); 99 115 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 100 stringBuilder.Append(" ifTrue: [");116 stringBuilder.Append(" > 0) ifTrue: ["); 101 117 stringBuilder.Append(FormatRecursively(node.SubTrees[1])); 102 118 stringBuilder.Append("] ifFalse: ["); … … 108 124 stringBuilder.Append("("); 109 125 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 110 stringBuilder.Append(" <");126 stringBuilder.Append(" < "); 111 127 stringBuilder.Append(FormatRecursively(node.SubTrees[1])); 112 stringBuilder.Append(") ");128 stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]"); 113 129 } else if (symbol is Logarithm) { 114 130 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 115 stringBuilder.Append("l og");131 stringBuilder.Append("ln"); 116 132 } else if (symbol is Multiplication) { 117 133 for (int i = 0; i < node.SubTrees.Count; i++) { 118 134 if (i > 0) stringBuilder.Append("*"); 119 stringBuilder.Append("(");120 135 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 121 stringBuilder.Append(")");122 136 } 123 137 } else if (symbol is Not) { 124 stringBuilder.Append(" !");138 stringBuilder.Append("-"); 125 139 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 126 140 } else if (symbol is Or) { 141 stringBuilder.Append("("); 127 142 for (int i = 0; i < node.SubTrees.Count; i++) { 128 143 if (i > 0) stringBuilder.Append("|"); 129 stringBuilder.Append("( ");144 stringBuilder.Append("(("); 130 145 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 131 stringBuilder.Append(") ");146 stringBuilder.Append(")>=0)"); 132 147 } 148 stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]"); 133 149 } else if (symbol is Sine) { 134 150 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 135 stringBuilder.Append(" sin");151 stringBuilder.Append(" sin"); 136 152 } else if (symbol is Subtraction) { 137 for (int i = 0; i < node.SubTrees.Count; i++) { 138 if (i > 0) stringBuilder.Append("-"); 139 stringBuilder.Append("("); 140 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 141 stringBuilder.Append(")"); 153 if (node.SubTrees.Count == 1) { 154 stringBuilder.Append("-"); 155 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 156 } else { 157 stringBuilder.Append(FormatRecursively(node.SubTrees[0])); 158 for (int i = 1; i < node.SubTrees.Count; i++) { 159 stringBuilder.Append("-"); 160 stringBuilder.Append(FormatRecursively(node.SubTrees[i])); 161 } 142 162 } 143 163 } else if (symbol is Tangent) { … … 145 165 stringBuilder.Append("tan"); 146 166 } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 147 stringBuilder.Append("not yet implemented"); 167 VariableTreeNode variableTreeNode = node as VariableTreeNode; 168 stringBuilder.Append(variableTreeNode.Weight); 169 stringBuilder.Append("*"); 170 stringBuilder.Append(variableTreeNode.VariableName); 148 171 } 149 172 … … 151 174 stringBuilder.Append("ERROR"); 152 175 } 176 177 stringBuilder.Append(")"); 153 178 154 179 return stringBuilder.ToString();
Note: See TracChangeset
for help on using the changeset viewer.