1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: reversi -*-
2 ;;;;***************************************************************************
4 ;;;; FILE IDENTIFICATION
6 ;;;; Name: strategies.lisp
7 ;;;; Purpose: Strategy routines for reversi
8 ;;;; Programer: Kevin Rosenberg based on code by Peter Norvig
9 ;;;; Date Started: 1 Nov 2001
11 ;;;; $Id: strategies.lisp,v 1.2 2002/10/25 09:23:39 kevin Exp $
13 ;;;; This file is Copyright (c) 2001-2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1998-2002 Peter Norvig
16 ;;;; Reversi users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;;***************************************************************************
22 (declaim (optimize (safety 1) (debug 3) (speed 3) (compilation-speed 0)))
25 (defun random-strategy (player board)
26 "Make any legal move."
27 (declare (type player player)
29 (random-elt (legal-moves player board)))
32 (defun maximize-difference (player board)
33 "A strategy that maximizes the difference in pieces."
34 (declare (type player player)
36 (funcall (maximizer #'count-difference) player board))
38 (defun maximizer (eval-fn)
39 "Return a strategy that will consider every legal move,
40 apply EVAL-FN to each resulting board, and choose
41 the move for which EVAL-FN returns the best score.
42 FN takes two arguments: the player-to-move and board"
43 #'(lambda (player board)
44 (declare (type player player)
46 (let* ((moves (legal-moves player board))
47 (scores (mapcar #'(lambda (move)
51 (make-move move player
54 (best (apply #'max scores)))
55 (declare (fixnum moves best))
56 (elt moves (position best scores)))))
58 (eval-when (:compile-toplevel :load-toplevel :execute)
59 (defparameter *weights*
60 (make-array 100 :element-type 'fixnum
61 :fill-pointer nil :adjustable nil
64 0 120 -20 20 5 5 20 -20 120 0
65 0 -20 -40 -5 -5 -5 -5 -40 -20 0
66 0 20 -5 15 3 3 15 -5 20 0
69 0 20 -5 15 3 3 15 -5 20 0
70 0 -20 -40 -5 -5 -5 -5 -40 -20 0
71 0 120 -20 20 5 5 20 -20 120 0
72 0 0 0 0 0 0 0 0 0 0)))
73 (declaim (type (simple-array fixnum (100)) *weights*))
76 (eval-when (:compile-toplevel :load-toplevel :execute)
78 (sort (loop for i from 11 to 88
79 when (<= 1 (mod i 10) 8) collect i)
80 #'> :key #'(lambda (sq) (elt *weights* sq)))))
83 (defun weighted-squares (player board)
84 "Sum of the weights of player's squares minus opponent's."
85 (declare (type player player)
87 (let ((opp (opponent player)))
88 (loop for i in all-squares
89 when (= (bref board i) player)
90 sum (aref *weights* i)
91 when (= (bref board i) opp)
92 sum (- (aref *weights* i)))))
94 (defconstant winning-value (- most-positive-fixnum 70))
95 (defconstant losing-value (+ most-negative-fixnum 70))
97 (defun final-value (player board)
98 "Is this a win, loss, or draw for player?"
99 (declare (type player player)
101 (case (signum (count-difference player board))
106 (defun final-value-weighted (player board)
107 "Is this a win, loss, or draw for player?"
108 (declare (type player player)
110 (let ((diff (count-difference player board)))
112 (-1 (+ losing-value diff))
114 (+1 (+ winning-value diff)))))
116 (defun minimax (player board ply eval-fn)
117 "Find the best move, for PLAYER, according to EVAL-FN,
118 searching PLY levels deep and backing up values."
119 (declare (type player player)
123 (funcall eval-fn player board)
124 (let ((moves (legal-moves player board)))
126 (if (any-legal-move? (opponent player) board)
127 (- (minimax (opponent player) board
129 (final-value player board))
130 (let ((best-move nil)
133 (let* ((board2 (make-move move player
136 (opponent player) board2
137 (- ply 1) eval-fn))))
138 (when (or (null best-val)
141 (setf best-move move))))
142 (values best-val best-move))))))
144 (defun minimax-searcher (ply eval-fn)
145 "A strategy that searches PLY levels and then uses EVAL-FN."
146 #'(lambda (player board)
147 (declare (type player player)
149 (multiple-value-bind (value move)
150 (minimax player board ply eval-fn)
151 (declare (ignore value))
154 (defun alpha-beta (player board achievable cutoff ply eval-fn)
155 "Find the best move, for PLAYER, according to EVAL-FN,
156 searching PLY levels deep and backing up values,
157 using cutoffs whenever possible."
158 (declare (type player player)
160 (fixnum achievable cutoff ply))
162 (funcall eval-fn player board)
163 (let ((moves (legal-moves player board)))
165 (if (any-legal-move? (opponent player) board)
166 (- (alpha-beta (opponent player) board
167 (- cutoff) (- achievable)
169 (final-value player board))
170 (let ((best-move (first moves)))
171 (declare (type move best-move))
172 (loop for move in moves do
173 (let* ((board2 (make-move move player
176 (opponent player) board2
177 (- cutoff) (- achievable)
178 (- ply 1) eval-fn))))
179 (when (> val achievable)
180 (setf achievable val)
181 (setf best-move move)))
182 until (>= achievable cutoff))
183 (values achievable best-move))))))
185 (defun alpha-beta-searcher (depth eval-fn)
186 "A strategy that searches to DEPTH and then uses EVAL-FN."
187 (declare (fixnum depth))
188 #'(lambda (player board)
189 (declare (type board board)
190 (type player player))
191 (multiple-value-bind (value move)
192 (alpha-beta player board losing-value winning-value
194 (declare (ignore value))
197 (defun modified-weighted-squares (player board)
198 "Like WEIGHTED-SQUARES, but don't take off for moving
199 near an occupied corner."
200 (declare (type player player)
202 (let ((w (weighted-squares player board)))
204 (dolist (corner '(11 18 81 88))
205 (declare (type square corner))
206 (when (not (= (bref board corner) empty))
207 (dolist (c (neighbors corner))
208 (declare (type square c))
209 (when (not (= (bref board c) empty))
210 (incf w (* (- 5 (aref *weights* c))
211 (if (= (bref board c) player)
215 (eval-when (:compile-toplevel :load-toplevel :execute)
216 (let ((neighbor-table (make-array 100 :initial-element nil)))
217 ;; Initialize the neighbor table
218 (dolist (square all-squares)
219 (declare (type square square))
220 (dolist (dir +all-directions+)
221 (declare (type dir dir))
222 (if (valid-p (+ square dir))
224 (aref neighbor-table square)))))
226 (defun neighbors (square)
227 "Return a list of all squares adjacent to a square."
228 (aref neighbor-table square))))
231 (defun mobility-simple (player board)
232 "The number of moves a player has."
233 (length (legal-moves player board)))
238 (square nil :type square)
239 (board nil :type board)
240 (value nil :type integer))
242 (defun alpha-beta-searcher2 (depth eval-fn)
243 "Return a strategy that does A-B search with sorted moves."
244 #'(lambda (player board)
245 (declare (type player player)
247 (multiple-value-bind (value node)
249 player (make-node :board board
250 :value (funcall eval-fn player board))
251 losing-value winning-value depth eval-fn)
252 (declare (ignore value))
253 (node-square node))))
255 (defun alpha-beta2 (player node achievable cutoff ply eval-fn)
256 "A-B search, sorting moves by eval-fn"
257 ;; Returns two values: achievable-value and move-to-make
259 (values (node-value node) node)
260 (let* ((board (node-board node))
261 (nodes (legal-nodes player board eval-fn)))
263 (if (any-legal-move? (opponent player) board)
264 (values (- (alpha-beta2 (opponent player)
266 (- cutoff) (- achievable)
269 (values (final-value player board) nil))
270 (let ((best-node (first nodes)))
271 (loop for move in nodes
272 for val = (- (alpha-beta2
275 (- cutoff) (- achievable)
277 do (when (> val achievable)
278 (setf achievable val)
279 (setf best-node move))
280 until (>= achievable cutoff))
281 (values achievable best-node))))))
283 (defun negate-value (node)
284 "Set the value of a node to its negative."
285 (setf (node-value node) (- (node-value node)))
288 (defun legal-nodes (player board eval-fn)
289 "Return a list of legal moves, each one packed into a node."
290 (let ((moves (legal-moves player board)))
294 (let ((new-board (make-move move player
295 (copy-board board))))
297 :square move :board new-board
298 :value (funcall eval-fn player new-board))))
300 #'> :key #'node-value)))
302 (defun alpha-beta3 (player board achievable cutoff ply eval-fn
304 (declare (type board board)
306 (type fixnum achievable cutoff ply))
307 "A-B search, putting killer move first."
309 (funcall eval-fn player board)
310 (let ((moves (put-first killer (legal-moves player board))))
312 (if (any-legal-move? (opponent player) board)
313 (- (alpha-beta3 (opponent player) board
314 (- cutoff) (- achievable)
315 (- ply 1) eval-fn nil))
316 (final-value player board))
317 (let ((best-move (first moves))
318 (new-board (svref *ply-boards* ply))
320 (killer2-val winning-value))
321 (declare (type move best-move)
322 (type board new-board)
323 (type fixnum killer2-val))
324 (loop for move in moves
325 do (multiple-value-bind (val reply)
328 (make-move move player
329 (replace-board new-board board))
330 (- cutoff) (- achievable)
331 (- ply 1) eval-fn killer2)
333 (when (> val achievable)
334 (setq achievable val)
335 (setq best-move move))
336 (when (and reply (< val killer2-val))
338 (setq killer2-val val)))
339 until (>= achievable cutoff))
340 (values achievable best-move))))))
342 (defun alpha-beta3w (player board achievable cutoff ply eval-fn
344 (declare (type board board)
346 (type fixnum achievable cutoff ply)
348 "A-B search, putting killer move first."
350 (funcall eval-fn player board)
351 (let ((moves (put-first killer (legal-moves player board))))
353 (if (any-legal-move? (opponent player) board)
354 (- (alpha-beta3 (opponent player) board
355 (- cutoff) (- achievable)
356 (- ply 1) eval-fn nil))
357 (final-value-weighted player board))
358 (let ((best-move (first moves))
359 (new-board (svref *ply-boards* ply))
361 (killer2-val winning-value))
362 (declare (type move best-move)
363 (type board new-board)
364 (type fixnum killer2-val))
365 (loop for move in moves
366 do (multiple-value-bind (val reply)
369 (make-move move player
370 (replace-board new-board board))
371 (- cutoff) (- achievable)
372 (- ply 1) eval-fn killer2)
374 (when (> val achievable)
375 (setq achievable val)
376 (setq best-move move))
377 (when (and reply (< val killer2-val))
379 (setq killer2-val val)))
380 until (>= achievable cutoff))
381 (values achievable best-move))))))
384 (defun alpha-beta-searcher3 (depth eval-fn)
385 "Return a strategy that does A-B search with killer moves."
386 #'(lambda (player board)
387 (declare (type board board)
388 (type player player))
389 (multiple-value-bind (value move)
390 (alpha-beta3 player board losing-value winning-value
392 (declare (ignore value))
395 (defun alpha-beta-searcher3w (depth eval-fn)
396 "Return a strategy that does A-B search with killer moves."
397 #'(lambda (player board)
398 (multiple-value-bind (value move)
399 (alpha-beta3w player board losing-value winning-value
401 (declare (ignore value))
404 (defun put-first (killer moves)
405 "Move the killer move to the front of moves,
406 if the killer move is in fact a legal move."
407 (if (member killer moves)
408 (cons killer (delete killer moves))
411 (defun mobility (player board)
412 "Current Mobility is the number of legal moves.
413 Potential mobility is the number of blank squares
414 adjacent to an opponent that are not legal moves.
415 Returns current and potential mobility for player."
416 (declare (type board board)
418 (optimize (speed 3) (safety 0 )))
419 (let ((opp (opponent player))
420 (current 0) ; player's current mobility
421 (potential 0)) ; player's potential mobility
422 (declare (type player opp)
423 (type fixnum current potential))
424 (dolist (square all-squares)
425 (declare (type square square))
426 (when (= (bref board square) empty)
427 (cond ((legal-p square player board)
429 ((some-neighbors board opp (neighbors square))
432 (values current (the fixnum (+ current potential)))))
435 (defun some-neighbors (board opp neighbors)
436 (declare (type board board)
438 (type cons neighbors)
439 (optimize (speed 3) (safety 0)))
441 (dolist (sq neighbors)
442 (declare (type square sq))
443 (when (= (bref board sq) opp)
444 (return-from search t)))
445 (return-from search nil)))
447 (defun edge-stability (player board)
448 "Total edge evaluation for player to move on board."
449 (declare (type board board)
450 (type player player))
451 (loop for edge-list in *edge-and-x-lists*
452 sum (aref *edge-table*
453 (edge-index player board edge-list))))
455 (defun iago-eval (player board)
456 "Combine edge-stability, current mobility and
457 potential mobility to arrive at an evaluation."
458 ;; The three factors are multiplied by coefficients
459 ;; that vary by move number:
460 (declare (type board board)
461 (type player player))
462 (let ((c-edg (+ 312000 (* 6240 *move-number*)))
463 (c-cur (if (< *move-number* 25)
464 (+ 50000 (* 2000 *move-number*))
465 (+ 75000 (* 1000 *move-number*))))
467 (declare (fixnum c-edg c-cur c-pot))
468 (multiple-value-bind (p-cur p-pot)
469 (mobility player board)
470 (multiple-value-bind (o-cur o-pot)
471 (mobility (opponent player) board)
472 ;; Combine the three factors into one sum:
473 (+ (round (* c-edg (edge-stability player board))
475 (round (* c-cur (- p-cur o-cur))
477 (round (* c-pot (- p-pot o-pot))
478 (+ p-pot o-pot 2)))))))
481 ;; Strategy Functions
484 "Use an approximation of Iago's evaluation function."
485 (alpha-beta-searcher3 depth #'iago-eval))
489 (maximizer #'count-difference))
492 (maximizer #'weighted-squares))
495 (maximizer #'modified-weighted-squares))
500 (minimax-searcher ply #'count-difference))
503 (minimax-searcher ply #'weighted-squares))
505 (defun mm-md-wt (ply)
506 (minimax-searcher ply #'modified-weighted-squares))
508 ;; Alpha-beta3 searcher
510 (alpha-beta-searcher3 ply #'count-difference))
513 (alpha-beta-searcher3 ply #'weighted-squares))
515 (defun ab3-md-wt (ply)
516 (alpha-beta-searcher3 ply #'modified-weighted-squares))
520 (alpha-beta-searcher3w ply #'count-difference))
523 (alpha-beta-searcher3w ply #'weighted-squares))
525 (defun ab3w-md-wt (ply)
526 (alpha-beta-searcher3w ply #'modified-weighted-squares))
529 (defun rr (ply n-pairs)
531 (list #'random-strategy (ab3-df ply) (ab3-wt ply) (ab3-md-wt ply) (iago 3))
534 '(random ab3-df ab3-wt ab3-md-wt iago)))