Presentation is loading. Please wait.

Presentation is loading. Please wait.

AI – Week 10 AI and Games (2) Lee McCluskey, room 3/10

Similar presentations


Presentation on theme: "AI – Week 10 AI and Games (2) Lee McCluskey, room 3/10"— Presentation transcript:

1 AI – Week 10 AI and Games (2) Lee McCluskey, room 3/10 Email lee@hud.ac.uklee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/

2 Recap Symbolic AI – Search Heuristics Knowledge Representation Experiment with appropriate language – Prolog Last Week: Introduction to AI in Games 2- player turn-based games Search – use “Minimax” through a “game tree” Heuristics - alpha beta pruning, static board evaluation This Week: A SIMPLE Game, how to implement it in Prolog

3 AI 2 Player Games – Knowledge Representation - knowledge about a “state” of a game – ie representation of a game “board” or “world”. - Could contain for example:  Strength Values of your Pieces / Forces / Resource  Position Value – how well your Pieces are positioned  Threat Value – what Pieces are being threatened - knowledge about moves tends to be represented procedurally ie encoded into the move generator

4 Fox and Goose goose fox Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose Rules: (i)If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. (ii)Fox/goose can move either v/h/d. (iii)Only ONE fox can move each turn. (iv)Fox can’t move onto HOME squares or eat the goose when HOME HOME

5 Fox and Goose goose fox HOME Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose Rules: (i)If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. (ii)Fox/goose can move either v/h/d. (iii)Only ONE fox can move each turn. (iv)Fox can’t move onto HOME squares or eat the goose when HOME

6 Fox and Goose goose fox HOME Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose Rules: (i)If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. (ii)Fox/goose can move either v/h/d. (iii)Only ONE fox can move each turn. (iv)Fox can’t move onto HOME squares or eat the goose when HOME

7 Fox and Goose – more elaborate game.. goose fox + Obstacles to reduce movement HOME Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose Rules: (i)If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. (ii)Fox/goose can move either v/h/d. (iii)Only ONE fox can move each turn. (iv)Fox can’t move onto HOME squares or eat the goose when HOME

8 2 Player Game Implementation 1. Board (state/world) representation 2. Board evaluation function – this is a heuristic function which applies “static” evaluation to each board giving an estimate of how close it is to the goal (called “board evaluation function”) 3. Move generation – which moves can I make? 4. Move application – simulate making a move 5. Choose Best Move – use search (Minimax algorithm) to find best move

9 Keep it Simple: Fox and Goose Board Representation square denoted by co-ordinate, move specified by pair of co-ordinates square value denotes fox/goose/obstacle/free/ board is a set of squares Operations: Board evaluation function: board -> value Move generation: board -> set of moves

10 Prolog Representation Square = s(,, ) Board = b(, ) Squares values f = fox, g = goose, 0= free e.g. b(1, [s(1,1,0), s(1,2,0), s(1,3,f), s(1,4,0), s(2,1,0), s(2,2,0), s(2,3,0), s(2,4,0), s(3,1,0), s(3,2,0), s(3,3,0), s(3,4,0), s(4,1,g), s(4,2,0), s(4,3,0), s(4,4,0), s(5,1,0), s(5,2,0), s(5,3,0), s(5,4,0), s(6,1,0), s(6,2,0), s(6,3,f), s(6,4,0)]). N.B. ORDER OF ELEMENTS IN LIST IS IRRELEVANT.

11 Board Evaluation Function Positive good for Goose, Negative good for Fox % Goose at home (goose won) +10000.. board_eval(Board, 10000) :- member(s(_,4,g), Board),!. % No Goose (eaten) -10000 board_eval(Board, -10000) :- \+ member(s(_,_,g), Board),!. % Otherwise 0 (don’t know) board_eval(Board, 0 ) :- !.

12 Move Application AND Generation apply( FROM, TO, BOARD_IN, BOARD_OUT) Move Generation: Precondition: BOARD_IN are instantiated. Postcondition: FROM,TO and BOARD_OUT get values. Move Application: Precondition: FROM, TO, and BOARD_IN are instantiated. Postcondition: BOARD_OUT gets a value.

13 Move Generation using Generate and Test 1.Generate a possible move from any (X,Y) to (X1,Y1) 2.Test to see if move satisfies necessary constraints, if no fail and backtrack to 1 3. Use the move, then fail and backtrack to 1 Generate possible move in Fox and Goose: select any 2 squares from Board Test in Fox and Goose: (X,Y) next to (X1,Y1) Z = g then Z1 =0 Z = f then Z1 = 0 or Z1 = g X,Y Value Z X1,Y1 Value Z1

14 Move Application AND Generation % Assume Z is either f or g apply( s(X,Y,Z), s(X1,Y1,Z1), B_IN, B_OUT) :- % move to empty square OR fox eats goose.. (Z1 = 0 ; (Z=f, Z1=g)), member(s(X,Y,Z), B_IN), member(s(X1,Y1,Z1),B_IN), % FOX can't go into the fourth column... \+ (Z=f, Y1=4), % two squares must be next to each other next(X,Y,X1,Y1), % checks over – now make the move remove(s(X,Y,Z), B_IN, B1), remove(s(X1,Y1,Z1), B1, B2), B_OUT = [s(X1,Y1,Z),s(X,Y,0)|B2]. Generate and Test Fixes Z1 Tests parameters Generates Deterministic procedures – only one possible output

15 Examples | ?- test(7,5,goose). Move goose from 4,1 to 5,1 Estimated value of Board is -5 unclear who wins Number of leaf boards evaluated is 43979 Move fox from 3,3 to 4,2 Move goose from 5,1 to 6,1 Move fox from 4,2 to 5,1 Move goose from 6,1 to 7,2 Move fox from 5,1 to 6,1 Move goose from 7,2 to 7,3 yes - not enough search space to see who wins so fox chases goose Initial call to minimax Finds best move Subsequent calls to Minimax from “finish_game” procedure Search depth Identifier of the starting board Who goes first

16 Examples | ?- test(8,5,goose). Move goose from 4,1 to 5,1 Estimated value of Board is 10000 goose wins Number of leaf boards evaluated is 222714 Move fox from 3,3 to 2,2 Move goose from 5,1 to 4,2 Move fox from 2,2 to 3,3 Move goose from 4,2 to 5,3 Move fox from 3,3 to 4,2 Move goose from 5,3 to 4,4 Move fox from 4,2 to 5,3 yes | ?- - This time enough search space to see that the goose wins - - Note the fox wanders about as it knows that no win is possible!


Download ppt "AI – Week 10 AI and Games (2) Lee McCluskey, room 3/10"

Similar presentations


Ads by Google