Commit 139f4b2e authored by Michal Jankowski's avatar Michal Jankowski
Browse files

Changed decision on which nodes are kept in queue to clarify what code does....

Changed decision on which nodes are kept in queue to clarify what code does. Now all nodes are in queue.
parent 799c4767
......@@ -7,10 +7,8 @@ public interface MemoryLimiter {
Node whichNodeToPrune();
// Marks nodes as recently accessed. Goes from bottom to root of tree.
void updateRecentlyAccessedNodes(Node startingNode);
// Tells memory limiter that count nodes have been removed.
void decreaseCount(int count);
// Creates node with certain value.
Node createNode(int value);
Node createNode(Node parent, int value);
// Removes node from queue, does nothing if it's not in queue.
void removeNodeFromQueue(Node node);
}
\ No newline at end of file
......@@ -62,40 +62,36 @@ public abstract class Tree {
memoryLimiter.updateRecentlyAccessedNodes(leaf);
while (memoryLimiter.shouldPruneTree()) {
pruneSubTree(memoryLimiter.whichNodeToPrune());
removeLeaf(memoryLimiter.whichNodeToPrune());
}
return solution;
}
// Prunes subtree completely. SubRoot's children should be all leaves.
private void pruneSubTree(Node subRoot) {
if (subRoot == root) { // If is a root then do nothing.
return;
}
memoryLimiter.removeNodeFromQueue(subRoot);
memoryLimiter.decreaseCount(subRoot.getChildrenSize());
subRoot.removeChildren();
removeLeaf(subRoot);
}
private void removeSubtreeWithNoSolutions(Node subtreeRoot) {
log.debug("Removing subtree at depth {}", subtreeRoot.getNodeStatistics().getDepth());
removeLeaf(subtreeRoot);
}
private void removeLeaf(Node node) {
memoryLimiter.decreaseCount(1);
assert(node.getChildrenSize() == 0);
// Shouldn't happen if node limit is not very small.
if (node == root) {
return;
}
assert(node.getParent() != null);
node.getParent().removeChild(node);
memoryLimiter.removeNodeFromQueue(node);
if (node.getParent() != root && node.getParent().getChildrenSize() == 0) {
memoryLimiter.removeNodeFromQueue(node.getParent()); // Parent is becoming a leaf and we should remove it from queue.
removeLeaf(node.getParent());
}
// There's a corner case in which root can lose its all children.
// If node limit is > ~10000 this should never happen.
else if (node.getParent() == root && root.getChildrenSize() == 0) {
if (node.getParent() == root && root.getChildrenSize() == 0) {
root.setUnexpanded();
}
}
......
......@@ -35,21 +35,21 @@ public class MemoryLimiterImpl implements MemoryLimiter {
}
@Override
public void decreaseCount(int count) {
this.count -= count;
}
@Override
public Node createNode(int value) {
public Node createNode(Node parent, int value) {
count++;
NodeImpl newNode = new NodeImpl(value);
newNode.linkToTree(parent);
updateRecentlyAccessedNode(newNode);
return new NodeImpl(value);
}
@Override
public void removeNodeFromQueue(Node node) {
count--;
accessQueue.removeNodeFromQueue((NodeImpl) node);
}
// Adds newly created or visited node to queue.
private void updateRecentlyAccessedNode(Node node) {
accessQueue.pushBack(node);
}
......
......@@ -64,8 +64,7 @@ public class MoveProviderImpl implements MoveProvider {
}
IntStream.range(mctsWrapper.getMinDomainValue(depth), mctsWrapper.getMaxDomainValue(depth) + 1).
forEach(value -> {
Node newNode = memoryLimiter.createNode(value);
newNode.linkToTree(toExpand);
Node newNode = memoryLimiter.createNode(toExpand, value);
});
toExpand.setExpanded();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment