873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

25
8 7 3 8 [8,.] [.,8]

Upload: vivien-merritt

Post on 29-Dec-2015

246 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8 7 3

8[8,.]

[.,8]

Page 2: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8

9 1 6

9

[.,8]

[9,.]

Page 3: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8

[.,8]

2 4

[2,.]

1

[4,.]4

4

Page 4: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

1 3 5

5[1,.]

[.,5]4

[3,.]

Page 5: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

5

3 9 2

9

[.,5]

[3,.]

4

[9,.]

Page 6: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

5

[.,5]

6 5

[6,.]

2

6

54

Page 7: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

54

3

5

Page 8: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

done

• Continue with Prolog code for minimax

• Jump to alpha-beta pruning

Page 9: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

Certainly the worst value/move

/* Uses: move(+Pos, -Move) :- Move is a legal move in position Pos.

move(+Move, +Pos, -Pos1) :- Making Move in position Pos results in position Pos1.

value(+Pos, -V) :- V is the static value of position Pos for player 1. Should be between -999 and +999, where +999 is best for player 1.*/

minimax(Pos, Move, Depth) :- minimax(Depth, Pos, 1, _, Move).

/* minimax(+Depth, +Position, +Player, -BestValue, -BestMove) :- Chooses the BestMove from the from the current Position using the minimax algorithm searching Depth ply ahead. Player indicates if this move is by player (1) or opponent (-1).*/minimax(0, Position, Player, Value, _) :- value(Position, V), Value is V*Player. % Value is from the current player’s perspective.minimax(D, Position, Player, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), % There must be at least one move! minimax(Moves, Position, D1, Player, -1000, nil, Value, Move).

Page 10: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

minimax(0, Position, Player, Value, _) :- value(Position, V), Value is V*Player. % Value is from the current player’s perspective.minimax(D, Position, Player, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), % There must be at least one move! minimax(Moves, Position, D1, Player, -1000, nil, Value, Move).

/* minimax(+Moves,+Position,+Depth,+Player,+Value0,+Move0,-BestValue,-BestMove) Chooses the Best move from the list of Moves from the current Position using the minimax algorithm searching Depth ply ahead. Player indicates if we are currently minimizing (-1) or maximizing (1). Move0 records the best move found so far and Value0 its value.*/minimax([], _, _, _, Value, Best, Value, Best).minimax([Move|Moves],Position,D,Player, Value0,Move0,BestValue,BestMove):- move(Move, Position, Position1), Opponent is -Player, minimax(D, Position1, Opponent, OppValue, _OppMove), Value is -OppValue, ( Value > Value0 -> minimax(Moves,Position,D,Player, Value ,Move ,BestValue,BestMove). ; minimax(Moves,Position,D,Player, Value0,Move0,BestValue,BestMove). ).

Page 11: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8 7 3

8[8,.]

[.,8]

Page 12: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8

9

[.,8]

[9,8][.,8]

X

Page 13: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

8

[.,8]

2 4

[2,8]

1

[4,8]4

4

[.,8]

Page 14: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

1 3 5

5[4,.]

[4,5]4

[4,.]

[4,.]

Page 15: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

5

3 9

X

[4,5]

[4,5]

4

[9,5]

[4,.]

Page 16: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

5

[4,5]

6

[6,5]X

54

[4,5]

[4,.]

Page 17: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

54

[5,.]

[5,.]

[5,.]

2

[5,.]X

Page 18: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

54

5[5,.]

[5,.]

2 3

< 5[5,.]

[5, <5]X

X

Page 19: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

done

Page 20: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

1000 serves as infinity

/* Uses: move(+Pos, -Move) :-

Move is a legal move in position Pos.

move(+Move, +Pos, -Pos1) :- Making Move in position Pos results in position Pos1.

value(+Pos, -V) :- V is the static value of position Pos for player 1. Should be between -999 and +999, where +999 is best for player 1.*/

alph_bet(Pos, Move, Depth) :- alph_bet(Depth, Pos, 1, -1000, 1000, _, Move).

/* alph_bet(+Depth, +Position, +Player, +Alpha, +Beta, -BestValue, -BestMove) :- Chooses the BestMove from the from the current Position using the alpha beta algorithm searching Depth ply ahead. Player indicates if the next move is by player (1) or opponent (-1).*/alph_bet(0, Position, Player, _, _, Value, _) :- value(Position, V), Value is V*Player.alph_bet(D, Position, Player, Alpha, Beta, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), alph_bet(Moves, Position, D1, Player, Alpha, Beta, nil, Value, Move).

Page 21: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

/* alph_bet(+Moves,+Position,+Depth,+Player,+Alpha,+Beta,+Move0, -BestValue,-BestMove) Chooses the Best move from the list of Moves from the current Position using the alpha beta algorithm searching Depth ply ahead. Player indicates if the next move is by player (1) or opponent (-1). Move0 records the best move found so far and Alpha its value. If a value >= Beta is found, then this position is too good to be true: the opponent will not move us into this position.*/alph_bet([], _, _, _, Value, _, Best, Value, Best).alph_bet([Move|Moves], Position, D, Player, Alpha, Beta, Move0,

BestValue, BestMove):- move(Move, Position, Position1), Opponent is -Player, OppAlpha is -Beta, OppBeta is -Alpha, alph_bet(D, Position1, Opponent, OppAlpha, OppBeta, OppValue, _OppMove), Value is -OppValue, ( Value >= Beta -> BestValue = Value, BestMove = Move % abort: too good to be true ; Value > Alpha -> alph_bet(Moves,Position,D,Player,Value,Beta,Move ,BestValue,BestMove) ; alph_bet(Moves,Position,D,Player,Alpha,Beta,Move0,BestValue,BestMove) ).

Page 22: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

Othello

Page 23: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

Othello

> prolog –l /it/kurs/logpro/othello/play_game.pl…| ?- start_game.Select white player. (1) human (2) program|: 2.White player full program name :|: std.…Black players full program name :|: ’/home/<username>/<path>/myothello.pl’.…(Othello window pops up.)

Page 24: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

OthelloWhite player program Shell program Black player program

initialize(white,SW)initialize(black,SB)

SW SB

best_move(SB, Move)

Move=6-5

Ask move

Initialize

move(6-5, SB, NSB)Execute move(shell executes movein Othello window)

opponent_move(6-5, SW, NSW)

Page 25: 873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

OthelloWhite player program Shell program Black player program

game_over(SB, …)

Game over?no

best_move(SW, Move)

SW SB

Move = c-r

Ask move

Execute move

move(c-r, SW, NSW) opponent_move(c-r, SB, NSB)