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