From d93dc77cd3ebbe0af3a83cf094cbf4f30088c259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Fri, 8 Nov 2013 00:10:42 +0100 Subject: [PATCH] AStar fixed. Close #43. --- .../citius/lab/hipster/algorithm/AStar.java | 30 ++++++------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/lab/hipster/algorithm/AStar.java b/hipster-core/src/main/java/es/usc/citius/lab/hipster/algorithm/AStar.java index 84393c2..c0fc845 100644 --- a/hipster-core/src/main/java/es/usc/citius/lab/hipster/algorithm/AStar.java +++ b/hipster-core/src/main/java/es/usc/citius/lab/hipster/algorithm/AStar.java @@ -78,6 +78,7 @@ public boolean hasNext() { return !open.values().isEmpty(); } + private HeuristicNode takePromising() { // Poll until a valid state is found HeuristicNode node = queue.poll(); @@ -96,9 +97,10 @@ private HeuristicNode takePromising() { * @see HeuristicNode */ public HeuristicNode next() { - - HeuristicNode current = takePromising(); + // Get and remove the best node in the queue + HeuristicNode current = queue.poll(); S currentState = current.transition().to(); + // Remove from open as well open.remove(currentState); // Analyze the cost of each movement from the current node @@ -107,7 +109,7 @@ public HeuristicNode next() { HeuristicNode successorNode = this.factory.node(current, successor); HeuristicNode successorOpen = open.get(successor.to()); if (successorOpen != null) { - if (successorOpen.getCost().compareTo(successorNode.getCost()) <= 0) { + if (successorOpen.getScore().compareTo(successorNode.getScore()) <= 0) { // Keep analyzing the other movements, discard this movement continue; } @@ -116,28 +118,14 @@ public HeuristicNode next() { HeuristicNode successorClose = closed.get(successor.to()); if (successorClose != null) { // Check if this path improves the cost of a closed neighbor. - if (successorClose.getCost().compareTo(successorNode.getCost()) <= 0) { + if (successorClose.getScore().compareTo(successorNode.getScore()) <= 0) { continue; } } - if (successorOpen != null) { - open.remove(successor.to()); - // Don't remove from queue, for performance purposes use - // function takePromising() and do not use queue.poll - } - - // If it is in the close list, then take it from that list - if (successorClose != null) { - closed.remove(successor.to()); - } - - // Add the new successor to the open list to explore later - HeuristicNode result = open.put(successor.to(), successorNode); - // If this state is not duplicated, enqueue - if (result == null) { - queue.add(successorNode); - } + // In any other case, add the new successor to the open list to explore later + open.put(successor.to(), successorNode); + queue.add(successorNode); } // Once analyzed, the current node moves to the closed list closed.put(currentState, current);