AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email [email protected]@hud.ac.uk

13
AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email [email protected] http://scom.hud.ac.uk/scomtlm/cha2 555/

Transcript of AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email [email protected]@hud.ac.uk

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

AI – Week 11AI and Games (3)

Lee McCluskey, room 2/09

Email [email protected]

http://scom.hud.ac.uk/scomtlm/cha2555/

Page 2: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

AI in Games: practical exampleFinish 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

Page 3: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

Fox and Goose – recall from last week…

goose

fox

fox

Goal: Goose in last column

HO

ME

Page 4: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

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

Page 5: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

Last Week-Move Application/ Generation% Assume Z is either f or gapply( 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

Page 6: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

More ‘intelligent’ board evaluationboard_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),!.% defaultboard_eval(B,_, 0 ) :- !.

We let the board evaluation function have extra information “next to move”

Page 7: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

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 = 0GLOBAL 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

Page 8: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

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 boardLayer 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 moveLayer with Goose to move - same

Page 9: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

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 searchminimax(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

Page 10: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

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

BoardValBoard

Board1ValBoard1

Move x(SX,SY,f) TO x(FX,FY,V)

Depth

Depth+1

Page 11: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

Minimax Design – Part 3% Fox to move, non base caseminimax(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(_,_,_) :- !.

Page 12: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

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

Page 13: AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09 Email lee@hud.ac.uklee@hud.ac.uk

Artform Research Group

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 experimentationBUT –Its slowIt can be dirty (assert and retract)It is possible but not always easy to hook it up to other systems

(eg graphics, robots, …)