1. Trang chủ
  2. » Thể loại khác

LAB4 pot

4 274 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Artificial Intelligence

  • I. Old Exercises

  • II. Local Search

    • II.1 Definition for N-Queens

    • II.2 Local Search Algorithm

    • II.3 Use code supplied by your instructor

Nội dung

Artificial Intelligence LAB 4: Local Search LE Thanh Sach, Ph.D. I. Old Exercises • Write programs to solve problems 1-5,7 in LAB2 using algorithms in uninformed search. • Write programs to solve Navigation problem using Greedy-Best First Search and A- Star using straight-line-distance heuristic given in textbook. II. Local Search N-Queens Problem: For this problem, the goal is the most important, not the path to goal. - The goal is “the game board containing N queens without any attacking from any queen” - In local search, the start state can be generated randomly. Algorithms: - In this LAB you will learn how to use local search (Hill-climbing and Simulated Annealing) to find a solution for N-Queens. - Your program is organized the rule as follows: Your Program = Local Search Algorithm (you will use AIMA library) + Definition for N-Queens (you have to specify 4 entities: 1. Start State 2. Goal Evaluator 3. Action Factory 4. Result Factory II.1 Definition for N-Queens Step 1: Create NqueensState • Draft code as follows. You need not to copy to the Java project. The code is suppied to you public class NQueensState { int[][] squares; int size; … } • Member variable “squares”: We represent a game board by 2D-array in Java • Member variable “size”: define the size of the array, “squares”. • Constructors: public NQueensState(int n) { //Create 2D array with the size given by input parameter “n” } • Methods: public void clear() { //Clear the queens on the game board } public void setRandomBoard(){ //Clear the queens on the game board //And then, randomly place N queens on the game board. clear(); Random rd = new Random(); for(int x=0; x<size; x++){ int y= rd.nextInt(size); squares[x][y] = 1; } } public int getNumberOfAttackingPairs() { //return number of attacking pairs. //IMPORTANT: if this value = 0, it mean we meet the goal. int result = 0; for (XYLocation location : getQueenPositions()) { result += getNumberOfAttacksOn(location); } return result / 2; } • Other methods are explained by your instructor Step 2: Create Goal evaluator • Call “getNumberOfAttackingPairs” to test whether we meet the goal or not. Step 3: Create Action Factory 3.1 We should override Dynamic Action supported by AIMA library to add parameter to our action. • Our action “MOVE_QUEEN” need the coordinate of the new position on the game board public class QueenAction extends DynamicAction { public static final String MOVE_QUEEN = "moveQueenTo"; public static final String ATTRIBUTE_QUEEN_LOC = "location"; public QueenAction(String type, XYLocation loc) { super(type); setAttribute(ATTRIBUTE_QUEEN_LOC, loc); } X Y public XYLocation getLocation() { return (XYLocation) getAttribute(ATTRIBUTE_QUEEN_LOC); } public int getX() { return getLocation().getXCoOrdinate(); } public int getY() { return getLocation().getYCoOrdinate(); } } 3.2 We create Action Factory • Before we start to run the algorithm, start state already contains N queens but still having some attacking pairs. • So, for each colum we have one queen. • In order to move, we need to move a queen on its column. public class ActionFactory implements ActionsFunction{ public Set<Action> actions(Object s) { Set<Action> actionList = new LinkedHashSet<Action>(); NQueensState board = (NQueensState) s; for (int i = 0; i < board.getSize(); i++) for (int j = 0; j < board.getSize(); j++) { XYLocation loc = new XYLocation(i, j); if (!board.queenExistsAt(loc)) actionList.add( new QueenAction( QueenAction.MOVE_QUEEN, loc)); } return actionList; } } Step 4: Create Result Factory • Based on the current state and the given action. We perform the moving public class ResultFactory implements ResultFunction{ @Override public Object result(Object s, Action a) { NQueensState state = (NQueensState)s; NQueensState newState = new NQueensState(state.getSize()); newState.setBoard(state.getQueenPositions()); QueenAction qa = (QueenAction) a; if (qa.getName() == QueenAction.MOVE_QUEEN) newState.moveQueenTo(qa.getLocation()); s = newState; // if action is not understood or is a NoOp // the result will be the current state. return s; } } II.2 Local Search Algorithm Now, we need to develop application use combine the search algorithm supplied in AIMA library to solve the problem defined above. Step 1: Create a class called “NQueensHillClimbing” Step 2: Edit code //Define your problem by creating the FOUR entities NQueensState start = new NQueensState(4); start.setRandomBoard(); Problem problem = new Problem( start, new ActionFactory(), new ResultFactory(), new GoalEvaluator() ); //Create search algorithm HillClimbingSearch search = new HillClimbingSearch( new AttackingPairsHeuristic()); //Use search algorithm to solve the problem SearchAgent agent = new SearchAgent(problem, search); List<Action> actions = agent.getActions(); //Print the output search algorithm to solve the problem if(search.getOutcome() == HillClimbingSearch.SearchOutcome.SOLUTION_FOUND){ System.out.println( "Solution Found after: " + actions.size() + " actions"); } else System.out.println("FAILURE after: " + actions.size() + " actions"); System.out.println("List of Actions have been performed:"); for(Action a: actions){ System.out.println(a.toString()); } NQueensState state = start; ResultFactory factory = new ResultFactory(); state.println(); int i =0; while(i < actions.size()){ state = (NQueensState)factory.result( state, actions.get(i)); state.println(); i++; } II.3 Use code supplied by your instructor Step 1: Create a package called search.nqueens in the project created in previous LAB Step 2: Copy the code in LAB4\src to your project Step 3: Compile and run Step 4: Explore and understand the algorithm . a package called search.nqueens in the project created in previous LAB Step 2: Copy the code in LAB4 src to your project Step 3: Compile and run Step 4: Explore and understand the algorithm

Ngày đăng: 10/08/2014, 21:20

Xem thêm

w