Package aima.gui.demo.search

Source Code of aima.gui.demo.search.NQueensDemo

package aima.gui.demo.search;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import aima.core.agent.Action;
import aima.core.environment.nqueens.AttackingPairsHeuristic;
import aima.core.environment.nqueens.NQueensBoard;
import aima.core.environment.nqueens.NQueensFitnessFunction;
import aima.core.environment.nqueens.NQueensFunctionFactory;
import aima.core.environment.nqueens.NQueensGoalTest;
import aima.core.search.framework.GraphSearch;
import aima.core.search.framework.Problem;
import aima.core.search.framework.Search;
import aima.core.search.framework.SearchAgent;
import aima.core.search.framework.TreeSearch;
import aima.core.search.local.GeneticAlgorithm;
import aima.core.search.local.HillClimbingSearch;
import aima.core.search.local.SimulatedAnnealingSearch;
import aima.core.search.uninformed.BreadthFirstSearch;
import aima.core.search.uninformed.DepthFirstSearch;
import aima.core.search.uninformed.DepthLimitedSearch;
import aima.core.search.uninformed.IterativeDeepeningSearch;

/**
* @author Ravi Mohan
*
*/

public class NQueensDemo {

  public static void main(String[] args) {

    newNQueensDemo();
  }

  private static void newNQueensDemo() {

    nQueensWithDepthFirstSearch();
    nQueensWithBreadthFirstSearch();
    nQueensWithRecursiveDLS();
    nQueensWithIterativeDeepeningSearch();
    nQueensSimulatedAnnealingSearch();
    nQueensHillClimbingSearch();
    nQueensGeneticAlgorithmSearch();
  }

  private static void nQueensWithRecursiveDLS() {
    System.out.println("\nNQueensDemo recursive DLS -->");
    try {
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      Search search = new DepthLimitedSearch(8);
      SearchAgent agent = new SearchAgent(problem, search);
      printActions(agent.getActions());
      printInstrumentation(agent.getInstrumentation());
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  private static void nQueensWithBreadthFirstSearch() {
    try {
      System.out.println("\nNQueensDemo BFS -->");
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      Search search = new BreadthFirstSearch(new TreeSearch());
      SearchAgent agent2 = new SearchAgent(problem, search);
      printActions(agent2.getActions());
      printInstrumentation(agent2.getInstrumentation());
    } catch (Exception e1) {

      e1.printStackTrace();
    }
  }

  private static void nQueensWithDepthFirstSearch() {
    System.out.println("\nNQueensDemo DFS -->");
    try {
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      Search search = new DepthFirstSearch(new GraphSearch());
      SearchAgent agent = new SearchAgent(problem, search);
      printActions(agent.getActions());
      printInstrumentation(agent.getInstrumentation());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void nQueensWithIterativeDeepeningSearch() {
    System.out.println("\nNQueensDemo Iterative DS  -->");
    try {
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      Search search = new IterativeDeepeningSearch();
      SearchAgent agent = new SearchAgent(problem, search);

      System.out.println();
      printActions(agent.getActions());
      printInstrumentation(agent.getInstrumentation());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void nQueensSimulatedAnnealingSearch() {
    System.out.println("\nNQueensDemo Simulated Annealing  -->");
    try {
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      SimulatedAnnealingSearch search = new SimulatedAnnealingSearch(
          new AttackingPairsHeuristic());
      SearchAgent agent = new SearchAgent(problem, search);

      System.out.println();
      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 nQueensHillClimbingSearch() {
    System.out.println("\nNQueensDemo HillClimbing  -->");
    try {
      Problem problem = new Problem(new NQueensBoard(8),
          NQueensFunctionFactory.getIActionsFunction(),
          NQueensFunctionFactory.getResultFunction(),
          new NQueensGoalTest());
      HillClimbingSearch search = new HillClimbingSearch(
          new AttackingPairsHeuristic());
      SearchAgent agent = new SearchAgent(problem, search);

      System.out.println();
      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();
    }
  }

  public static void nQueensGeneticAlgorithmSearch() {
    System.out.println("\nNQueensDemo GeneticAlgorithm  -->");
    try {
      int boardSize = 8;
      NQueensFitnessFunction fitnessFunction = new NQueensFitnessFunction();
      // Generate an initial population
      Set<String> population = new HashSet<String>();
      for (int i = 0; i < 20; i++) {
        population.add(fitnessFunction
            .generateRandomIndividual(boardSize));
      }

      GeneticAlgorithm ga = new GeneticAlgorithm(boardSize,
          fitnessFunction.getFiniteAlphabetForBoardOfSize(boardSize),
          0.15);

      // Run for a set number of iterations
      String bestIndividual = ga.geneticAlgorithm(population,
          fitnessFunction, 100);

      System.out.println("Iterations Best Individual=\n"
          + fitnessFunction.getBoardForIndividual(bestIndividual));
      System.out.println("Fitness="
          + fitnessFunction.getValue(bestIndividual));
      System.out.println("Is Goal="
          + fitnessFunction.isGoalState(bestIndividual));
      System.out.println("Itertions=" + ga.getIterations());

      // Run till goal is achieved
      bestIndividual = ga.geneticAlgorithm(population, fitnessFunction,
          fitnessFunction);

      System.out.println("Goal Test Best Individual=\n"
          + fitnessFunction.getBoardForIndividual(bestIndividual));
      System.out.println("Fitness="
          + fitnessFunction.getValue(bestIndividual));
      System.out.println("Is Goal="
          + fitnessFunction.isGoalState(bestIndividual));
      System.out.println("Itertions=" + ga.getIterations());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void printInstrumentation(Properties properties) {
    Iterator<Object> keys = properties.keySet().iterator();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      String property = properties.getProperty(key);
      System.out.println(key + " : " + property);
    }

  }

  private static void printActions(List<Action> actions) {
    for (int i = 0; i < actions.size(); i++) {
      String action = actions.get(i).toString();
      System.out.println(action);
    }
  }

}
TOP

Related Classes of aima.gui.demo.search.NQueensDemo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.