160 likes | 304 Vues
AI – Week 10 AI and Games (2). Lee McCluskey, room 3/10 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/. Recap. Symbolic AI – Search Heuristics Knowledge Representation Experiment with appropriate language – Prolog Last Week: Introduction to AI in Games
E N D
AI – Week 10AI and Games (2) Lee McCluskey, room 3/10 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/
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
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
Fox and Goose Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose fox Rules: • If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. • Fox/goose can move either v/h/d. • Only ONE fox can move each turn. • Fox can’t move onto HOME squares or eat the goose when HOME HOME goose fox
Fox and Goose Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose fox Rules: • If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. • Fox/goose can move either v/h/d. • Only ONE fox can move each turn. • Fox can’t move onto HOME squares or eat the goose when HOME HOME goose fox
Fox and Goose Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose fox Rules: • If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. • Fox/goose can move either v/h/d. • Only ONE fox can move each turn. • Fox can’t move onto HOME squares or eat the goose when HOME HOME goose fox
Fox and Goose – more elaborate game.. Goose Goal: Get Goose home (column 4) Fox Goal: Any Fox eats Goose Rules: • If fox next (v/h/d) to goose and fox’s turn, then fox can eat goose. • Fox/goose can move either v/h/d. • Only ONE fox can move each turn. • Fox can’t move onto HOME squares or eat the goose when HOME fox HOME fox goose + Obstacles to reduce movement
2 Player Game Implementation • Board (state/world) representation • 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”) • Move generation – which moves can I make? • Move application – simulate making a move • Choose Best Move – use search (Minimax algorithm) to find best move
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
Prolog Representation Square = s(<x-value>, <y-value>, <value>) Board = b(<board number>, <list of squares>) 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 .
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 ) :- !.
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.
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
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]. Fixes Z1 Generates Generate and Test Tests parameters Deterministic procedures – only one possible output
Examples Search depth Identifier of the starting board Who goes first | ?- 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 Initial call to minimax Finds best move - not enough search space to see who wins so fox chases goose Subsequent calls to Minimax from “finish_game” procedure
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!