Changeset 15017 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree
- Timestamp:
- 06/01/17 09:28:34 (7 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree/TreeExtensions.cs
r14834 r15017 8 8 9 9 public static class TreeExtensions { 10 public static Tree<T> ToTree<T>( 10 private static readonly Expression ExecNoop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>(); 11 private static readonly Expression CodeNoop = ExpressionTable.GetStatelessExpression<CodeNoopExpression>(); 12 13 public static TreeNode<T> ToTree<T>( 11 14 this IEnumerable<T> items, 12 15 Predicate<T> isChild, 13 16 Func<T, IReadOnlyList<T>> childrenResolver) { 14 var tree = new Tree <T>();17 var tree = new TreeNode<T>(); 15 18 16 19 foreach (var item in items) { … … 35 38 } else yield return child; 36 39 } 40 41 yield return node; 37 42 } 38 43 … … 46 51 } 47 52 53 public static PushProgram ToPushProgramWithoutNoopExpressions(this TreeNode<Expression> tree) { 54 return ToPushProgram(tree, e => !ReferenceEquals(e, ExecNoop) && !ReferenceEquals(e, CodeNoop)); 55 } 56 48 57 public static PushProgram ToPushProgram(this TreeNode<Expression> tree, Func<Expression, bool> condition = null) { 58 // wrap into program as depthlast expects that tree represents program and not a single expression 59 if (tree.Count == 1) { 60 var tmp = tree; 61 tree = new TreeNode<Expression>(); 62 tree.AddNode(tmp); 63 } 64 49 65 var expressions = tree.DepthLast(e => ResolveProgram(e, condition)); 50 66 … … 52 68 } 53 69 54 public static Tree <Expression> ToTree(this PushProgram program) {70 public static TreeNode<Expression> ToTree(this PushProgram program) { 55 71 var tree = program.Expressions.ToTree(e => e.IsProgram, e => ((PushProgram)e).Expressions); 56 72 tree.Value = program; -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree/TreeNode.cs
r14908 r15017 5 5 public T Value { get; set; } 6 6 public IList<TreeNode<T>> Children { get; private set; } 7 public int Count { get; private set; } 7 8 8 9 public TreeNode() { 9 10 Children = new List<TreeNode<T>>(); 11 Count = 1; 10 12 } 11 13 … … 16 18 public void Add(T value) { 17 19 Children.Add(new TreeNode<T>(value)); 20 Count++; 18 21 } 19 22 20 23 public void AddNode(TreeNode<T> node) { 21 24 Children.Add(node); 25 Count += node.Count; 22 26 } 23 27 24 28 public void ReplaceNode(int index, TreeNode<T> node) { 29 Count -= Children[index].Count; 25 30 Children[index] = node; 31 Count += node.Count; 26 32 } 27 33 }
Note: See TracChangeset
for help on using the changeset viewer.