Skip to content

Commit

Permalink
Multiple changes to adapt all algorithms to the new Operable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Rodríguez Mier committed Apr 19, 2013
1 parent f3db3cb commit 6ef9863
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 41 deletions.
5 changes: 3 additions & 2 deletions src/main/java/es/usc/citius/lab/hipster/algorithm/ADStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import es.usc.citius.lab.hipster.function.TransitionFunction;
import es.usc.citius.lab.hipster.node.adstar.ADStarNode;
import es.usc.citius.lab.hipster.node.adstar.ADStarNodeUpdater;
import es.usc.citius.lab.hipster.node.informed.HeuristicNode;
import es.usc.citius.lab.hipster.node.Node;
import es.usc.citius.lab.hipster.node.NodeFactory;
import es.usc.citius.lab.hipster.node.Transition;
Expand All @@ -22,7 +23,7 @@
* @since 26-03-2013
* @version 1.0
*/
public class ADStar<S, T extends Scalable<T>> implements Iterator<Node<S>> {
public class ADStar<S, T extends Scalable<T>> implements Iterator<HeuristicNode<S,T>> {

private final ADStarNode<S, T> beginNode;
private final ADStarNode<S, T> goalNode;
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean hasNext() {
return takePromising() != null;
}

public Node<S> next() {
public HeuristicNode<S,T> next() {
//First node in OPEN retrieved, not removed
ADStarNode<S, T> current = takePromising();
S state = current.transition().to();
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/es/usc/citius/lab/hipster/algorithm/BellmanFord.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import java.util.Set;

import es.usc.citius.lab.hipster.function.TransitionFunction;
import es.usc.citius.lab.hipster.node.HeuristicNode;
import es.usc.citius.lab.hipster.node.Node;
import es.usc.citius.lab.hipster.node.NodeFactory;
import es.usc.citius.lab.hipster.node.Transition;
import es.usc.citius.lab.hipster.node.informed.CostNode;
import es.usc.citius.lab.hipster.util.Operable;

/**
* Bellman Ford (BF) algorithm is a label correcting method that computes
Expand All @@ -40,13 +41,13 @@
*
* @param <S>
*/
public class BellmanFord<S> implements Iterator<Node<S>> {
public class BellmanFord<S, T extends Operable<T>> implements Iterator<CostNode<S,T>> {

private TransitionFunction<S> transition;
private NodeFactory<S, HeuristicNode<S>> builder;
private NodeFactory<S, CostNode<S,T>> factory;
private Queue<S> queue;
private Map<S, HeuristicNode<S>> explored;
//private Comparator<HeuristicNode<S>> comparator;
private Map<S, CostNode<S,T>> explored;
//private Comparator<CostNode<S,T>> comparator;

// Create a queue based on LinkedHashSet
private class HashQueue<S> extends AbstractQueue<S>{
Expand Down Expand Up @@ -91,13 +92,13 @@ public boolean contains(Object o) {

}

public BellmanFord(S initialState, TransitionFunction<S> transition, NodeFactory<S, HeuristicNode<S>> builder, Comparator<Node<S>> comparator){
this.builder = builder;
public BellmanFord(S initialState, TransitionFunction<S> transition, NodeFactory<S, CostNode<S,T>> builder){
this.factory = builder;
this.transition = transition;
//this.queue = new LinkedList<S>();
this.queue = new HashQueue<S>();
this.explored = new HashMap<S, HeuristicNode<S>>();
HeuristicNode<S> initialNode = builder.node(null, new Transition<S>(initialState));
this.explored = new HashMap<S, CostNode<S,T>>();
CostNode<S,T> initialNode = builder.node(null, new Transition<S>(initialState));
this.queue.add(initialState);
this.explored.put(initialState, initialNode);
}
Expand All @@ -106,35 +107,36 @@ public boolean hasNext() {
return !this.queue.isEmpty();
}

private void enqueue(HeuristicNode<S> node){
private void enqueue(CostNode<S,T> node){
S state = node.transition().to();
if (!this.queue.contains(state)){
this.queue.add(state);
}
this.explored.put(state, node);
}

private HeuristicNode<S> dequeue(){
private CostNode<S,T> dequeue(){
S state = this.queue.poll();
return this.explored.get(state);
}


public Node<S> next() {
public CostNode<S,T> next() {
// Take the next node
HeuristicNode<S> current = dequeue();
CostNode<S,T> current = dequeue();
// Calculate distances to each neighbor
S currentState = current.transition().to();
for(Transition<S> successor : this.transition.from(currentState)){
// Create the successor node
HeuristicNode<S> successorNode = this.builder.node(current, successor);
CostNode<S,T> successorNode = this.factory.node(current, successor);
// Check if there is any improvement in the old cost
HeuristicNode<S> previousNode = this.explored.get(successor.to());
CostNode<S,T> previousNode = this.explored.get(successor.to());
if (previousNode != null){
// Check both paths. If the new path is better than the previous
// path, update and enqueue. Else, discard this node.
//if (comparator.compare(successorNode, previousNode) <= 0){
if (successorNode.compareByCost(previousNode) < 0){
if (successorNode.getCost().compareTo(previousNode.getCost())<0){
//if (successorNode.compareByCost(previousNode) < 0){
// Replace the worst version and re-enqueue (if not in queue)
enqueue(successorNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import es.usc.citius.lab.hipster.node.AbstractNode;
import es.usc.citius.lab.hipster.node.Node;
import es.usc.citius.lab.hipster.node.Transition;
import es.usc.citius.lab.hipster.node.informed.HeuristicNode;
import es.usc.citius.lab.hipster.util.Scalable;

/**
Expand All @@ -28,7 +29,7 @@
* @since 16-04-2013
* @version 1.0
*/
public class ADStarNode<S, T extends Scalable<T>> extends AbstractNode<S> implements Comparable<ADStarNode<S, T>> {
public class ADStarNode<S, T extends Scalable<T>> extends AbstractNode<S> implements Comparable<ADStarNode<S, T>>, HeuristicNode<S, T> {

protected T g;
protected T v;
Expand Down Expand Up @@ -131,4 +132,12 @@ public int compareTo(Key<T> o) {
}
}
}

public T getCost() {
return this.g;
}

public T getScore() {
return this.v;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package es.usc.citius.lab.hipster.node.astar;

public class CostNodeFactory {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import es.usc.citius.lab.hipster.function.HeuristicFunction;
import es.usc.citius.lab.hipster.node.NodeFactory;
import es.usc.citius.lab.hipster.node.Transition;
import es.usc.citius.lab.hipster.node.informed.CostNode;
import es.usc.citius.lab.hipster.node.informed.HeuristicNode;
import es.usc.citius.lab.hipster.node.informed.InformedNode;
import es.usc.citius.lab.hipster.util.Operable;
Expand All @@ -21,6 +22,16 @@ public InformedNodeFactory(CostFunction<S,T> costFunction, HeuristicFunction<S,
this.defaultValue = defaultValue;
}

public InformedNodeFactory(CostFunction<S,T> costFunction, final T defaultValue){
this.gf = costFunction;
this.hf = new HeuristicFunction<S, T>() {
public T estimate(S state) {
return defaultValue;
}
};
this.defaultValue = defaultValue;
}

public HeuristicNode<S, T> node(HeuristicNode<S, T> from,
Transition<S> transition) {
T newCost, newScore;
Expand All @@ -34,5 +45,15 @@ public HeuristicNode<S, T> node(HeuristicNode<S, T> from,
}
return new InformedNode<S, T>(transition, from, newCost, newCost.add(newScore));
}

public NodeFactory<S, CostNode<S,T>> toCostNodeFactory(){
final NodeFactory<S, HeuristicNode<S,T>> factory = this;
return new NodeFactory<S, CostNode<S,T>>() {
public CostNode<S, T> node(CostNode<S, T> from,
Transition<S> transition) {
return factory.node((HeuristicNode<S, T>) from, transition);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
import es.usc.citius.lab.hipster.node.Transition;
import es.usc.citius.lab.hipster.node.astar.HeuristicNumericNode;
import es.usc.citius.lab.hipster.node.astar.HeuristicNumericNodeBuilder;
import es.usc.citius.lab.hipster.node.astar.InformedNodeFactory;
import es.usc.citius.lab.hipster.node.informed.CostNode;
import es.usc.citius.lab.hipster.testutils.AlgorithmIteratorFromMazeCreator;
import es.usc.citius.lab.hipster.testutils.JungDirectedGraphFromMazeCreator;
import es.usc.citius.lab.hipster.testutils.JungEdge;
import es.usc.citius.lab.hipster.testutils.MazeSearch;
import es.usc.citius.lab.hipster.util.DoubleOperable;
import es.usc.citius.lab.hipster.util.maze.Maze2D;

/**
Expand Down Expand Up @@ -115,18 +118,20 @@ public Iterable<Transition<String>> from(String current) {
}
};

NodeFactory<String, HeuristicNode<String>> builder = new HeuristicNumericNodeBuilder<String>(
new CostFunction<String, Double>() {
public Double evaluate(
Transition<String> transition) {
if (transition.from()==null){
return 0d;
}
return graph.findEdge(transition.from(), transition.to()).getCost();
}
});
NodeFactory<String, CostNode<String, DoubleOperable>> factory = new InformedNodeFactory<String, DoubleOperable>(new CostFunction<String, DoubleOperable>() {
public DoubleOperable evaluate(Transition<String> transition) {
if (transition.from()==null){
return DoubleOperable.MIN;
}
return new DoubleOperable(graph.findEdge(transition.from(), transition.to()).getCost());
}


}, DoubleOperable.MIN).toCostNodeFactory();



BellmanFord<String> it = new BellmanFord<String>("A", transition, builder, null);
BellmanFord<String, DoubleOperable> it = new BellmanFord<String, DoubleOperable>("A", transition, factory);
while(it.hasNext()){
Node<String> edgeNode = it.next();
System.out.println("Exploring " + edgeNode.transition().from() + "->" + edgeNode.transition().to());
Expand All @@ -150,7 +155,7 @@ public Double evaluate(

private void execute(Maze2D maze, boolean heuristic)
throws InterruptedException {
BellmanFord<Point> it = AlgorithmIteratorFromMazeCreator.bellmanFord(
BellmanFord<Point,DoubleOperable> it = AlgorithmIteratorFromMazeCreator.bellmanFord(
maze, heuristic);
DirectedGraph<Point, JungEdge<Point>> graph = JungDirectedGraphFromMazeCreator
.create(maze);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Result evaluate() {

// Bellman-Ford
bench.add("Hipster-Bellman-Ford", new Algorithm() {
BellmanFord<Point> it; Maze2D maze;
BellmanFord<Point, DoubleOperable> it; Maze2D maze;
public void initialize(Maze2D maze) {
it= AlgorithmIteratorFromMazeCreator.bellmanFord(maze, false);
this.maze = maze;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import es.usc.citius.lab.hipster.node.adstar.ADStarNodeBuilder;
import es.usc.citius.lab.hipster.node.Transition;
import es.usc.citius.lab.hipster.node.astar.HeuristicNumericNodeBuilder;
import es.usc.citius.lab.hipster.node.astar.InformedNodeFactory;
import es.usc.citius.lab.hipster.util.DoubleOperable;
import es.usc.citius.lab.hipster.util.maze.Maze2D;
import java.awt.Point;
Expand Down Expand Up @@ -94,15 +95,14 @@ public static ADStar<Point, DoubleOperable> adstar(final Maze2D maze) {
return adstar(maze, false);
}

public static BellmanFord<Point> bellmanFord(final Maze2D maze, boolean useHeuristic) {
public static BellmanFord<Point,DoubleOperable> bellmanFord(final Maze2D maze, boolean useHeuristic) {
CostFunction<Point, DoubleOperable> cost = defaultCostFunction();

TransitionFunction<Point> transition = defaultTransitionFunction(maze);

BellmanFord<Point> it;
BellmanFord<Point,DoubleOperable> it;

//it = new BellmanFord<Point>(maze.getInitialLoc(), transition, new HeuristicNumericNodeBuilder<Point>(cost), null);
it = null;
it = new BellmanFord<Point,DoubleOperable>(maze.getInitialLoc(), transition, new InformedNodeFactory<Point, DoubleOperable>(cost, DoubleOperable.MIN).toCostNodeFactory());
return it;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
import edu.uci.ics.jung.graph.DirectedGraph;
import es.usc.citius.lab.hipster.node.Node;
import es.usc.citius.lab.hipster.node.informed.CostNode;
import es.usc.citius.lab.hipster.node.informed.HeuristicNode;
import es.usc.citius.lab.hipster.util.DoubleCostEvaluator;
import es.usc.citius.lab.hipster.util.DoubleOperable;
Expand Down Expand Up @@ -170,16 +171,16 @@ public boolean equals(Object obj) {
}

//public static Result executePrintIteratorSearch(AStar<Point> it, StringMaze maze) throws InterruptedException {
public static Result executePrintIteratorSearch(Iterator<HeuristicNode<Point, DoubleOperable>> it, Maze2D maze) throws InterruptedException {
public static Result executePrintIteratorSearch(Iterator<? extends CostNode<Point, DoubleOperable>> it, Maze2D maze) throws InterruptedException {
return executePrintIteratorSearch(it, maze, true);
}

public static Result executePrintIteratorSearch(Iterator<HeuristicNode<Point, DoubleOperable>> it, Maze2D maze, boolean exitWhenGoalReached) throws InterruptedException {
public static Result executePrintIteratorSearch(Iterator<? extends CostNode<Point, DoubleOperable>> it, Maze2D maze, boolean exitWhenGoalReached) throws InterruptedException {
int steps = 0;
Result r = null;
Collection<Point> explored = new HashSet<Point>();
while (it.hasNext()) {
HeuristicNode<Point, DoubleOperable> currentNode = it.next();
CostNode<Point, DoubleOperable> currentNode = it.next();
explored.add(currentNode.transition().to());
steps++;
List<Node<Point>> nodePath = currentNode.path();
Expand Down Expand Up @@ -228,10 +229,10 @@ public static String getMazeStringSolution(Maze2D maze, Collection<Point> explor

//public static Result executeIteratorSearch(AStar<Point> it, StringMaze maze) {

public static Result executeIteratorSearch(Iterator<HeuristicNode<Point, DoubleOperable>> it, Maze2D maze) {
public static Result executeIteratorSearch(Iterator<? extends CostNode<Point, DoubleOperable>> it, Maze2D maze) {
int steps = 0;
while (it.hasNext()) {
HeuristicNode<Point, DoubleOperable> currentNode = it.next();
CostNode<Point, DoubleOperable> currentNode = it.next();
steps++;
if (currentNode.transition().to().equals(maze.getGoalLoc())) {
List<Node<Point>> nodePath = currentNode.path();
Expand Down

0 comments on commit 6ef9863

Please sign in to comment.