Advertisement
claukiller

8puzzlenetero

Oct 27th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.52 KB | None | 0 0
  1. package aima.gui.demo.search;
  2.  
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Properties;
  6.  
  7. import aima.core.agent.Action;
  8. import aima.core.environment.eightpuzzle.EightPuzzleBoard;
  9. import aima.core.environment.eightpuzzle.EightPuzzleFunctionFactory;
  10. import aima.core.environment.eightpuzzle.EightPuzzleGoalTest;
  11. import aima.core.environment.eightpuzzle.Heuristic12;
  12. import aima.core.search.framework.HeuristicFunction;
  13. import aima.core.environment.eightpuzzle.ManhattanHeuristicFunction;
  14. import aima.core.environment.eightpuzzle.MisplacedTilleHeuristicFunction;
  15. import aima.core.environment.eightpuzzle.NoConsistentHeuristicFunction;
  16. import aima.core.environment.eightpuzzle.NullHeuristicFunction;
  17. import aima.core.search.framework.Problem;
  18. import aima.core.search.framework.QSearch;
  19. import aima.core.search.framework.Search;
  20. import aima.core.search.framework.SearchAgent;
  21. import aima.core.search.framework.qsearch.TreeSearch;
  22. import aima.core.search.framework.qsearch.GraphSearch;
  23. import aima.core.search.framework.qsearch.GraphSearchRectifyExpanded;
  24. import aima.core.search.framework.qsearch.GraphSearchReinsertExpanded;
  25. import aima.core.search.framework.qsearch.QueueSearch;
  26. import aima.core.search.informed.AStarSearch;
  27. import aima.core.search.informed.GreedyBestFirstSearch;
  28. import aima.core.search.uninformed.BreadthFirstSearch;
  29. import aima.core.search.uninformed.DepthFirstSearch;
  30. import aima.core.search.uninformed.DepthLimitedSearch;
  31. import aima.core.search.uninformed.IterativeDeepeningSearch;
  32. import aima.core.search.uninformed.UniformCostSearch;
  33.  
  34. /**
  35. * @author Ravi Mohan
  36. * Ultima modificacion: 05/09/2016
  37. *
  38. */
  39.  
  40. public class EightPuzzleDemo {
  41. // Este es el estado inicial
  42. public static EightPuzzleBoard initialState = new EightPuzzleBoard(new int[]
  43. // Banco de ejemplos para el 8-puzzle
  44.  
  45. // Coste 5
  46. // {1, 0, 3, 8, 2, 5, 7, 4, 6}
  47. // {1, 4, 2, 0, 8, 3, 7, 6, 5}
  48. // {1, 3, 4, 8, 6, 2, 7, 0, 5}
  49. // {1, 2, 3, 7, 8, 0, 6, 5, 4}
  50. // {1, 4, 2, 8, 3, 0, 7, 6, 5}
  51. // {1, 2, 3, 0, 8, 6, 7, 5, 4}
  52. // {1, 2, 3, 0, 4, 5, 8, 7, 6}
  53. // {1, 0, 2, 8, 6, 3, 7, 5, 4}
  54. // {2, 8, 3, 1, 6, 4, 7, 0, 5}
  55.  
  56. // Coste 10
  57. {8, 2, 1, 7, 0, 3, 6, 5, 4}
  58. // {1, 4, 0, 8, 5, 2, 7, 3, 6}
  59. // {8, 1, 3, 7, 0, 5, 4, 2, 6}
  60. // {8, 1, 2, 4, 0, 6, 7, 5, 3}
  61. // {0, 1, 3, 7, 2, 5, 4, 8, 6}
  62. // {1, 4, 0, 7, 8, 2, 6, 5, 3}
  63. // {3, 8, 4, 1, 6, 2, 0, 7, 5}
  64. // {1, 3, 4, 6, 7, 2, 0, 8, 5}
  65. // {3, 2, 4, 1, 0, 5, 8, 7, 6}
  66. // {8, 1, 0, 2, 5, 3, 7, 4, 6}
  67.  
  68. // Coste 15
  69. // {4, 8, 2, 6, 3, 5, 1, 0, 7}
  70. // {1, 4, 5, 2, 7, 0, 8, 6, 3}
  71. // {1, 3, 8, 6, 7, 4, 2, 0, 5}
  72. // {2, 0, 8, 7, 5, 3, 4, 1, 6}
  73. // {7, 1, 3, 4, 5, 0, 8, 2, 6}
  74. // {1, 3, 6, 7, 2, 0, 4, 5, 8}
  75. // {7, 0, 3, 5, 1, 8, 2, 6, 4}
  76. // {6, 3, 5, 2, 1, 0, 8, 4, 7}
  77. // {6, 0, 3, 8, 1, 5, 4, 2, 7}
  78. // {7, 8, 3, 1, 5, 0, 4, 2, 6}
  79.  
  80. // Coste 20
  81. // {6, 2, 7, 4, 5, 1, 0, 8, 3}
  82. // {4, 7, 2, 1, 0, 6, 3, 5, 8}
  83. // {7, 1, 5, 4, 0, 8, 2, 6, 3}
  84. // {5, 1, 6, 4, 0, 3, 8, 7, 2}
  85. // {7, 1, 4, 5, 0, 6, 3, 2, 8}
  86. // {2, 4, 0, 6, 3, 1, 7, 8, 5}
  87. // {3, 5, 6, 2, 4, 7, 0, 1, 8}
  88. // {1, 4, 7, 6, 8, 5, 0, 3, 2}
  89. // {6, 4, 0, 2, 8, 1, 7, 3, 5}
  90. // {4, 1, 3, 7, 2, 8, 5, 6, 0}
  91.  
  92. // coste 25
  93. // {6, 7, 4, 0, 5, 1, 3, 2, 8}
  94. // {6, 0, 7, 5, 4, 1, 3, 8, 2}
  95. // {3, 4, 8, 5, 7, 1, 6, 0, 2}
  96. // {4, 5, 3, 7, 6, 2, 8, 0, 1}
  97. // {2, 7, 8, 5, 4, 0, 3, 1, 6}
  98.  
  99.  
  100. // coste 30
  101. // {5, 6, 7, 2, 8, 4, 0, 3, 1}
  102. // {5, 6, 7, 4, 0, 8, 3, 2, 1}
  103. // {5, 4, 7, 6, 0, 3, 8, 2, 1}
  104. // {3, 8, 7, 4, 0, 6, 5, 2, 1}
  105. // {5, 6, 3, 4, 0, 2, 7, 8, 1}
  106.  
  107. // Instancias que no se resuelven con h3 y la version de A* que no rectifica
  108. // { 6, 3, 2, 5, 7, 8, 0, 4, 1 } // 24
  109. // { 6, 3, 2, 5, 8, 1, 4, 0, 7 } // 23
  110. // { 3, 5, 6, 4, 2, 7, 0, 8, 1 } // 24
  111. // { 7, 0, 2, 3, 6, 1, 5, 8, 4 } // 21
  112.  
  113. );
  114.  
  115. public static EightPuzzleGoalTest goalState = new EightPuzzleGoalTest();
  116.  
  117. public static final String METRIC_TIME_TAKEN = "timeTaken";
  118.  
  119.  
  120. public static void main(String[] args) {
  121.  
  122. /* ----------------- */
  123. /* BUSQUEDA A CIEGAS */
  124. /* ----------------- */
  125.  
  126. /* ----------------------------------------------------------------------- */
  127. /* Algoritmos iterativos con busqueda basada en cola (implementan QSearch) */
  128. /* ----------------------------------------------------------------------- */
  129.  
  130. //eightPuzzleUninformedSearchDemo(new DepthFirstSearch(new TreeSearch())); // Busqueda en profundidad en arboles
  131. //eightPuzzleUninformedSearchDemo(new DepthFirstSearch(new GraphSearch())); // Busqueda en profundidad normal en grafos
  132.  
  133. //eightPuzzleUninformedSearchDemo(new BreadthFirstSearch(new TreeSearch())); // Busqueda en anchura en arboles
  134. //eightPuzzleUninformedSearchDemo(new BreadthFirstSearch(new GraphSearch())); // Busqueda en anchura en grafos
  135.  
  136. //eightPuzzleUninformedSearchDemo(new UniformCostSearch(new TreeSearch())); // Busqueda con coste uniforme en arboles
  137. //eightPuzzleUninformedSearchDemo(new UniformCostSearch(new GraphSearch())); // Busqueda con coste uniforme en grafos
  138.  
  139. /* ------------------------------------------ */
  140. /* Algoritmos recursivos (implementan Search) */
  141. /* ------------------------------------------ */
  142.  
  143. //eightPuzzleDepthLimitedSearchDemo(10); // Busqueda en profundidad limitada
  144. //eightPuzzleIterativeDeepeningSearchDemo(); // Busqueda en profundidad iterativa
  145.  
  146.  
  147. /* -------------------- */
  148. /* BUSQUEDA INTELIGENTE */
  149. /* -------------------- */
  150.  
  151. /* A* con busqueda en arboles */ //(mas complicada pero aun asi con h2 mas q en anchura)
  152. //eightPuzzleAStarDemo(new AStarSearch(new TreeSearch(),new NullHeuristicFunction()));
  153. //eightPuzzleAStarDemo(new AStarSearch(new TreeSearch(),new MisplacedT HeuristicFunction(goalState)));
  154. eightPuzzleAStarDemo(new AStarSearch(new TreeSearch(),new ManhattanHeuristicFunction(goalState)));
  155. //eightPuzzleAStarDemo(new AStarSearch(new TreeSearch(),new NoConsistentHeuristicFunction(goalState)));
  156.  
  157. //no va a llegar en muchos a solucion optima (mirar las instancias raras)
  158. /* A* con busqueda en grafos, sin rectificar ni reexpandir nodos ya expandidos */
  159. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearch(),new NullHeuristicFunction()));
  160. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearch(),new MisplacedTilleHeuristicFunction(goalState)));
  161. eightPuzzleAStarDemo(new AStarSearch(new GraphSearch(),new Heuristic12(goalState)));
  162. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearch(),new ManhattanHeuristicFunction(goalState)));
  163. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearch(),new NoConsistentHeuristicFunction(goalState)));
  164.  
  165.  
  166. //estos hasta el lunes nooooooooooooooooooooooo
  167.  
  168. /* A* con busqueda en grafos, con reexpansion de nodos ya expandidos */ //rectifica los nodos de abierta
  169. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchReinsertExpanded(),new NullHeuristicFunction()));
  170. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchReinsertExpanded(),new MisplacedTilleHeuristicFunction(goalState)));
  171. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchReinsertExpanded(),new ManhattanHeuristicFunction(goalState)));
  172. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchReinsertExpanded(),new NoConsistentHeuristicFunction(goalState)));
  173.  
  174. /* A* con busqueda en grafos, con rectificacion de nodos ya expandidos */ //algo general de busqueda, rectifica cascada
  175. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(),new NullHeuristicFunction()));
  176. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(),new MisplacedTilleHeuristicFunction(goalState)));
  177. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(),new ManhattanHeuristicFunction(goalState)));
  178. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(),new NoConsistentHeuristicFunction(goalState)));
  179.  
  180. /* A* con monitorizacion de los f-valores de los nodos expandidos */
  181. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(new NullHeuristicFunction()),new NullHeuristicFunction()));
  182. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(new MisplacedTilleHeuristicFunction(goalState)),new MisplacedTilleHeuristicFunction(goalState)));
  183. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(new ManhattanHeuristicFunction(goalState)),new ManhattanHeuristicFunction(goalState)));
  184. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchRectifyExpanded(new NoConsistentHeuristicFunction(goalState)),new NoConsistentHeuristicFunction(goalState)));
  185.  
  186. /* Greedy Best First Search */
  187. //eightPuzzleGreedyBestFirstDemo(new MisplacedTilleHeuristicFunction(goalState));
  188. //eightPuzzleGreedyBestFirstDemo(new ManhattanHeuristicFunction(goalState));
  189.  
  190. /* Algoritmo de Ponderacion Estatica, PEA*/
  191. //eightPuzzleAStarDemo(new AStarSearch(new GraphSearchReinsertExpanded(),new ManhattanHeuristicFunction(goalState, 0.2)));
  192.  
  193. }
  194.  
  195.  
  196. // BUSQUEDA A CIEGAS
  197.  
  198. // Busqueda iterativa basada en cola (QSearch)
  199. private static void eightPuzzleUninformedSearchDemo(QSearch search) {
  200. String searchAlgorithm = search.getClass().getName().substring(1+search.getClass().getName().lastIndexOf("."));
  201. String searchSpace = search.getImplementation().getClass().getName().substring(1+search.getImplementation().getClass().getName().lastIndexOf("."));
  202. System.out.println("\nEightPuzzleDemo: " + "[" + searchAlgorithm + "]" + " [" + searchSpace +"] -->");
  203. System.out.println("\nInitial State: \n" + initialState);
  204. try {
  205. long tIni = System.currentTimeMillis();
  206. Problem problem = new Problem(initialState, EightPuzzleFunctionFactory
  207. .getActionsFunction(), EightPuzzleFunctionFactory
  208. .getResultFunction(), goalState);
  209. SearchAgent agent = new SearchAgent(problem, search);
  210. long tFin = System.currentTimeMillis();
  211. search.getMetrics().set(METRIC_TIME_TAKEN, tFin - tIni);
  212. printActions(agent.getActions());
  213. printInstrumentation(agent.getInstrumentation());
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. }
  218.  
  219. // Busqueda en profundidad limitada
  220. private static void eightPuzzleDepthLimitedSearchDemo(int limit) {
  221. System.out.println("\nEightPuzzleDemo: [DepthLimitedSearch (" + limit + ")] -->");
  222. System.out.println("\nInitial State: \n" + initialState);
  223. try {
  224. long tIni = System.currentTimeMillis();
  225. Problem problem = new Problem(initialState, EightPuzzleFunctionFactory
  226. .getActionsFunction(), EightPuzzleFunctionFactory
  227. .getResultFunction(), goalState);
  228. Search search = new DepthLimitedSearch(limit);
  229. SearchAgent agent = new SearchAgent(problem, search);
  230. long tFin = System.currentTimeMillis();
  231. search.getMetrics().set(METRIC_TIME_TAKEN, tFin - tIni);
  232. printActions(agent.getActions());
  233. printInstrumentation(agent.getInstrumentation());
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
  238.  
  239. // Busqueda en profundidad iterativa
  240. private static void eightPuzzleIterativeDeepeningSearchDemo() {
  241. System.out.println("\nEightPuzzleDemo: [IterativeDeepeningSearch] -->");
  242. System.out.println("\nInitial State: " + initialState);
  243. try {
  244. long tIni = System.currentTimeMillis();
  245. Problem problem = new Problem(initialState, EightPuzzleFunctionFactory
  246. .getActionsFunction(), EightPuzzleFunctionFactory
  247. .getResultFunction(), goalState);
  248. Search search = new IterativeDeepeningSearch();
  249. SearchAgent agent = new SearchAgent(problem, search);
  250. long tFin = System.currentTimeMillis();
  251. search.getMetrics().set(METRIC_TIME_TAKEN, tFin - tIni);
  252. printActions(agent.getActions());
  253. printInstrumentation(agent.getInstrumentation());
  254. } catch (Exception e) {
  255. e.printStackTrace();
  256. }
  257.  
  258. }
  259.  
  260.  
  261. // BUSQUEDA INTELIGENTE (guiada por heuristicos)
  262.  
  263. /* ------------ */
  264. /* ALGORITMO A* */
  265. /* ------------ */
  266.  
  267. private static void eightPuzzleAStarDemo(QSearch search) {
  268. String searchAlgorithm = search.getClass().getName().substring(1+search.getClass().getName().lastIndexOf("."));
  269. String searchSpace = search.getImplementation().getClass().getName().substring(1+search.getImplementation().getClass().getName().lastIndexOf("."));
  270. String heuristicFunction = search.getHeuristicFunction().getClass().getName().substring(1+search.getHeuristicFunction().getClass().getName().lastIndexOf("."));
  271. System.out.println("\nEightPuzzleDemo: " + "[" + searchAlgorithm + "] [" + searchSpace +"] [" + heuristicFunction +"] -->");
  272. System.out.println("Initial State: \n" + initialState);
  273. try {
  274. long tIni = System.currentTimeMillis();
  275. Problem problem = new Problem(initialState, EightPuzzleFunctionFactory
  276. .getActionsFunction(), EightPuzzleFunctionFactory
  277. .getResultFunction(), goalState);
  278. SearchAgent agent = new SearchAgent(problem, search);
  279. long tFin = System.currentTimeMillis();
  280. search.getMetrics().set(METRIC_TIME_TAKEN, tFin - tIni);
  281. printActions(agent.getActions());
  282. printInstrumentation(agent.getInstrumentation());
  283. } catch (Exception e) {
  284. e.printStackTrace();
  285. }
  286.  
  287. }
  288.  
  289. // Greedy Best First es Best First con f = h
  290. private static void eightPuzzleGreedyBestFirstDemo(HeuristicFunction h) {
  291. String heuristicFunction = h.getClass().getName().substring(1+h.getClass().getName().lastIndexOf("."));
  292. System.out
  293. .println("\nEightPuzzleDemo: [Greedy Best First Search] [" + heuristicFunction + "] -->");
  294. try {
  295. long tIni = System.currentTimeMillis();
  296. Problem problem = new Problem(initialState,
  297. EightPuzzleFunctionFactory.getActionsFunction(),
  298. EightPuzzleFunctionFactory.getResultFunction(),
  299. goalState);
  300. Search search = new GreedyBestFirstSearch(new GraphSearch(),
  301. h);
  302. SearchAgent agent = new SearchAgent(problem, search);
  303. long tFin = System.currentTimeMillis();
  304. search.getMetrics().set(METRIC_TIME_TAKEN, tFin - tIni);
  305. printActions(agent.getActions());
  306. printInstrumentation(agent.getInstrumentation());
  307. } catch (Exception e) {
  308. e.printStackTrace();
  309. }
  310.  
  311. }
  312.  
  313.  
  314. private static void printInstrumentation(Properties properties) {
  315. Iterator<Object> keys = properties.keySet().iterator();
  316. while (keys.hasNext()) {
  317. String key = (String) keys.next();
  318. String property = properties.getProperty(key);
  319. System.out.println(key + " : " + property);
  320. }
  321.  
  322. }
  323.  
  324. private static void printActions(List<Action> actions) {
  325. for (int i = 0; i < actions.size(); i++) {
  326. String action = actions.get(i).toString();
  327. System.out.println(action);
  328. }
  329. }
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement