Learn

Saturday, 2 December 2017

How to Use Java Framework for Search and Game Playing in Artificial Intelligence



A Java Framework for Search and Game Playing:

               The general interface for the Java classes that we will develop in this section was inspired by the Common LISP game-playing framework written by Kevin Knight and described in (Rich, Knight 1991). The abstract class GameSearch contains the code for running a two-player game and performing an alpha-beta search. This class needs to be sub-classed to provide the eight methods:


public abstract boolean drawnPosition(Position p) 

public abstract boolean wonPosition(Position p, boolean player) 

positionEvaluation(Position p, boolean player) 

public abstract void printPosition(Position p) 

public abstract Position [ ] 

possibleMoves(Position p, boolean player) 

public abstract Position makeMove(Position p, boolean player, Move move) 

public abstract boolean reachedMaxDepth(Position p, int depth) 

public abstract Move getMove() 


              The method drawnP osition should return a Boolean true value if the given position evaluates to a draw situation. The method wonP osition should return a true value if the input position is won for the indicated player. By convention, I use a Boolean true value to represent the computer and a Boolean false value to represent the human opponent. The method positionEvaluation returns a position evaluation for a specified board position and player. Note that if we call positionEvaluation switching the player for the same board position, then the value returned is the negative of the value calculated for the opposing player. The method possibleMoves returns an array of objects belonging to the class Position. In an actual game like chess, the position objects will actually belong to a chess-specific refinement of the Position class (e.g., for the chess program developed later in this chapter, the method possibleMoves will return an array of ChessP osition objects). The method makeMove will return a new position object for a specified board position, side to move, and move.

                   The method reachedM axDepth returns a Boolean true value if the search process has reached a satisfactory depth. For the tic-tac-toe program, the method reachedM axDepth does not return true unless either side has won the game or the board is full; for the chess program, the method reachedM axDepth returns true if the search has reached a depth of 4 half moves deep (this is not the best strategy, but it has the advantage of making the example program short and easy to understand). The method getMove returns an object of a class derived from the class Move (e.g., Tic Tac Toe Move or Chess Move).


The GameSearch class implements the following methods to perform game search: 

protected Vector alphaBeta(int depth, Position p, boolean player) 

protected Vector alphaBetaHelper(int depth, Position p, boolean player, float alpha, float beta) 

public void playGame(Position startingPosition, boolean humanPlayFirst) 


How to find Paths and Mazes in Artificial Intelligence Algorithm

Find Paths and Mazes:



Fig:A directed graph representation is shown on the left and a twodimensional grid (or maze) representation is shown on the right. In both representations, the letter R is used to represent the current position (or reference point) and the arrowheads indicate legal moves generated by a search operator.


          The example program used in this section is MazeSearch.java in the directory src/search/maze and I assume that the reader has downloaded the entire example ZIP file for this book and placed the source files for the examples in a convenient place.

           Figure shows the UML class diagram for the maze search classes: depth first and breadth first search. The abstract base class AbstractSearchEngine contains common code and data that is required by both the classes DepthF irstSearch and BreadthF irstSearch. The class M aze is used to record the data for a twodimensional maze, including which grid locations contain walls or obstacles. The class M aze defines three static short integer values used to indicate obstacles, the starting location, and the ending location. The Java class M aze defines the search space. 

          This class allocates a two-dimensional array of short integers to represent the state of any grid location in the maze. Whenever we need to store a pair of integers, we will use an instance of the standard Java class java.awt.Dimension, which has two integer data components: width and height. Whenever we need to store an x-y grid location, we create a new Dimension object (if required), and store the x coordinate in Dimension.width and the y coordinate in Dimension.height. As in the right-hand side of Figure 2.1, the operator for moving through the search space from given x-y coordinates allows a transition to any adjacent grid location that is empty. The Maze class also contains the x-y location for the starting location (startLoc) and goal location (goalLoc). Note that for these examples, the class Maze sets the starting location to grid coordinates 0-0 (upper left corner of the maze in the figures to follow) and the goal node in (width - 1)-(height - 1) (lower right corner in the following figures).