r4846: Auto commit for Debian build
[reversi.git] / edge-table.lisp
1 ;;;;***************************************************************************
2 ;;;;
3 ;;;; FILE IDENTIFICATION
4 ;;;; 
5 ;;;;  Name:           edge-table.lisp
6 ;;;;  Purpose:        Edge table routines for reversi
7 ;;;;  Programer:      Kevin M. Rosenberg based on code by Peter Norvig
8 ;;;;  Date Started:   1 Nov 2001
9 ;;;;
10 ;;;; $Id: edge-table.lisp,v 1.3 2003/05/06 15:51:20 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2001-2002 by Kevin M. Rosenberg 
13 ;;;; and Copyright (c) 1998-2002 Peter Norvig
14 ;;;;
15 ;;;; Reversi users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;;***************************************************************************
19
20
21 (in-package #:reversi)
22
23 (eval-when (:compile-toplevel)
24   (declaim (optimize (safety 1) (space 0) (speed 3) (compilation-speed 0))))
25
26 (eval-when (:compile-toplevel :load-toplevel :execute)
27   (defparameter *edge-and-x-lists*
28     '((22 11 12 13 14 15 16 17 18 27)
29       (72 81 82 83 84 85 86 87 88 77)
30       (22 11 21 31 41 51 61 71 81 72)
31       (27 18 28 38 48 58 68 78 88 77))
32     "The four edges (with their X-squares)."))
33
34 (defparameter *top-edge* (first *edge-and-x-lists*))
35
36 (defvar *edge-table* nil
37   "Array of values to player-to-move for edge positions.")
38
39 ;;(declaim (type (simple-array fixnum #.(expt 3 10)) *edge-table*))
40
41 (defun make-edge-table ()
42   (setq *edge-table* (make-array (expt 3 10) :element-type 'fixnum
43                                  :adjustable nil :fill-pointer nil))
44   (init-edge-table)
45   *edge-table*)
46
47
48 (defun map-edge-n-pieces (fn player board n squares index)
49   "Call fn on all edges with n pieces."
50   ;; Index counts 1 for player; 2 for opponent
51   (declare (fixnum index)
52            (type player player)
53            (type square index)
54            (type (simple-array fixnum (100)) board)
55            (list squares))
56   (cond
57     ((< (length squares) n) nil)
58     ((null squares) (funcall fn board index))
59     (t (let ((index3 (* 3 index))
60              (sq (first squares)))
61          (declare (fixnum index3 sq))
62          (map-edge-n-pieces fn player board n (rest squares) index3)
63          (when (and (> n 0) (eql (bref board sq) empty))
64            (setf (bref board sq) player)
65            (map-edge-n-pieces fn player board (- n 1) (rest squares)
66                               (+ 1 index3))
67            (setf (bref board sq) (opponent player))
68            (map-edge-n-pieces fn player board (- n 1) (rest squares)
69                               (+ 2 index3))
70            (setf (bref board sq) empty))))))
71
72
73
74 (defun possible-edge-moves-value (player board index)
75   "Consider all possible edge moves. 
76   Combine their values into a single number."
77   (declare (type board board)
78            (type player player)
79            (type square index))
80   (combine-edge-moves
81    (cons
82       (list 1.0 (aref *edge-table* index)) ;; no move
83       (loop for sq in *top-edge*             ;; possible moves
84             when (= (bref board sq) empty)
85             collect (possible-edge-move player board sq)))
86     player))
87
88
89 (defun edge-index (player board squares)
90   "The index counts 1 for player; 2 for opponent,
91   on each square---summed as a base 3 number."
92   (declare (type board board)
93            (type player player)
94            (type cons squares)
95            (optimize (speed 3) (safety 1)))
96   (let ((index 0))
97     (declare (fixnum index))
98     (dolist (sq squares)
99       (declare (type square sq))
100       (setq index 
101         (the fixnum 
102           (+ 
103            (the fixnum (* index 3))
104            (the fixnum (cond ((= (bref board sq) empty) 0)
105                              ((= (bref board sq) player) 1)
106                              (t 2)))))))
107     index))
108
109 (defun possible-edge-move (player board sq)
110   "Return a (prob val) pair for a possible edge move."
111   (declare (type board board)
112            (type player player)
113            (type square sq))
114   (let ((new-board (replace-board (svref *ply-boards* player) board)))
115     (make-move sq player new-board)
116     (list (edge-move-probability player board sq)
117           (- (aref *edge-table*
118                     (edge-index (opponent player)
119                                new-board *top-edge*))))))
120
121 (defun combine-edge-moves (possibilities player)
122   "Combine the best moves."
123   (let ((prob 1.0)
124         (val 0.0)
125         (fn (if (= player black) #'> #'<)))
126     (declare (short-float prob val))
127     (loop for pair in (sort possibilities fn :key #'second)
128           while (>= prob 0.0)
129         do (incf val (* prob (first pair) (second pair)))
130            (decf prob (* prob (first pair))))
131     (round val)))
132
133
134 (eval-when (:compile-toplevel :load-toplevel :execute)
135   (let ((corner/xsqs '((11 . 22) (18 . 27) (81. 72) (88 . 77))))
136     (defun corner-p (sq) (assoc sq corner/xsqs))
137     (defun x-square-p (sq) (rassoc sq corner/xsqs))
138     (defun x-square-for (corner) (cdr (assoc corner corner/xsqs)))
139     (defun corner-for (xsq) (car (rassoc xsq corner/xsqs)))))
140
141 (defun edge-move-probability (player board square)
142   "What's the probability that player can move to this square?"
143   (declare (type board board)
144            (type player player)
145            (type square square))
146   (cond
147     ((x-square-p square) .5) ;; X-squares
148     ((legal-p square player board) 1.0) ;; immediate capture
149     ((corner-p square) ;; move to corner depends on X-square
150      (let ((x-sq (x-square-for square)))
151        (declare (type square x-sq))
152        (cond
153          ((= (bref board x-sq) empty) .1)
154          ((= (bref board x-sq) player) 0.001)
155          (t .9))))
156     (t (/ (aref
157             '#2A((.1  .4 .7)
158                  (.05 .3  *)
159                  (.01  *  *))
160             (count-edge-neighbors player board square)
161             (count-edge-neighbors (opponent player) board square))
162           (if (legal-p square (opponent player) board) 2 1)))))
163
164 (defun count-edge-neighbors (player board square)
165   "Count the neighbors of this square occupied by player."
166   (declare (type board board)
167            (type player player)
168            (type square square))
169   (count-if #'(lambda (inc)
170                 (declare (type square inc))
171                 (= (bref board (+ square inc)) player))
172             '(+1 -1)))
173
174 (defparameter *static-edge-table*
175   '#2A(;stab  semi    un 
176        (   *    0 -2000) ; X
177        ( 700    *     *) ; corner
178        (1200  200   -25) ; C
179        (1000  200    75) ; A
180        (1000  200    50) ; B
181        (1000  200    50) ; B
182        (1000  200    75) ; A
183        (1200  200   -25) ; C
184        ( 700    *     *) ; corner
185        (   *    0 -2000) ; X
186        ))
187 (declaim (type (simple-array t (* *)) *static-edge-table*))
188
189 (defun static-edge-stability (player board)
190   "Compute this edge's static stability"
191   (declare (type board board)
192            (type player player))
193   (loop for sq in *top-edge*
194       for i from 0
195       sum (the fixnum 
196             (cond
197              ((= (bref board sq) empty) 0)
198              ((= (bref board sq) player)
199               (aref *static-edge-table* i
200                     (piece-stability board sq)))
201              (t (- (aref *static-edge-table* i
202                          (piece-stability board sq))))))))
203
204 (eval-when (:compile-toplevel :load-toplevel :execute)
205   (let ((stable 0) (semi-stable 1) (unstable 2))
206     (declaim (type fixnum stable semi-stable unstable))
207     
208     (defun piece-stability (board sq)
209       (declare (type board board)
210                (fixnum sq))
211     (cond
212       ((corner-p sq) stable)
213       ((x-square-p sq)
214        (if (eql (bref board (corner-for sq)) empty)
215            unstable semi-stable))
216       (t (let* ((player (bref board sq))
217                 (opp (opponent player))
218                 (p1 (find player board :test-not #'eql
219                           :start sq :end 19))
220                 (p2 (find player board :test-not #'eql
221                           :start 11 :end sq
222                           :from-end t)))
223            (declare (fixnum player opp))
224            (cond
225              ;; unstable pieces can be captured immediately
226              ;; by playing in the empty square
227              ((or (and (eql p1 empty) (eql p2 opp))
228                   (and (eql p2 empty) (eql p1 opp)))
229               unstable)
230              ;; Semi-stable pieces might be captured
231              ((and (eql p1 opp) (eql p2 opp)
232                    (find empty board :start 11 :end 19))
233               semi-stable)
234              ((and (eql p1 empty) (eql p2 empty))
235               semi-stable)
236              ;; Stable pieces can never be captured
237              (t stable))))))))
238
239
240 (defun init-edge-table ()
241   "Initialize *edge-table*, starting from the empty board."
242   ;; Initialize the static values
243   (loop for n-pieces from 0 to 10 do 
244         (map-edge-n-pieces
245          #'(lambda (board index)
246              (declare (type board board)
247                       (fixnum index))
248               (setf (aref *edge-table* index)
249                     (the fixnum (static-edge-stability black board))))
250          black (initial-board) n-pieces *top-edge* 0))
251   ;; Now iterate five times trying to improve:
252   (dotimes (i 5) 
253     (declare (fixnum i))
254     ;; Do the indexes with most pieces first
255     (loop for n-pieces from 9 downto 1 do 
256           (map-edge-n-pieces
257             #'(lambda (board index)
258              (declare (type board board)
259                       (fixnum index))
260                 (setf (aref *edge-table* index)
261                       (the fixnum (possible-edge-moves-value black board index))))
262             black (initial-board) n-pieces *top-edge* 0))))
263