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