Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree/TreeExtensions.cs @ 15189

Last change on this file since 15189 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Data.Tree {
5  using System.Linq;
6
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
8
9  public static class TreeExtensions {
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>(
14      this IEnumerable<T> items,
15      Predicate<T> isChild,
16      Func<T, IReadOnlyList<T>> childrenResolver) {
17      var tree = new TreeNode<T>();
18
19      foreach (var item in items) {
20        if (isChild(item)) {
21          var subNode = childrenResolver(item).ToTree(isChild, childrenResolver);
22          subNode.Value = item;
23          tree.AddNode(subNode);
24        } else {
25          tree.Add(item);
26        }
27      }
28
29      return tree;
30    }
31
32    public static IEnumerable<TreeNode<T>> DepthLast<T>(this TreeNode<T> node) {
33      foreach (var child in node.Children) {
34        if (child.Children.Count > 0) {
35          foreach (var subChild in child.DepthLast()) {
36            yield return subChild;
37          }
38        } else yield return child;
39      }
40
41      yield return node;
42    }
43
44    public static IEnumerable<T> DepthLast<T>(this TreeNode<T> node, Func<IEnumerable<T>, T> resolveParent) {
45      foreach (var child in node.Children) {
46        if (child.Children.Count > 0) {
47          var subExpressions = child.DepthLast(resolveParent);
48          yield return resolveParent(subExpressions);
49        } else yield return child.Value;
50      }
51    }
52
53    public static PushProgram ToPushProgramWithoutNoopExpressions(this TreeNode<Expression> tree) {
54      return ToPushProgram(tree, e => !ReferenceEquals(e, ExecNoop) && !ReferenceEquals(e, CodeNoop));
55    }
56
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
65      var expressions = tree.DepthLast(e => ResolveProgram(e, condition));
66
67      return ResolveProgram(expressions, condition);
68    }
69
70    public static TreeNode<Expression> ToTree(this PushProgram program) {
71      var tree = program.Expressions.ToTree(e => e.IsProgram, e => ((PushProgram)e).Expressions);
72      tree.Value = program;
73
74      return tree;
75    }
76
77    private static PushProgram ResolveProgram(IEnumerable<Expression> expressions, Func<Expression, bool> condition = null) {
78
79      if (condition != null) {
80        expressions = expressions.Where(condition);
81      }
82
83      return new PushProgram(expressions.ToList());
84    }
85  }
86
87}
Note: See TracBrowser for help on using the repository browser.