Changeset 5750 for branches/DataAnalysis Refactoring/HeuristicLab.Problems.ExternalEvaluation.GP/3.4/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs
- Timestamp:
- 03/18/11 12:50:28 (13 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Problems.ExternalEvaluation.GP/3.4/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs
r5745 r5750 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using HeuristicLab.Problems.DataAnalysis.Symbolic; 29 using System; 27 30 28 namespace HeuristicLab. Encodings.SymbolicExpressionTreeEncoding{31 namespace HeuristicLab.Problems.ExternalEvaluation.GP { 29 32 30 [Item(" SymbolicExpressionTreeStringFormatter", "The default string formatter for symbolic expression trees.")]33 [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")] 31 34 [StorableClass] 32 public class SymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {35 public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 33 36 34 37 public bool Indent { get; set; } 35 38 36 39 [StorableConstructor] 37 protected SymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { }38 protected SymbolicExpressionTreeStringFormatter(SymbolicExpressionTreeStringFormatter original, Cloner cloner)40 protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { } 41 protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(ExternalEvaluationSymbolicExpressionTreeStringFormatter original, Cloner cloner) 39 42 : base(original, cloner) { 40 43 Indent = original.Indent; 41 44 } 42 public SymbolicExpressionTreeStringFormatter()45 public ExternalEvaluationSymbolicExpressionTreeStringFormatter() 43 46 : base() { 44 Name = " Default StringFormatter";47 Name = "External Evaluation Symbolic Expression Tree Formatter"; 45 48 Indent = true; 46 49 } 47 50 48 51 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 49 return FormatRecursively(symbolicExpressionTree.Root, 0); 52 // skip root and start symbols 53 return FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 0); 50 54 } 51 55 … … 57 61 if (node.Subtrees.Count() > 0) { 58 62 // symbol on same line as '(' 59 strBuilder.AppendLine(node.ToString()); 63 if (node.Symbol is Addition) { 64 strBuilder.AppendLine("+"); 65 } else if (node.Symbol is And) { 66 strBuilder.AppendLine("&&"); 67 } else if (node.Symbol is Average) { 68 strBuilder.AppendLine("avg"); 69 } else if (node.Symbol is Cosine) { 70 strBuilder.AppendLine("cos"); 71 } else if (node.Symbol is Division) { 72 strBuilder.AppendLine("/"); 73 } else if (node.Symbol is Exponential) { 74 strBuilder.AppendLine("exp"); 75 } else if (node.Symbol is GreaterThan) { 76 strBuilder.AppendLine(">"); 77 } else if (node.Symbol is IfThenElse) { 78 strBuilder.AppendLine("if"); 79 } else if (node.Symbol is LessThan) { 80 strBuilder.AppendLine("<"); 81 } else if (node.Symbol is Logarithm) { 82 strBuilder.AppendLine("ln"); 83 } else if (node.Symbol is Multiplication) { 84 strBuilder.AppendLine("*"); 85 } else if (node.Symbol is Not) { 86 strBuilder.AppendLine("!"); 87 } else if (node.Symbol is Or) { 88 strBuilder.AppendLine("||"); 89 } else if (node.Symbol is Sine) { 90 strBuilder.AppendLine("sin"); 91 } else if (node.Symbol is Subtraction) { 92 strBuilder.AppendLine("-"); 93 } else if (node.Symbol is Tangent) { 94 strBuilder.AppendLine("tan"); 95 } else { 96 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation."); 97 } 60 98 // each subtree expression on a new line 61 99 // and closing ')' also on new line 100 62 101 foreach (var subtree in node.Subtrees) { 63 102 strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2)); … … 66 105 strBuilder.Append(")"); 67 106 } else { 68 // symbol in the same line with as '(' and ')' 69 strBuilder.Append(node.ToString()); 70 strBuilder.Append(")"); 107 if (node is VariableTreeNode) { 108 var varNode = node as VariableTreeNode; 109 // symbol in the same line with as '(' and ')' 110 strBuilder.Append(";" + varNode.VariableName + ";" + varNode.Weight.ToString("E4")); 111 strBuilder.Append(")"); 112 } else if (node is ConstantTreeNode) { 113 var constNode = node as ConstantTreeNode; 114 // symbol in the same line with as '(' and ')' 115 strBuilder.Append(";" + constNode.Value.ToString("E4")); 116 strBuilder.Append(")"); 117 } else { 118 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation."); 119 } 71 120 } 72 121 return strBuilder.ToString(); … … 74 123 75 124 public override IDeepCloneable Clone(Cloner cloner) { 76 return new SymbolicExpressionTreeStringFormatter(this, cloner);125 return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner); 77 126 } 78 127 }
Note: See TracChangeset
for help on using the changeset viewer.