Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / play / landmark.el
blob5c516e70f9901254d3057c1420a3eace161e142c
1 ;;; landmark.el --- neural-network robot that learns landmarks
3 ;; Copyright (C) 1996-1997, 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Terrence Brannon (was: <brannon@rana.usc.edu>)
6 ;; Created: December 16, 1996 - first release to usenet
7 ;; Keywords: games, gomoku, neural network, adaptive search, chemotaxis
9 ;;;_* Usage
10 ;;; Just type
11 ;;; M-x eval-buffer
12 ;;; M-x landmark-test-run
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
32 ;; Landmark is a relatively non-participatory game in which a robot
33 ;; attempts to maneuver towards a tree at the center of the window
34 ;; based on unique olfactory cues from each of the 4 directions. If
35 ;; the smell of the tree increases, then the weights in the robot's
36 ;; brain are adjusted to encourage this odor-driven behavior in the
37 ;; future. If the smell of the tree decreases, the robots weights are
38 ;; adjusted to discourage a correct move.
40 ;; In laymen's terms, the search space is initially flat. The point
41 ;; of training is to "turn up the edges of the search space" so that
42 ;; the robot rolls toward the center.
44 ;; Further, do not become alarmed if the robot appears to oscillate
45 ;; back and forth between two or a few positions. This simply means
46 ;; it is currently caught in a local minimum and is doing its best to
47 ;; work its way out.
49 ;; The version of this program as described has a small problem. a
50 ;; move in a net direction can produce gross credit assignment. for
51 ;; example, if moving south will produce positive payoff, then, if in
52 ;; a single move, one moves east,west and south, then both east and
53 ;; west will be improved when they shouldn't
55 ;; Many thanks to Yuri Pryadkin <yuri@rana.usc.edu> for this
56 ;; concise problem description.
58 ;;;_* Require
59 (eval-when-compile (require 'cl-lib))
61 ;;;_* From Gomoku
63 ;;; Code:
65 (defgroup landmark nil
66 "Neural-network robot that learns landmarks."
67 :prefix "landmark-"
68 :group 'games)
70 ;;;_ + THE BOARD.
72 ;; The board is a rectangular grid. We code empty squares with 0, X's with 1
73 ;; and O's with 6. The rectangle is recorded in a one dimensional vector
74 ;; containing padding squares (coded with -1). These squares allow us to
75 ;; detect when we are trying to move out of the board. We denote a square by
76 ;; its (X,Y) coords, or by the INDEX corresponding to them in the vector. The
77 ;; leftmost topmost square has coords (1,1) and index landmark-board-width + 2.
78 ;; Similarly, vectors between squares may be given by two DX, DY coords or by
79 ;; one DEPL (the difference between indexes).
81 (defvar landmark-board-width nil
82 "Number of columns on the Landmark board.")
83 (defvar landmark-board-height nil
84 "Number of lines on the Landmark board.")
86 (defvar landmark-board nil
87 "Vector recording the actual state of the Landmark board.")
89 (defvar landmark-vector-length nil
90 "Length of landmark-board vector.")
92 (defvar landmark-draw-limit nil
93 ;; This is usually set to 70% of the number of squares.
94 "After how many moves will Emacs offer a draw?")
96 (defvar landmark-cx 0
97 "This is the x coordinate of the center of the board.")
99 (defvar landmark-cy 0
100 "This is the y coordinate of the center of the board.")
102 (defvar landmark-m 0
103 "This is the x dimension of the playing board.")
105 (defvar landmark-n 0
106 "This is the y dimension of the playing board.")
109 (defun landmark-xy-to-index (x y)
110 "Translate X, Y cartesian coords into the corresponding board index."
111 (+ (* y landmark-board-width) x y))
113 (defun landmark-index-to-x (index)
114 "Return corresponding x-coord of board INDEX."
115 (% index (1+ landmark-board-width)))
117 (defun landmark-index-to-y (index)
118 "Return corresponding y-coord of board INDEX."
119 (/ index (1+ landmark-board-width)))
121 (defun landmark-init-board ()
122 "Create the landmark-board vector and fill it with initial values."
123 (setq landmark-board (make-vector landmark-vector-length 0))
124 ;; Every square is 0 (i.e. empty) except padding squares:
125 (let ((i 0) (ii (1- landmark-vector-length)))
126 (while (<= i landmark-board-width) ; The squares in [0..width] and in
127 (aset landmark-board i -1) ; [length - width - 1..length - 1]
128 (aset landmark-board ii -1) ; are padding squares.
129 (setq i (1+ i)
130 ii (1- ii))))
131 (let ((i 0))
132 (while (< i landmark-vector-length)
133 (aset landmark-board i -1) ; and also all k*(width+1)
134 (setq i (+ i landmark-board-width 1)))))
136 ;;;_ + DISPLAYING THE BOARD.
138 ;; You may change these values if you have a small screen or if the squares
139 ;; look rectangular, but spacings SHOULD be at least 2 (MUST BE at least 1).
141 (defconst landmark-square-width 2
142 "Horizontal spacing between squares on the Landmark board.")
144 (defconst landmark-square-height 1
145 "Vertical spacing between squares on the Landmark board.")
147 (defconst landmark-x-offset 3
148 "Number of columns between the Landmark board and the side of the window.")
150 (defconst landmark-y-offset 1
151 "Number of lines between the Landmark board and the top of the window.")
154 ;;;_ + LANDMARK MODE AND KEYMAP.
156 (defcustom landmark-mode-hook nil
157 "If non-nil, its value is called on entry to Landmark mode."
158 :type 'hook
159 :group 'landmark)
161 (defvar landmark-mode-map
162 (let ((map (make-sparse-keymap)))
163 ;; Key bindings for cursor motion.
164 (define-key map "y" 'landmark-move-nw) ; y
165 (define-key map "u" 'landmark-move-ne) ; u
166 (define-key map "b" 'landmark-move-sw) ; b
167 (define-key map "n" 'landmark-move-se) ; n
168 (define-key map "h" 'backward-char) ; h
169 (define-key map "l" 'forward-char) ; l
170 (define-key map "j" 'landmark-move-down) ; j
171 (define-key map "k" 'landmark-move-up) ; k
173 (define-key map [kp-7] 'landmark-move-nw)
174 (define-key map [kp-9] 'landmark-move-ne)
175 (define-key map [kp-1] 'landmark-move-sw)
176 (define-key map [kp-3] 'landmark-move-se)
177 (define-key map [kp-4] 'backward-char)
178 (define-key map [kp-6] 'forward-char)
179 (define-key map [kp-2] 'landmark-move-down)
180 (define-key map [kp-8] 'landmark-move-up)
182 (define-key map "\C-n" 'landmark-move-down) ; C-n
183 (define-key map "\C-p" 'landmark-move-up) ; C-p
185 ;; Key bindings for entering Human moves.
186 (define-key map "X" 'landmark-human-plays) ; X
187 (define-key map "x" 'landmark-human-plays) ; x
189 (define-key map " " 'landmark-start-robot) ; SPC
190 (define-key map [down-mouse-1] 'landmark-start-robot)
191 (define-key map [drag-mouse-1] 'landmark-click)
192 (define-key map [mouse-1] 'landmark-click)
193 (define-key map [down-mouse-2] 'landmark-click)
194 (define-key map [mouse-2] 'landmark-mouse-play)
195 (define-key map [drag-mouse-2] 'landmark-mouse-play)
197 (define-key map [remap previous-line] 'landmark-move-up)
198 (define-key map [remap next-line] 'landmark-move-down)
199 (define-key map [remap beginning-of-line] 'landmark-beginning-of-line)
200 (define-key map [remap end-of-line] 'landmark-end-of-line)
201 (define-key map [remap undo] 'landmark-human-takes-back)
202 (define-key map [remap advertised-undo] 'landmark-human-takes-back)
203 map)
204 "Local keymap to use in Landmark mode.")
208 (defvar landmark-emacs-won ()
209 "For making font-lock use the winner's face for the line.")
211 (defface landmark-font-lock-face-O '((((class color)) :foreground "red")
212 (t :weight bold))
213 "Face to use for Emacs's O."
214 :version "22.1"
215 :group 'landmark)
217 (defface landmark-font-lock-face-X '((((class color)) :foreground "green")
218 (t :weight bold))
219 "Face to use for your X."
220 :version "22.1"
221 :group 'landmark)
223 (defvar landmark-font-lock-keywords
224 '(("O" . 'landmark-font-lock-face-O)
225 ("X" . 'landmark-font-lock-face-X)
226 ("[-|/\\]" 0 (if landmark-emacs-won
227 'landmark-font-lock-face-O
228 'landmark-font-lock-face-X)))
229 "Font lock rules for Landmark.")
231 (put 'landmark-mode 'front-sticky
232 (put 'landmark-mode 'rear-nonsticky '(intangible)))
233 (put 'landmark-mode 'intangible 1)
234 ;; This one is for when they set view-read-only to t: Landmark cannot
235 ;; allow View Mode to be activated in its buffer.
236 (define-derived-mode landmark-mode special-mode "Lm"
237 "Major mode for playing Lm against Emacs.
238 You and Emacs play in turn by marking a free square. You mark it with X
239 and Emacs marks it with O. The winner is the first to get five contiguous
240 marks horizontally, vertically or in diagonal.
242 You play by moving the cursor over the square you choose and hitting \\[landmark-human-plays].
244 Other useful commands:
245 \\{landmark-mode-map}
246 Entry to this mode calls the value of `landmark-mode-hook' if that value
247 is non-nil. One interesting value is `turn-on-font-lock'."
248 (landmark-display-statistics)
249 (setq-local font-lock-defaults '(landmark-font-lock-keywords t))
250 (setq buffer-read-only t))
253 ;;;_ + THE SCORE TABLE.
256 ;; Every (free) square has a score associated to it, recorded in the
257 ;; LANDMARK-SCORE-TABLE vector. The program always plays in the square having
258 ;; the highest score.
260 (defvar landmark-score-table nil
261 "Vector recording the actual score of the free squares.")
264 ;; The key point point about the algorithm is that, rather than considering
265 ;; the board as just a set of squares, we prefer to see it as a "space" of
266 ;; internested 5-tuples of contiguous squares (called qtuples).
268 ;; The aim of the program is to fill one qtuple with its O's while preventing
269 ;; you from filling another one with your X's. To that effect, it computes a
270 ;; score for every qtuple, with better qtuples having better scores. Of
271 ;; course, the score of a qtuple (taken in isolation) is just determined by
272 ;; its contents as a set, i.e. not considering the order of its elements. The
273 ;; highest score is given to the "OOOO" qtuples because playing in such a
274 ;; qtuple is winning the game. Just after this comes the "XXXX" qtuple because
275 ;; not playing in it is just losing the game, and so on. Note that a
276 ;; "polluted" qtuple, i.e. one containing at least one X and at least one O,
277 ;; has score zero because there is no more any point in playing in it, from
278 ;; both an attacking and a defending point of view.
280 ;; Given the score of every qtuple, the score of a given free square on the
281 ;; board is just the sum of the scores of all the qtuples to which it belongs,
282 ;; because playing in that square is playing in all its containing qtuples at
283 ;; once. And it is that function which takes into account the internesting of
284 ;; the qtuples.
286 ;; This algorithm is rather simple but anyway it gives a not so dumb level of
287 ;; play. It easily extends to "n-dimensional Landmark", where a win should not
288 ;; be obtained with as few as 5 contiguous marks: 6 or 7 (depending on n !)
289 ;; should be preferred.
292 ;; Here are the scores of the nine "non-polluted" configurations. Tuning
293 ;; these values will change (hopefully improve) the strength of the program
294 ;; and may change its style (rather aggressive here).
296 (defconst landmark-nil-score 7 "Score of an empty qtuple.")
298 (defconst landmark-score-trans-table
299 (let ((Xscore 15) ; Score of a qtuple containing one X.
300 (XXscore 400) ; Score of a qtuple containing two X's.
301 (XXXscore 1800) ; Score of a qtuple containing three X's.
302 (XXXXscore 100000) ; Score of a qtuple containing four X's.
303 (Oscore 35) ; Score of a qtuple containing one O.
304 (OOscore 800) ; Score of a qtuple containing two O's.
305 (OOOscore 15000) ; Score of a qtuple containing three O's.
306 (OOOOscore 800000)) ; Score of a qtuple containing four O's.
308 ;; These values are not just random: if, given the following situation:
310 ;; . . . . . . . O .
311 ;; . X X a . . . X .
312 ;; . . . X . . . X .
313 ;; . . . X . . . X .
314 ;; . . . . . . . b .
316 ;; you want Emacs to play in "a" and not in "b", then the parameters must
317 ;; satisfy the inequality:
319 ;; 6 * XXscore > XXXscore + XXscore
321 ;; because "a" mainly belongs to six "XX" qtuples (the others are less
322 ;; important) while "b" belongs to one "XXX" and one "XX" qtuples.
323 ;; Other conditions are required to obtain sensible moves, but the
324 ;; previous example should illustrate the point. If you manage to
325 ;; improve on these values, please send me a note. Thanks.
328 ;; As we chose values 0, 1 and 6 to denote empty, X and O squares,
329 ;; the contents of a qtuple are uniquely determined by the sum of
330 ;; its elements and we just have to set up a translation table.
331 (vector landmark-nil-score Xscore XXscore XXXscore XXXXscore 0
332 Oscore 0 0 0 0 0
333 OOscore 0 0 0 0 0
334 OOOscore 0 0 0 0 0
335 OOOOscore 0 0 0 0 0
337 "Vector associating qtuple contents to their score.")
340 ;; If you do not modify drastically the previous constants, the only way for a
341 ;; square to have a score higher than OOOOscore is to belong to a "OOOO"
342 ;; qtuple, thus to be a winning move. Similarly, the only way for a square to
343 ;; have a score between XXXXscore and OOOOscore is to belong to a "XXXX"
344 ;; qtuple. We may use these considerations to detect when a given move is
345 ;; winning or losing.
347 (defconst landmark-winning-threshold
348 (aref landmark-score-trans-table (+ 6 6 6 6)) ;; OOOOscore
349 "Threshold score beyond which an Emacs move is winning.")
351 (defconst landmark-losing-threshold
352 (aref landmark-score-trans-table (+ 1 1 1 1)) ;; XXXXscore
353 "Threshold score beyond which a human move is winning.")
356 (defun landmark-strongest-square ()
357 "Compute index of free square with highest score, or nil if none."
358 ;; We just have to loop other all squares. However there are two problems:
359 ;; 1/ The SCORE-TABLE only gives correct scores to free squares. To speed
360 ;; up future searches, we set the score of padding or occupied squares
361 ;; to -1 whenever we meet them.
362 ;; 2/ We want to choose randomly between equally good moves.
363 (let ((score-max 0)
364 (count 0) ; Number of equally good moves
365 (square (landmark-xy-to-index 1 1)) ; First square
366 (end (landmark-xy-to-index landmark-board-width landmark-board-height))
367 best-square score)
368 (while (<= square end)
369 (cond
370 ;; If score is lower (i.e. most of the time), skip to next:
371 ((< (aref landmark-score-table square) score-max))
372 ;; If score is better, beware of non free squares:
373 ((> (setq score (aref landmark-score-table square)) score-max)
374 (if (zerop (aref landmark-board square)) ; is it free ?
375 (setq count 1 ; yes: take it !
376 best-square square
377 score-max score)
378 (aset landmark-score-table square -1))) ; no: kill it !
379 ;; If score is equally good, choose randomly. But first check freedom:
380 ((not (zerop (aref landmark-board square)))
381 (aset landmark-score-table square -1))
382 ((zerop (random (setq count (1+ count))))
383 (setq best-square square
384 score-max score)))
385 (setq square (1+ square))) ; try next square
386 best-square))
388 ;;;_ - INITIALIZING THE SCORE TABLE.
390 ;; At initialization the board is empty so that every qtuple amounts for
391 ;; nil-score. Therefore, the score of any square is nil-score times the number
392 ;; of qtuples that pass through it. This number is 3 in a corner and 20 if you
393 ;; are sufficiently far from the sides. As computing the number is time
394 ;; consuming, we initialize every square with 20*nil-score and then only
395 ;; consider squares at less than 5 squares from one side. We speed this up by
396 ;; taking symmetry into account.
397 ;; Also, as it is likely that successive games will be played on a board with
398 ;; same size, it is a good idea to save the initial SCORE-TABLE configuration.
400 (defvar landmark-saved-score-table nil
401 "Recorded initial value of previous score table.")
403 (defvar landmark-saved-board-width nil
404 "Recorded value of previous board width.")
406 (defvar landmark-saved-board-height nil
407 "Recorded value of previous board height.")
410 (defun landmark-init-score-table ()
411 "Create the score table vector and fill it with initial values."
412 (if (and landmark-saved-score-table ; Has it been stored last time ?
413 (= landmark-board-width landmark-saved-board-width)
414 (= landmark-board-height landmark-saved-board-height))
415 (setq landmark-score-table (copy-sequence landmark-saved-score-table))
416 ;; No, compute it:
417 (setq landmark-score-table
418 (make-vector landmark-vector-length (* 20 landmark-nil-score)))
419 (let (i j maxi maxj maxi2 maxj2)
420 (setq maxi (/ (1+ landmark-board-width) 2)
421 maxj (/ (1+ landmark-board-height) 2)
422 maxi2 (min 4 maxi)
423 maxj2 (min 4 maxj))
424 ;; We took symmetry into account and could use it more if the board
425 ;; would have been square and not rectangular !
426 ;; In our case we deal with all (i,j) in the set [1..maxi2]*[1..maxj] U
427 ;; [maxi2+1..maxi]*[1..maxj2]. Maxi2 and maxj2 are used because the
428 ;; board may well be less than 8 by 8 !
429 (setq i 1)
430 (while (<= i maxi2)
431 (setq j 1)
432 (while (<= j maxj)
433 (landmark-init-square-score i j)
434 (setq j (1+ j)))
435 (setq i (1+ i)))
436 (while (<= i maxi)
437 (setq j 1)
438 (while (<= j maxj2)
439 (landmark-init-square-score i j)
440 (setq j (1+ j)))
441 (setq i (1+ i))))
442 (setq landmark-saved-score-table (copy-sequence landmark-score-table)
443 landmark-saved-board-width landmark-board-width
444 landmark-saved-board-height landmark-board-height)))
446 (defun landmark-nb-qtuples (i j)
447 "Return the number of qtuples containing square I,J."
448 ;; This function is complicated because we have to deal
449 ;; with ugly cases like 3 by 6 boards, but it works.
450 ;; If you have a simpler (and correct) solution, send it to me. Thanks !
451 (let ((left (min 4 (1- i)))
452 (right (min 4 (- landmark-board-width i)))
453 (up (min 4 (1- j)))
454 (down (min 4 (- landmark-board-height j))))
455 (+ -12
456 (min (max (+ left right) 3) 8)
457 (min (max (+ up down) 3) 8)
458 (min (max (+ (min left up) (min right down)) 3) 8)
459 (min (max (+ (min right up) (min left down)) 3) 8))))
461 (defun landmark-init-square-score (i j)
462 "Give initial score to square I,J and to its mirror images."
463 (let ((ii (1+ (- landmark-board-width i)))
464 (jj (1+ (- landmark-board-height j)))
465 (sc (* (landmark-nb-qtuples i j) (aref landmark-score-trans-table 0))))
466 (aset landmark-score-table (landmark-xy-to-index i j) sc)
467 (aset landmark-score-table (landmark-xy-to-index ii j) sc)
468 (aset landmark-score-table (landmark-xy-to-index i jj) sc)
469 (aset landmark-score-table (landmark-xy-to-index ii jj) sc)))
470 ;;;_ - MAINTAINING THE SCORE TABLE.
473 ;; We do not provide functions for computing the SCORE-TABLE given the
474 ;; contents of the BOARD. This would involve heavy nested loops, with time
475 ;; proportional to the size of the board. It is better to update the
476 ;; SCORE-TABLE after each move. Updating needs not modify more than 36
477 ;; squares: it is done in constant time.
479 (defun landmark-update-score-table (square dval)
480 "Update score table after SQUARE received a DVAL increment."
481 ;; The board has already been updated when this function is called.
482 ;; Updating scores is done by looking for qtuples boundaries in all four
483 ;; directions and then calling update-score-in-direction.
484 ;; Finally all squares received the right increment, and then are up to
485 ;; date, except possibly for SQUARE itself if we are taking a move back for
486 ;; its score had been set to -1 at the time.
487 (let* ((x (landmark-index-to-x square))
488 (y (landmark-index-to-y square))
489 (imin (max -4 (- 1 x)))
490 (jmin (max -4 (- 1 y)))
491 (imax (min 0 (- landmark-board-width x 4)))
492 (jmax (min 0 (- landmark-board-height y 4))))
493 (landmark-update-score-in-direction imin imax
494 square 1 0 dval)
495 (landmark-update-score-in-direction jmin jmax
496 square 0 1 dval)
497 (landmark-update-score-in-direction (max imin jmin) (min imax jmax)
498 square 1 1 dval)
499 (landmark-update-score-in-direction (max (- 1 y) -4
500 (- x landmark-board-width))
501 (min 0 (- x 5)
502 (- landmark-board-height y 4))
503 square -1 1 dval)))
505 (defun landmark-update-score-in-direction (left right square dx dy dval)
506 "Update scores for all squares in the qtuples in range.
507 That is, those between the LEFTth square and the RIGHTth after SQUARE,
508 along the DX, DY direction, considering that DVAL has been added on SQUARE."
509 ;; We always have LEFT <= 0, RIGHT <= 0 and DEPL > 0 but we may very well
510 ;; have LEFT > RIGHT, indicating that no qtuple contains SQUARE along that
511 ;; DX,DY direction.
512 (cond
513 ((> left right)) ; Quit
514 (t ; Else ..
515 (let (depl square0 square1 square2 count delta)
516 (setq depl (landmark-xy-to-index dx dy)
517 square0 (+ square (* left depl))
518 square1 (+ square (* right depl))
519 square2 (+ square0 (* 4 depl)))
520 ;; Compute the contents of the first qtuple:
521 (setq square square0
522 count 0)
523 (while (<= square square2)
524 (setq count (+ count (aref landmark-board square))
525 square (+ square depl)))
526 (while (<= square0 square1)
527 ;; Update the squares of the qtuple beginning in SQUARE0 and ending
528 ;; in SQUARE2.
529 (setq delta (- (aref landmark-score-trans-table count)
530 (aref landmark-score-trans-table (- count dval))))
531 (cond ((not (zerop delta)) ; or else nothing to update
532 (setq square square0)
533 (while (<= square square2)
534 (if (zerop (aref landmark-board square)) ; only for free squares
535 (aset landmark-score-table square
536 (+ (aref landmark-score-table square) delta)))
537 (setq square (+ square depl)))))
538 ;; Then shift the qtuple one square along DEPL, this only requires
539 ;; modifying SQUARE0 and SQUARE2.
540 (setq square2 (+ square2 depl)
541 count (+ count (- (aref landmark-board square0))
542 (aref landmark-board square2))
543 square0 (+ square0 depl)))))))
546 ;;; GAME CONTROL.
549 ;; Several variables are used to monitor a game, including a GAME-HISTORY (the
550 ;; list of all (SQUARE . PREVSCORE) played) that allows to take moves back
551 ;; (anti-updating the score table) and to compute the table from scratch in
552 ;; case of an interruption.
554 (defvar landmark-game-in-progress nil
555 "Non-nil if a game is in progress.")
557 (defvar landmark-game-history nil
558 "A record of all moves that have been played during current game.")
560 (defvar landmark-number-of-moves nil
561 "Number of moves already played in current game.")
563 (defvar landmark-number-of-human-moves nil
564 "Number of moves already played by human in current game.")
566 (defvar landmark-emacs-played-first nil
567 "Non-nil if Emacs played first.")
569 (defvar landmark-human-took-back nil
570 "Non-nil if Human took back a move during the game.")
572 (defvar landmark-human-refused-draw nil
573 "Non-nil if Human refused Emacs offer of a draw.")
575 (defvar landmark-emacs-is-computing nil
576 ;; This is used to detect interruptions. Hopefully, it should not be needed.
577 "Non-nil if Emacs is in the middle of a computation.")
580 (defun landmark-start-game (n m)
581 "Initialize a new game on an N by M board."
582 (setq landmark-emacs-is-computing t) ; Raise flag
583 (setq landmark-game-in-progress t)
584 (setq landmark-board-width n
585 landmark-board-height m
586 landmark-vector-length (1+ (* (+ m 2) (1+ n)))
587 landmark-draw-limit (/ (* 7 n m) 10))
588 (setq landmark-emacs-won nil
589 landmark-game-history nil
590 landmark-number-of-moves 0
591 landmark-number-of-human-moves 0
592 landmark-emacs-played-first nil
593 landmark-human-took-back nil
594 landmark-human-refused-draw nil)
595 (landmark-init-display n m) ; Display first: the rest takes time
596 (landmark-init-score-table) ; INIT-BOARD requires that the score
597 (landmark-init-board) ; table be already created.
598 (setq landmark-emacs-is-computing nil))
600 (defun landmark-play-move (square val &optional dont-update-score)
601 "Go to SQUARE, play VAL and update everything."
602 (setq landmark-emacs-is-computing t) ; Raise flag
603 (cond ((= 1 val) ; a Human move
604 (setq landmark-number-of-human-moves (1+ landmark-number-of-human-moves)))
605 ((zerop landmark-number-of-moves) ; an Emacs move. Is it first ?
606 (setq landmark-emacs-played-first t)))
607 (setq landmark-game-history
608 (cons (cons square (aref landmark-score-table square))
609 landmark-game-history)
610 landmark-number-of-moves (1+ landmark-number-of-moves))
611 (landmark-plot-square square val)
612 (aset landmark-board square val) ; *BEFORE* UPDATE-SCORE !
613 (if dont-update-score nil
614 (landmark-update-score-table square val) ; previous val was 0: dval = val
615 (aset landmark-score-table square -1))
616 (setq landmark-emacs-is-computing nil))
618 (defun landmark-take-back ()
619 "Take back last move and update everything."
620 (setq landmark-emacs-is-computing t)
621 (let* ((last-move (car landmark-game-history))
622 (square (car last-move))
623 (oldval (aref landmark-board square)))
624 (if (= 1 oldval)
625 (setq landmark-number-of-human-moves (1- landmark-number-of-human-moves)))
626 (setq landmark-game-history (cdr landmark-game-history)
627 landmark-number-of-moves (1- landmark-number-of-moves))
628 (landmark-plot-square square 0)
629 (aset landmark-board square 0) ; *BEFORE* UPDATE-SCORE !
630 (landmark-update-score-table square (- oldval))
631 (aset landmark-score-table square (cdr last-move)))
632 (setq landmark-emacs-is-computing nil))
635 ;;;_ + SESSION CONTROL.
637 (defvar landmark-number-of-trials 0
638 "The number of times that landmark has been run.")
640 (defvar landmark-sum-of-moves 0
641 "The total number of moves made in all games.")
643 (defvar landmark-number-of-emacs-wins 0
644 "Number of games Emacs won in this session.")
646 (defvar landmark-number-of-human-wins 0
647 "Number of games you won in this session.")
649 (defvar landmark-number-of-draws 0
650 "Number of games already drawn in this session.")
653 (defun landmark-terminate-game (result)
654 "Terminate the current game with RESULT."
655 (setq landmark-number-of-trials (1+ landmark-number-of-trials))
656 (setq landmark-sum-of-moves (+ landmark-sum-of-moves landmark-number-of-moves))
657 (if (eq result 'crash-game)
658 (message
659 "Sorry, I have been interrupted and cannot resume that game..."))
660 (landmark-display-statistics)
661 ;;(ding)
662 (setq landmark-game-in-progress nil))
664 (defun landmark-crash-game ()
665 "What to do when Emacs detects it has been interrupted."
666 (setq landmark-emacs-is-computing nil)
667 (landmark-terminate-game 'crash-game)
668 (sit-for 4) ; Let's see the message
669 (landmark-prompt-for-other-game))
672 ;;;_ + INTERACTIVE COMMANDS.
674 (defun landmark-emacs-plays ()
675 "Compute Emacs next move and play it."
676 (interactive)
677 (landmark-switch-to-window)
678 (cond
679 (landmark-emacs-is-computing
680 (landmark-crash-game))
681 ((not landmark-game-in-progress)
682 (landmark-prompt-for-other-game))
684 (message "Let me think...")
685 (let (square score)
686 (setq square (landmark-strongest-square))
687 (cond ((null square)
688 (landmark-terminate-game 'nobody-won))
690 (setq score (aref landmark-score-table square))
691 (landmark-play-move square 6)
692 (cond ((>= score landmark-winning-threshold)
693 (setq landmark-emacs-won t) ; for font-lock
694 (landmark-find-filled-qtuple square 6)
695 (landmark-terminate-game 'emacs-won))
696 ((zerop score)
697 (landmark-terminate-game 'nobody-won))
698 ((and (> landmark-number-of-moves landmark-draw-limit)
699 (not landmark-human-refused-draw)
700 (landmark-offer-a-draw))
701 (landmark-terminate-game 'draw-agreed))
703 (landmark-prompt-for-move)))))))))
705 ;; For small square dimensions this is approximate, since though measured in
706 ;; pixels, event's (X . Y) is a character's top-left corner.
707 (defun landmark-click (click)
708 "Position at the square where you click."
709 (interactive "e")
710 (and (windowp (posn-window (setq click (event-end click))))
711 (numberp (posn-point click))
712 (select-window (posn-window click))
713 (setq click (posn-col-row click))
714 (landmark-goto-xy
715 (min (max (/ (+ (- (car click)
716 landmark-x-offset
718 (window-hscroll)
719 landmark-square-width
720 (% landmark-square-width 2)
721 (/ landmark-square-width 2))
722 landmark-square-width)
724 landmark-board-width)
725 (min (max (/ (+ (- (cdr click)
726 landmark-y-offset
728 (let ((inhibit-point-motion-hooks t))
729 (count-lines 1 (window-start)))
730 landmark-square-height
731 (% landmark-square-height 2)
732 (/ landmark-square-height 2))
733 landmark-square-height)
735 landmark-board-height))))
737 (defun landmark-mouse-play (click)
738 "Play at the square where you click."
739 (interactive "e")
740 (if (landmark-click click)
741 (landmark-human-plays)))
743 (defun landmark-human-plays ()
744 "Signal to the Landmark program that you have played.
745 You must have put the cursor on the square where you want to play.
746 If the game is finished, this command requests for another game."
747 (interactive)
748 (landmark-switch-to-window)
749 (cond
750 (landmark-emacs-is-computing
751 (landmark-crash-game))
752 ((not landmark-game-in-progress)
753 (landmark-prompt-for-other-game))
755 (let (square score)
756 (setq square (landmark-point-square))
757 (cond ((null square)
758 (error "Your point is not on a square. Retry!"))
759 ((not (zerop (aref landmark-board square)))
760 (error "Your point is not on a free square. Retry!"))
762 (setq score (aref landmark-score-table square))
763 (landmark-play-move square 1)
764 (cond ((and (>= score landmark-losing-threshold)
765 ;; Just testing SCORE > THRESHOLD is not enough for
766 ;; detecting wins, it just gives an indication that
767 ;; we confirm with LANDMARK-FIND-FILLED-QTUPLE.
768 (landmark-find-filled-qtuple square 1))
769 (landmark-terminate-game 'human-won))
771 (landmark-emacs-plays)))))))))
773 (defun landmark-human-takes-back ()
774 "Signal to the Landmark program that you wish to take back your last move."
775 (interactive)
776 (landmark-switch-to-window)
777 (cond
778 (landmark-emacs-is-computing
779 (landmark-crash-game))
780 ((not landmark-game-in-progress)
781 (message "Too late for taking back...")
782 (sit-for 4)
783 (landmark-prompt-for-other-game))
784 ((zerop landmark-number-of-human-moves)
785 (message "You have not played yet... Your move?"))
787 (message "One moment, please...")
788 ;; It is possible for the user to let Emacs play several consecutive
789 ;; moves, so that the best way to know when to stop taking back moves is
790 ;; to count the number of human moves:
791 (setq landmark-human-took-back t)
792 (let ((number landmark-number-of-human-moves))
793 (while (= number landmark-number-of-human-moves)
794 (landmark-take-back)))
795 (landmark-prompt-for-move))))
797 (defun landmark-human-resigns ()
798 "Signal to the Landmark program that you may want to resign."
799 (interactive)
800 (landmark-switch-to-window)
801 (cond
802 (landmark-emacs-is-computing
803 (landmark-crash-game))
804 ((not landmark-game-in-progress)
805 (message "There is no game in progress"))
806 ((y-or-n-p "You mean, you resign? ")
807 (landmark-terminate-game 'human-resigned))
808 ((y-or-n-p "You mean, we continue? ")
809 (landmark-prompt-for-move))
811 (landmark-terminate-game 'human-resigned)))) ; OK. Accept it
813 ;;;_ + PROMPTING THE HUMAN PLAYER.
815 (defun landmark-prompt-for-move ()
816 "Display a message asking for Human's move."
817 (message (if (zerop landmark-number-of-human-moves)
818 "Your move? (move to a free square and hit X, RET ...)"
819 "Your move?")))
821 (defun landmark-prompt-for-other-game ()
822 "Ask for another game, and start it."
823 (if (y-or-n-p "Another game? ")
824 (if (y-or-n-p "Retain learned weights ")
825 (landmark 2)
826 (landmark 1))
827 (message "Chicken!")))
829 (defun landmark-offer-a-draw ()
830 "Offer a draw and return t if Human accepted it."
831 (or (y-or-n-p "I offer you a draw. Do you accept it? ")
832 (not (setq landmark-human-refused-draw t))))
835 (defun landmark-max-width ()
836 "Largest possible board width for the current window."
837 (1+ (/ (- (window-width)
838 landmark-x-offset landmark-x-offset 1)
839 landmark-square-width)))
841 (defun landmark-max-height ()
842 "Largest possible board height for the current window."
843 (1+ (/ (- (window-height)
844 landmark-y-offset landmark-y-offset 2)
845 ;; 2 instead of 1 because WINDOW-HEIGHT includes the mode line !
846 landmark-square-height)))
848 (defun landmark-point-y ()
849 "Return the board row where point is."
850 (let ((inhibit-point-motion-hooks t))
851 (1+ (/ (- (count-lines 1 (point)) landmark-y-offset (if (bolp) 0 1))
852 landmark-square-height))))
854 (defun landmark-point-square ()
855 "Return the index of the square point is on."
856 (let ((inhibit-point-motion-hooks t))
857 (landmark-xy-to-index (1+ (/ (- (current-column) landmark-x-offset)
858 landmark-square-width))
859 (landmark-point-y))))
861 (defun landmark-goto-square (index)
862 "Move point to square number INDEX."
863 (landmark-goto-xy (landmark-index-to-x index) (landmark-index-to-y index)))
865 (defun landmark-goto-xy (x y)
866 "Move point to square at X, Y coords."
867 (let ((inhibit-point-motion-hooks t))
868 (goto-char (point-min))
869 (forward-line (+ landmark-y-offset (* landmark-square-height (1- y)))))
870 (move-to-column (+ landmark-x-offset (* landmark-square-width (1- x)))))
872 (defun landmark-plot-square (square value)
873 "Draw 'X', 'O' or '.' on SQUARE depending on VALUE, leave point there."
874 (or (= value 1)
875 (landmark-goto-square square))
876 (let ((inhibit-read-only t)
877 (inhibit-point-motion-hooks t))
878 (insert-and-inherit (cond ((= value 1) ?.)
879 ((= value 2) ?N)
880 ((= value 3) ?S)
881 ((= value 4) ?E)
882 ((= value 5) ?W)
883 ((= value 6) ?^)))
885 (and (zerop value)
886 (add-text-properties (1- (point)) (point)
887 '(mouse-face highlight
888 help-echo "\
889 mouse-1: get robot moving, mouse-2: play on this square")))
890 (delete-char 1)
891 (backward-char 1))
892 (sit-for 0)) ; Display NOW
894 (defun landmark-init-display (n m)
895 "Display an N by M Landmark board."
896 (buffer-disable-undo (current-buffer))
897 (let ((inhibit-read-only t)
898 (point 1) opoint
899 (intangible t)
900 (i m) j x)
901 ;; Try to minimize number of chars (because of text properties)
902 (setq tab-width
903 (if (zerop (% landmark-x-offset landmark-square-width))
904 landmark-square-width
905 (max (/ (+ (% landmark-x-offset landmark-square-width)
906 landmark-square-width 1) 2) 2)))
907 (erase-buffer)
908 (newline landmark-y-offset)
909 (while (progn
910 (setq j n
911 x (- landmark-x-offset landmark-square-width))
912 (while (>= (setq j (1- j)) 0)
913 (insert-char ?\t (/ (- (setq x (+ x landmark-square-width))
914 (current-column))
915 tab-width))
916 (insert-char ? (- x (current-column)))
917 (if (setq intangible (not intangible))
918 (put-text-property point (point) 'intangible 2))
919 (and (zerop j)
920 (= i (- m 2))
921 (progn
922 (while (>= i 3)
923 (append-to-buffer (current-buffer) opoint (point))
924 (setq i (- i 2)))
925 (goto-char (point-max))))
926 (setq point (point))
927 (insert ?=)
928 (add-text-properties point (point)
929 '(mouse-face highlight help-echo "\
930 mouse-1: get robot moving, mouse-2: play on this square")))
931 (> (setq i (1- i)) 0))
932 (if (= i (1- m))
933 (setq opoint point))
934 (insert-char ?\n landmark-square-height))
935 (or (eq (char-after 1) ?.)
936 (put-text-property 1 2 'point-entered
937 (lambda (_x _y) (if (bobp) (forward-char)))))
938 (or intangible
939 (put-text-property point (point) 'intangible 2))
940 (put-text-property point (point) 'point-entered
941 (lambda (_x _y) (if (eobp) (backward-char))))
942 (put-text-property (point-min) (point) 'category 'landmark-mode))
943 (landmark-goto-xy (/ (1+ n) 2) (/ (1+ m) 2)) ; center of the board
944 (sit-for 0)) ; Display NOW
946 (defun landmark-display-statistics ()
947 "Obnoxiously display some statistics about previous games in mode line."
948 ;; We store this string in the mode-line-process local variable.
949 ;; This is certainly not the cleanest way out ...
950 (setq mode-line-process
951 (format ": Trials: %d, Avg#Moves: %d"
952 landmark-number-of-trials
953 (if (zerop landmark-number-of-trials)
955 (/ landmark-sum-of-moves landmark-number-of-trials))))
956 (force-mode-line-update))
958 (defun landmark-switch-to-window ()
959 "Find or create the Landmark buffer, and display it."
960 (interactive)
961 (let ((buff (get-buffer "*Landmark*")))
962 (if buff ; Buffer exists:
963 (switch-to-buffer buff) ; no problem.
964 (if landmark-game-in-progress
965 (landmark-crash-game)) ; buffer has been killed or something
966 (switch-to-buffer "*Landmark*") ; Anyway, start anew.
967 (landmark-mode))))
970 ;;;_ + CROSSING WINNING QTUPLES.
972 ;; When someone succeeds in filling a qtuple, we draw a line over the five
973 ;; corresponding squares. One problem is that the program does not know which
974 ;; squares ! It only knows the square where the last move has been played and
975 ;; who won. The solution is to scan the board along all four directions.
977 (defun landmark-find-filled-qtuple (square value)
978 "Return t if SQUARE belongs to a qtuple filled with VALUEs."
979 (or (landmark-check-filled-qtuple square value 1 0)
980 (landmark-check-filled-qtuple square value 0 1)
981 (landmark-check-filled-qtuple square value 1 1)
982 (landmark-check-filled-qtuple square value -1 1)))
984 (defun landmark-check-filled-qtuple (square value dx dy)
985 "Return t if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
986 (let ((a 0) (b 0)
987 (left square) (right square)
988 (depl (landmark-xy-to-index dx dy)))
989 (while (and (> a -4) ; stretch tuple left
990 (= value (aref landmark-board (setq left (- left depl)))))
991 (setq a (1- a)))
992 (while (and (< b (+ a 4)) ; stretch tuple right
993 (= value (aref landmark-board (setq right (+ right depl)))))
994 (setq b (1+ b)))
995 (cond ((= b (+ a 4)) ; tuple length = 5 ?
996 (landmark-cross-qtuple (+ square (* a depl)) (+ square (* b depl))
997 dx dy)
998 t))))
1000 (defun landmark-cross-qtuple (square1 square2 dx dy)
1001 "Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
1002 (save-excursion ; Not moving point from last square
1003 (let ((depl (landmark-xy-to-index dx dy))
1004 (inhibit-read-only t)
1005 (inhibit-point-motion-hooks t))
1006 ;; WARNING: this function assumes DEPL > 0 and SQUARE2 > SQUARE1
1007 (while (/= square1 square2)
1008 (landmark-goto-square square1)
1009 (setq square1 (+ square1 depl))
1010 (cond
1011 ((= dy 0) ; Horizontal
1012 (forward-char 1)
1013 (insert-char ?- (1- landmark-square-width) t)
1014 (delete-region (point) (progn
1015 (skip-chars-forward " \t")
1016 (point))))
1017 ((= dx 0) ; Vertical
1018 (let ((landmark-n 1)
1019 (column (current-column)))
1020 (while (< landmark-n landmark-square-height)
1021 (setq landmark-n (1+ landmark-n))
1022 (forward-line 1)
1023 (indent-to column)
1024 (insert-and-inherit ?|))))
1025 ((= dx -1) ; 1st Diagonal
1026 (indent-to (prog1 (- (current-column) (/ landmark-square-width 2))
1027 (forward-line (/ landmark-square-height 2))))
1028 (insert-and-inherit ?/))
1029 (t ; 2nd Diagonal
1030 (indent-to (prog1 (+ (current-column) (/ landmark-square-width 2))
1031 (forward-line (/ landmark-square-height 2))))
1032 (insert-and-inherit ?\\))))))
1033 (sit-for 0)) ; Display NOW
1036 ;;;_ + CURSOR MOTION.
1038 ;; previous-line and next-line don't work right with intangible newlines
1039 (defun landmark-move-down ()
1040 "Move point down one row on the Landmark board."
1041 (interactive)
1042 (if (< (landmark-point-y) landmark-board-height)
1043 (forward-line 1)));;; landmark-square-height)))
1045 (defun landmark-move-up ()
1046 "Move point up one row on the Landmark board."
1047 (interactive)
1048 (if (> (landmark-point-y) 1)
1049 (forward-line (- landmark-square-height))))
1051 (defun landmark-move-ne ()
1052 "Move point North East on the Landmark board."
1053 (interactive)
1054 (landmark-move-up)
1055 (forward-char))
1057 (defun landmark-move-se ()
1058 "Move point South East on the Landmark board."
1059 (interactive)
1060 (landmark-move-down)
1061 (forward-char))
1063 (defun landmark-move-nw ()
1064 "Move point North West on the Landmark board."
1065 (interactive)
1066 (landmark-move-up)
1067 (backward-char))
1069 (defun landmark-move-sw ()
1070 "Move point South West on the Landmark board."
1071 (interactive)
1072 (landmark-move-down)
1073 (backward-char))
1075 (defun landmark-beginning-of-line ()
1076 "Move point to first square on the Landmark board row."
1077 (interactive)
1078 (move-to-column landmark-x-offset))
1080 (defun landmark-end-of-line ()
1081 "Move point to last square on the Landmark board row."
1082 (interactive)
1083 (move-to-column (+ landmark-x-offset
1084 (* landmark-square-width (1- landmark-board-width)))))
1087 ;;;_ + Simulation variables
1089 ;;;_ - landmark-nvar
1090 (defvar landmark-nvar 0.0075
1091 "Not used.
1092 Affects a noise generator which was used in an earlier incarnation of
1093 this program to add a random element to the way moves were made.")
1094 ;;;_ - lists of cardinal directions
1095 ;;;_ :
1096 (defvar landmark-ns '(landmark-n landmark-s)
1097 "Used when doing something relative to the north and south axes.")
1098 (defvar landmark-ew '(landmark-e landmark-w)
1099 "Used when doing something relative to the east and west axes.")
1100 (defvar landmark-directions '(landmark-n landmark-s landmark-e landmark-w)
1101 "The cardinal directions.")
1102 (defvar landmark-8-directions
1103 '((landmark-n) (landmark-n landmark-w) (landmark-w) (landmark-s landmark-w)
1104 (landmark-s) (landmark-s landmark-e) (landmark-e) (landmark-n landmark-e))
1105 "The full 8 possible directions.")
1107 (defvar landmark-number-of-moves
1108 "The number of moves made by the robot so far.")
1111 ;;;_* Terry's mods to create lm.el
1113 ;;;(setq landmark-debug nil)
1114 (defvar landmark-debug nil
1115 "If non-nil, debugging is printed.")
1116 (defcustom landmark-one-moment-please nil
1117 "If non-nil, print \"One moment please\" when a new board is generated.
1118 The drawback of this is you don't see how many moves the last run took
1119 because it is overwritten by \"One moment please\"."
1120 :type 'boolean
1121 :group 'landmark)
1122 (defcustom landmark-output-moves t
1123 "If non-nil, output number of moves so far on a move-by-move basis."
1124 :type 'boolean
1125 :group 'landmark)
1128 (defun landmark-weights-debug ()
1129 (if landmark-debug
1130 (progn (landmark-print-wts) (landmark-blackbox) (landmark-print-y-s-noise)
1131 (landmark-print-smell))))
1133 ;;;_ - Printing various things
1134 (defun landmark-print-distance-int (direction)
1135 (interactive)
1136 (insert (format "%S %S " direction (get direction 'distance))))
1139 (defun landmark-print-distance ()
1140 (insert (format "tree: %S \n" (calc-distance-of-robot-from 'landmark-tree)))
1141 (mapc 'landmark-print-distance-int landmark-directions))
1144 ;;(setq direction 'landmark-n)
1145 ;;(get 'landmark-n 'landmark-s)
1146 (defun landmark-nslify-wts-int (direction)
1147 (mapcar (lambda (target-direction)
1148 (get direction target-direction))
1149 landmark-directions))
1152 (defun landmark-nslify-wts ()
1153 (interactive)
1154 (let ((l (apply 'append (mapcar 'landmark-nslify-wts-int landmark-directions))))
1155 (insert (format "set data_value WTS \n %s \n" l))
1156 (insert (format "/* max: %S min: %S */"
1157 (eval (cons 'max l)) (eval (cons 'min l))))))
1159 (defun landmark-print-wts-int (direction)
1160 (mapc (lambda (target-direction)
1161 (insert (format "%S %S %S "
1162 direction
1163 target-direction
1164 (get direction target-direction))))
1165 landmark-directions)
1166 (insert "\n"))
1168 (defun landmark-print-wts ()
1169 (interactive)
1170 (with-current-buffer "*landmark-wts*"
1171 (insert "==============================\n")
1172 (mapc 'landmark-print-wts-int landmark-directions)))
1174 (defun landmark-print-moves (moves)
1175 (interactive)
1176 (with-current-buffer "*landmark-moves*"
1177 (insert (format "%S\n" moves))))
1180 (defun landmark-print-y-s-noise-int (direction)
1181 (insert (format "%S:landmark-y %S, s %S, noise %S \n"
1182 (symbol-name direction)
1183 (get direction 'y_t)
1184 (get direction 's)
1185 (get direction 'noise)
1188 (defun landmark-print-y-s-noise ()
1189 (interactive)
1190 (with-current-buffer "*landmark-y,s,noise*"
1191 (insert "==============================\n")
1192 (mapc 'landmark-print-y-s-noise-int landmark-directions)))
1194 (defun landmark-print-smell-int (direction)
1195 (insert (format "%S: smell: %S \n"
1196 (symbol-name direction)
1197 (get direction 'smell))))
1199 (defun landmark-print-smell ()
1200 (interactive)
1201 (with-current-buffer "*landmark-smell*"
1202 (insert "==============================\n")
1203 (insert (format "tree: %S \n" (get 'z 't)))
1204 (mapc 'landmark-print-smell-int landmark-directions)))
1206 (defun landmark-print-w0-int (direction)
1207 (insert (format "%S: w0: %S \n"
1208 (symbol-name direction)
1209 (get direction 'w0))))
1211 (defun landmark-print-w0 ()
1212 (interactive)
1213 (with-current-buffer "*landmark-w0*"
1214 (insert "==============================\n")
1215 (mapc 'landmark-print-w0-int landmark-directions)))
1217 (defun landmark-blackbox ()
1218 (with-current-buffer "*landmark-blackbox*"
1219 (insert "==============================\n")
1220 (insert "I smell: ")
1221 (mapc (lambda (direction)
1222 (if (> (get direction 'smell) 0)
1223 (insert (format "%S " direction))))
1224 landmark-directions)
1225 (insert "\n")
1227 (insert "I move: ")
1228 (mapc (lambda (direction)
1229 (if (> (get direction 'y_t) 0)
1230 (insert (format "%S " direction))))
1231 landmark-directions)
1232 (insert "\n")
1233 (landmark-print-wts-blackbox)
1234 (insert (format "z_t-z_t-1: %S" (- (get 'z 't) (get 'z 't-1))))
1235 (landmark-print-distance)
1236 (insert "\n")))
1238 (defun landmark-print-wts-blackbox ()
1239 (interactive)
1240 (mapc 'landmark-print-wts-int landmark-directions))
1242 ;;;_ - learning parameters
1243 (defcustom landmark-bound 0.005
1244 "The maximum that w0j may be."
1245 :type 'number
1246 :group 'landmark)
1247 (defcustom landmark-c 1.0
1248 "A factor applied to modulate the increase in wij.
1249 Used in the function landmark-update-normal-weights."
1250 :type 'number
1251 :group 'landmark)
1252 (defcustom landmark-c-naught 0.5
1253 "A factor applied to modulate the increase in w0j.
1254 Used in the function landmark-update-naught-weights."
1255 :type 'number
1256 :group 'landmark)
1257 (defvar landmark-initial-w0 0.0)
1258 (defvar landmark-initial-wij 0.0)
1259 (defcustom landmark-no-payoff 0
1260 "The amount of simulation cycles that have occurred with no movement.
1261 Used to move the robot when he is stuck in a rut for some reason."
1262 :type 'integer
1263 :group 'landmark)
1264 (defcustom landmark-max-stall-time 2
1265 "The maximum number of cycles that the robot can remain stuck in a place.
1266 After this limit is reached, landmark-random-move is called to push him out of it."
1267 :type 'integer
1268 :group 'landmark)
1271 ;;;_ + Randomizing functions
1272 ;;;_ - landmark-flip-a-coin ()
1273 (defun landmark-flip-a-coin ()
1274 (if (> (random 5000) 2500)
1277 ;;;_ : landmark-very-small-random-number ()
1278 ;(defun landmark-very-small-random-number ()
1279 ; (/
1280 ; (* (/ (random 900000) 900000.0) .0001)))
1281 ;;;_ : landmark-randomize-weights-for (direction)
1282 (defun landmark-randomize-weights-for (direction)
1283 (mapc (lambda (target-direction)
1284 (put direction
1285 target-direction
1286 (* (landmark-flip-a-coin) (/ (random 10000) 10000.0))))
1287 landmark-directions))
1288 ;;;_ : landmark-noise ()
1289 (defun landmark-noise ()
1290 (* (- (/ (random 30001) 15000.0) 1) landmark-nvar))
1292 ;;;_ : landmark-fix-weights-for (direction)
1293 (defun landmark-fix-weights-for (direction)
1294 (mapc (lambda (target-direction)
1295 (put direction
1296 target-direction
1297 landmark-initial-wij))
1298 landmark-directions))
1301 ;;;_ + Plotting functions
1302 ;;;_ - landmark-plot-internal (sym)
1303 (defun landmark-plot-internal (sym)
1304 (landmark-plot-square (landmark-xy-to-index
1305 (get sym 'x)
1306 (get sym 'y))
1307 (get sym 'sym)))
1308 ;;;_ - landmark-plot-landmarks ()
1309 (defun landmark-plot-landmarks ()
1310 (setq landmark-cx (/ landmark-board-width 2))
1311 (setq landmark-cy (/ landmark-board-height 2))
1313 (put 'landmark-n 'x landmark-cx)
1314 (put 'landmark-n 'y 1)
1315 (put 'landmark-n 'sym 2)
1317 (put 'landmark-tree 'x landmark-cx)
1318 (put 'landmark-tree 'y landmark-cy)
1319 (put 'landmark-tree 'sym 6)
1321 (put 'landmark-s 'x landmark-cx)
1322 (put 'landmark-s 'y landmark-board-height)
1323 (put 'landmark-s 'sym 3)
1325 (put 'landmark-w 'x 1)
1326 (put 'landmark-w 'y (/ landmark-board-height 2))
1327 (put 'landmark-w 'sym 5)
1329 (put 'landmark-e 'x landmark-board-width)
1330 (put 'landmark-e 'y (/ landmark-board-height 2))
1331 (put 'landmark-e 'sym 4)
1333 (mapc 'landmark-plot-internal '(landmark-n landmark-s landmark-e landmark-w landmark-tree)))
1337 ;;;_ + Distance-calculation functions
1338 ;;;_ - square (a)
1339 (defun square (a)
1340 (* a a))
1342 ;;;_ - distance (x x0 y y0)
1343 (defun distance (x x0 y y0)
1344 (sqrt (+ (square (- x x0)) (square (- y y0)))))
1346 ;;;_ - calc-distance-of-robot-from (direction)
1347 (defun calc-distance-of-robot-from (direction)
1348 (put direction 'distance
1349 (distance (get direction 'x)
1350 (landmark-index-to-x (landmark-point-square))
1351 (get direction 'y)
1352 (landmark-index-to-y (landmark-point-square)))))
1354 ;;;_ - calc-smell-internal (sym)
1355 (defun calc-smell-internal (sym)
1356 (let ((r (get sym 'r))
1357 (d (calc-distance-of-robot-from sym)))
1358 (if (> (* 0.5 (- 1 (/ d r))) 0)
1359 (* 0.5 (- 1 (/ d r)))
1360 0)))
1363 ;;;_ + Learning (neural) functions
1364 (defun landmark-f (x)
1365 (cond
1366 ((> x landmark-bound) landmark-bound)
1367 ((< x 0.0) 0.0)
1368 (t x)))
1370 (defun landmark-y (direction)
1371 (put direction 'noise (landmark-noise))
1372 (put direction 'y_t
1373 (if (> (get direction 's) 0.0)
1375 0.0)))
1377 (defun landmark-update-normal-weights (direction)
1378 (mapc (lambda (target-direction)
1379 (put direction target-direction
1381 (get direction target-direction)
1382 (* landmark-c
1383 (- (get 'z 't) (get 'z 't-1))
1384 (get target-direction 'y_t)
1385 (get direction 'smell)))))
1386 landmark-directions))
1388 (defun landmark-update-naught-weights (direction)
1389 (mapc (lambda (_target-direction)
1390 (put direction 'w0
1391 (landmark-f
1393 (get direction 'w0)
1394 (* landmark-c-naught
1395 (- (get 'z 't) (get 'z 't-1))
1396 (get direction 'y_t))))))
1397 landmark-directions))
1400 ;;;_ + Statistics gathering and creating functions
1402 (defun landmark-calc-current-smells ()
1403 (mapc (lambda (direction)
1404 (put direction 'smell (calc-smell-internal direction)))
1405 landmark-directions))
1407 (defun landmark-calc-payoff ()
1408 (put 'z 't-1 (get 'z 't))
1409 (put 'z 't (calc-smell-internal 'landmark-tree))
1410 (if (= (- (get 'z 't) (get 'z 't-1)) 0.0)
1411 (cl-incf landmark-no-payoff)
1412 (setf landmark-no-payoff 0)))
1414 (defun landmark-store-old-y_t ()
1415 (mapc (lambda (direction)
1416 (put direction 'y_t-1 (get direction 'y_t)))
1417 landmark-directions))
1420 ;;;_ + Functions to move robot
1422 (defun landmark-confidence-for (target-direction)
1423 (apply '+
1424 (get target-direction 'w0)
1425 (mapcar (lambda (direction)
1427 (get direction target-direction)
1428 (get direction 'smell)))
1429 landmark-directions)))
1432 (defun landmark-calc-confidences ()
1433 (mapc (lambda (direction)
1434 (put direction 's (landmark-confidence-for direction)))
1435 landmark-directions))
1437 (defun landmark-move ()
1438 (if (and (= (get 'landmark-n 'y_t) 1.0) (= (get 'landmark-s 'y_t) 1.0))
1439 (progn
1440 (mapc (lambda (dir) (put dir 'y_t 0)) landmark-ns)
1441 (if landmark-debug
1442 (message "n-s normalization."))))
1443 (if (and (= (get 'landmark-w 'y_t) 1.0) (= (get 'landmark-e 'y_t) 1.0))
1444 (progn
1445 (mapc (lambda (dir) (put dir 'y_t 0)) landmark-ew)
1446 (if landmark-debug
1447 (message "e-w normalization"))))
1449 (mapc (lambda (pair)
1450 (if (> (get (car pair) 'y_t) 0)
1451 (funcall (car (cdr pair)))))
1453 (landmark-n landmark-move-up)
1454 (landmark-s landmark-move-down)
1455 (landmark-e forward-char)
1456 (landmark-w backward-char)))
1457 (landmark-plot-square (landmark-point-square) 1)
1458 (cl-incf landmark-number-of-moves)
1459 (if landmark-output-moves
1460 (message "Moves made: %d" landmark-number-of-moves)))
1463 (defun landmark-random-move ()
1464 (mapc
1465 (lambda (direction) (put direction 'y_t 0))
1466 landmark-directions)
1467 (dolist (direction (nth (random 8) landmark-8-directions))
1468 (put direction 'y_t 1.0))
1469 (landmark-move))
1471 (defun landmark-amble-robot ()
1472 (interactive)
1473 (while (> (calc-distance-of-robot-from 'landmark-tree) 0)
1475 (landmark-store-old-y_t)
1476 (landmark-calc-current-smells)
1478 (if (> landmark-no-payoff landmark-max-stall-time)
1479 (landmark-random-move)
1480 (progn
1481 (landmark-calc-confidences)
1482 (mapc 'landmark-y landmark-directions)
1483 (landmark-move)))
1485 (landmark-calc-payoff)
1487 (mapc 'landmark-update-normal-weights landmark-directions)
1488 (mapc 'landmark-update-naught-weights landmark-directions)
1489 (if landmark-debug
1490 (landmark-weights-debug)))
1491 (landmark-terminate-game nil))
1494 ;;;_ - landmark-start-robot ()
1495 (defun landmark-start-robot ()
1496 "Signal to the Landmark program that you have played.
1497 You must have put the cursor on the square where you want to play.
1498 If the game is finished, this command requests for another game."
1499 (interactive)
1500 (landmark-switch-to-window)
1501 (cond
1502 (landmark-emacs-is-computing
1503 (landmark-crash-game))
1504 ((not landmark-game-in-progress)
1505 (landmark-prompt-for-other-game))
1507 (let (square)
1508 (setq square (landmark-point-square))
1509 (cond ((null square)
1510 (error "Your point is not on a square. Retry!"))
1511 ((not (zerop (aref landmark-board square)))
1512 (error "Your point is not on a free square. Retry!"))
1514 (progn
1515 (landmark-plot-square square 1)
1517 (landmark-store-old-y_t)
1518 (landmark-calc-current-smells)
1519 (put 'z 't (calc-smell-internal 'landmark-tree))
1521 (landmark-random-move)
1523 (landmark-calc-payoff)
1525 (mapc 'landmark-update-normal-weights landmark-directions)
1526 (mapc 'landmark-update-naught-weights landmark-directions)
1527 (landmark-amble-robot)
1528 )))))))
1531 ;;;_ + Misc functions
1532 ;;;_ - landmark-init (auto-start save-weights)
1533 (defvar landmark-tree-r "")
1535 (defun landmark-init (auto-start save-weights)
1537 (setq landmark-number-of-moves 0)
1539 (landmark-plot-landmarks)
1541 (if landmark-debug
1542 (save-current-buffer
1543 (set-buffer (get-buffer-create "*landmark-w0*"))
1544 (erase-buffer)
1545 (set-buffer (get-buffer-create "*landmark-moves*"))
1546 (set-buffer (get-buffer-create "*landmark-wts*"))
1547 (erase-buffer)
1548 (set-buffer (get-buffer-create "*landmark-y,s,noise*"))
1549 (erase-buffer)
1550 (set-buffer (get-buffer-create "*landmark-smell*"))
1551 (erase-buffer)
1552 (set-buffer (get-buffer-create "*landmark-blackbox*"))
1553 (erase-buffer)
1554 (set-buffer (get-buffer-create "*landmark-distance*"))
1555 (erase-buffer)))
1558 (landmark-set-landmark-signal-strengths)
1560 (dolist (direction landmark-directions)
1561 (put direction 'y_t 0.0))
1563 (if (not save-weights)
1564 (progn
1565 (mapc 'landmark-fix-weights-for landmark-directions)
1566 (dolist (direction landmark-directions)
1567 (put direction 'w0 landmark-initial-w0)))
1568 (message "Weights preserved for this run."))
1570 (if auto-start
1571 (progn
1572 (landmark-goto-xy (1+ (random landmark-board-width)) (1+ (random landmark-board-height)))
1573 (landmark-start-robot))))
1576 ;;;_ - something which doesn't work
1577 ; no-a-worka!!
1578 ;(defun landmark-sum-list (list)
1579 ; (if (> (length list) 0)
1580 ; (+ (car list) (landmark-sum-list (cdr list)))
1581 ; 0))
1582 ; this a worka!
1583 ; (eval (cons '+ list))
1584 ;;;_ - landmark-set-landmark-signal-strengths ()
1585 ;; on a screen higher than wide, I noticed that the robot would amble
1586 ;; left and right and not move forward. examining *landmark-blackbox*
1587 ;; revealed that there was no scent from the north and south
1588 ;; landmarks, hence, they need less factoring down of the effect of
1589 ;; distance on scent.
1591 (defun landmark-set-landmark-signal-strengths ()
1592 (setq landmark-tree-r (* (sqrt (+ (square landmark-cx) (square landmark-cy))) 1.5))
1593 (mapc (lambda (direction)
1594 (put direction 'r (* landmark-cx 1.1)))
1595 landmark-ew)
1596 (mapc (lambda (direction)
1597 (put direction 'r (* landmark-cy 1.1)))
1598 landmark-ns)
1599 (put 'landmark-tree 'r landmark-tree-r))
1602 ;;;_ + landmark-test-run ()
1604 ;;;###autoload
1605 (defalias 'landmark-repeat 'landmark-test-run)
1606 ;;;###autoload
1607 (defun landmark-test-run ()
1608 "Run 100 Landmark games, each time saving the weights from the previous game."
1609 (interactive)
1610 (landmark 1)
1611 (dotimes (scratch-var 100)
1612 (landmark 2)))
1614 ;;;###autoload
1615 (defun landmark (parg)
1616 "Start or resume an Landmark game.
1617 If a game is in progress, this command allows you to resume it.
1618 Here is the relation between prefix args and game options:
1620 prefix arg | robot is auto-started | weights are saved from last game
1621 ---------------------------------------------------------------------
1622 none / 1 | yes | no
1623 2 | yes | yes
1624 3 | no | yes
1625 4 | no | no
1627 You start by moving to a square and typing \\[landmark-start-robot],
1628 if you did not use a prefix arg to ask for automatic start.
1629 Use \\[describe-mode] for more info."
1630 (interactive "p")
1632 (setf landmark-n nil landmark-m nil)
1633 (landmark-switch-to-window)
1634 (cond
1635 (landmark-emacs-is-computing
1636 (landmark-crash-game))
1637 ((or (not landmark-game-in-progress)
1638 (<= landmark-number-of-moves 2))
1639 (let ((max-width (landmark-max-width))
1640 (max-height (landmark-max-height)))
1641 (or landmark-n (setq landmark-n max-width))
1642 (or landmark-m (setq landmark-m max-height))
1643 (cond ((< landmark-n 1)
1644 (error "I need at least 1 column"))
1645 ((< landmark-m 1)
1646 (error "I need at least 1 row"))
1647 ((> landmark-n max-width)
1648 (error "I cannot display %d columns in that window" landmark-n)))
1649 (if (and (> landmark-m max-height)
1650 (not (eq landmark-m landmark-saved-board-height))
1651 ;; Use EQ because SAVED-BOARD-HEIGHT may be nil
1652 (not (y-or-n-p (format "Do you really want %d rows? " landmark-m))))
1653 (setq landmark-m max-height)))
1654 (if landmark-one-moment-please
1655 (message "One moment, please..."))
1656 (landmark-start-game landmark-n landmark-m)
1657 (eval (cons 'landmark-init
1658 (cond
1659 ((= parg 1) '(t nil))
1660 ((= parg 2) '(t t))
1661 ((= parg 3) '(nil t))
1662 ((= parg 4) '(nil nil))
1663 (t '(nil t))))))))
1666 ;;;_ + Local variables
1668 ;;; The following `allout-layout' local variable setting:
1669 ;;; - closes all topics from the first topic to just before the third-to-last,
1670 ;;; - shows the children of the third to last (config vars)
1671 ;;; - and the second to last (code section),
1672 ;;; - and closes the last topic (this local-variables section).
1673 ;;;Local variables:
1674 ;;;allout-layout: (0 : -1 -1 0)
1675 ;;;End:
1677 (provide 'landmark)
1679 ;;; landmark.el ends here