forked from citiususc/hipster
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestUtils completely rewritten. Now it's possible to test all the
algorithms using the Maze2D or the jung-graph. Close citiususc#44
- Loading branch information
Pablo Rodríguez Mier
committed
Apr 21, 2013
1 parent
6ef9863
commit 898e3ce
Showing
13 changed files
with
351 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/es/usc/citius/lab/hipster/testutils/ADStarIteratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package es.usc.citius.lab.hipster.testutils; | ||
|
||
|
||
import java.util.Iterator; | ||
|
||
import es.usc.citius.lab.hipster.algorithm.ADStar; | ||
import es.usc.citius.lab.hipster.node.NodeFactory; | ||
import es.usc.citius.lab.hipster.node.adstar.ADStarNode; | ||
import es.usc.citius.lab.hipster.node.adstar.ADStarNodeBuilder; | ||
import es.usc.citius.lab.hipster.node.adstar.ADStarNodeUpdater; | ||
import es.usc.citius.lab.hipster.node.informed.CostNode; | ||
import es.usc.citius.lab.hipster.util.Scalable; | ||
|
||
public class ADStarIteratorFactory<S, T extends Scalable<T>> implements | ||
AlgorithmIteratorFactory<S, T> { | ||
private final SearchComponentFactory<S, T> f; | ||
|
||
public ADStarIteratorFactory(SearchComponentFactory<S, T> componentFactory) { | ||
this.f = componentFactory; | ||
} | ||
|
||
public Iterator<? extends CostNode<S, T>> buildIteratorSearch() { | ||
|
||
NodeFactory<S, ADStarNode<S, T>> defaultBuilder = new ADStarNodeBuilder<S, T>( | ||
f.getDefaultValue(), f.getMaxValue()); | ||
|
||
ADStarNodeUpdater<S, T> updater = new ADStarNodeUpdater<S, T>( | ||
f.getCostFunction(), f.getHeuristicFunction(), 1.0, | ||
f.getMaxValue()); | ||
|
||
return new ADStar<S, T>(f.getInitialState(), f.getGoalState(), | ||
f.getTransitionFunction(), f.getTransitionFunction(), | ||
defaultBuilder, updater); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/es/usc/citius/lab/hipster/testutils/AStarIteratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package es.usc.citius.lab.hipster.testutils; | ||
|
||
import java.util.Iterator; | ||
|
||
import es.usc.citius.lab.hipster.algorithm.AStar; | ||
import es.usc.citius.lab.hipster.node.NodeFactory; | ||
import es.usc.citius.lab.hipster.node.astar.InformedNodeFactory; | ||
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.Operable; | ||
|
||
public class AStarIteratorFactory<S, T extends Operable<T>> implements | ||
AlgorithmIteratorFactory<S, T> { | ||
private final SearchComponentFactory<S, T> f; | ||
|
||
public AStarIteratorFactory( | ||
SearchComponentFactory<S, T> componentFactory) { | ||
this.f = componentFactory; | ||
} | ||
|
||
public Iterator<? extends CostNode<S, T>> buildIteratorSearch() { | ||
NodeFactory<S, HeuristicNode<S, T>> factory = new InformedNodeFactory<S, T>( | ||
f.getCostFunction(), | ||
f.getHeuristicFunction(), | ||
f.getDefaultValue()); | ||
|
||
return new AStar<S, T>(f.getInitialState(), f.getTransitionFunction(), factory); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/es/usc/citius/lab/hipster/testutils/AlgorithmIteratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package es.usc.citius.lab.hipster.testutils; | ||
|
||
import java.util.Iterator; | ||
|
||
import es.usc.citius.lab.hipster.node.informed.CostNode; | ||
import es.usc.citius.lab.hipster.util.Operable; | ||
|
||
|
||
|
||
public interface AlgorithmIteratorFactory<S, T extends Operable<T>> { | ||
|
||
Iterator<? extends CostNode<S,T>> buildIteratorSearch(); | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/es/usc/citius/lab/hipster/testutils/BellmanFordIteratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package es.usc.citius.lab.hipster.testutils; | ||
|
||
import java.util.Iterator; | ||
|
||
import es.usc.citius.lab.hipster.algorithm.BellmanFord; | ||
import es.usc.citius.lab.hipster.node.NodeFactory; | ||
import es.usc.citius.lab.hipster.node.astar.InformedNodeFactory; | ||
import es.usc.citius.lab.hipster.node.informed.CostNode; | ||
import es.usc.citius.lab.hipster.util.Operable; | ||
|
||
public class BellmanFordIteratorFactory<S, T extends Operable<T>> implements | ||
AlgorithmIteratorFactory<S, T> { | ||
private final SearchComponentFactory<S, T> componentFactory; | ||
|
||
public BellmanFordIteratorFactory( | ||
SearchComponentFactory<S, T> componentFactory) { | ||
this.componentFactory = componentFactory; | ||
} | ||
|
||
public Iterator<? extends CostNode<S, T>> buildIteratorSearch() { | ||
NodeFactory<S, CostNode<S, T>> factory = new InformedNodeFactory<S, T>( | ||
componentFactory.getCostFunction(), componentFactory.getDefaultValue()) | ||
.toCostNodeFactory(); | ||
|
||
return new BellmanFord<S, T>( | ||
componentFactory.getInitialState(), | ||
componentFactory.getTransitionFunction(), factory); | ||
|
||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
src/test/java/es/usc/citius/lab/hipster/testutils/JungMazeGraphComponentFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package es.usc.citius.lab.hipster.testutils; | ||
|
||
import java.awt.Point; | ||
|
||
import edu.uci.ics.jung.graph.DirectedGraph; | ||
import edu.uci.ics.jung.graph.DirectedSparseGraph; | ||
import es.usc.citius.lab.hipster.function.CostFunction; | ||
import es.usc.citius.lab.hipster.function.HeuristicFunction; | ||
import es.usc.citius.lab.hipster.function.TransitionFunction; | ||
import es.usc.citius.lab.hipster.node.Transition; | ||
import es.usc.citius.lab.hipster.util.DoubleOperable; | ||
import es.usc.citius.lab.hipster.util.maze.Maze2D; | ||
|
||
public class JungMazeGraphComponentFactory implements SearchComponentFactory<Point, DoubleOperable> { | ||
private final DirectedGraph<Point, JungEdge<Point>> graph; | ||
private final Maze2D maze; | ||
private boolean useHeuristic; | ||
|
||
public JungMazeGraphComponentFactory(Maze2D maze, boolean useHeuristic){ | ||
this.maze = maze; | ||
this.graph = createGraphFrom(maze); | ||
this.useHeuristic = useHeuristic; | ||
} | ||
|
||
public TransitionFunction<Point> getTransitionFunction() { | ||
return new TransitionFunction<Point>() { | ||
public Iterable<Transition<Point>> from(Point current) { | ||
return Transition.map(current, graph.getNeighbors(current)); | ||
} | ||
}; | ||
} | ||
|
||
public CostFunction<Point, DoubleOperable> getCostFunction() { | ||
return new CostFunction<Point, DoubleOperable>() { | ||
public DoubleOperable evaluate(Transition<Point> transition) { | ||
return new DoubleOperable(graph.findEdge(transition.from(), transition.to()).getCost()); | ||
} | ||
}; | ||
} | ||
|
||
public HeuristicFunction<Point, DoubleOperable> getHeuristicFunction() { | ||
return new HeuristicFunction<Point, DoubleOperable>() { | ||
public DoubleOperable estimate(Point state) { | ||
if (useHeuristic){ | ||
return new DoubleOperable(state.distance(maze.getGoalLoc())); | ||
} else { | ||
return DoubleOperable.MIN; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public Point getInitialState() { | ||
return maze.getInitialLoc(); | ||
} | ||
|
||
public Point getGoalState() { | ||
return maze.getGoalLoc(); | ||
} | ||
|
||
public static DirectedGraph<Point, JungEdge<Point>> createGraphFrom(Maze2D maze) { | ||
// Create a graph from maze | ||
DirectedGraph<Point, JungEdge<Point>> graph = new DirectedSparseGraph<Point, JungEdge<Point>>(); | ||
// Convert maze to graph. For each cell, add all valid neighbors with | ||
// their costs | ||
for (Point source : maze.getMazePoints()) { | ||
if (!graph.containsVertex(source)) { | ||
graph.addVertex(source); | ||
} | ||
for (Point dest : maze.validLocationsFrom(source)) { | ||
if (!graph.containsVertex(dest)) { | ||
graph.addVertex(dest); | ||
} | ||
double edgeCost = Math.sqrt((source.x - dest.x) | ||
* (source.x - dest.x) + (source.y - dest.y) | ||
* (source.y - dest.y)); | ||
JungEdge<Point> e = new JungEdge<Point>(source, dest, edgeCost); | ||
if (!graph.containsEdge(e)) { | ||
graph.addEdge(e, source, dest); | ||
} | ||
} | ||
} | ||
return graph; | ||
} | ||
|
||
public DoubleOperable getDefaultValue() { | ||
return DoubleOperable.MIN; | ||
} | ||
|
||
public DoubleOperable getMaxValue() { | ||
return DoubleOperable.MAX; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.