Changeset 14764 for branches/symbreg-factors-2650
- Timestamp:
- 03/18/17 14:22:46 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r14399 r14764 72 72 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 73 73 return strBuilder.ToString(); 74 } catch 74 } catch(NotImplementedException ex) { 75 75 return ex.Message + Environment.NewLine + ex.StackTrace; 76 76 } … … 85 85 FormatBegin(node, strBuilder); 86 86 87 if 87 if(node.SubtreeCount > 0) { 88 88 strBuilder.Append(FormatRecursively(node.GetSubtree(0))); 89 89 } 90 90 int i = 1; 91 foreach 91 foreach(var subTree in node.Subtrees.Skip(1)) { 92 92 FormatSep(node, strBuilder, i); 93 93 // format the whole subtree … … 102 102 103 103 private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 104 if 104 if(node.Symbol is Addition) { 105 105 strBuilder.Append(@" \left( "); 106 } else if 107 if 106 } else if(node.Symbol is Subtraction) { 107 if(node.SubtreeCount == 1) { 108 108 strBuilder.Append(@"- \left( "); 109 109 } else { 110 110 strBuilder.Append(@" \left( "); 111 111 } 112 } else if 113 } else if 114 if 112 } else if(node.Symbol is Multiplication) { 113 } else if(node.Symbol is Division) { 114 if(node.SubtreeCount == 1) { 115 115 strBuilder.Append(@" \cfrac{1}{"); 116 116 } else { 117 117 strBuilder.Append(@" \cfrac{ "); 118 118 } 119 } else if 119 } else if(node.Symbol is Average) { 120 120 // skip output of (1/1) if only one subtree 121 if 121 if(node.SubtreeCount > 1) { 122 122 strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}"); 123 123 } 124 124 strBuilder.Append(@" \left( "); 125 } else if 125 } else if(node.Symbol is Logarithm) { 126 126 strBuilder.Append(@"\log \left( "); 127 } else if 127 } else if(node.Symbol is Exponential) { 128 128 strBuilder.Append(@"\exp \left( "); 129 } else if 129 } else if(node.Symbol is Square) { 130 130 strBuilder.Append(@"\left("); 131 } else if 131 } else if(node.Symbol is SquareRoot) { 132 132 strBuilder.Append(@"\sqrt{"); 133 } else if 133 } else if(node.Symbol is Sine) { 134 134 strBuilder.Append(@"\sin \left( "); 135 } else if 135 } else if(node.Symbol is Cosine) { 136 136 strBuilder.Append(@"\cos \left( "); 137 } else if 137 } else if(node.Symbol is Tangent) { 138 138 strBuilder.Append(@"\tan \left( "); 139 } else if 139 } else if(node.Symbol is AiryA) { 140 140 strBuilder.Append(@"\operatorname{airy}_a \left( "); 141 } else if 141 } else if(node.Symbol is AiryB) { 142 142 strBuilder.Append(@"\operatorname{airy}_b \left( "); 143 } else if 143 } else if(node.Symbol is Bessel) { 144 144 strBuilder.Append(@"\operatorname{bessel}_1 \left( "); 145 } else if 145 } else if(node.Symbol is CosineIntegral) { 146 146 strBuilder.Append(@"\operatorname{cosInt} \left( "); 147 } else if 147 } else if(node.Symbol is Dawson) { 148 148 strBuilder.Append(@"\operatorname{dawson} \left( "); 149 } else if 149 } else if(node.Symbol is Erf) { 150 150 strBuilder.Append(@"\operatorname{erf} \left( "); 151 } else if 151 } else if(node.Symbol is ExponentialIntegralEi) { 152 152 strBuilder.Append(@"\operatorname{expInt}_i \left( "); 153 } else if 153 } else if(node.Symbol is FresnelCosineIntegral) { 154 154 strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( "); 155 } else if 155 } else if(node.Symbol is FresnelSineIntegral) { 156 156 strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( "); 157 } else if 157 } else if(node.Symbol is Gamma) { 158 158 strBuilder.Append(@"\Gamma \left( "); 159 } else if 159 } else if(node.Symbol is HyperbolicCosineIntegral) { 160 160 strBuilder.Append(@"\operatorname{hypCosInt} \left( "); 161 } else if 161 } else if(node.Symbol is HyperbolicSineIntegral) { 162 162 strBuilder.Append(@"\operatorname{hypSinInt} \left( "); 163 } else if 163 } else if(node.Symbol is Norm) { 164 164 strBuilder.Append(@"\operatorname{norm} \left( "); 165 } else if 165 } else if(node.Symbol is Psi) { 166 166 strBuilder.Append(@"\operatorname{digamma} \left( "); 167 } else if 167 } else if(node.Symbol is SineIntegral) { 168 168 strBuilder.Append(@"\operatorname{sinInt} \left( "); 169 } else if 169 } else if(node.Symbol is GreaterThan) { 170 170 strBuilder.Append(@" \left( "); 171 } else if 171 } else if(node.Symbol is LessThan) { 172 172 strBuilder.Append(@" \left( "); 173 } else if 173 } else if(node.Symbol is And) { 174 174 strBuilder.Append(@" \left( \left( "); 175 } else if 175 } else if(node.Symbol is Or) { 176 176 strBuilder.Append(@" \left( \left( "); 177 } else if 177 } else if(node.Symbol is Not) { 178 178 strBuilder.Append(@" \neg \left( "); 179 } else if 179 } else if(node.Symbol is IfThenElse) { 180 180 strBuilder.Append(@" \operatorname{if} \left( "); 181 } else if 181 } else if(node.Symbol is Constant) { 182 182 var constName = "c_{" + constIndex + "}"; 183 183 constIndex++; 184 184 var constNode = node as ConstantTreeNode; 185 if 185 if(constNode.Value.IsAlmost(1.0)) { 186 186 strBuilder.Append("1 "); 187 187 } else { … … 190 190 } 191 191 192 } else if 192 } else if(node.Symbol is FactorVariable) { 193 193 var factorNode = node as FactorVariableTreeNode; 194 194 var constName = "c_{" + constIndex + "}"; 195 195 strBuilder.Append(constName + " "); 196 foreach 196 foreach(var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName) 197 197 .Zip(factorNode.Weights, Tuple.Create)) { 198 198 constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2)); 199 199 } 200 200 constIndex++; 201 } else if 201 } else if(node.Symbol is BinaryFactorVariable) { 202 202 var binFactorNode = node as BinaryFactorVariableTreeNode; 203 if 203 if(!binFactorNode.Weight.IsAlmost((1.0))) { 204 204 var constName = "c_{" + constIndex + "}"; 205 205 strBuilder.Append(constName + " \\cdot"); … … 207 207 constIndex++; 208 208 } 209 strBuilder.Append( EscapeLatexString(binFactorNode.VariableName));209 strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName)); 210 210 strBuilder.Append(LagToString(currentLag)); 211 strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue) );212 } else if 211 strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue) + " )"); 212 } else if(node.Symbol is LaggedVariable) { 213 213 var laggedVarNode = node as LaggedVariableTreeNode; 214 if 214 if(!laggedVarNode.Weight.IsAlmost(1.0)) { 215 215 var constName = "c_{" + constIndex + "}"; 216 216 strBuilder.Append(constName + " \\cdot"); … … 221 221 strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag)); 222 222 223 } else if 223 } else if(node.Symbol is Variable) { 224 224 var varNode = node as VariableTreeNode; 225 if 225 if(!varNode.Weight.IsAlmost((1.0))) { 226 226 var constName = "c_{" + constIndex + "}"; 227 227 strBuilder.Append(constName + " \\cdot"); … … 231 231 strBuilder.Append(EscapeLatexString(varNode.VariableName)); 232 232 strBuilder.Append(LagToString(currentLag)); 233 } else if 233 } else if(node.Symbol is ProgramRootSymbol) { 234 234 strBuilder 235 235 .AppendLine("\\begin{align*}") 236 236 .AppendLine("\\nonumber"); 237 } else if 237 } else if(node.Symbol is Defun) { 238 238 var defunNode = node as DefunTreeNode; 239 239 strBuilder.Append(defunNode.FunctionName + " & = "); 240 } else if 240 } else if(node.Symbol is InvokeFunction) { 241 241 var invokeNode = node as InvokeFunctionTreeNode; 242 242 strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( "); 243 } else if 243 } else if(node.Symbol is StartSymbol) { 244 244 FormatStartSymbol(strBuilder); 245 } else if 245 } else if(node.Symbol is Argument) { 246 246 var argSym = node.Symbol as Argument; 247 247 strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " "); 248 } else if 248 } else if(node.Symbol is Derivative) { 249 249 strBuilder.Append(@" \cfrac{d \left( "); 250 } else if 250 } else if(node.Symbol is TimeLag) { 251 251 var laggedNode = node as ILaggedTreeNode; 252 252 currentLag += laggedNode.Lag; 253 } else if 253 } else if(node.Symbol is Power) { 254 254 strBuilder.Append(@" \left( "); 255 } else if 255 } else if(node.Symbol is Root) { 256 256 strBuilder.Append(@" \left( "); 257 } else if 257 } else if(node.Symbol is Integral) { 258 258 // actually a new variable for t is needed in all subtrees (TODO) 259 259 var laggedTreeNode = node as ILaggedTreeNode; 260 260 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( "); 261 } else if 261 } else if(node.Symbol is VariableCondition) { 262 262 var conditionTreeNode = node as VariableConditionTreeNode; 263 263 var constName = "c_{" + constants.Count + "}"; … … 276 276 277 277 private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) { 278 if 278 if(node.Symbol is Addition) { 279 279 strBuilder.Append(" + "); 280 } else if 280 } else if(node.Symbol is Subtraction) { 281 281 strBuilder.Append(" - "); 282 } else if 282 } else if(node.Symbol is Multiplication) { 283 283 strBuilder.Append(@" \cdot "); 284 } else if 285 if 284 } else if(node.Symbol is Division) { 285 if(step + 1 == node.SubtreeCount) 286 286 strBuilder.Append(@"}{"); 287 287 else 288 288 strBuilder.Append(@" }{ \cfrac{ "); 289 } else if 289 } else if(node.Symbol is Average) { 290 290 strBuilder.Append(@" + "); 291 } else if 292 throw new InvalidOperationException(); 293 } else if 294 throw new InvalidOperationException(); 295 } else if 296 throw new InvalidOperationException(); 297 } else if 298 throw new InvalidOperationException(); 299 } else if 300 throw new InvalidOperationException(); 301 } else if 302 throw new InvalidOperationException(); 303 } else if 304 throw new InvalidOperationException(); 305 } else if 306 throw new InvalidOperationException(); 307 } else if 308 throw new InvalidOperationException(); 309 } else if 310 throw new InvalidOperationException(); 311 } else if 312 throw new InvalidOperationException(); 313 } else if 314 throw new InvalidOperationException(); 315 } else if 316 throw new InvalidOperationException(); 317 } else if 318 throw new InvalidOperationException(); 319 } else if 320 throw new InvalidOperationException(); 321 } else if 322 throw new InvalidOperationException(); 323 } else if 324 throw new InvalidOperationException(); 325 } else if 326 throw new InvalidOperationException(); 327 } else if 328 throw new InvalidOperationException(); 329 } else if 330 throw new InvalidOperationException(); 331 } else if 332 throw new InvalidOperationException(); 333 } else if 334 throw new InvalidOperationException(); 335 } else if 291 } else if(node.Symbol is Logarithm) { 292 throw new InvalidOperationException(); 293 } else if(node.Symbol is Exponential) { 294 throw new InvalidOperationException(); 295 } else if(node.Symbol is Square) { 296 throw new InvalidOperationException(); 297 } else if(node.Symbol is SquareRoot) { 298 throw new InvalidOperationException(); 299 } else if(node.Symbol is Sine) { 300 throw new InvalidOperationException(); 301 } else if(node.Symbol is Cosine) { 302 throw new InvalidOperationException(); 303 } else if(node.Symbol is Tangent) { 304 throw new InvalidOperationException(); 305 } else if(node.Symbol is AiryA) { 306 throw new InvalidOperationException(); 307 } else if(node.Symbol is AiryB) { 308 throw new InvalidOperationException(); 309 } else if(node.Symbol is Bessel) { 310 throw new InvalidOperationException(); 311 } else if(node.Symbol is CosineIntegral) { 312 throw new InvalidOperationException(); 313 } else if(node.Symbol is Dawson) { 314 throw new InvalidOperationException(); 315 } else if(node.Symbol is Erf) { 316 throw new InvalidOperationException(); 317 } else if(node.Symbol is ExponentialIntegralEi) { 318 throw new InvalidOperationException(); 319 } else if(node.Symbol is FresnelCosineIntegral) { 320 throw new InvalidOperationException(); 321 } else if(node.Symbol is FresnelSineIntegral) { 322 throw new InvalidOperationException(); 323 } else if(node.Symbol is Gamma) { 324 throw new InvalidOperationException(); 325 } else if(node.Symbol is HyperbolicCosineIntegral) { 326 throw new InvalidOperationException(); 327 } else if(node.Symbol is HyperbolicSineIntegral) { 328 throw new InvalidOperationException(); 329 } else if(node.Symbol is Norm) { 330 throw new InvalidOperationException(); 331 } else if(node.Symbol is Psi) { 332 throw new InvalidOperationException(); 333 } else if(node.Symbol is SineIntegral) { 334 throw new InvalidOperationException(); 335 } else if(node.Symbol is GreaterThan) { 336 336 strBuilder.Append(@" > "); 337 } else if 337 } else if(node.Symbol is LessThan) { 338 338 strBuilder.Append(@" < "); 339 } else if 339 } else if(node.Symbol is And) { 340 340 strBuilder.Append(@" > 0 \right) \land \left("); 341 } else if 341 } else if(node.Symbol is Or) { 342 342 strBuilder.Append(@" > 0 \right) \lor \left("); 343 } else if 344 throw new InvalidOperationException(); 345 } else if 343 } else if(node.Symbol is Not) { 344 throw new InvalidOperationException(); 345 } else if(node.Symbol is IfThenElse) { 346 346 strBuilder.Append(@" , "); 347 } else if 347 } else if(node.Symbol is ProgramRootSymbol) { 348 348 strBuilder.Append(@"\\" + Environment.NewLine); 349 } else if 350 } else if 349 } else if(node.Symbol is Defun) { 350 } else if(node.Symbol is InvokeFunction) { 351 351 strBuilder.Append(" , "); 352 } else if 352 } else if(node.Symbol is StartSymbol) { 353 353 strBuilder.Append(@"\\" + Environment.NewLine); 354 354 FormatStartSymbol(strBuilder); 355 } else if 355 } else if(node.Symbol is Power) { 356 356 strBuilder.Append(@"\right) ^ { \operatorname{round} \left("); 357 } else if 357 } else if(node.Symbol is Root) { 358 358 strBuilder.Append(@"\right) ^ { \cfrac{1}{ \operatorname{round} \left("); 359 } else if 359 } else if(node.Symbol is VariableCondition) { 360 360 var conditionTreeNode = node as VariableConditionTreeNode; 361 361 var const1Name = "c_{" + constants.Count + "}"; … … 374 374 375 375 private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 376 if 377 strBuilder.Append(@" \right) "); 378 } else if 379 strBuilder.Append(@" \right) "); 380 } else if 381 } else if 376 if(node.Symbol is Addition) { 377 strBuilder.Append(@" \right) "); 378 } else if(node.Symbol is Subtraction) { 379 strBuilder.Append(@" \right) "); 380 } else if(node.Symbol is Multiplication) { 381 } else if(node.Symbol is Division) { 382 382 strBuilder.Append(" } "); 383 for 383 for(int i = 2; i < node.SubtreeCount; i++) 384 384 strBuilder.Append(" } "); 385 } else if 386 strBuilder.Append(@" \right) "); 387 } else if 388 strBuilder.Append(@" \right) "); 389 } else if 390 strBuilder.Append(@" \right) "); 391 } else if 385 } else if(node.Symbol is Average) { 386 strBuilder.Append(@" \right) "); 387 } else if(node.Symbol is Logarithm) { 388 strBuilder.Append(@" \right) "); 389 } else if(node.Symbol is Exponential) { 390 strBuilder.Append(@" \right) "); 391 } else if(node.Symbol is Square) { 392 392 strBuilder.Append(@"\right)^2"); 393 } else if 393 } else if(node.Symbol is SquareRoot) { 394 394 strBuilder.Append(@"}"); 395 } else if 396 strBuilder.Append(@" \right) "); 397 } else if 398 strBuilder.Append(@" \right) "); 399 } else if 400 strBuilder.Append(@" \right) "); 401 } else if 402 strBuilder.Append(@" \right) "); 403 } else if 404 strBuilder.Append(@" \right) "); 405 } else if 406 strBuilder.Append(@" \right) "); 407 } else if 408 strBuilder.Append(@" \right) "); 409 } else if 410 strBuilder.Append(@" \right) "); 411 } else if 412 strBuilder.Append(@" \right) "); 413 } else if 414 strBuilder.Append(@" \right) "); 415 } else if 416 strBuilder.Append(@" \right) "); 417 } else if 418 strBuilder.Append(@" \right) "); 419 } else if 420 strBuilder.Append(@" \right) "); 421 } else if 422 strBuilder.Append(@" \right) "); 423 } else if 424 strBuilder.Append(@" \right) "); 425 } else if 426 strBuilder.Append(@" \right) "); 427 } else if 428 strBuilder.Append(@" \right) "); 429 } else if 430 strBuilder.Append(@" \right) "); 431 } else if 432 strBuilder.Append(@" \right) "); 433 } else if 434 strBuilder.Append(@" \right) "); 435 } else if 395 } else if(node.Symbol is Sine) { 396 strBuilder.Append(@" \right) "); 397 } else if(node.Symbol is Cosine) { 398 strBuilder.Append(@" \right) "); 399 } else if(node.Symbol is Tangent) { 400 strBuilder.Append(@" \right) "); 401 } else if(node.Symbol is AiryA) { 402 strBuilder.Append(@" \right) "); 403 } else if(node.Symbol is AiryB) { 404 strBuilder.Append(@" \right) "); 405 } else if(node.Symbol is Bessel) { 406 strBuilder.Append(@" \right) "); 407 } else if(node.Symbol is CosineIntegral) { 408 strBuilder.Append(@" \right) "); 409 } else if(node.Symbol is Dawson) { 410 strBuilder.Append(@" \right) "); 411 } else if(node.Symbol is Erf) { 412 strBuilder.Append(@" \right) "); 413 } else if(node.Symbol is ExponentialIntegralEi) { 414 strBuilder.Append(@" \right) "); 415 } else if(node.Symbol is FresnelCosineIntegral) { 416 strBuilder.Append(@" \right) "); 417 } else if(node.Symbol is FresnelSineIntegral) { 418 strBuilder.Append(@" \right) "); 419 } else if(node.Symbol is Gamma) { 420 strBuilder.Append(@" \right) "); 421 } else if(node.Symbol is HyperbolicCosineIntegral) { 422 strBuilder.Append(@" \right) "); 423 } else if(node.Symbol is HyperbolicSineIntegral) { 424 strBuilder.Append(@" \right) "); 425 } else if(node.Symbol is Norm) { 426 strBuilder.Append(@" \right) "); 427 } else if(node.Symbol is Psi) { 428 strBuilder.Append(@" \right) "); 429 } else if(node.Symbol is SineIntegral) { 430 strBuilder.Append(@" \right) "); 431 } else if(node.Symbol is GreaterThan) { 432 strBuilder.Append(@" \right) "); 433 } else if(node.Symbol is LessThan) { 434 strBuilder.Append(@" \right) "); 435 } else if(node.Symbol is And) { 436 436 strBuilder.Append(@" > 0 \right) \right) "); 437 } else if 437 } else if(node.Symbol is Or) { 438 438 strBuilder.Append(@" > 0 \right) \right) "); 439 } else if 440 strBuilder.Append(@" \right) "); 441 } else if 442 strBuilder.Append(@" \right) "); 443 } else if 444 } else if 445 } else if 446 } else if 447 } else if 448 } else if 439 } else if(node.Symbol is Not) { 440 strBuilder.Append(@" \right) "); 441 } else if(node.Symbol is IfThenElse) { 442 strBuilder.Append(@" \right) "); 443 } else if(node.Symbol is Constant) { 444 } else if(node.Symbol is LaggedVariable) { 445 } else if(node.Symbol is Variable) { 446 } else if(node.Symbol is FactorVariable) { 447 } else if(node.Symbol is BinaryFactorVariable) { 448 } else if(node.Symbol is ProgramRootSymbol) { 449 449 strBuilder 450 450 .AppendLine("\\end{align*}") … … 452 452 .AppendLine("\\nonumber"); 453 453 // output all constant values 454 if 455 foreach 454 if(constants.Count > 0) { 455 foreach(var constant in constants) { 456 456 // replace "." with ".&" to align decimal points 457 457 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value); 458 if 458 if(!constStr.Contains(".")) constStr = constStr + ".0"; 459 459 constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions 460 460 strBuilder.Append(constant.Key + "& = & " + constStr); … … 463 463 } 464 464 strBuilder.AppendLine("\\end{align*}"); 465 } else if 466 } else if 467 strBuilder.Append(@" \right) "); 468 } else if 469 } else if 470 } else if 465 } else if(node.Symbol is Defun) { 466 } else if(node.Symbol is InvokeFunction) { 467 strBuilder.Append(@" \right) "); 468 } else if(node.Symbol is StartSymbol) { 469 } else if(node.Symbol is Argument) { 470 } else if(node.Symbol is Derivative) { 471 471 strBuilder.Append(@" \right) }{dt} "); 472 } else if 472 } else if(node.Symbol is TimeLag) { 473 473 var laggedNode = node as ILaggedTreeNode; 474 474 currentLag -= laggedNode.Lag; 475 } else if 475 } else if(node.Symbol is Power) { 476 476 strBuilder.Append(@" \right) } "); 477 } else if 477 } else if(node.Symbol is Root) { 478 478 strBuilder.Append(@" \right) } } "); 479 } else if 480 strBuilder.Append(@" \right) "); 481 } else if 479 } else if(node.Symbol is Integral) { 480 strBuilder.Append(@" \right) "); 481 } else if(node.Symbol is VariableCondition) { 482 482 strBuilder.Append(@"\right) "); 483 483 } else { … … 488 488 private void FormatStartSymbol(StringBuilder strBuilder) { 489 489 strBuilder.Append(targetVariable ?? "target_" + (targetCount++)); 490 if 490 if(containsTimeSeriesSymbol) 491 491 strBuilder.Append("(t)"); 492 492 strBuilder.Append(" & = "); … … 494 494 495 495 private string LagToString(int lag) { 496 if 496 if(lag < 0) { 497 497 return "(t" + lag + ")"; 498 } else if 498 } else if(lag > 0) { 499 499 return "(t+" + lag + ")"; 500 500 } else return ""; -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r14185 r14764 21 21 22 22 using System.Globalization; 23 using System.Linq; 23 24 using System.Text; 24 25 using HeuristicLab.Common; … … 77 78 stringBuilder.AppendLine(" y = (f0 + 2*f1 - 2*f3 - f4) / 8;"); 78 79 stringBuilder.AppendLine("end"); 80 81 var factorVariableNames = 82 symbolicExpressionTree.IterateNodesPostfix() 83 .OfType<FactorVariableTreeNode>() 84 .Select(n => n.VariableName) 85 .Distinct(); 86 87 foreach(var factorVarName in factorVariableNames) 88 { 89 var factorSymb = symbolicExpressionTree.IterateNodesPostfix() 90 .OfType<FactorVariableTreeNode>() 91 .First(n => n.VariableName == factorVarName) 92 .Symbol; 93 stringBuilder.AppendFormat("function y = switch_{0}(val, v)", factorVarName).AppendLine(); 94 var values = factorSymb.GetVariableValues(factorVarName).ToArray(); 95 stringBuilder.AppendLine("switch val"); 96 for(int i = 0; i < values.Length; i++) { 97 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, " case \"{0}\" y = v({1})", values[i], i).AppendLine(); 98 } 99 stringBuilder.AppendLine("end"); 100 stringBuilder.AppendLine(); 101 } 102 79 103 return stringBuilder.ToString(); 80 104 } … … 92 116 StringBuilder stringBuilder = new StringBuilder(); 93 117 94 if 118 if(symbol is ProgramRootSymbol) { 95 119 stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0))); 96 } else if 120 } else if(symbol is StartSymbol) 97 121 return FormatRecursively(node.GetSubtree(0)); 98 else if 99 stringBuilder.Append("("); 100 for 101 if 102 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 103 } 104 stringBuilder.Append(")"); 105 } else if 122 else if(symbol is Addition) { 123 stringBuilder.Append("("); 124 for(int i = 0; i < node.SubtreeCount; i++) { 125 if(i > 0) stringBuilder.Append("+"); 126 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 127 } 128 stringBuilder.Append(")"); 129 } else if(symbol is And) { 106 130 stringBuilder.Append("(("); 107 for 108 if 131 for(int i = 0; i < node.SubtreeCount; i++) { 132 if(i > 0) stringBuilder.Append("&"); 109 133 stringBuilder.Append("(("); 110 134 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 113 137 stringBuilder.Append(")-0.5)*2"); 114 138 // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp. 115 } else if 139 } else if(symbol is Average) { 116 140 stringBuilder.Append("(1/"); 117 141 stringBuilder.Append(node.SubtreeCount); 118 142 stringBuilder.Append(")*("); 119 for 120 if 143 for(int i = 0; i < node.SubtreeCount; i++) { 144 if(i > 0) stringBuilder.Append("+"); 121 145 stringBuilder.Append("("); 122 146 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 124 148 } 125 149 stringBuilder.Append(")"); 126 } else if 150 } else if(symbol is Constant) { 127 151 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 128 152 stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 129 } else if 153 } else if(symbol is Cosine) { 130 154 stringBuilder.Append("cos("); 131 155 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 132 156 stringBuilder.Append(")"); 133 } else if 134 if 157 } else if(symbol is Division) { 158 if(node.SubtreeCount == 1) { 135 159 stringBuilder.Append("1/"); 136 160 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 138 162 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 139 163 stringBuilder.Append("/("); 140 for 141 if 164 for(int i = 1; i < node.SubtreeCount; i++) { 165 if(i > 1) stringBuilder.Append("*"); 142 166 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 143 167 } 144 168 stringBuilder.Append(")"); 145 169 } 146 } else if 170 } else if(symbol is Exponential) { 147 171 stringBuilder.Append("exp("); 148 172 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 149 173 stringBuilder.Append(")"); 150 } else if 174 } else if(symbol is Square) { 151 175 stringBuilder.Append("("); 152 176 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 153 177 stringBuilder.Append(").^2"); 154 } else if 178 } else if(symbol is SquareRoot) { 155 179 stringBuilder.Append("sqrt("); 156 180 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 157 181 stringBuilder.Append(")"); 158 } else if 182 } else if(symbol is GreaterThan) { 159 183 stringBuilder.Append("(("); 160 184 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 163 187 stringBuilder.Append(")-0.5)*2"); 164 188 // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp. 165 } else if 189 } else if(symbol is IfThenElse) { 166 190 stringBuilder.Append("("); 167 191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 173 197 stringBuilder.Append("<=0)*"); 174 198 stringBuilder.Append(FormatRecursively(node.GetSubtree(2))); 175 } else if 199 } else if(symbol is LaggedVariable) { 176 200 // this if must be checked before if(symbol is LaggedVariable) 177 201 LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode; … … 180 204 stringBuilder.Append(laggedVariableTreeNode.VariableName + 181 205 LagToString(currentLag + laggedVariableTreeNode.Lag)); 182 } else if 206 } else if(symbol is LessThan) { 183 207 stringBuilder.Append("(("); 184 208 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 187 211 stringBuilder.Append(")-0.5)*2"); 188 212 // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp. 189 } else if 213 } else if(symbol is Logarithm) { 190 214 stringBuilder.Append("log_("); 191 215 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 216 stringBuilder.Append(")"); 193 } else if 194 for 195 if 196 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 197 } 198 } else if 217 } else if(symbol is Multiplication) { 218 for(int i = 0; i < node.SubtreeCount; i++) { 219 if(i > 0) stringBuilder.Append("*"); 220 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 221 } 222 } else if(symbol is Not) { 199 223 stringBuilder.Append("~("); 200 224 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 201 225 stringBuilder.Append(" > 0 )"); 202 } else if 226 } else if(symbol is Or) { 203 227 stringBuilder.Append("(("); 204 for 205 if 228 for(int i = 0; i < node.SubtreeCount; i++) { 229 if(i > 0) stringBuilder.Append("|"); 206 230 stringBuilder.Append("(("); 207 231 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 210 234 stringBuilder.Append(")-0.5)*2"); 211 235 // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp. 212 } else if 236 } else if(symbol is Sine) { 213 237 stringBuilder.Append("sin("); 214 238 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 215 239 stringBuilder.Append(")"); 216 } else if 217 stringBuilder.Append("("); 218 if 240 } else if(symbol is Subtraction) { 241 stringBuilder.Append("("); 242 if(node.SubtreeCount == 1) { 219 243 stringBuilder.Append("-"); 220 244 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 221 245 } else { 222 246 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 223 for 247 for(int i = 1; i < node.SubtreeCount; i++) { 224 248 stringBuilder.Append("-"); 225 249 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 227 251 } 228 252 stringBuilder.Append(")"); 229 } else if 253 } else if(symbol is Tangent) { 230 254 stringBuilder.Append("tan("); 231 255 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 232 256 stringBuilder.Append(")"); 233 } else if 257 } else if(node.Symbol is AiryA) { 234 258 stringBuilder.Append("airy("); 235 259 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 236 260 stringBuilder.Append(")"); 237 } else if 261 } else if(node.Symbol is AiryB) { 238 262 stringBuilder.Append("airy(2, "); 239 263 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 240 264 stringBuilder.Append(")"); 241 } else if 265 } else if(node.Symbol is Bessel) { 242 266 stringBuilder.Append("besseli(0.0,"); 243 267 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 244 268 stringBuilder.Append(")"); 245 } else if 269 } else if(node.Symbol is CosineIntegral) { 246 270 stringBuilder.Append("cosint("); 247 271 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 248 272 stringBuilder.Append(")"); 249 } else if 273 } else if(node.Symbol is Dawson) { 250 274 stringBuilder.Append("dawson("); 251 275 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 252 276 stringBuilder.Append(")"); 253 } else if 277 } else if(node.Symbol is Erf) { 254 278 stringBuilder.Append("erf("); 255 279 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 256 280 stringBuilder.Append(")"); 257 } else if 281 } else if(node.Symbol is ExponentialIntegralEi) { 258 282 stringBuilder.Append("expint("); 259 283 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 260 284 stringBuilder.Append(")"); 261 } else if 285 } else if(node.Symbol is FresnelCosineIntegral) { 262 286 stringBuilder.Append("FresnelC("); 263 287 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 264 288 stringBuilder.Append(")"); 265 } else if 289 } else if(node.Symbol is FresnelSineIntegral) { 266 290 stringBuilder.Append("FresnelS("); 267 291 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 268 292 stringBuilder.Append(")"); 269 } else if 293 } else if(node.Symbol is Gamma) { 270 294 stringBuilder.Append("gamma("); 271 295 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 272 296 stringBuilder.Append(")"); 273 } else if 297 } else if(node.Symbol is HyperbolicCosineIntegral) { 274 298 stringBuilder.Append("Chi("); 275 299 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 276 300 stringBuilder.Append(")"); 277 } else if 301 } else if(node.Symbol is HyperbolicSineIntegral) { 278 302 stringBuilder.Append("Shi("); 279 303 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 280 304 stringBuilder.Append(")"); 281 } else if 305 } else if(node.Symbol is Norm) { 282 306 stringBuilder.Append("normpdf("); 283 307 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 284 308 stringBuilder.Append(")"); 285 } else if 309 } else if(node.Symbol is Psi) { 286 310 stringBuilder.Append("psi("); 287 311 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 288 312 stringBuilder.Append(")"); 289 } else if 313 } else if(node.Symbol is SineIntegral) { 290 314 stringBuilder.Append("sinint("); 291 315 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 292 316 stringBuilder.Append(")"); 293 } else if 317 } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) { 294 318 VariableTreeNode variableTreeNode = node as VariableTreeNode; 295 319 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 296 320 stringBuilder.Append("*"); 297 321 stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag)); 298 } else if (symbol is Power) { 322 } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.FactorVariable) { 323 var factorNode = node as FactorVariableTreeNode; 324 var weights = string.Join(" ", factorNode.Weights.Select(w => w.ToString("G17", CultureInfo.InvariantCulture))); 325 stringBuilder.AppendFormat("switch_{0}(\"{1}\",[{2}])", 326 factorNode.VariableName, factorNode.VariableName, weights) 327 .AppendLine(); 328 } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable) { 329 var factorNode = node as BinaryFactorVariableTreeNode; 330 stringBuilder.AppendFormat(CultureInfo.InvariantCulture, 331 "((strcmp({0},\"{1}\")==1) * {2:G17})", factorNode.VariableName, factorNode.VariableValue, factorNode.Weight); 332 } else if(symbol is Power) { 299 333 stringBuilder.Append("("); 300 334 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 302 336 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 303 337 stringBuilder.Append(")"); 304 } else if 338 } else if(symbol is Root) { 305 339 stringBuilder.Append("("); 306 340 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 308 342 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 309 343 stringBuilder.Append("))"); 310 } else if 344 } else if(symbol is Derivative) { 311 345 stringBuilder.Append("fivePoint("); 312 346 // f0 … … 326 360 stringBuilder.Append(")"); 327 361 currentLag += 4; 328 } else if 362 } else if(symbol is Integral) { 329 363 var laggedNode = node as LaggedTreeNode; 330 364 string prevCounterVariable = CurrentIndexVariable; … … 334 368 "))"); 335 369 ReleaseIndexVariable(); 336 } else if 370 } else if(symbol is TimeLag) { 337 371 var laggedNode = node as LaggedTreeNode; 338 372 currentLag += laggedNode.Lag; … … 347 381 348 382 private string LagToString(int lag) { 349 if 383 if(lag < 0) { 350 384 return "(" + CurrentIndexVariable + "" + lag + ")"; 351 } else if 385 } else if(lag > 0) { 352 386 return "(" + CurrentIndexVariable + "+" + lag + ")"; 353 387 } else return "(" + CurrentIndexVariable + ")"; -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r14185 r14764 53 53 54 54 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 55 if 56 if 55 if(node.Subtrees.Any()) { 56 if(node.Symbol is Addition) { 57 57 FormatFunction(node, "Plus", strBuilder); 58 } else if 58 } else if(node.Symbol is Average) { 59 59 FormatAverage(node, strBuilder); 60 } else if 60 } else if(node.Symbol is Multiplication) { 61 61 FormatFunction(node, "Times", strBuilder); 62 } else if 62 } else if(node.Symbol is Subtraction) { 63 63 FormatSubtraction(node, strBuilder); 64 } else if 64 } else if(node.Symbol is Division) { 65 65 FormatDivision(node, strBuilder); 66 } else if 66 } else if(node.Symbol is Sine) { 67 67 FormatFunction(node, "Sin", strBuilder); 68 } else if 68 } else if(node.Symbol is Cosine) { 69 69 FormatFunction(node, "Cos", strBuilder); 70 } else if 70 } else if(node.Symbol is Tangent) { 71 71 FormatFunction(node, "Tan", strBuilder); 72 } else if 72 } else if(node.Symbol is Exponential) { 73 73 FormatFunction(node, "Exp", strBuilder); 74 } else if 74 } else if(node.Symbol is Logarithm) { 75 75 FormatFunction(node, "Log", strBuilder); 76 } else if 76 } else if(node.Symbol is IfThenElse) { 77 77 FormatIf(node, strBuilder); 78 } else if 78 } else if(node.Symbol is GreaterThan) { 79 79 strBuilder.Append("If[Greater["); 80 80 FormatRecursively(node.GetSubtree(0), strBuilder); … … 82 82 FormatRecursively(node.GetSubtree(1), strBuilder); 83 83 strBuilder.Append("], 1, -1]"); 84 } else if 84 } else if(node.Symbol is LessThan) { 85 85 strBuilder.Append("If[Less["); 86 86 FormatRecursively(node.GetSubtree(0), strBuilder); … … 88 88 FormatRecursively(node.GetSubtree(1), strBuilder); 89 89 strBuilder.Append("], 1, -1]"); 90 } else if 90 } else if(node.Symbol is And) { 91 91 FormatAnd(node, strBuilder); 92 } else if 92 } else if(node.Symbol is Not) { 93 93 strBuilder.Append("If[Greater["); 94 94 FormatRecursively(node.GetSubtree(0), strBuilder); 95 95 strBuilder.Append(", 0], -1, 1]"); 96 } else if 96 } else if(node.Symbol is Or) { 97 97 FormatOr(node, strBuilder); 98 } else if 98 } else if(node.Symbol is Xor) { 99 99 FormatXor(node, strBuilder); 100 } else if 100 } else if(node.Symbol is Square) { 101 101 FormatSquare(node, strBuilder); 102 } else if 102 } else if(node.Symbol is SquareRoot) { 103 103 FormatFunction(node, "Sqrt", strBuilder); 104 } else if 104 } else if(node.Symbol is Power) { 105 105 FormatFunction(node, "Power", strBuilder); 106 } else if 106 } else if(node.Symbol is Root) { 107 107 FormatRoot(node, strBuilder); 108 108 } else { … … 110 110 } 111 111 } else { 112 if (node is VariableTreeNode) { 112 // terminals 113 if(node.Symbol is Variable) { 113 114 var varNode = node as VariableTreeNode; 114 115 strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture)); 115 } else if (node is ConstantTreeNode) {116 } else if(node.Symbol is Constant) { 116 117 var constNode = node as ConstantTreeNode; 117 118 strBuilder.Append(constNode.Value.ToString("G17", CultureInfo.InvariantCulture)); 119 } else if(node.Symbol is FactorVariable) { 120 var factorNode = node as FactorVariableTreeNode; 121 strBuilder.AppendFormat("Switch[{0},", factorNode.VariableName); 122 var varValues = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray(); 123 var weights = varValues.Select(factorNode.GetValue).ToArray(); 124 125 var weightStr = string.Join(", ", 126 varValues.Zip(weights, (s, d) => string.Format(CultureInfo.InvariantCulture, "\"{0}\", {1:G17}", s, d))); 127 strBuilder.Append(weightStr); 128 strBuilder.Append("]"); 129 } else if(node.Symbol is BinaryFactorVariable) { 130 var factorNode = node as BinaryFactorVariableTreeNode; 131 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "If[{0}==\"{1}\",{2:G17},0.0]", 132 factorNode.VariableName, factorNode.VariableValue, factorNode.Weight); 118 133 } else { 119 134 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " is not supported."); … … 124 139 private void FormatXor(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 125 140 strBuilder.Append("If[Xor["); 126 foreach 141 foreach(var t in node.Subtrees) { 127 142 strBuilder.Append("Greater["); 128 143 FormatRecursively(t, strBuilder); 129 144 strBuilder.Append(", 0]"); 130 if 145 if(t != node.Subtrees.Last()) strBuilder.Append(","); 131 146 } 132 147 strBuilder.Append("], 1, -1]"); … … 135 150 private void FormatOr(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 136 151 strBuilder.Append("If[Or["); 137 foreach 152 foreach(var t in node.Subtrees) { 138 153 strBuilder.Append("Greater["); 139 154 FormatRecursively(t, strBuilder); 140 155 strBuilder.Append(", 0]"); 141 if 156 if(t != node.Subtrees.Last()) strBuilder.Append(","); 142 157 } 143 158 strBuilder.Append("], 1, -1]"); … … 146 161 private void FormatAnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 147 162 strBuilder.Append("If[And["); 148 foreach 163 foreach(var t in node.Subtrees) { 149 164 strBuilder.Append("Greater["); 150 165 FormatRecursively(t, strBuilder); 151 166 strBuilder.Append(", 0]"); 152 if 167 if(t != node.Subtrees.Last()) strBuilder.Append(","); 153 168 } 154 169 strBuilder.Append("], 1, -1]"); … … 169 184 strBuilder.Append("Mean[{"); 170 185 FormatRecursively(node.GetSubtree(0), strBuilder); 171 for 186 for(int i = 1; i < node.SubtreeCount; i++) { 172 187 strBuilder.Append(","); 173 188 FormatRecursively(node.GetSubtree(i), strBuilder); … … 180 195 FormatRecursively(node.GetSubtree(0), strBuilder); 181 196 strBuilder.Append(", Times[-1"); 182 foreach 197 foreach(var t in node.Subtrees) { 183 198 strBuilder.Append(","); 184 199 FormatRecursively(t, strBuilder); … … 202 217 203 218 private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 204 if 219 if(node.SubtreeCount == 1) { 205 220 strBuilder.Append("Divide[1, "); 206 221 FormatRecursively(node.GetSubtree(0), strBuilder); … … 211 226 strBuilder.Append(", Times["); 212 227 FormatRecursively(node.GetSubtree(1), strBuilder); 213 for 228 for(int i = 2; i < node.SubtreeCount; i++) { 214 229 strBuilder.Append(","); 215 230 FormatRecursively(node.GetSubtree(i), strBuilder); … … 221 236 private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) { 222 237 strBuilder.Append(function + "["); 223 foreach 238 foreach(var child in node.Subtrees) { 224 239 FormatRecursively(child, strBuilder); 225 if 240 if(child != node.Subtrees.Last()) 226 241 strBuilder.Append(", "); 227 242 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r14185 r14764 48 48 ISymbol symbol = node.Symbol; 49 49 50 if 50 if(symbol is ProgramRootSymbol || symbol is StartSymbol) 51 51 return FormatRecursively(node.GetSubtree(0)); 52 52 … … 55 55 stringBuilder.Append("("); 56 56 57 if 58 for 59 if 57 if(symbol is Addition) { 58 for(int i = 0; i < node.SubtreeCount; i++) { 59 if(i > 0) stringBuilder.Append("+"); 60 60 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 61 61 } 62 } else if 62 } else if(symbol is And) { 63 63 stringBuilder.Append("("); 64 for 65 if 64 for(int i = 0; i < node.SubtreeCount; i++) { 65 if(i > 0) stringBuilder.Append("&"); 66 66 stringBuilder.Append("("); 67 67 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 69 69 } 70 70 stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]"); 71 } else if 71 } else if(symbol is Average) { 72 72 stringBuilder.Append("(1/"); 73 73 stringBuilder.Append(node.SubtreeCount); 74 74 stringBuilder.Append(")*("); 75 for 76 if 75 for(int i = 0; i < node.SubtreeCount; i++) { 76 if(i > 0) stringBuilder.Append("+"); 77 77 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 78 78 } 79 79 stringBuilder.Append(")"); 80 } else if 80 } else if(symbol is Constant) { 81 81 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; 82 82 stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 83 } else if 83 } else if(symbol is Cosine) { 84 84 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 85 85 stringBuilder.Append(" cos"); 86 } else if 87 if 86 } else if(symbol is Division) { 87 if(node.SubtreeCount == 1) { 88 88 stringBuilder.Append("1/"); 89 89 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 91 91 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 92 92 stringBuilder.Append("/("); 93 for 94 if 93 for(int i = 1; i < node.SubtreeCount; i++) { 94 if(i > 1) stringBuilder.Append("*"); 95 95 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 96 96 } 97 97 stringBuilder.Append(")"); 98 98 } 99 } else if 99 } else if(symbol is Exponential) { 100 100 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 101 101 stringBuilder.Append(" exp"); 102 } else if 102 } else if(symbol is GreaterThan) { 103 103 stringBuilder.Append("("); 104 104 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 106 106 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 107 107 stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]"); 108 } else if 108 } else if(symbol is IfThenElse) { 109 109 stringBuilder.Append("("); 110 110 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 114 114 stringBuilder.Append(FormatRecursively(node.GetSubtree(2))); 115 115 stringBuilder.Append("]"); 116 } else if 117 stringBuilder.Append("lagged variable not implemented");118 } else if 116 } else if(symbol is LaggedVariable) { 117 stringBuilder.Append("lagged variables are not supported"); 118 } else if(symbol is LessThan) { 119 119 stringBuilder.Append("("); 120 120 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); … … 122 122 stringBuilder.Append(FormatRecursively(node.GetSubtree(1))); 123 123 stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]"); 124 } else if 124 } else if(symbol is Logarithm) { 125 125 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 126 126 stringBuilder.Append("ln"); 127 } else if 128 for 129 if 127 } else if(symbol is Multiplication) { 128 for(int i = 0; i < node.SubtreeCount; i++) { 129 if(i > 0) stringBuilder.Append("*"); 130 130 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 131 131 } 132 } else if 132 } else if(symbol is Not) { 133 133 stringBuilder.Append("("); 134 134 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 135 135 stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]"); 136 } else if 136 } else if(symbol is Or) { 137 137 stringBuilder.Append("("); 138 for 139 if 138 for(int i = 0; i < node.SubtreeCount; i++) { 139 if(i > 0) stringBuilder.Append("|"); 140 140 stringBuilder.Append("("); 141 141 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); … … 143 143 } 144 144 stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]"); 145 } else if 145 } else if(symbol is Sine) { 146 146 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 147 147 stringBuilder.Append(" sin"); 148 } else if 149 if 148 } else if(symbol is Subtraction) { 149 if(node.SubtreeCount == 1) { 150 150 stringBuilder.Append("-1*"); 151 151 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 152 152 } else { 153 153 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 154 for 154 for(int i = 1; i < node.SubtreeCount; i++) { 155 155 stringBuilder.Append(" - "); 156 156 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 157 157 } 158 158 } 159 } else if 159 } else if(symbol is Tangent) { 160 160 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 161 161 stringBuilder.Append(" tan"); 162 } else if 162 } else if(symbol is Variable) { 163 163 VariableTreeNode variableTreeNode = node as VariableTreeNode; 164 164 stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture)); 165 165 stringBuilder.Append("*"); 166 166 stringBuilder.Append(variableTreeNode.VariableName); 167 } else if(symbol is BinaryFactorVariable || symbol is FactorVariable) { 168 stringBuilder.Append("factor variables are not supported"); 167 169 } else { 168 170 stringBuilder.Append("("); 169 for 170 if 171 for(int i = 0; i < node.SubtreeCount; i++) { 172 if(i > 0) stringBuilder.Append(", "); 171 173 stringBuilder.Append(FormatRecursively(node.GetSubtree(i))); 172 174 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs
r14185 r14764 58 58 StringBuilder strBuilder = new StringBuilder(); 59 59 if (Indent) strBuilder.Append(' ', indentLength); 60 if (node.Subtrees. Count() > 0) { // internal node60 if (node.Subtrees.Any()) { // internal node 61 61 strBuilder.Append("("); 62 62 if (node.Symbol is Addition) { … … 103 103 strBuilder.Append(")"); 104 104 } else { 105 if (node is VariableTreeNode) {105 if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) { 106 106 var varNode = node as VariableTreeNode; 107 107 strBuilder.AppendFormat("(* {0} {1})", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture)); 108 } else if (node is ConstantTreeNode) {108 } else if (node.Symbol is Constant) { 109 109 var constNode = node as ConstantTreeNode; 110 110 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
Note: See TracChangeset
for help on using the changeset viewer.