Download presentation
Presentation is loading. Please wait.
Published byWesley Potter Modified over 9 years ago
1
AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cha2555/
2
Artform Research Group AI in Games: practical example Finish off implementation of a 2-player board game in Prolog. 1. Board (state) representation 2. Board (state) evaluation 3. Move application 4. Move generation 5. Best Move choice: Search (Mini-max) algorithm This week we will cover 4, but leave out alpha/beta pruning! LAST WEEK THIS WEEK
3
Artform Research Group Fox and Goose – recall from last week… goose fox Goal: Goose in last column HOME
4
Artform Research Group 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
Artform Research Group Last Week-Move Application/ Generation % 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]. Generate and Test Fixes parameters Tests parameters Generates Deterministic procedures – only one possible output
6
Artform Research Group 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
Artform Research Group 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)])), …. % minimax initialisation - 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
Artform Research Group MiniMax: outline spec n Bottom Layer -Base Case - we have reached the limit (depth) of the Game Tree u Use static evaluation to get a value for the board n Layer with Fox to move: u Generate Next Fox Move u Initialise Level Value to Worst u Minimax with goose to move u Eventually get value for move u If it is better than current stored move then change current stored best move n Layer with Goose to move - same
9
Artform Research Group 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
Artform Research Group Minimax Design – Part 2 % 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. Goose to go Fox to go Board ValBoard Board1 ValBoard1 Move x(SX,SY,f) TO x(FX,FY,V) Depth Depth+1
11
Artform Research Group 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
Artform Research Group Conclusions (AI and Games) To build AI into a game one method is to: u Design a representation of the world (game) state u Design a static state evaluation function u Design Move application and Move generation simulation functions (possibly use an explicit representation for moves) u 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
Artform Research Group Conclusions (Prolog) Prolog is a good implementation platform for AI because: u Very expressive state/action representations can be written as Prolog facts – even containing parameters u It has a form of Depth First search built into it, and its backtracking technique allows the implementation of any type of search strategy u Its parameter matching techniques allows procedures to be used for more than one function (eg move application AND generation) u Its procedures can be interpreted as rules u 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, …)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.