001 package aima.search.demos; 002 003 import java.util.HashSet; 004 import java.util.Iterator; 005 import java.util.List; 006 import java.util.Properties; 007 import java.util.Set; 008 009 import aima.search.framework.GraphSearch; 010 import aima.search.framework.Problem; 011 import aima.search.framework.Search; 012 import aima.search.framework.SearchAgent; 013 import aima.search.framework.TreeSearch; 014 import aima.search.informed.HillClimbingSearch; 015 import aima.search.informed.SimulatedAnnealingSearch; 016 import aima.search.informed.ga.GeneticAlgorithm; 017 import aima.search.nqueens.NQueensBoard; 018 import aima.search.nqueens.NQueensFitnessFunction; 019 import aima.search.nqueens.NQueensGoalTest; 020 import aima.search.nqueens.NQueensSuccessorFunction; 021 import aima.search.nqueens.QueensToBePlacedHeuristic; 022 import aima.search.uninformed.BreadthFirstSearch; 023 import aima.search.uninformed.DepthFirstSearch; 024 import aima.search.uninformed.DepthLimitedSearch; 025 import aima.search.uninformed.IterativeDeepeningSearch; 026 027 /** 028 * @author Ravi Mohan 029 * 030 */ 031 032 public class NQueensDemo { 033 034 public static void main(String[] args) { 035 036 newNQueensDemo(); 037 } 038 039 private static void newNQueensDemo() { 040 041 nQueensWithDepthFirstSearch(); 042 nQueensWithBreadthFirstSearch(); 043 nQueensWithRecursiveDLS(); 044 nQueensWithIterativeDeepeningSearch(); 045 nQueensSimulatedAnnealingSearch(); 046 nQueensHillClimbingSearch(); 047 nQueensGeneticAlgorithmSearch(); 048 } 049 050 private static void nQueensWithRecursiveDLS() { 051 System.out.println("\nNQueensDemo recursive DLS -->"); 052 try { 053 Problem problem = new Problem(new NQueensBoard(8), 054 new NQueensSuccessorFunction(), new NQueensGoalTest()); 055 Search search = new DepthLimitedSearch(8); 056 SearchAgent agent = new SearchAgent(problem, search); 057 printActions(agent.getActions()); 058 printInstrumentation(agent.getInstrumentation()); 059 } catch (Exception e) { 060 e.printStackTrace(); 061 } 062 063 } 064 065 private static void nQueensWithBreadthFirstSearch() { 066 try { 067 System.out.println("\nNQueensDemo BFS -->"); 068 Problem problem = new Problem(new NQueensBoard(8), 069 new NQueensSuccessorFunction(), new NQueensGoalTest()); 070 Search search = new BreadthFirstSearch(new TreeSearch()); 071 SearchAgent agent2 = new SearchAgent(problem, search); 072 printActions(agent2.getActions()); 073 printInstrumentation(agent2.getInstrumentation()); 074 } catch (Exception e1) { 075 076 e1.printStackTrace(); 077 } 078 } 079 080 private static void nQueensWithDepthFirstSearch() { 081 System.out.println("\nNQueensDemo DFS -->"); 082 try { 083 Problem problem = new Problem(new NQueensBoard(8), 084 new NQueensSuccessorFunction(), new NQueensGoalTest()); 085 Search search = new DepthFirstSearch(new GraphSearch()); 086 SearchAgent agent = new SearchAgent(problem, search); 087 printActions(agent.getActions()); 088 printInstrumentation(agent.getInstrumentation()); 089 } catch (Exception e) { 090 e.printStackTrace(); 091 } 092 } 093 094 private static void nQueensWithIterativeDeepeningSearch() { 095 System.out.println("\nNQueensDemo Iterative DS -->"); 096 try { 097 Problem problem = new Problem(new NQueensBoard(8), 098 new NQueensSuccessorFunction(), new NQueensGoalTest()); 099 Search search = new IterativeDeepeningSearch(); 100 SearchAgent agent = new SearchAgent(problem, search); 101 102 System.out.println(); 103 printActions(agent.getActions()); 104 printInstrumentation(agent.getInstrumentation()); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 } 109 110 private static void nQueensSimulatedAnnealingSearch() { 111 System.out.println("\nNQueensDemo Simulated Annealing -->"); 112 try { 113 Problem problem = new Problem(new NQueensBoard(8), 114 new NQueensSuccessorFunction(), new NQueensGoalTest(), 115 new QueensToBePlacedHeuristic()); 116 SimulatedAnnealingSearch search = new SimulatedAnnealingSearch(); 117 SearchAgent agent = new SearchAgent(problem, search); 118 119 System.out.println(); 120 printActions(agent.getActions()); 121 System.out.println("Search Outcome=" + search.getOutcome()); 122 System.out.println("Final State=\n" + search.getLastSearchState()); 123 printInstrumentation(agent.getInstrumentation()); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 129 private static void nQueensHillClimbingSearch() { 130 System.out.println("\nNQueensDemo HillClimbing -->"); 131 try { 132 Problem problem = new Problem(new NQueensBoard(8), 133 new NQueensSuccessorFunction(), new NQueensGoalTest(), 134 new QueensToBePlacedHeuristic()); 135 HillClimbingSearch search = new HillClimbingSearch(); 136 SearchAgent agent = new SearchAgent(problem, search); 137 138 System.out.println(); 139 printActions(agent.getActions()); 140 System.out.println("Search Outcome=" + search.getOutcome()); 141 System.out.println("Final State=\n" + search.getLastSearchState()); 142 printInstrumentation(agent.getInstrumentation()); 143 } catch (Exception e) { 144 e.printStackTrace(); 145 } 146 } 147 148 public static void nQueensGeneticAlgorithmSearch() { 149 System.out.println("\nNQueensDemo GeneticAlgorithm -->"); 150 try { 151 int boardSize = 8; 152 NQueensFitnessFunction fitnessFunction = new NQueensFitnessFunction(); 153 // Generate an initial population 154 Set<String> population = new HashSet<String>(); 155 for (int i = 0; i < 20; i++) { 156 population.add(fitnessFunction 157 .generateRandomIndividual(boardSize)); 158 } 159 160 GeneticAlgorithm ga = new GeneticAlgorithm(boardSize, 161 fitnessFunction.getFiniteAlphabetForBoardOfSize(boardSize), 162 0.15); 163 164 // Run for a set number of iterations 165 String bestIndividual = ga.geneticAlgorithm(population, 166 fitnessFunction, 100); 167 168 System.out.println("Iterations Best Individual=\n" 169 + fitnessFunction.getBoardForIndividual(bestIndividual)); 170 System.out.println("Fitness=" 171 + fitnessFunction.getValue(bestIndividual)); 172 System.out.println("Is Goal=" 173 + fitnessFunction.isGoalState(bestIndividual)); 174 System.out.println("Itertions=" + ga.getIterations()); 175 176 // Run till goal is achieved 177 bestIndividual = ga.geneticAlgorithm(population, fitnessFunction, 178 fitnessFunction); 179 180 System.out.println("Goal Test Best Individual=\n" 181 + fitnessFunction.getBoardForIndividual(bestIndividual)); 182 System.out.println("Fitness=" 183 + fitnessFunction.getValue(bestIndividual)); 184 System.out.println("Is Goal=" 185 + fitnessFunction.isGoalState(bestIndividual)); 186 System.out.println("Itertions=" + ga.getIterations()); 187 188 } catch (Exception e) { 189 e.printStackTrace(); 190 } 191 } 192 193 private static void printInstrumentation(Properties properties) { 194 Iterator keys = properties.keySet().iterator(); 195 while (keys.hasNext()) { 196 String key = (String) keys.next(); 197 String property = properties.getProperty(key); 198 System.out.println(key + " : " + property); 199 } 200 201 } 202 203 private static void printActions(List actions) { 204 for (int i = 0; i < actions.size(); i++) { 205 String action = (String) actions.get(i); 206 System.out.println(action); 207 } 208 } 209 210 }