package aima.gui.demo.search;
import java.util.List;
import java.util.Properties;
import aima.core.agent.Action;
import aima.core.environment.eightpuzzle.EightPuzzleBoard;
import aima.core.environment.eightpuzzle.EightPuzzleFunctionFactory;
import aima.core.environment.eightpuzzle.EightPuzzleGoalTest;
import aima.core.environment.eightpuzzle.ManhattanHeuristicFunction;
import aima.core.environment.eightpuzzle.MisplacedTilleHeuristicFunction;
import aima.core.search.framework.SearchAgent;
import aima.core.search.framework.SearchForActions;
import aima.core.search.framework.problem.Problem;
import aima.core.search.framework.qsearch.GraphSearch;
import aima.core.search.informed.AStarSearch;
import aima.core.search.informed.GreedyBestFirstSearch;
import aima.core.search.local.SimulatedAnnealingSearch;
import aima.core.search.uninformed.DepthLimitedSearch;
import aima.core.search.uninformed.IterativeDeepeningSearch;
/**
* @author Ravi Mohan
* @author Ruediger Lunde
*
*/
public class EightPuzzleDemo {
private static EightPuzzleBoard boardWithThreeMoveSolution =
new EightPuzzleBoard(new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });
private static EightPuzzleBoard random1 =
new EightPuzzleBoard(new int[] { 1, 4, 2, 7, 5, 8, 3, 0, 6 });
private static EightPuzzleBoard extreme =
new EightPuzzleBoard(new int[] { 0, 8, 7, 6, 5, 4, 3, 2, 1 });
public static void main(String[] args) {
eightPuzzleDLSDemo();
eightPuzzleIDLSDemo();
eightPuzzleGreedyBestFirstDemo();
eightPuzzleGreedyBestFirstManhattanDemo();
eightPuzzleAStarDemo();
eightPuzzleAStarManhattanDemo();
eightPuzzleSimulatedAnnealingDemo();
}
private static void eightPuzzleDLSDemo() {
System.out.println("\nEightPuzzleDemo recursive DLS (9) -->");
try {
Problem problem = new Problem(boardWithThreeMoveSolution, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new DepthLimitedSearch(9);
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleIDLSDemo() {
System.out.println("\nEightPuzzleDemo Iterative DLS -->");
try {
Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new IterativeDeepeningSearch();
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstDemo() {
System.out.println("\nEightPuzzleDemo Greedy Best First Search (MisplacedTileHeursitic)-->");
try {
Problem problem = new Problem(boardWithThreeMoveSolution, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new GreedyBestFirstSearch(new GraphSearch(),
new MisplacedTilleHeuristicFunction());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleGreedyBestFirstManhattanDemo() {
System.out.println("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->");
try {
Problem problem = new Problem(boardWithThreeMoveSolution, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new GreedyBestFirstSearch(new GraphSearch(), new ManhattanHeuristicFunction());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarDemo() {
System.out.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
try {
Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new AStarSearch(new GraphSearch(), new MisplacedTilleHeuristicFunction());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleSimulatedAnnealingDemo() {
System.out.println("\nEightPuzzleDemo Simulated Annealing Search -->");
try {
Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SimulatedAnnealingSearch search = new SimulatedAnnealingSearch(new ManhattanHeuristicFunction());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
System.out.println("Search Outcome=" + search.getOutcome());
System.out.println("Final State=\n" + search.getLastSearchState());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void eightPuzzleAStarManhattanDemo() {
System.out.println("\nEightPuzzleDemo AStar Search (ManhattanHeursitic)-->");
try {
Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(),
EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
SearchForActions search = new AStarSearch(new GraphSearch(), new ManhattanHeuristicFunction());
SearchAgent agent = new SearchAgent(problem, search);
printActions(agent.getActions());
printInstrumentation(agent.getInstrumentation());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printInstrumentation(Properties properties) {
for (Object o : properties.keySet()) {
String key = (String) o;
String property = properties.getProperty(key);
System.out.println(key + " : " + property);
}
}
private static void printActions(List<Action> actions) {
for (Action action : actions)
System.out.println(action.toString());
}
}