Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/SearchTree.cs @ 12967

Last change on this file since 12967 was 12924, checked in by gkronber, 9 years ago

#2471

  • implemented a demo for value approximation with gbt and a trivial feature function
File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics.Contracts;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
9  internal class SearchTree<TValue> {
10    private class Node {
11      internal TValue value;
12      internal Node parent;
13      internal Node[] children;
14      // children == null -> never visited
15      // children[i] != null -> visited at least once, still allowed
16      // children[i] = null -> fully explored
17    }
18
19    private Node root;
20
21    // for iteration
22    private Node currentNode;
23
24    public SearchTree() {
25      root = new Node();
26      currentNode = root;
27    }
28
29    public void ResetPosition() {
30      currentNode = root;
31    }
32
33    public bool IsLeafNode() {
34      return currentNode.children == null;
35    }
36
37    public void ExpandCurrentNode(IEnumerable<TValue> values) {
38      Contract.Assert(values.Any());
39      Contract.Assert(currentNode.children == null);
40      currentNode.children = values.Select(val => new Node() { value = val, parent = currentNode }).ToArray();
41    }
42
43    public void Follow(TValue value) {
44      // TODO: perf
45      int i = 0;
46      while (i < currentNode.children.Length && (
47        currentNode.children[i] == null || !currentNode.children[i].value.Equals(value))) i++;
48
49      if (i >= currentNode.children.Length) throw new InvalidProgramException();
50      currentNode = currentNode.children[i];
51    }
52
53    public IEnumerable<TValue> ChildValues {
54      get {
55        return from ch in currentNode.children
56               where ch != null
57               select ch.value;
58      }
59    }
60
61    public void RemoveBranch(TValue value) {
62      // TODO: perf
63      int i = 0;
64      while (i < currentNode.children.Length && (
65        currentNode.children[i] == null || !currentNode.children[i].value.Equals(value))) i++;
66
67      if (i >= currentNode.children.Length) throw new InvalidProgramException();
68      currentNode.children[i] = null;
69
70      RemoveRecursively(currentNode);
71    }
72
73    private void RemoveRecursively(Node node) {
74      // when the last child has been removed we must remove the current node from it's parent
75      while (node.parent != null && node.children.All(ch => ch == null)) {
76        node.parent.children[Array.IndexOf(node.parent.children, node)] = null;
77        node = node.parent;
78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.