1 / 13

AI – Week 11 AI and Games (3)

AI – Week 11 AI and Games (3). Lee McCluskey, room 2/09 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/. AI in Games: practical example. Finish off implementation of a 2-player board game in Prolog. Board (state) representation Board (state) evaluation Move application

Télécharger la présentation

AI – Week 11 AI and Games (3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. AI in Games: practical example Finish off implementation of a 2-player board game in Prolog. • Board (state) representation • Board (state) evaluation • Move application • Move generation • Best Move choice: Search (Mini-max) algorithm This week we will cover 4, but leave out alpha/beta pruning! LAST WEEK THIS WEEK

  3. Fox and Goose – recall from last week… Goal: Goose in last column fox HOME fox goose

  4. Last Week - Representation of Board b(1, [ x(1,1,w), x(1,2,w), x(1,3,w), x(1,4,0), x(2,1,w), x(2,2,0), x(2,3,w), x(2,4,0), x(3,1,0), x(3,2,f), x(3,3,0), x(3,4,0), x(4,1,0), x(4,2,0), x(4,3,f), x(4,4,0), x(5,1,g), x(5,2,0), x(5,3,0), x(5,4,0), x(6,1,0), x(6,2,w), x(6,3,w), x(6,4,0), x(7,1,0), x(7,2,0), x(7,3,0), x(7,4,0), ]). NB ORDER of co-ordinate data in list is NOT RELEVANT

  5. Last Week-Move Application/ Generation Fixes parameters % Assume Z is either f or g apply( x(X,Y,Z), x(X1,Y1,Z1), B_IN, B_OUT) :- % move to empty square OR fox eats goose .. (Z1 = 0 ; (Z=f, Z1=g)), member(x(X,Y,Z), B_IN), member(x(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(x(X,Y,Z), B_IN, B1), remove(x(X1,Y1,Z1), B1, B2), B_OUT = [x(X1,Y1,Z),x(X,Y,0)|B2]. Generates Generate and Test Tests parameters Deterministic procedures – only one possible output

  6. More ‘intelligent’ board evaluation board_eval(B, _, 10000) :- % if Goose is in column 4 then +10000 member(x(_,4,g), B),!. board_eval(B, goose, 10000) :- % if Goose to move in col 3 then +10000 member(x(_,3,g), B),!. board_eval(B, goose, -10) :- % if fox next to goose and goose to move then -10 member(x(X,Y,g), B), member(x(X1,Y1,f), B), next(X,Y,X1,Y1),!. board_eval(B, _, -10000) :- % if no Goose then -10000 \+ member(x(_,_,g), B),!. board_eval(B, fox, -10000) :- % if fox next to goose and fox to move then -10000 member(x(X,Y,g), B), member(x(X1,Y1,f), B), next(X,Y,X1,Y1),!. board_eval(B, fox, -5) :- % if fox one away from goose and fox to move then -5 member(x(X,Y,g), B), member(x(X1,Y1,f), B), member(x(X2,Y2,0), B), next(X,Y,X2,Y2), next(X2,Y2,X1,Y1),!. % default board_eval(B,_, 0 ) :- !. We let the board evaluation function have extra information “next to move”

  7. MiniMax: Initialisation and I/O Spec *** minimax(B,F_OR_G,0) *** INPUTS: PARAMETERS: BOARD (B), who is move (F_OR_G), start ply = 0 GLOBAL DATA: - Leaf count (leaf_count(0) – a fact), - Search Depth( limit(D) – a fact) - Initiative data structure attached to the route node – level( LEVEL OF NODE, VALUE OF BOARD, BEST MOVE) – initially level(0,worst value, don’t know) OUTPUTS: The best move attached to the root node and its value – line retract(level(0,Val,[x(A1,B1,Z1),x(A2,B2,Z2)])), …. % minimaxinitialisation - assert_start_information(fox,D) :- assert(level(0,1000000,_)), % starts with worst value for fox assert(leaf_count(0)), % for statistics assert(limit(D)), !. % asserts the SEARCH DEPTH as a fact

  8. MiniMax: outline spec • Bottom Layer -Base Case - we have reached the limit (depth) of the Game Tree • Use static evaluation to get a value for the board • Layer with Fox to move: • Generate Next Fox Move • Initialise Level Value to Worst • Minimax with goose to move • Eventually get value for move • If it is better than current stored move then change current stored best move • Layer with Goose to move - same

  9. Minimax Design – Bottom Layer % BASE case - we are at the bottom of the search tree % Player = who's turn, Depth = depth of search minimax(Board,Player,Depth) :- limit(Depthlimit), Depthlimit = Depth, % we are at the bottom of the search tree board_eval(Board,Player,Boardval), retract(level(Depth,_,_)), assert(level(Depth,Boardval,_)), The level(Depth,Boardval,M) predicate is like a global variable – it stores: Depth – current depth of search Boardval – estimated value of board M – current best move LEADING FROM current board

  10. Minimax Design – Part 2 Move x(SX,SY,f) TO x(FX,FY,V) % RECURSIVE case - we are NOT at the bottom of the search tree % Board = BOARD, fox's turn, Depth = current depth of search minimax(Board,fox,Depth) :- % generate the next fox move and new board Board1 apply(x(SX,SY,f), x(FX,FY,V), Board,Board1), % initialise new level Depth1, and dummy val. -10000 Depth1 is Depth+1, assert(level(Depth1,-10000,_)), % find the best value of Board1 - goose to go minimax(Board1,goose,Depth1), retract(level(Depth1,ValBoard1,_)), level(Depth,ValBoard,Move), % if the new value recorded is better (less) that the % current value of this node then % record the new value ValBoard1 < ValBoard, retract(level(Depth,ValBoard,Move)), assert(level(Depth,ValBoard1,[x(SX,SY,f), x(FX,FY,V)])), % now backtrack to “apply” to get the next move fail. Board ValBoard Depth Fox to go Depth+1 Board1 ValBoard1 Goose to go

  11. Minimax Design – Part 3 % Fox to move, non base case minimax(Board,goose,Depth) :- % generate the next fox move and new board Board1 apply(x(SX,SY,g), x(FX,FY,0), Board,Board1), % initialise new level Depth1, and dummy valuation 10000 Depth1 is Depth+1, assert(level(Depth1,10000,_)), % find the best value of Board1 - fox to go minimax(Board1,fox,Depth1), retract(level(Depth1,ValBoard1,_)), level(Depth,ValBoard,Move), % if the new value recorded is better (more) that the % current value of this node then % record the new value ValBoard1 > ValBoard, retract(level(Depth,ValBoard,Move)), assert(level(Depth,ValBoard1,[x(SX,SY,g), x(FX,FY,0)])), fail. minimax(_,_,_) :- !.

  12. Conclusions (AI and Games) To build AI into a game one method is to: • Design a representation of the world (game) state • Design a static state evaluation function • Design Move application and Move generation simulation functions (possibly use an explicit representation for moves) • Design a search algorithm that searches through states to find the optimum move For two player/turns/perfect info games, a good search techniques is Minimax

  13. Conclusions (Prolog) Prolog is a good implementation platform for AI because: • Very expressive state/action representations can be written as Prolog facts – even containing parameters • It has a form of Depth First search built into it, and its backtracking technique allows the implementation of any type of search strategy • Its parameter matching techniques allows procedures to be used for more than one function (eg move application AND generation) • Its procedures can be interpreted as rules • Its interpreted nature is flexible to allow easy experimentation BUT – Its slow It can be dirty (assert and retract) It is possible but not always easy to hook it up to other systems (eg graphics, robots, …)

More Related