Commit dd7312e9 authored by Michal Jankowski's avatar Michal Jankowski
Browse files

Changed the way nodes are removed from queue and the way whichNodeToPrune...

Changed the way nodes are removed from queue and the way whichNodeToPrune works (now it doesnt remove node).
parent 1ce46825
......@@ -3,8 +3,8 @@ package eu.melodic.upperware.mcts_solver.solver.mcts.tree;
public interface MemoryLimiter {
// Tells tree whether it should cut branch.
boolean shouldPruneTree();
// Tells tree which node to prune. Pops a node from queue in process.
Node popNodeToPrune();
// Tells tree which node to prune.
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.
......
......@@ -55,15 +55,14 @@ public abstract class Tree {
Solution solution = rollout(path);
backPropagate(leaf, solution);
if (solution.isEmpty() && leaf.getNodeStatistics().getDepth() > minDepthSubtreeRemoval) {
removeSubtreeWithNoSolutions((leaf));
}
memoryLimiter.updateRecentlyAccessedNodes(leaf);
while (memoryLimiter.shouldPruneTree()) {
pruneSubTree(memoryLimiter.popNodeToPrune());
pruneSubTree(memoryLimiter.whichNodeToPrune());
}
return solution;
......@@ -74,6 +73,8 @@ public abstract class Tree {
if (subRoot == root) { // If is a root then do nothing.
return;
}
memoryLimiter.removeNodeFromQueue(subRoot);
memoryLimiter.decreaseCount(subRoot.getChildrenSize());
subRoot.removeChildren();
removeLeaf(subRoot);
......@@ -89,7 +90,7 @@ public abstract class Tree {
node.getParent().removeChild(node);
if (node.getParent() != root && node.getParent().getChildrenSize() == 0) {
memoryLimiter.removeNodeFromQueue(node.getParent());
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.
......
......@@ -18,8 +18,8 @@ public class MemoryLimiterImpl implements MemoryLimiter {
}
@Override
public Node popNodeToPrune() {
return accessQueue.popFront();
public Node whichNodeToPrune() {
return accessQueue.getFront();
}
@Override
......
package eu.melodic.upperware.mcts_solver.solver.mcts.tree_impl;
import eu.melodic.upperware.mcts_solver.solver.mcts.tree.Node;
import lombok.Getter;
public class Queue {
@Getter
private NodeImpl front = null;
private NodeImpl back = null;
// Removes front element of queue and returns it. Does nothing if queue is empty and returns null.
public Node popFront() {
if (front == null) {
return null;
}
NodeImpl toReturn = front;
removeNodeFromQueue(front);
return toReturn;
}
/*
Moves node to the back of queue.
If node was already in queue then its previous occurrence is forgotten and it's added as a new element.
......
......@@ -21,22 +21,23 @@ public class QueueTest {
Queue queue = new Queue();
assertTrue(queue.empty());
assertNull(queue.popFront());
queue.pushBack(node1);
assertFalse(queue.empty());
assertNotNull(queue.popFront());
assertNotNull(queue.getFront());
queue.removeNodeFromQueue(queue.getFront());
queue.pushBack(node1);
queue.pushBack(node2);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node1);
assertEquals(queue.getFront(), node1);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node2);
assertEquals(queue.getFront(), node2);
queue.removeNodeFromQueue(queue.getFront());
assertTrue(queue.empty());
......@@ -45,9 +46,11 @@ public class QueueTest {
queue.pushBack(node1);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node2);
assertEquals(queue.getFront(), node2);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node1);
assertEquals(queue.getFront(), node1);
queue.removeNodeFromQueue(queue.getFront());
assertTrue(queue.empty());
......@@ -55,7 +58,8 @@ public class QueueTest {
queue.pushBack(node1);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node1);
assertEquals(queue.getFront(), node1);
queue.removeNodeFromQueue(queue.getFront());
assertTrue(queue.empty());
......@@ -69,11 +73,14 @@ public class QueueTest {
queue.pushBack(node3);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node1);
assertEquals(queue.getFront(), node1);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node2);
assertEquals(queue.getFront(), node2);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node3);
assertEquals(queue.getFront(), node3);
queue.removeNodeFromQueue(queue.getFront());
assertTrue(queue.empty());
......@@ -84,11 +91,14 @@ public class QueueTest {
queue.pushBack(node1);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node3);
assertEquals(queue.getFront(), node3);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node2);
assertEquals(queue.getFront(), node2);
queue.removeNodeFromQueue(queue.getFront());
assertFalse(queue.empty());
assertEquals(queue.popFront(), node1);
assertEquals(queue.getFront(), node1);
queue.removeNodeFromQueue(queue.getFront());
assertTrue(queue.empty());
queue.pushBack(node1);
......@@ -97,8 +107,8 @@ public class QueueTest {
queue.pushBack(node1);
assertFalse(queue.empty());
assertEquals(queue.popFront(), node2);
assertEquals(queue.getFront(), node2);
queue.removeNodeFromQueue(queue.getFront());
}
@Test
......@@ -121,7 +131,8 @@ public class QueueTest {
}
for (int i = 0; i < NODE_NUMBER; i++) {
assertSame(queue.popFront(), pseudo_queue.get(i));
assertSame(queue.getFront(), pseudo_queue.get(i));
queue.removeNodeFromQueue(queue.getFront());
}
}
}
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