Changeset 14814
- Timestamp:
- 03/31/17 15:47:55 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r14720 r14814 56 56 } 57 57 58 private string VariableName2Identifier(string name) { 59 // tries to convert a variable name to a valid C# identifier. 60 // the following code would work for all possible variable names 61 // return "_" + string.Join("_", Encoding.UTF8.GetBytes(name)); 62 58 private string VariableName2Identifier(string name) { 63 59 /* 64 60 * identifier-start-character: … … 100 96 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 101 97 // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals 102 if (node.Subtrees.Any()) {103 if (node.Symbol is Addition) {98 if (node.Subtrees.Any()) { 99 if (node.Symbol is Addition) { 104 100 FormatOperator(node, "+", strBuilder); 105 } else if (node.Symbol is And) {101 } else if (node.Symbol is And) { 106 102 FormatOperator(node, "&&", strBuilder); 107 } else if (node.Symbol is Average) {103 } else if (node.Symbol is Average) { 108 104 FormatFunction(node, "Average", strBuilder); 109 } else if (node.Symbol is Cosine) {105 } else if (node.Symbol is Cosine) { 110 106 FormatFunction(node, "Math.Cos", strBuilder); 111 } else if (node.Symbol is Division) {107 } else if (node.Symbol is Division) { 112 108 FormatDivision(node, strBuilder); 113 } else if (node.Symbol is Exponential) {109 } else if (node.Symbol is Exponential) { 114 110 FormatFunction(node, "Math.Exp", strBuilder); 115 } else if (node.Symbol is GreaterThan) {111 } else if (node.Symbol is GreaterThan) { 116 112 FormatOperator(node, ">", strBuilder); 117 } else if (node.Symbol is IfThenElse) {113 } else if (node.Symbol is IfThenElse) { 118 114 FormatFunction(node, "EvaluateIf", strBuilder); 119 } else if (node.Symbol is LessThan) {115 } else if (node.Symbol is LessThan) { 120 116 FormatOperator(node, "<", strBuilder); 121 } else if (node.Symbol is Logarithm) {117 } else if (node.Symbol is Logarithm) { 122 118 FormatFunction(node, "Math.Log", strBuilder); 123 } else if (node.Symbol is Multiplication) {119 } else if (node.Symbol is Multiplication) { 124 120 FormatOperator(node, "*", strBuilder); 125 } else if (node.Symbol is Not) {121 } else if (node.Symbol is Not) { 126 122 FormatOperator(node, "!", strBuilder); 127 } else if (node.Symbol is Or) {123 } else if (node.Symbol is Or) { 128 124 FormatOperator(node, "||", strBuilder); 129 } else if (node.Symbol is Xor) {125 } else if (node.Symbol is Xor) { 130 126 FormatOperator(node, "^", strBuilder); 131 } else if (node.Symbol is Sine) {127 } else if (node.Symbol is Sine) { 132 128 FormatFunction(node, "Math.Sin", strBuilder); 133 } else if (node.Symbol is Subtraction) {129 } else if (node.Symbol is Subtraction) { 134 130 FormatSubtraction(node, strBuilder); 135 } else if (node.Symbol is Tangent) {131 } else if (node.Symbol is Tangent) { 136 132 FormatFunction(node, "Math.Tan", strBuilder); 137 } else if (node.Symbol is Square) {133 } else if (node.Symbol is Square) { 138 134 FormatSquare(node, strBuilder); 139 } else if (node.Symbol is SquareRoot) {135 } else if (node.Symbol is SquareRoot) { 140 136 FormatFunction(node, "Math.Sqrt", strBuilder); 141 } else if (node.Symbol is Power) {137 } else if (node.Symbol is Power) { 142 138 FormatFunction(node, "Math.Pow", strBuilder); 143 } else if (node.Symbol is Root) {139 } else if (node.Symbol is Root) { 144 140 FormatRoot(node, strBuilder); 145 141 } else { … … 147 143 } 148 144 } else { 149 if (node is VariableTreeNode) {145 if (node is VariableTreeNode) { 150 146 var varNode = node as VariableTreeNode; 151 147 strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture)); 152 } else if (node is ConstantTreeNode) {148 } else if (node is ConstantTreeNode) { 153 149 var constNode = node as ConstantTreeNode; 154 150 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 155 } else if (node.Symbol is FactorVariable) {151 } else if (node.Symbol is FactorVariable) { 156 152 var factorNode = node as FactorVariableTreeNode; 157 153 FormatFactor(factorNode, strBuilder); 158 } else if (node.Symbol is BinaryFactorVariable) {154 } else if (node.Symbol is BinaryFactorVariable) { 159 155 var binFactorNode = node as BinaryFactorVariableTreeNode; 160 156 FormatBinaryFactor(binFactorNode, strBuilder); … … 189 185 190 186 private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 191 if (node.SubtreeCount == 1) {187 if (node.SubtreeCount == 1) { 192 188 strBuilder.Append("1.0 / "); 193 189 FormatRecursively(node.GetSubtree(0), strBuilder); … … 195 191 FormatRecursively(node.GetSubtree(0), strBuilder); 196 192 strBuilder.Append("/ ("); 197 for (int i = 1; i < node.SubtreeCount; i++) {198 if (i > 1) strBuilder.Append(" * ");193 for (int i = 1; i < node.SubtreeCount; i++) { 194 if (i > 1) strBuilder.Append(" * "); 199 195 FormatRecursively(node.GetSubtree(i), strBuilder); 200 196 } … … 204 200 205 201 private void FormatSubtraction(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 206 if (node.SubtreeCount == 1) {202 if (node.SubtreeCount == 1) { 207 203 strBuilder.Append("-"); 208 204 FormatRecursively(node.GetSubtree(0), strBuilder); … … 215 211 private void FormatOperator(ISymbolicExpressionTreeNode node, string symbol, StringBuilder strBuilder) { 216 212 strBuilder.Append("("); 217 foreach (var child in node.Subtrees) {213 foreach (var child in node.Subtrees) { 218 214 FormatRecursively(child, strBuilder); 219 if (child != node.Subtrees.Last())215 if (child != node.Subtrees.Last()) 220 216 strBuilder.Append(" " + symbol + " "); 221 217 } … … 225 221 private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) { 226 222 strBuilder.Append(function + "("); 227 foreach (var child in node.Subtrees) {223 foreach (var child in node.Subtrees) { 228 224 FormatRecursively(child, strBuilder); 229 if (child != node.Subtrees.Last())225 if (child != node.Subtrees.Last()) 230 226 strBuilder.Append(", "); 231 227 } … … 246 242 // here we don't have access to problemData to determine the type for each variable (double/string) therefore we must distinguish based on the symbol type 247 243 HashSet<string> doubleVarNames = new HashSet<string>(); 248 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {244 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) { 249 245 doubleVarNames.Add(((IVariableTreeNode)node).VariableName); 250 246 } 251 247 252 248 HashSet<string> stringVarNames = new HashSet<string>(); 253 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {249 foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) { 254 250 stringVarNames.Add(((IVariableTreeNode)node).VariableName); 255 251 } … … 258 254 strBuilder.Append(string.Join(", ", orderedNames)); 259 255 260 if (stringVarNames.Any() && doubleVarNames.Any())256 if (stringVarNames.Any() && doubleVarNames.Any()) 261 257 strBuilder.AppendLine(","); 262 258 orderedNames = doubleVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + VariableName2Identifier(n) + " /* " + n + " */");
Note: See TracChangeset
for help on using the changeset viewer.