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