1 ;;;;***************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: strategies.cl
6 ;;;; Purpose: Strategy routines for reversi
7 ;;;; Programer: Kevin M. Rosenberg, M.D.
8 ;;;; Date Started: 1 Nov 2001
9 ;;;; CVS Id: $Id: strategies.lisp,v 1.1 2002/10/25 08:36:42 kevin Exp $
11 ;;;;***************************************************************************
14 (declaim (optimize (safety 1) (debug 3) (speed 3) (compilation-speed 0)))
17 (defun random-strategy (player board)
18 "Make any legal move."
19 (declare (type player player)
21 (random-elt (legal-moves player board)))
24 (defun maximize-difference (player board)
25 "A strategy that maximizes the difference in pieces."
26 (declare (type player player)
28 (funcall (maximizer #'count-difference) player board))
30 (defun maximizer (eval-fn)
31 "Return a strategy that will consider every legal move,
32 apply EVAL-FN to each resulting board, and choose
33 the move for which EVAL-FN returns the best score.
34 FN takes two arguments: the player-to-move and board"
35 #'(lambda (player board)
36 (declare (type player player)
38 (let* ((moves (legal-moves player board))
39 (scores (mapcar #'(lambda (move)
43 (make-move move player
46 (best (apply #'max scores)))
47 (declare (fixnum moves best))
48 (elt moves (position best scores)))))
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51 (defparameter *weights*
52 (make-array 100 :element-type 'fixnum
53 :fill-pointer nil :adjustable nil
56 0 120 -20 20 5 5 20 -20 120 0
57 0 -20 -40 -5 -5 -5 -5 -40 -20 0
58 0 20 -5 15 3 3 15 -5 20 0
61 0 20 -5 15 3 3 15 -5 20 0
62 0 -20 -40 -5 -5 -5 -5 -40 -20 0
63 0 120 -20 20 5 5 20 -20 120 0
64 0 0 0 0 0 0 0 0 0 0)))
65 (declaim (type (simple-array fixnum (100)) *weights*))
68 (eval-when (:compile-toplevel :load-toplevel :execute)
70 (sort (loop for i from 11 to 88
71 when (<= 1 (mod i 10) 8) collect i)
72 #'> :key #'(lambda (sq) (elt *weights* sq)))))
75 (defun weighted-squares (player board)
76 "Sum of the weights of player's squares minus opponent's."
77 (declare (type player player)
79 (let ((opp (opponent player)))
80 (loop for i in all-squares
81 when (= (bref board i) player)
82 sum (aref *weights* i)
83 when (= (bref board i) opp)
84 sum (- (aref *weights* i)))))
86 (defconstant winning-value (- most-positive-fixnum 70))
87 (defconstant losing-value (+ most-negative-fixnum 70))
89 (defun final-value (player board)
90 "Is this a win, loss, or draw for player?"
91 (declare (type player player)
93 (case (signum (count-difference player board))
98 (defun final-value-weighted (player board)
99 "Is this a win, loss, or draw for player?"
100 (declare (type player player)
102 (let ((diff (count-difference player board)))
104 (-1 (+ losing-value diff))
106 (+1 (+ winning-value diff)))))
108 (defun minimax (player board ply eval-fn)
109 "Find the best move, for PLAYER, according to EVAL-FN,
110 searching PLY levels deep and backing up values."
111 (declare (type player player)
115 (funcall eval-fn player board)
116 (let ((moves (legal-moves player board)))
118 (if (any-legal-move? (opponent player) board)
119 (- (minimax (opponent player) board
121 (final-value player board))
122 (let ((best-move nil)
125 (let* ((board2 (make-move move player
128 (opponent player) board2
129 (- ply 1) eval-fn))))
130 (when (or (null best-val)
133 (setf best-move move))))
134 (values best-val best-move))))))
136 (defun minimax-searcher (ply eval-fn)
137 "A strategy that searches PLY levels and then uses EVAL-FN."
138 #'(lambda (player board)
139 (declare (type player player)
141 (multiple-value-bind (value move)
142 (minimax player board ply eval-fn)
143 (declare (ignore value))
146 (defun alpha-beta (player board achievable cutoff ply eval-fn)
147 "Find the best move, for PLAYER, according to EVAL-FN,
148 searching PLY levels deep and backing up values,
149 using cutoffs whenever possible."
150 (declare (type player player)
152 (fixnum achievable cutoff ply))
154 (funcall eval-fn player board)
155 (let ((moves (legal-moves player board)))
157 (if (any-legal-move? (opponent player) board)
158 (- (alpha-beta (opponent player) board
159 (- cutoff) (- achievable)
161 (final-value player board))
162 (let ((best-move (first moves)))
163 (declare (type move best-move))
164 (loop for move in moves do
165 (let* ((board2 (make-move move player
168 (opponent player) board2
169 (- cutoff) (- achievable)
170 (- ply 1) eval-fn))))
171 (when (> val achievable)
172 (setf achievable val)
173 (setf best-move move)))
174 until (>= achievable cutoff))
175 (values achievable best-move))))))
177 (defun alpha-beta-searcher (depth eval-fn)
178 "A strategy that searches to DEPTH and then uses EVAL-FN."
179 (declare (fixnum depth))
180 #'(lambda (player board)
181 (declare (type board board)
182 (type player player))
183 (multiple-value-bind (value move)
184 (alpha-beta player board losing-value winning-value
186 (declare (ignore value))
189 (defun modified-weighted-squares (player board)
190 "Like WEIGHTED-SQUARES, but don't take off for moving
191 near an occupied corner."
192 (declare (type player player)
194 (let ((w (weighted-squares player board)))
196 (dolist (corner '(11 18 81 88))
197 (declare (type square corner))
198 (when (not (= (bref board corner) empty))
199 (dolist (c (neighbors corner))
200 (declare (type square c))
201 (when (not (= (bref board c) empty))
202 (incf w (* (- 5 (aref *weights* c))
203 (if (= (bref board c) player)
207 (eval-when (:compile-toplevel :load-toplevel :execute)
208 (let ((neighbor-table (make-array 100 :initial-element nil)))
209 ;; Initialize the neighbor table
210 (dolist (square all-squares)
211 (declare (type square square))
212 (dolist (dir +all-directions+)
213 (declare (type dir dir))
214 (if (valid-p (+ square dir))
216 (aref neighbor-table square)))))
218 (defun neighbors (square)
219 "Return a list of all squares adjacent to a square."
220 (aref neighbor-table square))))
223 (defun mobility-simple (player board)
224 "The number of moves a player has."
225 (length (legal-moves player board)))
230 (square nil :type square)
231 (board nil :type board)
232 (value nil :type integer))
234 (defun alpha-beta-searcher2 (depth eval-fn)
235 "Return a strategy that does A-B search with sorted moves."
236 #'(lambda (player board)
237 (declare (type player player)
239 (multiple-value-bind (value node)
241 player (make-node :board board
242 :value (funcall eval-fn player board))
243 losing-value winning-value depth eval-fn)
244 (declare (ignore value))
245 (node-square node))))
247 (defun alpha-beta2 (player node achievable cutoff ply eval-fn)
248 "A-B search, sorting moves by eval-fn"
249 ;; Returns two values: achievable-value and move-to-make
251 (values (node-value node) node)
252 (let* ((board (node-board node))
253 (nodes (legal-nodes player board eval-fn)))
255 (if (any-legal-move? (opponent player) board)
256 (values (- (alpha-beta2 (opponent player)
258 (- cutoff) (- achievable)
261 (values (final-value player board) nil))
262 (let ((best-node (first nodes)))
263 (loop for move in nodes
264 for val = (- (alpha-beta2
267 (- cutoff) (- achievable)
269 do (when (> val achievable)
270 (setf achievable val)
271 (setf best-node move))
272 until (>= achievable cutoff))
273 (values achievable best-node))))))
275 (defun negate-value (node)
276 "Set the value of a node to its negative."
277 (setf (node-value node) (- (node-value node)))
280 (defun legal-nodes (player board eval-fn)
281 "Return a list of legal moves, each one packed into a node."
282 (let ((moves (legal-moves player board)))
286 (let ((new-board (make-move move player
287 (copy-board board))))
289 :square move :board new-board
290 :value (funcall eval-fn player new-board))))
292 #'> :key #'node-value)))
294 (defun alpha-beta3 (player board achievable cutoff ply eval-fn
296 (declare (type board board)
298 (type fixnum achievable cutoff ply))
299 "A-B search, putting killer move first."
301 (funcall eval-fn player board)
302 (let ((moves (put-first killer (legal-moves player board))))
304 (if (any-legal-move? (opponent player) board)
305 (- (alpha-beta3 (opponent player) board
306 (- cutoff) (- achievable)
307 (- ply 1) eval-fn nil))
308 (final-value player board))
309 (let ((best-move (first moves))
310 (new-board (svref *ply-boards* ply))
312 (killer2-val winning-value))
313 (declare (type move best-move)
314 (type board new-board)
315 (type fixnum killer2-val))
316 (loop for move in moves
317 do (multiple-value-bind (val reply)
320 (make-move move player
321 (replace-board new-board board))
322 (- cutoff) (- achievable)
323 (- ply 1) eval-fn killer2)
325 (when (> val achievable)
326 (setq achievable val)
327 (setq best-move move))
328 (when (and reply (< val killer2-val))
330 (setq killer2-val val)))
331 until (>= achievable cutoff))
332 (values achievable best-move))))))
334 (defun alpha-beta3w (player board achievable cutoff ply eval-fn
336 (declare (type board board)
338 (type fixnum achievable cutoff ply)
340 "A-B search, putting killer move first."
342 (funcall eval-fn player board)
343 (let ((moves (put-first killer (legal-moves player board))))
345 (if (any-legal-move? (opponent player) board)
346 (- (alpha-beta3 (opponent player) board
347 (- cutoff) (- achievable)
348 (- ply 1) eval-fn nil))
349 (final-value-weighted player board))
350 (let ((best-move (first moves))
351 (new-board (svref *ply-boards* ply))
353 (killer2-val winning-value))
354 (declare (type move best-move)
355 (type board new-board)
356 (type fixnum killer2-val))
357 (loop for move in moves
358 do (multiple-value-bind (val reply)
361 (make-move move player
362 (replace-board new-board board))
363 (- cutoff) (- achievable)
364 (- ply 1) eval-fn killer2)
366 (when (> val achievable)
367 (setq achievable val)
368 (setq best-move move))
369 (when (and reply (< val killer2-val))
371 (setq killer2-val val)))
372 until (>= achievable cutoff))
373 (values achievable best-move))))))
376 (defun alpha-beta-searcher3 (depth eval-fn)
377 "Return a strategy that does A-B search with killer moves."
378 #'(lambda (player board)
379 (declare (type board board)
380 (type player player))
381 (multiple-value-bind (value move)
382 (alpha-beta3 player board losing-value winning-value
384 (declare (ignore value))
387 (defun alpha-beta-searcher3w (depth eval-fn)
388 "Return a strategy that does A-B search with killer moves."
389 #'(lambda (player board)
390 (multiple-value-bind (value move)
391 (alpha-beta3w player board losing-value winning-value
393 (declare (ignore value))
396 (defun put-first (killer moves)
397 "Move the killer move to the front of moves,
398 if the killer move is in fact a legal move."
399 (if (member killer moves)
400 (cons killer (delete killer moves))
403 (defun mobility (player board)
404 "Current Mobility is the number of legal moves.
405 Potential mobility is the number of blank squares
406 adjacent to an opponent that are not legal moves.
407 Returns current and potential mobility for player."
408 (declare (type board board)
410 (optimize (speed 3) (safety 0 )))
411 (let ((opp (opponent player))
412 (current 0) ; player's current mobility
413 (potential 0)) ; player's potential mobility
414 (declare (type player opp)
415 (type fixnum current potential))
416 (dolist (square all-squares)
417 (declare (type square square))
418 (when (= (bref board square) empty)
419 (cond ((legal-p square player board)
421 ((some-neighbors board opp (neighbors square))
424 (values current (the fixnum (+ current potential)))))
427 (defun some-neighbors (board opp neighbors)
428 (declare (type board board)
430 (type cons neighbors)
431 (optimize (speed 3) (safety 0)))
433 (dolist (sq neighbors)
434 (declare (type square sq))
435 (when (= (bref board sq) opp)
436 (return-from search t)))
437 (return-from search nil)))
439 (defun edge-stability (player board)
440 "Total edge evaluation for player to move on board."
441 (declare (type board board)
442 (type player player))
443 (loop for edge-list in *edge-and-x-lists*
444 sum (aref *edge-table*
445 (edge-index player board edge-list))))
447 (defun iago-eval (player board)
448 "Combine edge-stability, current mobility and
449 potential mobility to arrive at an evaluation."
450 ;; The three factors are multiplied by coefficients
451 ;; that vary by move number:
452 (declare (type board board)
453 (type player player))
454 (let ((c-edg (+ 312000 (* 6240 *move-number*)))
455 (c-cur (if (< *move-number* 25)
456 (+ 50000 (* 2000 *move-number*))
457 (+ 75000 (* 1000 *move-number*))))
459 (declare (fixnum c-edg c-cur c-pot))
460 (multiple-value-bind (p-cur p-pot)
461 (mobility player board)
462 (multiple-value-bind (o-cur o-pot)
463 (mobility (opponent player) board)
464 ;; Combine the three factors into one sum:
465 (+ (round (* c-edg (edge-stability player board))
467 (round (* c-cur (- p-cur o-cur))
469 (round (* c-pot (- p-pot o-pot))
470 (+ p-pot o-pot 2)))))))
473 ;; Strategy Functions
476 "Use an approximation of Iago's evaluation function."
477 (alpha-beta-searcher3 depth #'iago-eval))
481 (maximizer #'count-difference))
484 (maximizer #'weighted-squares))
487 (maximizer #'modified-weighted-squares))
492 (minimax-searcher ply #'count-difference))
495 (minimax-searcher ply #'weighted-squares))
497 (defun mm-md-wt (ply)
498 (minimax-searcher ply #'modified-weighted-squares))
500 ;; Alpha-beta3 searcher
502 (alpha-beta-searcher3 ply #'count-difference))
505 (alpha-beta-searcher3 ply #'weighted-squares))
507 (defun ab3-md-wt (ply)
508 (alpha-beta-searcher3 ply #'modified-weighted-squares))
512 (alpha-beta-searcher3w ply #'count-difference))
515 (alpha-beta-searcher3w ply #'weighted-squares))
517 (defun ab3w-md-wt (ply)
518 (alpha-beta-searcher3w ply #'modified-weighted-squares))
521 (defun rr (ply n-pairs)
523 (list #'random-strategy (ab3-df ply) (ab3-wt ply) (ab3-md-wt ply) (iago 3))
526 '(random ab3-df ab3-wt ab3-md-wt iago)))