001    /*
002     * Created on Sep 12, 2004
003     *
004     */
005    package aima.search.demos;
006    
007    import java.util.Iterator;
008    import java.util.List;
009    import java.util.Properties;
010    
011    import aima.search.eightpuzzle.EightPuzzleBoard;
012    import aima.search.eightpuzzle.EightPuzzleGoalTest;
013    import aima.search.eightpuzzle.EightPuzzleSuccessorFunction;
014    import aima.search.eightpuzzle.ManhattanHeuristicFunction;
015    import aima.search.eightpuzzle.MisplacedTilleHeuristicFunction;
016    import aima.search.framework.GraphSearch;
017    import aima.search.framework.Problem;
018    import aima.search.framework.Search;
019    import aima.search.framework.SearchAgent;
020    import aima.search.informed.AStarSearch;
021    import aima.search.informed.GreedyBestFirstSearch;
022    import aima.search.informed.SimulatedAnnealingSearch;
023    import aima.search.uninformed.DepthLimitedSearch;
024    import aima.search.uninformed.IterativeDeepeningSearch;
025    
026    /**
027     * @author Ravi Mohan
028     * 
029     */
030    
031    public class EightPuzzleDemo {
032            static EightPuzzleBoard boardWithThreeMoveSolution = new EightPuzzleBoard(
033                            new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });;
034    
035            static EightPuzzleBoard random1 = new EightPuzzleBoard(new int[] { 1, 4, 2,
036                            7, 5, 8, 3, 0, 6 });
037    
038            static EightPuzzleBoard extreme = new EightPuzzleBoard(new int[] { 0, 8, 7,
039                            6, 5, 4, 3, 2, 1 });
040    
041            public static void main(String[] args) {
042                    eightPuzzleDLSDemo();
043                    eightPuzzleIDLSDemo();
044                    eightPuzzleGreedyBestFirstDemo();
045                    eightPuzzleGreedyBestFirstManhattanDemo();
046                    eightPuzzleAStarDemo();
047                    eightPuzzleAStarManhattanDemo();
048                    eightPuzzleSimulatedAnnealingDemo();
049            }
050    
051            private static void eightPuzzleDLSDemo() {
052                    System.out.println("\nEightPuzzleDemo recursive DLS -->");
053                    try {
054                            Problem problem = new Problem(random1,
055                                            new EightPuzzleSuccessorFunction(),
056                                            new EightPuzzleGoalTest());
057                            Search search = new DepthLimitedSearch(9);
058                            SearchAgent agent = new SearchAgent(problem, search);
059                            printActions(agent.getActions());
060                            printInstrumentation(agent.getInstrumentation());
061                    } catch (Exception e) {
062                            e.printStackTrace();
063                    }
064    
065            }
066    
067            private static void eightPuzzleIDLSDemo() {
068                    System.out.println("\nEightPuzzleDemo Iterative DLS -->");
069                    try {
070                            Problem problem = new Problem(random1,
071                                            new EightPuzzleSuccessorFunction(),
072                                            new EightPuzzleGoalTest());
073                            Search search = new IterativeDeepeningSearch();
074                            SearchAgent agent = new SearchAgent(problem, search);
075                            printActions(agent.getActions());
076                            printInstrumentation(agent.getInstrumentation());
077                    } catch (Exception e) {
078                            e.printStackTrace();
079                    }
080    
081            }
082    
083            private static void eightPuzzleGreedyBestFirstDemo() {
084                    System.out
085                                    .println("\nEightPuzzleDemo Greedy Best First Search (MisplacedTileHeursitic)-->");
086                    try {
087                            Problem problem = new Problem(boardWithThreeMoveSolution,
088                                            new EightPuzzleSuccessorFunction(),
089                                            new EightPuzzleGoalTest(),
090                                            new MisplacedTilleHeuristicFunction());
091                            Search search = new GreedyBestFirstSearch(new GraphSearch());
092                            SearchAgent agent = new SearchAgent(problem, search);
093                            printActions(agent.getActions());
094                            printInstrumentation(agent.getInstrumentation());
095                    } catch (Exception e) {
096                            e.printStackTrace();
097                    }
098    
099            }
100    
101            private static void eightPuzzleGreedyBestFirstManhattanDemo() {
102                    System.out
103                                    .println("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->");
104                    try {
105                            Problem problem = new Problem(boardWithThreeMoveSolution,
106                                            new EightPuzzleSuccessorFunction(),
107                                            new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
108                            Search search = new GreedyBestFirstSearch(new GraphSearch());
109                            SearchAgent agent = new SearchAgent(problem, search);
110                            printActions(agent.getActions());
111                            printInstrumentation(agent.getInstrumentation());
112                    } catch (Exception e) {
113                            e.printStackTrace();
114                    }
115    
116            }
117    
118            private static void eightPuzzleAStarDemo() {
119                    System.out
120                                    .println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
121                    try {
122                            Problem problem = new Problem(random1,
123                                            new EightPuzzleSuccessorFunction(),
124                                            new EightPuzzleGoalTest(),
125                                            new MisplacedTilleHeuristicFunction());
126                            Search search = new AStarSearch(new GraphSearch());
127                            SearchAgent agent = new SearchAgent(problem, search);
128                            printActions(agent.getActions());
129                            printInstrumentation(agent.getInstrumentation());
130                    } catch (Exception e) {
131                            e.printStackTrace();
132                    }
133    
134            }
135    
136            private static void eightPuzzleSimulatedAnnealingDemo() {
137                    System.out.println("\nEightPuzzleDemo Simulated Annealing  Search -->");
138                    try {
139                            Problem problem = new Problem(random1,
140                                            new EightPuzzleSuccessorFunction(),
141                                            new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
142                            SimulatedAnnealingSearch search = new SimulatedAnnealingSearch();
143                            SearchAgent agent = new SearchAgent(problem, search);
144                            printActions(agent.getActions());
145                            System.out.println("Search Outcome=" + search.getOutcome());
146                            System.out.println("Final State=\n" + search.getLastSearchState());
147                            printInstrumentation(agent.getInstrumentation());
148                    } catch (Exception e) {
149                            e.printStackTrace();
150                    }
151            }
152    
153            private static void eightPuzzleAStarManhattanDemo() {
154                    System.out
155                                    .println("\nEightPuzzleDemo AStar Search (ManhattanHeursitic)-->");
156                    try {
157                            Problem problem = new Problem(random1,
158                                            new EightPuzzleSuccessorFunction(),
159                                            new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
160                            Search search = new AStarSearch(new GraphSearch());
161                            SearchAgent agent = new SearchAgent(problem, search);
162                            printActions(agent.getActions());
163                            printInstrumentation(agent.getInstrumentation());
164                    } catch (Exception e) {
165                            e.printStackTrace();
166                    }
167    
168            }
169    
170            private static void printInstrumentation(Properties properties) {
171                    Iterator keys = properties.keySet().iterator();
172                    while (keys.hasNext()) {
173                            String key = (String) keys.next();
174                            String property = properties.getProperty(key);
175                            System.out.println(key + " : " + property);
176                    }
177    
178            }
179    
180            private static void printActions(List actions) {
181                    for (int i = 0; i < actions.size(); i++) {
182                            String action = (String) actions.get(i);
183                            System.out.println(action);
184                    }
185            }
186    
187    }