Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12966 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
RevLine 
[12909]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 {
[12923]9  internal class SearchTree<TValue> {
[12924]10    private class Node {
[12923]11      internal TValue value;
[12924]12      internal Node parent;
13      internal Node[] children;
[12909]14      // children == null -> never visited
15      // children[i] != null -> visited at least once, still allowed
16      // children[i] = null -> fully explored
17    }
18
[12924]19    private Node root;
[12909]20
21    // for iteration
[12924]22    private Node currentNode;
[12909]23
24    public SearchTree() {
[12924]25      root = new Node();
[12909]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
[12923]37    public void ExpandCurrentNode(IEnumerable<TValue> values) {
38      Contract.Assert(values.Any());
[12909]39      Contract.Assert(currentNode.children == null);
[12924]40      currentNode.children = values.Select(val => new Node() { value = val, parent = currentNode }).ToArray();
[12909]41    }
42
[12923]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];
[12909]51    }
52
[12923]53    public IEnumerable<TValue> ChildValues {
[12909]54      get {
[12923]55        return from ch in currentNode.children
56               where ch != null
57               select ch.value;
[12909]58      }
59    }
60
[12923]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++;
[12909]66
[12923]67      if (i >= currentNode.children.Length) throw new InvalidProgramException();
68      currentNode.children[i] = null;
69
[12909]70      RemoveRecursively(currentNode);
71    }
72
[12924]73    private void RemoveRecursively(Node node) {
[12909]74      // when the last child has been removed we must remove the current node from it's parent
[12922]75      while (node.parent != null && node.children.All(ch => ch == null)) {
[12909]76        node.parent.children[Array.IndexOf(node.parent.children, node)] = null;
[12922]77        node = node.parent;
[12909]78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.