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