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