1 ;;; landmark.el --- neural-network robot that learns landmarks
3 ;; Copyright (C) 1996, 1997, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Terrence Brannon (was: <brannon@rana.usc.edu>)
7 ;; Created: December 16, 1996 - first release to usenet
8 ;; Keywords: gomoku, neural network, adaptive search, chemotaxis
12 ;;; M-x eval-current-buffer
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 2, or (at your option)
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31 ;; Boston, MA 02110-1301, USA.
35 ;;; Lm is a relatively non-participatory game in which a robot
36 ;;; attempts to maneuver towards a tree at the center of the window
37 ;;; based on unique olfactory cues from each of the 4 directions. If
38 ;;; the smell of the tree increases, then the weights in the robot's
39 ;;; brain are adjusted to encourage this odor-driven behavior in the
40 ;;; future. If the smell of the tree decreases, the robots weights are
41 ;;; adjusted to discourage a correct move.
43 ;;; In laymen's terms, the search space is initially flat. The point
44 ;;; of training is to "turn up the edges of the search space" so that
45 ;;; the robot rolls toward the center.
47 ;;; Further, do not become alarmed if the robot appears to oscillate
48 ;;; back and forth between two or a few positions. This simply means
49 ;;; it is currently caught in a local minimum and is doing its best to
52 ;;; The version of this program as described has a small problem. a
53 ;;; move in a net direction can produce gross credit assignment. for
54 ;;; example, if moving south will produce positive payoff, then, if in
55 ;;; a single move, one moves east,west and south, then both east and
56 ;;; west will be improved when they shouldn't
58 ;;; Many thanks to Yuri Pryadkin (yuri@rana.usc.edu) for this
59 ;;; concise problem description.
62 (eval-when-compile (require 'cl
))
69 "Neural-network robot that learns landmarks."
75 ;; The board is a rectangular grid. We code empty squares with 0, X's with 1
76 ;; and O's with 6. The rectangle is recorded in a one dimensional vector
77 ;; containing padding squares (coded with -1). These squares allow us to
78 ;; detect when we are trying to move out of the board. We denote a square by
79 ;; its (X,Y) coords, or by the INDEX corresponding to them in the vector. The
80 ;; leftmost topmost square has coords (1,1) and index lm-board-width + 2.
81 ;; Similarly, vectors between squares may be given by two DX, DY coords or by
82 ;; one DEPL (the difference between indexes).
84 (defvar lm-board-width nil
85 "Number of columns on the Lm board.")
86 (defvar lm-board-height nil
87 "Number of lines on the Lm board.")
90 "Vector recording the actual state of the Lm board.")
92 (defvar lm-vector-length nil
93 "Length of lm-board vector.")
95 (defvar lm-draw-limit nil
96 ;; This is usually set to 70% of the number of squares.
97 "After how many moves will Emacs offer a draw?")
100 "This is the x coordinate of the center of the board.")
103 "This is the y coordinate of the center of the board.")
106 "This is the x dimension of the playing board.")
109 "This is the y dimension of the playing board.")
112 (defun lm-xy-to-index (x y
)
113 "Translate X, Y cartesian coords into the corresponding board index."
114 (+ (* y lm-board-width
) x y
))
116 (defun lm-index-to-x (index)
117 "Return corresponding x-coord of board INDEX."
118 (% index
(1+ lm-board-width
)))
120 (defun lm-index-to-y (index)
121 "Return corresponding y-coord of board INDEX."
122 (/ index
(1+ lm-board-width
)))
124 (defun lm-init-board ()
125 "Create the lm-board vector and fill it with initial values."
126 (setq lm-board
(make-vector lm-vector-length
0))
127 ;; Every square is 0 (i.e. empty) except padding squares:
128 (let ((i 0) (ii (1- lm-vector-length
)))
129 (while (<= i lm-board-width
) ; The squares in [0..width] and in
130 (aset lm-board i -
1) ; [length - width - 1..length - 1]
131 (aset lm-board ii -
1) ; are padding squares.
135 (while (< i lm-vector-length
)
136 (aset lm-board i -
1) ; and also all k*(width+1)
137 (setq i
(+ i lm-board-width
1)))))
139 ;;;_ + DISPLAYING THE BOARD.
141 ;; You may change these values if you have a small screen or if the squares
142 ;; look rectangular, but spacings SHOULD be at least 2 (MUST BE at least 1).
144 (defconst lm-square-width
2
145 "*Horizontal spacing between squares on the Lm board.")
147 (defconst lm-square-height
1
148 "*Vertical spacing between squares on the Lm board.")
150 (defconst lm-x-offset
3
151 "*Number of columns between the Lm board and the side of the window.")
153 (defconst lm-y-offset
1
154 "*Number of lines between the Lm board and the top of the window.")
157 ;;;_ + LM MODE AND KEYMAP.
159 (defcustom lm-mode-hook nil
160 "If non-nil, its value is called on entry to Lm mode."
164 (defvar lm-mode-map nil
165 "Local keymap to use in Lm mode.")
168 (setq lm-mode-map
(make-sparse-keymap))
170 ;; Key bindings for cursor motion.
171 (define-key lm-mode-map
"y" 'lm-move-nw
) ; y
172 (define-key lm-mode-map
"u" 'lm-move-ne
) ; u
173 (define-key lm-mode-map
"b" 'lm-move-sw
) ; b
174 (define-key lm-mode-map
"n" 'lm-move-se
) ; n
175 (define-key lm-mode-map
"h" 'backward-char
) ; h
176 (define-key lm-mode-map
"l" 'forward-char
) ; l
177 (define-key lm-mode-map
"j" 'lm-move-down
) ; j
178 (define-key lm-mode-map
"k" 'lm-move-up
) ; k
180 (define-key lm-mode-map
[kp-7
] 'lm-move-nw
)
181 (define-key lm-mode-map
[kp-9
] 'lm-move-ne
)
182 (define-key lm-mode-map
[kp-1
] 'lm-move-sw
)
183 (define-key lm-mode-map
[kp-3
] 'lm-move-se
)
184 (define-key lm-mode-map
[kp-4
] 'backward-char
)
185 (define-key lm-mode-map
[kp-6
] 'forward-char
)
186 (define-key lm-mode-map
[kp-2
] 'lm-move-down
)
187 (define-key lm-mode-map
[kp-8
] 'lm-move-up
)
189 (define-key lm-mode-map
"\C-n" 'lm-move-down
) ; C-n
190 (define-key lm-mode-map
"\C-p" 'lm-move-up
) ; C-p
192 ;; Key bindings for entering Human moves.
193 (define-key lm-mode-map
"X" 'lm-human-plays
) ; X
194 (define-key lm-mode-map
"x" 'lm-human-plays
) ; x
196 (define-key lm-mode-map
" " 'lm-start-robot
) ; SPC
197 (define-key lm-mode-map
[down-mouse-1
] 'lm-start-robot
)
198 (define-key lm-mode-map
[drag-mouse-1
] 'lm-click
)
199 (define-key lm-mode-map
[mouse-1
] 'lm-click
)
200 (define-key lm-mode-map
[down-mouse-2
] 'lm-click
)
201 (define-key lm-mode-map
[mouse-2
] 'lm-mouse-play
)
202 (define-key lm-mode-map
[drag-mouse-2
] 'lm-mouse-play
)
204 (define-key lm-mode-map
[remap previous-line
] 'lm-move-up
)
205 (define-key lm-mode-map
[remap next-line
] 'lm-move-down
)
206 (define-key lm-mode-map
[remap beginning-of-line
] 'lm-beginning-of-line
)
207 (define-key lm-mode-map
[remap end-of-line
] 'lm-end-of-line
)
208 (define-key lm-mode-map
[remap undo
] 'lm-human-takes-back
)
209 (define-key lm-mode-map
[remap advertised-undo
] 'lm-human-takes-back
))
211 (defvar lm-emacs-won
()
212 "*For making font-lock use the winner's face for the line.")
214 (defvar lm-font-lock-face-O
215 (if (display-color-p)
216 (list (facemenu-get-face 'fg
:red
) 'bold
))
217 "*Face to use for Emacs' O.")
219 (defvar lm-font-lock-face-X
220 (if (display-color-p)
221 (list (facemenu-get-face 'fg
:green
) 'bold
))
222 "*Face to use for your X.")
224 (defvar lm-font-lock-keywords
225 '(("O" . lm-font-lock-face-O
)
226 ("X" . lm-font-lock-face-X
)
227 ("[-|/\\]" 0 (if lm-emacs-won
229 lm-font-lock-face-X
)))
230 "*Font lock rules for Lm.")
232 (put 'lm-mode
'front-sticky
233 (put 'lm-mode
'rear-nonsticky
'(intangible)))
234 (put 'lm-mode
'intangible
1)
235 ;; This one is for when they set view-read-only to t: Landmark cannot
236 ;; allow View Mode to be activated in its buffer.
237 (put 'lm-mode
'mode-class
'special
)
240 "Major mode for playing Lm against Emacs.
241 You and Emacs play in turn by marking a free square. You mark it with X
242 and Emacs marks it with O. The winner is the first to get five contiguous
243 marks horizontally, vertically or in diagonal.
245 You play by moving the cursor over the square you choose and hitting \\[lm-human-plays].
247 Other useful commands:
249 Entry to this mode calls the value of `lm-mode-hook' if that value
250 is non-nil. One interesting value is `turn-on-font-lock'."
252 (kill-all-local-variables)
253 (setq major-mode
'lm-mode
255 (lm-display-statistics)
256 (use-local-map lm-mode-map
)
257 (make-local-variable 'font-lock-defaults
)
258 (setq font-lock-defaults
'(lm-font-lock-keywords t
))
260 (run-mode-hooks 'lm-mode-hook
))
263 ;;;_ + THE SCORE TABLE.
266 ;; Every (free) square has a score associated to it, recorded in the
267 ;; LM-SCORE-TABLE vector. The program always plays in the square having
268 ;; the highest score.
270 (defvar lm-score-table nil
271 "Vector recording the actual score of the free squares.")
274 ;; The key point point about the algorithm is that, rather than considering
275 ;; the board as just a set of squares, we prefer to see it as a "space" of
276 ;; internested 5-tuples of contiguous squares (called qtuples).
278 ;; The aim of the program is to fill one qtuple with its O's while preventing
279 ;; you from filling another one with your X's. To that effect, it computes a
280 ;; score for every qtuple, with better qtuples having better scores. Of
281 ;; course, the score of a qtuple (taken in isolation) is just determined by
282 ;; its contents as a set, i.e. not considering the order of its elements. The
283 ;; highest score is given to the "OOOO" qtuples because playing in such a
284 ;; qtuple is winning the game. Just after this comes the "XXXX" qtuple because
285 ;; not playing in it is just loosing the game, and so on. Note that a
286 ;; "polluted" qtuple, i.e. one containing at least one X and at least one O,
287 ;; has score zero because there is no more any point in playing in it, from
288 ;; both an attacking and a defending point of view.
290 ;; Given the score of every qtuple, the score of a given free square on the
291 ;; board is just the sum of the scores of all the qtuples to which it belongs,
292 ;; because playing in that square is playing in all its containing qtuples at
293 ;; once. And it is that function which takes into account the internesting of
296 ;; This algorithm is rather simple but anyway it gives a not so dumb level of
297 ;; play. It easily extends to "n-dimensional Lm", where a win should not
298 ;; be obtained with as few as 5 contiguous marks: 6 or 7 (depending on n !)
299 ;; should be preferred.
302 ;; Here are the scores of the nine "non-polluted" configurations. Tuning
303 ;; these values will change (hopefully improve) the strength of the program
304 ;; and may change its style (rather aggressive here).
306 (defconst nil-score
7 "Score of an empty qtuple.")
307 (defconst Xscore
15 "Score of a qtuple containing one X.")
308 (defconst XXscore
400 "Score of a qtuple containing two X's.")
309 (defconst XXXscore
1800 "Score of a qtuple containing three X's.")
310 (defconst XXXXscore
100000 "Score of a qtuple containing four X's.")
311 (defconst Oscore
35 "Score of a qtuple containing one O.")
312 (defconst OOscore
800 "Score of a qtuple containing two O's.")
313 (defconst OOOscore
15000 "Score of a qtuple containing three O's.")
314 (defconst OOOOscore
800000 "Score of a qtuple containing four O's.")
316 ;; These values are not just random: if, given the following situation:
324 ;; you want Emacs to play in "a" and not in "b", then the parameters must
325 ;; satisfy the inequality:
327 ;; 6 * XXscore > XXXscore + XXscore
329 ;; because "a" mainly belongs to six "XX" qtuples (the others are less
330 ;; important) while "b" belongs to one "XXX" and one "XX" qtuples. Other
331 ;; conditions are required to obtain sensible moves, but the previous example
332 ;; should illustrate the point. If you manage to improve on these values,
333 ;; please send me a note. Thanks.
336 ;; As we chose values 0, 1 and 6 to denote empty, X and O squares, the
337 ;; contents of a qtuple are uniquely determined by the sum of its elements and
338 ;; we just have to set up a translation table.
340 (defconst lm-score-trans-table
341 (vector nil-score Xscore XXscore XXXscore XXXXscore
0
347 "Vector associating qtuple contents to their score.")
350 ;; If you do not modify drastically the previous constants, the only way for a
351 ;; square to have a score higher than OOOOscore is to belong to a "OOOO"
352 ;; qtuple, thus to be a winning move. Similarly, the only way for a square to
353 ;; have a score between XXXXscore and OOOOscore is to belong to a "XXXX"
354 ;; qtuple. We may use these considerations to detect when a given move is
355 ;; winning or loosing.
357 (defconst lm-winning-threshold OOOOscore
358 "Threshold score beyond which an Emacs move is winning.")
360 (defconst lm-loosing-threshold XXXXscore
361 "Threshold score beyond which a human move is winning.")
364 (defun lm-strongest-square ()
365 "Compute index of free square with highest score, or nil if none."
366 ;; We just have to loop other all squares. However there are two problems:
367 ;; 1/ The SCORE-TABLE only gives correct scores to free squares. To speed
368 ;; up future searches, we set the score of padding or occupied squares
369 ;; to -1 whenever we meet them.
370 ;; 2/ We want to choose randomly between equally good moves.
372 (count 0) ; Number of equally good moves
373 (square (lm-xy-to-index 1 1)) ; First square
374 (end (lm-xy-to-index lm-board-width lm-board-height
))
376 (while (<= square end
)
378 ;; If score is lower (i.e. most of the time), skip to next:
379 ((< (aref lm-score-table square
) score-max
))
380 ;; If score is better, beware of non free squares:
381 ((> (setq score
(aref lm-score-table square
)) score-max
)
382 (if (zerop (aref lm-board square
)) ; is it free ?
383 (setq count
1 ; yes: take it !
386 (aset lm-score-table square -
1))) ; no: kill it !
387 ;; If score is equally good, choose randomly. But first check freeness:
388 ((not (zerop (aref lm-board square
)))
389 (aset lm-score-table square -
1))
390 ((zerop (random (setq count
(1+ count
))))
391 (setq best-square square
393 (setq square
(1+ square
))) ; try next square
396 ;;;_ - INITIALIZING THE SCORE TABLE.
398 ;; At initialization the board is empty so that every qtuple amounts for
399 ;; nil-score. Therefore, the score of any square is nil-score times the number
400 ;; of qtuples that pass through it. This number is 3 in a corner and 20 if you
401 ;; are sufficiently far from the sides. As computing the number is time
402 ;; consuming, we initialize every square with 20*nil-score and then only
403 ;; consider squares at less than 5 squares from one side. We speed this up by
404 ;; taking symmetry into account.
405 ;; Also, as it is likely that successive games will be played on a board with
406 ;; same size, it is a good idea to save the initial SCORE-TABLE configuration.
408 (defvar lm-saved-score-table nil
409 "Recorded initial value of previous score table.")
411 (defvar lm-saved-board-width nil
412 "Recorded value of previous board width.")
414 (defvar lm-saved-board-height nil
415 "Recorded value of previous board height.")
418 (defun lm-init-score-table ()
419 "Create the score table vector and fill it with initial values."
420 (if (and lm-saved-score-table
; Has it been stored last time ?
421 (= lm-board-width lm-saved-board-width
)
422 (= lm-board-height lm-saved-board-height
))
423 (setq lm-score-table
(copy-sequence lm-saved-score-table
))
426 (make-vector lm-vector-length
(* 20 nil-score
)))
427 (let (i j maxi maxj maxi2 maxj2
)
428 (setq maxi
(/ (1+ lm-board-width
) 2)
429 maxj
(/ (1+ lm-board-height
) 2)
432 ;; We took symmetry into account and could use it more if the board
433 ;; would have been square and not rectangular !
434 ;; In our case we deal with all (i,j) in the set [1..maxi2]*[1..maxj] U
435 ;; [maxi2+1..maxi]*[1..maxj2]. Maxi2 and maxj2 are used because the
436 ;; board may well be less than 8 by 8 !
441 (lm-init-square-score i j
)
447 (lm-init-square-score i j
)
450 (setq lm-saved-score-table
(copy-sequence lm-score-table
)
451 lm-saved-board-width lm-board-width
452 lm-saved-board-height lm-board-height
)))
454 (defun lm-nb-qtuples (i j
)
455 "Return the number of qtuples containing square I,J."
456 ;; This function is complicated because we have to deal
457 ;; with ugly cases like 3 by 6 boards, but it works.
458 ;; If you have a simpler (and correct) solution, send it to me. Thanks !
459 (let ((left (min 4 (1- i
)))
460 (right (min 4 (- lm-board-width i
)))
462 (down (min 4 (- lm-board-height j
))))
464 (min (max (+ left right
) 3) 8)
465 (min (max (+ up down
) 3) 8)
466 (min (max (+ (min left up
) (min right down
)) 3) 8)
467 (min (max (+ (min right up
) (min left down
)) 3) 8))))
469 (defun lm-init-square-score (i j
)
470 "Give initial score to square I,J and to its mirror images."
471 (let ((ii (1+ (- lm-board-width i
)))
472 (jj (1+ (- lm-board-height j
)))
473 (sc (* (lm-nb-qtuples i j
) (aref lm-score-trans-table
0))))
474 (aset lm-score-table
(lm-xy-to-index i j
) sc
)
475 (aset lm-score-table
(lm-xy-to-index ii j
) sc
)
476 (aset lm-score-table
(lm-xy-to-index i jj
) sc
)
477 (aset lm-score-table
(lm-xy-to-index ii jj
) sc
)))
478 ;;;_ - MAINTAINING THE SCORE TABLE.
481 ;; We do not provide functions for computing the SCORE-TABLE given the
482 ;; contents of the BOARD. This would involve heavy nested loops, with time
483 ;; proportional to the size of the board. It is better to update the
484 ;; SCORE-TABLE after each move. Updating needs not modify more than 36
485 ;; squares: it is done in constant time.
487 (defun lm-update-score-table (square dval
)
488 "Update score table after SQUARE received a DVAL increment."
489 ;; The board has already been updated when this function is called.
490 ;; Updating scores is done by looking for qtuples boundaries in all four
491 ;; directions and then calling update-score-in-direction.
492 ;; Finally all squares received the right increment, and then are up to
493 ;; date, except possibly for SQUARE itself if we are taking a move back for
494 ;; its score had been set to -1 at the time.
495 (let* ((x (lm-index-to-x square
))
496 (y (lm-index-to-y square
))
497 (imin (max -
4 (- 1 x
)))
498 (jmin (max -
4 (- 1 y
)))
499 (imax (min 0 (- lm-board-width x
4)))
500 (jmax (min 0 (- lm-board-height y
4))))
501 (lm-update-score-in-direction imin imax
503 (lm-update-score-in-direction jmin jmax
505 (lm-update-score-in-direction (max imin jmin
) (min imax jmax
)
507 (lm-update-score-in-direction (max (- 1 y
) -
4
508 (- x lm-board-width
))
510 (- lm-board-height y
4))
513 (defun lm-update-score-in-direction (left right square dx dy dval
)
514 "Update scores for all squares in the qtuples in range.
515 That is, those between the LEFTth square and the RIGHTth after SQUARE,
516 along the DX, DY direction, considering that DVAL has been added on SQUARE."
517 ;; We always have LEFT <= 0, RIGHT <= 0 and DEPL > 0 but we may very well
518 ;; have LEFT > RIGHT, indicating that no qtuple contains SQUARE along that
521 ((> left right
)) ; Quit
523 (let (depl square0 square1 square2 count delta
)
524 (setq depl
(lm-xy-to-index dx dy
)
525 square0
(+ square
(* left depl
))
526 square1
(+ square
(* right depl
))
527 square2
(+ square0
(* 4 depl
)))
528 ;; Compute the contents of the first qtuple:
531 (while (<= square square2
)
532 (setq count
(+ count
(aref lm-board square
))
533 square
(+ square depl
)))
534 (while (<= square0 square1
)
535 ;; Update the squares of the qtuple beginning in SQUARE0 and ending
537 (setq delta
(- (aref lm-score-trans-table count
)
538 (aref lm-score-trans-table
(- count dval
))))
539 (cond ((not (zerop delta
)) ; or else nothing to update
540 (setq square square0
)
541 (while (<= square square2
)
542 (if (zerop (aref lm-board square
)) ; only for free squares
543 (aset lm-score-table square
544 (+ (aref lm-score-table square
) delta
)))
545 (setq square
(+ square depl
)))))
546 ;; Then shift the qtuple one square along DEPL, this only requires
547 ;; modifying SQUARE0 and SQUARE2.
548 (setq square2
(+ square2 depl
)
549 count
(+ count
(- (aref lm-board square0
))
550 (aref lm-board square2
))
551 square0
(+ square0 depl
)))))))
557 ;; Several variables are used to monitor a game, including a GAME-HISTORY (the
558 ;; list of all (SQUARE . PREVSCORE) played) that allows to take moves back
559 ;; (anti-updating the score table) and to compute the table from scratch in
560 ;; case of an interruption.
562 (defvar lm-game-in-progress nil
563 "Non-nil if a game is in progress.")
565 (defvar lm-game-history nil
566 "A record of all moves that have been played during current game.")
568 (defvar lm-number-of-moves nil
569 "Number of moves already played in current game.")
571 (defvar lm-number-of-human-moves nil
572 "Number of moves already played by human in current game.")
574 (defvar lm-emacs-played-first nil
575 "Non-nil if Emacs played first.")
577 (defvar lm-human-took-back nil
578 "Non-nil if Human took back a move during the game.")
580 (defvar lm-human-refused-draw nil
581 "Non-nil if Human refused Emacs offer of a draw.")
583 (defvar lm-emacs-is-computing nil
584 ;; This is used to detect interruptions. Hopefully, it should not be needed.
585 "Non-nil if Emacs is in the middle of a computation.")
588 (defun lm-start-game (n m
)
589 "Initialize a new game on an N by M board."
590 (setq lm-emacs-is-computing t
) ; Raise flag
591 (setq lm-game-in-progress t
)
592 (setq lm-board-width n
594 lm-vector-length
(1+ (* (+ m
2) (1+ n
)))
595 lm-draw-limit
(/ (* 7 n m
) 10))
596 (setq lm-emacs-won nil
599 lm-number-of-human-moves
0
600 lm-emacs-played-first nil
601 lm-human-took-back nil
602 lm-human-refused-draw nil
)
603 (lm-init-display n m
) ; Display first: the rest takes time
604 (lm-init-score-table) ; INIT-BOARD requires that the score
605 (lm-init-board) ; table be already created.
606 (setq lm-emacs-is-computing nil
))
608 (defun lm-play-move (square val
&optional dont-update-score
)
609 "Go to SQUARE, play VAL and update everything."
610 (setq lm-emacs-is-computing t
) ; Raise flag
611 (cond ((= 1 val
) ; a Human move
612 (setq lm-number-of-human-moves
(1+ lm-number-of-human-moves
)))
613 ((zerop lm-number-of-moves
) ; an Emacs move. Is it first ?
614 (setq lm-emacs-played-first t
)))
615 (setq lm-game-history
616 (cons (cons square
(aref lm-score-table square
))
618 lm-number-of-moves
(1+ lm-number-of-moves
))
619 (lm-plot-square square val
)
620 (aset lm-board square val
) ; *BEFORE* UPDATE-SCORE !
621 (if dont-update-score nil
622 (lm-update-score-table square val
) ; previous val was 0: dval = val
623 (aset lm-score-table square -
1))
624 (setq lm-emacs-is-computing nil
))
626 (defun lm-take-back ()
627 "Take back last move and update everything."
628 (setq lm-emacs-is-computing t
)
629 (let* ((last-move (car lm-game-history
))
630 (square (car last-move
))
631 (oldval (aref lm-board square
)))
633 (setq lm-number-of-human-moves
(1- lm-number-of-human-moves
)))
634 (setq lm-game-history
(cdr lm-game-history
)
635 lm-number-of-moves
(1- lm-number-of-moves
))
636 (lm-plot-square square
0)
637 (aset lm-board square
0) ; *BEFORE* UPDATE-SCORE !
638 (lm-update-score-table square
(- oldval
))
639 (aset lm-score-table square
(cdr last-move
)))
640 (setq lm-emacs-is-computing nil
))
643 ;;;_ + SESSION CONTROL.
645 (defvar lm-number-of-trials
0
646 "The number of times that landmark has been run.")
648 (defvar lm-sum-of-moves
0
649 "The total number of moves made in all games.")
651 (defvar lm-number-of-emacs-wins
0
652 "Number of games Emacs won in this session.")
654 (defvar lm-number-of-human-wins
0
655 "Number of games you won in this session.")
657 (defvar lm-number-of-draws
0
658 "Number of games already drawn in this session.")
661 (defun lm-terminate-game (result)
662 "Terminate the current game with RESULT."
663 (setq lm-number-of-trials
(1+ lm-number-of-trials
))
664 (setq lm-sum-of-moves
(+ lm-sum-of-moves lm-number-of-moves
))
665 (if (eq result
'crash-game
)
667 "Sorry, I have been interrupted and cannot resume that game..."))
668 (lm-display-statistics)
670 (setq lm-game-in-progress nil
))
672 (defun lm-crash-game ()
673 "What to do when Emacs detects it has been interrupted."
674 (setq lm-emacs-is-computing nil
)
675 (lm-terminate-game 'crash-game
)
676 (sit-for 4) ; Let's see the message
677 (lm-prompt-for-other-game))
680 ;;;_ + INTERACTIVE COMMANDS.
682 (defun lm-emacs-plays ()
683 "Compute Emacs next move and play it."
685 (lm-switch-to-window)
687 (lm-emacs-is-computing
689 ((not lm-game-in-progress
)
690 (lm-prompt-for-other-game))
692 (message "Let me think...")
694 (setq square
(lm-strongest-square))
696 (lm-terminate-game 'nobody-won
))
698 (setq score
(aref lm-score-table square
))
699 (lm-play-move square
6)
700 (cond ((>= score lm-winning-threshold
)
701 (setq lm-emacs-won t
) ; for font-lock
702 (lm-find-filled-qtuple square
6)
703 (lm-terminate-game 'emacs-won
))
705 (lm-terminate-game 'nobody-won
))
706 ((and (> lm-number-of-moves lm-draw-limit
)
707 (not lm-human-refused-draw
)
709 (lm-terminate-game 'draw-agreed
))
711 (lm-prompt-for-move)))))))))
713 ;; For small square dimensions this is approximate, since though measured in
714 ;; pixels, event's (X . Y) is a character's top-left corner.
715 (defun lm-click (click)
716 "Position at the square where you click."
718 (and (windowp (posn-window (setq click
(event-end click
))))
719 (numberp (posn-point click
))
720 (select-window (posn-window click
))
721 (setq click
(posn-col-row click
))
723 (min (max (/ (+ (- (car click
)
728 (% lm-square-width
2)
729 (/ lm-square-width
2))
733 (min (max (/ (+ (- (cdr click
)
736 (let ((inhibit-point-motion-hooks t
))
737 (count-lines 1 (window-start)))
739 (% lm-square-height
2)
740 (/ lm-square-height
2))
745 (defun lm-mouse-play (click)
746 "Play at the square where you click."
751 (defun lm-human-plays ()
752 "Signal to the Lm program that you have played.
753 You must have put the cursor on the square where you want to play.
754 If the game is finished, this command requests for another game."
756 (lm-switch-to-window)
758 (lm-emacs-is-computing
760 ((not lm-game-in-progress
)
761 (lm-prompt-for-other-game))
764 (setq square
(lm-point-square))
766 (error "Your point is not on a square. Retry!"))
767 ((not (zerop (aref lm-board square
)))
768 (error "Your point is not on a free square. Retry!"))
770 (setq score
(aref lm-score-table square
))
771 (lm-play-move square
1)
772 (cond ((and (>= score lm-loosing-threshold
)
773 ;; Just testing SCORE > THRESHOLD is not enough for
774 ;; detecting wins, it just gives an indication that
775 ;; we confirm with LM-FIND-FILLED-QTUPLE.
776 (lm-find-filled-qtuple square
1))
777 (lm-terminate-game 'human-won
))
779 (lm-emacs-plays)))))))))
781 (defun lm-human-takes-back ()
782 "Signal to the Lm program that you wish to take back your last move."
784 (lm-switch-to-window)
786 (lm-emacs-is-computing
788 ((not lm-game-in-progress
)
789 (message "Too late for taking back...")
791 (lm-prompt-for-other-game))
792 ((zerop lm-number-of-human-moves
)
793 (message "You have not played yet... Your move?"))
795 (message "One moment, please...")
796 ;; It is possible for the user to let Emacs play several consecutive
797 ;; moves, so that the best way to know when to stop taking back moves is
798 ;; to count the number of human moves:
799 (setq lm-human-took-back t
)
800 (let ((number lm-number-of-human-moves
))
801 (while (= number lm-number-of-human-moves
)
803 (lm-prompt-for-move))))
805 (defun lm-human-resigns ()
806 "Signal to the Lm program that you may want to resign."
808 (lm-switch-to-window)
810 (lm-emacs-is-computing
812 ((not lm-game-in-progress
)
813 (message "There is no game in progress"))
814 ((y-or-n-p "You mean, you resign? ")
815 (lm-terminate-game 'human-resigned
))
816 ((y-or-n-p "You mean, we continue? ")
817 (lm-prompt-for-move))
819 (lm-terminate-game 'human-resigned
)))) ; OK. Accept it
821 ;;;_ + PROMPTING THE HUMAN PLAYER.
823 (defun lm-prompt-for-move ()
824 "Display a message asking for Human's move."
825 (message (if (zerop lm-number-of-human-moves
)
826 "Your move? (move to a free square and hit X, RET ...)"
828 ;; This may seem silly, but if one omits the following line (or a similar
829 ;; one), the cursor may very well go to some place where POINT is not.
830 (save-excursion (set-buffer (other-buffer))))
832 (defun lm-prompt-for-other-game ()
833 "Ask for another game, and start it."
834 (if (y-or-n-p "Another game? ")
835 (if (y-or-n-p "Retain learned weights ")
838 (message "Chicken!")))
840 (defun lm-offer-a-draw ()
841 "Offer a draw and return t if Human accepted it."
842 (or (y-or-n-p "I offer you a draw. Do you accept it? ")
843 (not (setq lm-human-refused-draw t
))))
846 (defun lm-max-width ()
847 "Largest possible board width for the current window."
848 (1+ (/ (- (window-width (selected-window))
849 lm-x-offset lm-x-offset
1)
852 (defun lm-max-height ()
853 "Largest possible board height for the current window."
854 (1+ (/ (- (window-height (selected-window))
855 lm-y-offset lm-y-offset
2)
856 ;; 2 instead of 1 because WINDOW-HEIGHT includes the mode line !
860 "Return the board row where point is."
861 (let ((inhibit-point-motion-hooks t
))
862 (1+ (/ (- (count-lines 1 (point)) lm-y-offset
(if (bolp) 0 1))
865 (defun lm-point-square ()
866 "Return the index of the square point is on."
867 (let ((inhibit-point-motion-hooks t
))
868 (lm-xy-to-index (1+ (/ (- (current-column) lm-x-offset
)
872 (defun lm-goto-square (index)
873 "Move point to square number INDEX."
874 (lm-goto-xy (lm-index-to-x index
) (lm-index-to-y index
)))
876 (defun lm-goto-xy (x y
)
877 "Move point to square at X, Y coords."
878 (let ((inhibit-point-motion-hooks t
))
879 (goto-line (+ 1 lm-y-offset
(* lm-square-height
(1- y
)))))
880 (move-to-column (+ lm-x-offset
(* lm-square-width
(1- x
)))))
882 (defun lm-plot-square (square value
)
883 "Draw 'X', 'O' or '.' on SQUARE depending on VALUE, leave point there."
885 (lm-goto-square square
))
886 (let ((inhibit-read-only t
)
887 (inhibit-point-motion-hooks t
))
888 (insert-and-inherit (cond ((= value
1) ?.
)
896 (add-text-properties (1- (point)) (point)
897 '(mouse-face highlight
899 mouse-1: get robot moving, mouse-2: play on this square")))
902 (sit-for 0)) ; Display NOW
904 (defun lm-init-display (n m
)
905 "Display an N by M Lm board."
906 (buffer-disable-undo (current-buffer))
907 (let ((inhibit-read-only t
)
911 ;; Try to minimize number of chars (because of text properties)
913 (if (zerop (% lm-x-offset lm-square-width
))
915 (max (/ (+ (% lm-x-offset lm-square-width
)
916 lm-square-width
1) 2) 2)))
918 (newline lm-y-offset
)
921 x
(- lm-x-offset lm-square-width
))
922 (while (>= (setq j
(1- j
)) 0)
923 (insert-char ?
\t (/ (- (setq x
(+ x lm-square-width
))
926 (insert-char ?
(- x
(current-column)))
927 (if (setq intangible
(not intangible
))
928 (put-text-property point
(point) 'intangible
2))
933 (append-to-buffer (current-buffer) opoint
(point))
935 (goto-char (point-max))))
938 (add-text-properties point
(point)
939 '(mouse-face highlight help-echo
"\
940 mouse-1: get robot moving, mouse-2: play on this square")))
941 (> (setq i
(1- i
)) 0))
944 (insert-char ?
\n lm-square-height
))
945 (or (eq (char-after 1) ?.
)
946 (put-text-property 1 2 'point-entered
947 (lambda (x y
) (if (bobp) (forward-char)))))
949 (put-text-property point
(point) 'intangible
2))
950 (put-text-property point
(point) 'point-entered
951 (lambda (x y
) (if (eobp) (backward-char))))
952 (put-text-property (point-min) (point) 'category
'lm-mode
))
953 (lm-goto-xy (/ (1+ n
) 2) (/ (1+ m
) 2)) ; center of the board
954 (sit-for 0)) ; Display NOW
956 (defun lm-display-statistics ()
957 "Obnoxiously display some statistics about previous games in mode line."
958 ;; We store this string in the mode-line-process local variable.
959 ;; This is certainly not the cleanest way out ...
960 (setq mode-line-process
961 (format ": Trials: %d, Avg#Moves: %d"
963 (if (zerop lm-number-of-trials
)
965 (/ lm-sum-of-moves lm-number-of-trials
))))
966 (force-mode-line-update))
968 (defun lm-switch-to-window ()
969 "Find or create the Lm buffer, and display it."
971 (let ((buff (get-buffer "*Lm*")))
972 (if buff
; Buffer exists:
973 (switch-to-buffer buff
) ; no problem.
974 (if lm-game-in-progress
975 (lm-crash-game)) ; buffer has been killed or something
976 (switch-to-buffer "*Lm*") ; Anyway, start anew.
980 ;;;_ + CROSSING WINNING QTUPLES.
982 ;; When someone succeeds in filling a qtuple, we draw a line over the five
983 ;; corresponding squares. One problem is that the program does not know which
984 ;; squares ! It only knows the square where the last move has been played and
985 ;; who won. The solution is to scan the board along all four directions.
987 (defun lm-find-filled-qtuple (square value
)
988 "Return t if SQUARE belongs to a qtuple filled with VALUEs."
989 (or (lm-check-filled-qtuple square value
1 0)
990 (lm-check-filled-qtuple square value
0 1)
991 (lm-check-filled-qtuple square value
1 1)
992 (lm-check-filled-qtuple square value -
1 1)))
994 (defun lm-check-filled-qtuple (square value dx dy
)
995 "Return t if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
997 (left square
) (right square
)
998 (depl (lm-xy-to-index dx dy
)))
999 (while (and (> a -
4) ; stretch tuple left
1000 (= value
(aref lm-board
(setq left
(- left depl
)))))
1002 (while (and (< b
(+ a
4)) ; stretch tuple right
1003 (= value
(aref lm-board
(setq right
(+ right depl
)))))
1005 (cond ((= b
(+ a
4)) ; tuple length = 5 ?
1006 (lm-cross-qtuple (+ square
(* a depl
)) (+ square
(* b depl
))
1010 (defun lm-cross-qtuple (square1 square2 dx dy
)
1011 "Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
1012 (save-excursion ; Not moving point from last square
1013 (let ((depl (lm-xy-to-index dx dy
))
1014 (inhibit-read-only t
)
1015 (inhibit-point-motion-hooks t
))
1016 ;; WARNING: this function assumes DEPL > 0 and SQUARE2 > SQUARE1
1017 (while (/= square1 square2
)
1018 (lm-goto-square square1
)
1019 (setq square1
(+ square1 depl
))
1021 ((= dy
0) ; Horizontal
1023 (insert-char ?-
(1- lm-square-width
) t
)
1024 (delete-region (point) (progn
1025 (skip-chars-forward " \t")
1027 ((= dx
0) ; Vertical
1029 (column (current-column)))
1030 (while (< lm-n lm-square-height
)
1031 (setq lm-n
(1+ lm-n
))
1034 (insert-and-inherit ?|
))))
1035 ((= dx -
1) ; 1st Diagonal
1036 (indent-to (prog1 (- (current-column) (/ lm-square-width
2))
1037 (forward-line (/ lm-square-height
2))))
1038 (insert-and-inherit ?
/))
1040 (indent-to (prog1 (+ (current-column) (/ lm-square-width
2))
1041 (forward-line (/ lm-square-height
2))))
1042 (insert-and-inherit ?
\\))))))
1043 (sit-for 0)) ; Display NOW
1046 ;;;_ + CURSOR MOTION.
1048 ;; previous-line and next-line don't work right with intangible newlines
1049 (defun lm-move-down ()
1050 "Move point down one row on the Lm board."
1052 (if (< (lm-point-y) lm-board-height
)
1053 (next-line 1)));;; lm-square-height)))
1055 (defun lm-move-up ()
1056 "Move point up one row on the Lm board."
1058 (if (> (lm-point-y) 1)
1059 (previous-line lm-square-height
)))
1061 (defun lm-move-ne ()
1062 "Move point North East on the Lm board."
1067 (defun lm-move-se ()
1068 "Move point South East on the Lm board."
1073 (defun lm-move-nw ()
1074 "Move point North West on the Lm board."
1079 (defun lm-move-sw ()
1080 "Move point South West on the Lm board."
1085 (defun lm-beginning-of-line ()
1086 "Move point to first square on the Lm board row."
1088 (move-to-column lm-x-offset
))
1090 (defun lm-end-of-line ()
1091 "Move point to last square on the Lm board row."
1093 (move-to-column (+ lm-x-offset
1094 (* lm-square-width
(1- lm-board-width
)))))
1097 ;;;_ + Simulation variables
1100 (defvar lm-nvar
0.0075
1102 Affects a noise generator which was used in an earlier incarnation of
1103 this program to add a random element to the way moves were made.")
1104 ;;;_ - lists of cardinal directions
1106 (defvar lm-ns
'(lm-n lm-s
)
1107 "Used when doing something relative to the north and south axes.")
1108 (defvar lm-ew
'(lm-e lm-w
)
1109 "Used when doing something relative to the east and west axes.")
1110 (defvar lm-directions
'(lm-n lm-s lm-e lm-w
)
1111 "The cardinal directions.")
1112 (defvar lm-8-directions
1113 '((lm-n) (lm-n lm-w
) (lm-w) (lm-s lm-w
)
1114 (lm-s) (lm-s lm-e
) (lm-e) (lm-n lm-e
))
1115 "The full 8 possible directions.")
1117 (defvar lm-number-of-moves
1118 "The number of moves made by the robot so far.")
1121 ;;;_* Terry's mods to create lm.el
1123 ;;;(setq lm-debug nil)
1124 (defvar lm-debug nil
1125 "If non-nil, debugging is printed.")
1126 (defcustom lm-one-moment-please nil
1127 "If non-nil, print \"One moment please\" when a new board is generated.
1128 The drawback of this is you don't see how many moves the last run took
1129 because it is overwritten by \"One moment please\"."
1132 (defcustom lm-output-moves t
1133 "If non-nil, output number of moves so far on a move-by-move basis."
1138 (defun lm-weights-debug ()
1140 (progn (lm-print-wts) (lm-blackbox) (lm-print-y,s
,noise
)
1143 ;;;_ - Printing various things
1144 (defun lm-print-distance-int (direction)
1146 (insert (format "%S %S " direction
(get direction
'distance
))))
1149 (defun lm-print-distance ()
1150 (insert (format "tree: %S \n" (calc-distance-of-robot-from 'lm-tree
)))
1151 (mapc 'lm-print-distance-int lm-directions
))
1154 ;;(setq direction 'lm-n)
1156 (defun lm-nslify-wts-int (direction)
1157 (mapcar (lambda (target-direction)
1158 (get direction target-direction
))
1162 (defun lm-nslify-wts ()
1164 (let ((l (apply 'append
(mapcar 'lm-nslify-wts-int lm-directions
))))
1165 (insert (format "set data_value WTS \n %s \n" l
))
1166 (insert (format "/* max: %S min: %S */"
1167 (eval (cons 'max l
)) (eval (cons 'min l
))))))
1169 (defun lm-print-wts-int (direction)
1170 (mapc (lambda (target-direction)
1171 (insert (format "%S %S %S "
1174 (get direction target-direction
))))
1178 (defun lm-print-wts ()
1181 (set-buffer "*lm-wts*")
1182 (insert "==============================\n")
1183 (mapc 'lm-print-wts-int lm-directions
)))
1185 (defun lm-print-moves (moves)
1188 (set-buffer "*lm-moves*")
1189 (insert (format "%S\n" moves
))))
1192 (defun lm-print-y,s
,noise-int
(direction)
1193 (insert (format "%S:lm-y %S, s %S, noise %S \n"
1194 (symbol-name direction
)
1195 (get direction
'y_t
)
1197 (get direction
'noise
)
1200 (defun lm-print-y,s
,noise
()
1203 (set-buffer "*lm-y,s,noise*")
1204 (insert "==============================\n")
1205 (mapc 'lm-print-y
,s
,noise-int lm-directions
)))
1207 (defun lm-print-smell-int (direction)
1208 (insert (format "%S: smell: %S \n"
1209 (symbol-name direction
)
1210 (get direction
'smell
))))
1212 (defun lm-print-smell ()
1215 (set-buffer "*lm-smell*")
1216 (insert "==============================\n")
1217 (insert (format "tree: %S \n" (get 'z
't
)))
1218 (mapc 'lm-print-smell-int lm-directions
)))
1220 (defun lm-print-w0-int (direction)
1221 (insert (format "%S: w0: %S \n"
1222 (symbol-name direction
)
1223 (get direction
'w0
))))
1225 (defun lm-print-w0 ()
1228 (set-buffer "*lm-w0*")
1229 (insert "==============================\n")
1230 (mapc 'lm-print-w0-int lm-directions
)))
1232 (defun lm-blackbox ()
1234 (set-buffer "*lm-blackbox*")
1235 (insert "==============================\n")
1236 (insert "I smell: ")
1237 (mapc (lambda (direction)
1238 (if (> (get direction
'smell
) 0)
1239 (insert (format "%S " direction
))))
1244 (mapc (lambda (direction)
1245 (if (> (get direction
'y_t
) 0)
1246 (insert (format "%S " direction
))))
1249 (lm-print-wts-blackbox)
1250 (insert (format "z_t-z_t-1: %S" (- (get 'z
't
) (get 'z
't-1
))))
1254 (defun lm-print-wts-blackbox ()
1256 (mapc 'lm-print-wts-int lm-directions
))
1258 ;;;_ - learning parameters
1259 (defcustom lm-bound
0.005
1260 "The maximum that w0j may be."
1264 "A factor applied to modulate the increase in wij.
1265 Used in the function lm-update-normal-weights."
1268 (defcustom lm-c-naught
0.5
1269 "A factor applied to modulate the increase in w0j.
1270 Used in the function lm-update-naught-weights."
1273 (defvar lm-initial-w0
0.0)
1274 (defvar lm-initial-wij
0.0)
1275 (defcustom lm-no-payoff
0
1276 "The amount of simulation cycles that have occurred with no movement.
1277 Used to move the robot when he is stuck in a rut for some reason."
1280 (defcustom lm-max-stall-time
2
1281 "The maximum number of cycles that the robot can remain stuck in a place.
1282 After this limit is reached, lm-random-move is called to push him out of it."
1287 ;;;_ + Randomizing functions
1288 ;;;_ - lm-flip-a-coin ()
1289 (defun lm-flip-a-coin ()
1290 (if (> (random 5000) 2500)
1293 ;;;_ : lm-very-small-random-number ()
1294 ;(defun lm-very-small-random-number ()
1296 ; (* (/ (random 900000) 900000.0) .0001)))
1297 ;;;_ : lm-randomize-weights-for (direction)
1298 (defun lm-randomize-weights-for (direction)
1299 (mapc (lambda (target-direction)
1302 (* (lm-flip-a-coin) (/ (random 10000) 10000.0))))
1306 (* (- (/ (random 30001) 15000.0) 1) lm-nvar
))
1308 ;;;_ : lm-fix-weights-for (direction)
1309 (defun lm-fix-weights-for (direction)
1310 (mapc (lambda (target-direction)
1317 ;;;_ + Plotting functions
1318 ;;;_ - lm-plot-internal (sym)
1319 (defun lm-plot-internal (sym)
1320 (lm-plot-square (lm-xy-to-index
1324 ;;;_ - lm-plot-landmarks ()
1325 (defun lm-plot-landmarks ()
1326 (setq lm-cx
(/ lm-board-width
2))
1327 (setq lm-cy
(/ lm-board-height
2))
1329 (put 'lm-n
'x lm-cx
)
1333 (put 'lm-tree
'x lm-cx
)
1334 (put 'lm-tree
'y lm-cy
)
1335 (put 'lm-tree
'sym
6)
1337 (put 'lm-s
'x lm-cx
)
1338 (put 'lm-s
'y lm-board-height
)
1342 (put 'lm-w
'y
(/ lm-board-height
2))
1345 (put 'lm-e
'x lm-board-width
)
1346 (put 'lm-e
'y
(/ lm-board-height
2))
1349 (mapc 'lm-plot-internal
'(lm-n lm-s lm-e lm-w lm-tree
)))
1353 ;;;_ + Distance-calculation functions
1358 ;;;_ - distance (x x0 y y0)
1359 (defun distance (x x0 y y0
)
1360 (sqrt (+ (square (- x x0
)) (square (- y y0
)))))
1362 ;;;_ - calc-distance-of-robot-from (direction)
1363 (defun calc-distance-of-robot-from (direction)
1364 (put direction
'distance
1365 (distance (get direction
'x
)
1366 (lm-index-to-x (lm-point-square))
1368 (lm-index-to-y (lm-point-square)))))
1370 ;;;_ - calc-smell-internal (sym)
1371 (defun calc-smell-internal (sym)
1372 (let ((r (get sym
'r
))
1373 (d (calc-distance-of-robot-from sym
)))
1374 (if (> (* 0.5 (- 1 (/ d r
))) 0)
1375 (* 0.5 (- 1 (/ d r
)))
1379 ;;;_ + Learning (neural) functions
1382 ((> x lm-bound
) lm-bound
)
1386 (defun lm-y (direction)
1387 (let ((noise (put direction
'noise
(lm-noise))))
1389 (if (> (get direction
's
) 0.0)
1393 (defun lm-update-normal-weights (direction)
1394 (mapc (lambda (target-direction)
1395 (put direction target-direction
1397 (get direction target-direction
)
1399 (- (get 'z
't
) (get 'z
't-1
))
1400 (get target-direction
'y_t
)
1401 (get direction
'smell
)))))
1404 (defun lm-update-naught-weights (direction)
1405 (mapc (lambda (target-direction)
1411 (- (get 'z
't
) (get 'z
't-1
))
1412 (get direction
'y_t
))))))
1416 ;;;_ + Statistics gathering and creating functions
1418 (defun lm-calc-current-smells ()
1419 (mapc (lambda (direction)
1420 (put direction
'smell
(calc-smell-internal direction
)))
1423 (defun lm-calc-payoff ()
1424 (put 'z
't-1
(get 'z
't
))
1425 (put 'z
't
(calc-smell-internal 'lm-tree
))
1426 (if (= (- (get 'z
't
) (get 'z
't-1
)) 0.0)
1428 (setf lm-no-payoff
0)))
1430 (defun lm-store-old-y_t ()
1431 (mapc (lambda (direction)
1432 (put direction
'y_t-1
(get direction
'y_t
)))
1436 ;;;_ + Functions to move robot
1438 (defun lm-confidence-for (target-direction)
1440 (get target-direction
'w0
)
1441 (mapcar (lambda (direction)
1443 (get direction target-direction
)
1444 (get direction
'smell
)))
1448 (defun lm-calc-confidences ()
1449 (mapc (lambda (direction)
1450 (put direction
's
(lm-confidence-for direction
)))
1454 (if (and (= (get 'lm-n
'y_t
) 1.0) (= (get 'lm-s
'y_t
) 1.0))
1456 (mapc (lambda (dir) (put dir
'y_t
0)) lm-ns
)
1458 (message "n-s normalization."))))
1459 (if (and (= (get 'lm-w
'y_t
) 1.0) (= (get 'lm-e
'y_t
) 1.0))
1461 (mapc (lambda (dir) (put dir
'y_t
0)) lm-ew
)
1463 (message "e-w normalization"))))
1465 (mapc (lambda (pair)
1466 (if (> (get (car pair
) 'y_t
) 0)
1467 (funcall (car (cdr pair
)))))
1472 (lm-w backward-char
)))
1473 (lm-plot-square (lm-point-square) 1)
1474 (incf lm-number-of-moves
)
1476 (message "Moves made: %d" lm-number-of-moves
)))
1479 (defun lm-random-move ()
1481 (lambda (direction) (put direction
'y_t
0))
1483 (dolist (direction (nth (random 8) lm-8-directions
))
1484 (put direction
'y_t
1.0))
1487 (defun lm-amble-robot ()
1489 (while (> (calc-distance-of-robot-from 'lm-tree
) 0)
1492 (lm-calc-current-smells)
1494 (if (> lm-no-payoff lm-max-stall-time
)
1497 (lm-calc-confidences)
1498 (mapc 'lm-y lm-directions
)
1503 (mapc 'lm-update-normal-weights lm-directions
)
1504 (mapc 'lm-update-naught-weights lm-directions
)
1506 (lm-weights-debug)))
1507 (lm-terminate-game nil
))
1510 ;;;_ - lm-start-robot ()
1511 (defun lm-start-robot ()
1512 "Signal to the Lm program that you have played.
1513 You must have put the cursor on the square where you want to play.
1514 If the game is finished, this command requests for another game."
1516 (lm-switch-to-window)
1518 (lm-emacs-is-computing
1520 ((not lm-game-in-progress
)
1521 (lm-prompt-for-other-game))
1524 (setq square
(lm-point-square))
1525 (cond ((null square
)
1526 (error "Your point is not on a square. Retry!"))
1527 ((not (zerop (aref lm-board square
)))
1528 (error "Your point is not on a free square. Retry!"))
1531 (lm-plot-square square
1)
1534 (lm-calc-current-smells)
1535 (put 'z
't
(calc-smell-internal 'lm-tree
))
1541 (mapc 'lm-update-normal-weights lm-directions
)
1542 (mapc 'lm-update-naught-weights lm-directions
)
1547 ;;;_ + Misc functions
1548 ;;;_ - lm-init (auto-start save-weights)
1549 (defvar lm-tree-r
"")
1551 (defun lm-init (auto-start save-weights
)
1553 (setq lm-number-of-moves
0)
1560 (set-buffer (get-buffer-create "*lm-w0*"))
1562 (set-buffer (get-buffer-create "*lm-moves*"))
1563 (set-buffer (get-buffer-create "*lm-wts*"))
1565 (set-buffer (get-buffer-create "*lm-y,s,noise*"))
1567 (set-buffer (get-buffer-create "*lm-smell*"))
1569 (set-buffer (get-buffer-create "*lm-blackbox*"))
1571 (set-buffer (get-buffer-create "*lm-distance*"))
1575 (lm-set-landmark-signal-strengths)
1577 (mapc (lambda (direction)
1578 (put direction
'y_t
0.0))
1581 (if (not save-weights
)
1583 (mapc 'lm-fix-weights-for lm-directions
)
1584 (mapc (lambda (direction)
1585 (put direction
'w0 lm-initial-w0
))
1587 (message "Weights preserved for this run."))
1591 (lm-goto-xy (1+ (random lm-board-width
)) (1+ (random lm-board-height
)))
1595 ;;;_ - something which doesn't work
1597 ;(defum lm-sum-list (list)
1598 ; (if (> (length list) 0)
1599 ; (+ (car list) (lm-sum-list (cdr list)))
1602 ; (eval (cons '+ list))
1603 ;;;_ - lm-set-landmark-signal-strengths ()
1604 ;;; on a screen higher than wide, I noticed that the robot would amble
1605 ;;; left and right and not move forward. examining *lm-blackbox*
1606 ;;; revealed that there was no scent from the north and south
1607 ;;; landmarks, hence, they need less factoring down of the effect of
1608 ;;; distance on scent.
1610 (defun lm-set-landmark-signal-strengths ()
1612 (setq lm-tree-r
(* (sqrt (+ (square lm-cx
) (square lm-cy
))) 1.5))
1614 (mapc (lambda (direction)
1615 (put direction
'r
(* lm-cx
1.1)))
1617 (mapc (lambda (direction)
1618 (put direction
'r
(* lm-cy
1.1)))
1620 (put 'lm-tree
'r lm-tree-r
))
1623 ;;;_ + lm-test-run ()
1626 (defalias 'landmark-repeat
'lm-test-run
)
1628 (defun lm-test-run ()
1629 "Run 100 Lm games, each time saving the weights from the previous game."
1634 (dotimes (scratch-var 100)
1639 ;;;_ + lm: The function you invoke to play
1642 (defalias 'landmark
'lm
)
1645 "Start or resume an Lm game.
1646 If a game is in progress, this command allows you to resume it.
1647 Here is the relation between prefix args and game options:
1649 prefix arg | robot is auto-started | weights are saved from last game
1650 ---------------------------------------------------------------------
1656 You start by moving to a square and typing \\[lm-start-robot],
1657 if you did not use a prefix arg to ask for automatic start.
1658 Use \\[describe-mode] for more info."
1661 (setf lm-n nil lm-m nil
)
1662 (lm-switch-to-window)
1664 (lm-emacs-is-computing
1666 ((or (not lm-game-in-progress
)
1667 (<= lm-number-of-moves
2))
1668 (let ((max-width (lm-max-width))
1669 (max-height (lm-max-height)))
1670 (or lm-n
(setq lm-n max-width
))
1671 (or lm-m
(setq lm-m max-height
))
1673 (error "I need at least 1 column"))
1675 (error "I need at least 1 row"))
1677 (error "I cannot display %d columns in that window" lm-n
)))
1678 (if (and (> lm-m max-height
)
1679 (not (eq lm-m lm-saved-board-height
))
1680 ;; Use EQ because SAVED-BOARD-HEIGHT may be nil
1681 (not (y-or-n-p (format "Do you really want %d rows? " lm-m
))))
1682 (setq lm-m max-height
)))
1683 (if lm-one-moment-please
1684 (message "One moment, please..."))
1685 (lm-start-game lm-n lm-m
)
1686 (eval (cons 'lm-init
1688 ((= parg
1) '(t nil
))
1690 ((= parg
3) '(nil t
))
1691 ((= parg
4) '(nil nil
))
1695 ;;;_ + Local variables
1697 ;;; The following `outline-layout' local variable setting:
1698 ;;; - closes all topics from the first topic to just before the third-to-last,
1699 ;;; - shows the children of the third to last (config vars)
1700 ;;; - and the second to last (code section),
1701 ;;; - and closes the last topic (this local-variables section).
1703 ;;;outline-layout: (0 : -1 -1 0)
1708 ;;; arch-tag: ae5031be-96e6-459e-a3df-1df53117d3f2
1709 ;;; landmark.el ends here