1 / 25

[8,.]

[.,8]. [8,.]. 8. 8. 7. 3. [.,8]. [9,.]. 9. 8. 9. 1. 6. [.,8]. 4. [2,.]. 8. [4,.]. 4. 1. 2. 4. [.,5]. 4. [1,.]. [3,.]. 5. 1. 3. 5. 4. [.,5]. [3,.]. [9,.]. 9. 5. 3. 9. 2. [.,5]. 4. 5. [6,.]. 5. 6. 2. 6. 5. 5. 4. 5. 3. …. done.

pooky
Download Presentation

[8,.]

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. [.,8] [8,.] 8 8 7 3

  2. [.,8] [9,.] 9 8 9 1 6

  3. [.,8] 4 [2,.] 8 [4,.] 4 1 2 4

  4. [.,5] 4 [1,.] [3,.] 5 1 3 5

  5. 4 [.,5] [3,.] [9,.] 9 5 3 9 2

  6. [.,5] 4 5 [6,.] 5 6 2 6 5

  7. 5 4 5 3 …

  8. done • Continue with Prolog code for minimax • Jump to alpha-beta pruning

  9. /* 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). Certainly the worst value/move

  10. 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). ).

  11. [.,8] [8,.] 8 8 7 3

  12. [.,8] [.,8] [9,8] X 8 9

  13. [.,8] 4 [.,8] [2,8] 8 [4,8] 4 1 2 4

  14. [4,.] [4,.] [4,5] 4 [4,.] 5 1 3 5

  15. [4,.] [4,5] 4 [4,5] 5 [9,5] X 3 9

  16. [4,.] [4,5] 4 5 [4,5] 5 [6,5] X 6

  17. [5,.] 4 5 [5,.] [5,.] [5,.] X 2

  18. 5 [5,.] 4 5 [5,.] X [5, <5] [5,.] < 5 X 2 3

  19. done

  20. /* 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). 1000 serves as infinity

  21. /* 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) ).

  22. Othello

  23. 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.)

  24. Othello White player program Shell program Black player program initialize(white,SW) initialize(black,SB) Initialize SW SB best_move(SB, Move) Ask move Move=6-5 move(6-5, SB, NSB) opponent_move(6-5, SW, NSW) Execute move (shell executes move in Othello window)

  25. Othello White player program Shell program Black player program SW SB game_over(SB, …) Game over? no best_move(SW, Move) Ask move Move = c-r move(c-r, SW, NSW) opponent_move(c-r, SB, NSB) Execute move

More Related