001    package aima.search.demos;
002    
003    import aima.basic.BasicEnvironmentView;
004    import aima.search.framework.GraphSearch;
005    import aima.search.framework.HeuristicFunction;
006    import aima.search.framework.TreeSearch;
007    import aima.search.informed.AStarEvaluationFunction;
008    import aima.search.informed.RecursiveBestFirstSearch;
009    import aima.search.map.ExtendableMap;
010    import aima.search.map.MapAgent;
011    import aima.search.map.MapEnvironment;
012    import aima.search.map.Point2D;
013    import aima.search.map.SimplifiedRoadMapOfPartOfRomania;
014    import aima.search.uninformed.BidirectionalSearch;
015    import aima.search.uninformed.BreadthFirstSearch;
016    import aima.search.uninformed.DepthFirstSearch;
017    import aima.search.uninformed.DepthLimitedSearch;
018    import aima.search.uninformed.IterativeDeepeningSearch;
019    import aima.search.uninformed.UniformCostSearch;
020    
021    /**
022     * @author Ciaran O'Reilly
023     * 
024     */
025    public class MapDemo {
026    
027            public static void main(String[] args) {
028                    newMapDemo();
029            }
030    
031            private static void newMapDemo() {
032                    mapWithBreadthFirstSearch();
033                    mapWithUniformCostSearch();
034                    mapWithDepthFirstSearch();
035                    mapWithRecursiveDLS();
036                    mapWithIterativeDeepeningSearch();
037                    mapWithBidrectionalSearch();
038                    mapWithRecursiveBestFirstSearch();
039            }
040    
041            private static void mapWithBreadthFirstSearch() {
042                    System.out.println("\nMapDemo BFS -->");
043    
044                    MapEnvironment me = new MapEnvironment(
045                                    new SimplifiedRoadMapOfPartOfRomania());
046                    MapAgent ma = new MapAgent(me,
047                                    new BreadthFirstSearch(new GraphSearch()), 2);
048                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
049                    me.registerView(new BasicEnvironmentView());
050                    me.stepUntilDone();
051            }
052    
053            private static void mapWithUniformCostSearch() {
054                    // Both Tree and Graph Search should find the same route (for this
055                    // problem), however, the number
056                    // of nodes expanded etc... will more than likely differ
057                    System.out.println("\nMapDemo UCS (using a TreeSearch) -->");
058    
059                    MapEnvironment me = new MapEnvironment(
060                                    new SimplifiedRoadMapOfPartOfRomania());
061                    MapAgent ma = new MapAgent(me, new UniformCostSearch(new TreeSearch()),
062                                    new String[] { SimplifiedRoadMapOfPartOfRomania.BUCHAREST });
063                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
064                    me.registerView(new BasicEnvironmentView());
065                    me.stepUntilDone();
066    
067                    System.out.println("\nMapDemo UCS (using a GraphSearch) -->");
068    
069                    me = new MapEnvironment(new SimplifiedRoadMapOfPartOfRomania());
070                    ma = new MapAgent(me, new UniformCostSearch(new GraphSearch()),
071                                    new String[] { SimplifiedRoadMapOfPartOfRomania.BUCHAREST });
072                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
073                    me.registerView(new BasicEnvironmentView());
074                    me.stepUntilDone();
075            }
076    
077            private static void mapWithDepthFirstSearch() {
078                    System.out.println("\nMapDemo DFS -->");
079    
080                    MapEnvironment me = new MapEnvironment(
081                                    new SimplifiedRoadMapOfPartOfRomania());
082                    MapAgent ma = new MapAgent(me, new DepthFirstSearch(new GraphSearch()),
083                                    2);
084                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
085                    me.registerView(new BasicEnvironmentView());
086                    me.stepUntilDone();
087            }
088    
089            private static void mapWithRecursiveDLS() {
090                    System.out.println("\nMapDemo recursive DLS -->");
091    
092                    MapEnvironment me = new MapEnvironment(
093                                    new SimplifiedRoadMapOfPartOfRomania());
094                    MapAgent ma = new MapAgent(me, new DepthLimitedSearch(8), 2);
095                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
096                    me.registerView(new BasicEnvironmentView());
097                    me.stepUntilDone();
098            }
099    
100            private static void mapWithIterativeDeepeningSearch() {
101                    System.out.println("\nMapDemo Iterative DS  -->");
102    
103                    MapEnvironment me = new MapEnvironment(
104                                    new SimplifiedRoadMapOfPartOfRomania());
105                    MapAgent ma = new MapAgent(me, new IterativeDeepeningSearch(), 2);
106                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
107                    me.registerView(new BasicEnvironmentView());
108                    me.stepUntilDone();
109            }
110    
111            private static void mapWithBidrectionalSearch() {
112                    System.out.println("\nMapDemo Bidirectional Search  -->");
113    
114                    MapEnvironment me = new MapEnvironment(
115                                    new SimplifiedRoadMapOfPartOfRomania());
116                    MapAgent ma = new MapAgent(me, new BidirectionalSearch(),
117                                    new String[] { SimplifiedRoadMapOfPartOfRomania.BUCHAREST });
118                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ORADEA);
119                    me.registerView(new BasicEnvironmentView());
120                    me.stepUntilDone();
121            }
122    
123            private static void mapWithRecursiveBestFirstSearch() {
124                    System.out.println("\nMapDemo RecursiveBestFirstSearch Search  -->");
125    
126                    MapEnvironment me = new MapEnvironment(
127                                    new SimplifiedRoadMapOfPartOfRomania());
128                    MapAgent ma = new MapAgent(me, new RecursiveBestFirstSearch(
129                                    new AStarEvaluationFunction()),
130                                    new String[] { SimplifiedRoadMapOfPartOfRomania.BUCHAREST });
131                    ma.setHeuristicFunction(new HeuristicFunction() {
132                            ExtendableMap map = new SimplifiedRoadMapOfPartOfRomania();
133    
134                            public double getHeuristicValue(Object state) {
135                                    Point2D pt1 = map.getPosition((String) state);
136                                    Point2D pt2 = map.getPosition(SimplifiedRoadMapOfPartOfRomania.BUCHAREST);
137                                    return pt1.distance(pt2);
138                            }
139                    });
140    
141                    me.addAgent(ma, SimplifiedRoadMapOfPartOfRomania.ARAD);
142                    me.registerView(new BasicEnvironmentView());
143                    me.stepUntilDone();
144            }
145    }