Skip to content

Commit

Permalink
chess-polyglot.el: Ship default book file.
Browse files Browse the repository at this point in the history
Also, move some customisation settings from chess-uci to chess-polyglot.
  • Loading branch information
mlang committed Apr 16, 2014
1 parent 76fac39 commit 6511a37
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
*.aux
*.bin
*.cp
*.cps
*.elc
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -45,6 +45,7 @@ dist_lisp_LISP = \
chess-phalanx.el \
chess-plain.el \
chess-ply.el \
chess-polyglot.el \
chess-puzzle.el \
chess-random.el \
chess-scid.el \
Expand Down
Binary file added chess-polyglot.bin
Binary file not shown.
38 changes: 35 additions & 3 deletions chess-polyglot.el
Expand Up @@ -45,6 +45,22 @@
(require 'chess-ply)
(require 'chess-pos)

(defgroup chess-polyglot ()
"Polyglot opening book support."
:group 'chess)

(defcustom chess-polyglot-book-strength 1.0
"Influence random distribution when picking a ply from the book.
A value above 1.0 means to prefer known good moves while a value below
1.0 means to penalize known good moves. 0.0 will force uniform
distribution of move weights. For reasons of numerical overflow,
this should be strictly less than 4.0."
:group 'chess-polyglot
:type '(float :match (lambda (widget value) (and (>= value 0) (< value 4)))))

(defvar chess-polyglot-book nil
"The default polyglot book object.")

(defsubst chess-polyglot-read-octets (n)
"Read N octets from the current buffer."
(let ((val 0))
Expand Down Expand Up @@ -483,10 +499,11 @@ Use `chess-ply-keyword' on elements of the returned list to retrieve them."
Random distribution is defined by the relative weights of the found plies.
If non-nil, STRENGTH defines the bias towards better moves.
A value below 1.0 will penalize known good moves while a value
above 1.0 will prefer known good moves. The default is 1.0.
above 1.0 will prefer known good moves. The default is the value
of `chess-polyglot-book-strength'.
A strength value of 0.0 will completely ignore move weights and evenly
distribute the probability that a move gets picked."
(unless strength (setq strength 1.0))
(unless strength (setq strength chess-polyglot-book-strength))
(cl-assert (and (>= strength 0) (< strength 4)))
(cl-flet ((ply-weight (ply)
(round (expt (chess-ply-keyword ply :polyglot-book-weight)
Expand All @@ -495,13 +512,28 @@ distribute the probability that a move gets picked."
(random-value (random (cl-reduce #'+ (mapcar #'ply-weight plies))))
(max 0) ply)
(while plies
(if (< random-value (setq max (+ max (ply-weight (car plies)))))
(if (< random-value (cl-incf max (ply-weight (car plies))))
(setq ply (car plies) plies nil)
(setq plies (cdr plies))))
(cl-assert ply)
ply)))

(defalias 'chess-polyglot-book-close 'kill-buffer
"Close a polyglot book.")

(defun chess-polyglot-book-reload (symbol value)
(set symbol value)
(when (eq symbol 'chess-polyglot-book-file)
(setq chess-polyglot-book (chess-polyglot-book-open value))))

(defcustom chess-polyglot-book-file (expand-file-name "chess-polyglot.bin"
(file-name-directory
(or load-file-name
buffer-file-name)))
"Path to default polyglot book file."
:group 'chess-polyglot
:set 'chess-polyglot-book-reload
:type '(file :must-match t))

(provide 'chess-polyglot)
;;; chess-polyglot.el ends here
36 changes: 9 additions & 27 deletions chess-uci.el
Expand Up @@ -34,24 +34,6 @@
"Customisations for Chess engines based on the UCI protocol"
:group 'chess)

(defcustom chess-uci-polyglot-book-file nil
"The path to a polyglot binary opening book file."
:group 'chess-uci
:type '(choice (const :tag "Not specified" nil) (file :must-match t)))

(defcustom chess-uci-polyglot-book-strength 1.0
"Influence random distribution when picking a ply from the book.
A value above 1.0 means to prefer known good moves while a value below
1.0 means to penalize known good moves. 0.0 will stop to consider
move weights and simply pick a move at random. For simple
reasons of numerical overflow, this should be strictly less than 4.0."
:group 'chess-uci
:type '(float :match (lambda (widget value) (and (>= value 0) (< value 4)))))

(defvar chess-uci-book nil
"A (polyglot) opening book object.
See `chess-uci-polyglot-book-file' for details on how to enable this.")

(defvar chess-uci-long-algebraic-regexp "\\([a-h][1-8]\\)\\([a-h][1-8]\\)\\([nbrq]\\)?"
"A regular expression matching a UCI log algebraic move.")

Expand Down Expand Up @@ -118,10 +100,11 @@ If conversion fails, this function fired an 'illegal event."
(unless chess-engine-handling-event
(cond
((eq event 'initialize)
(when chess-uci-polyglot-book-file
(unless chess-uci-book
(setq chess-uci-book (chess-polyglot-book-open
chess-uci-polyglot-book-file))))
(when (and chess-polyglot-book-file
(file-exists-p chess-polyglot-book-file))
(unless chess-polyglot-book
(setq chess-polyglot-book (chess-polyglot-book-open
chess-polyglot-book-file))))
(apply #'chess-common-handler game event args))

((eq event 'new)
Expand All @@ -140,12 +123,11 @@ If conversion fails, this function fired an 'illegal event."
(chess-game-set-data game 'active nil)))

((eq event 'post-move)
(let ((book-ply (and chess-uci-book (bufferp chess-uci-book)
(buffer-live-p chess-uci-book)
(let ((book-ply (and chess-polyglot-book (bufferp chess-polyglot-book)
(buffer-live-p chess-polyglot-book)
(chess-polyglot-book-ply
chess-uci-book
(chess-game-pos game)
chess-uci-polyglot-book-strength))))
chess-polyglot-book
(chess-game-pos game)))))
(if book-ply
(let ((chess-display-handling-event nil))
(funcall chess-engine-response-handler 'move book-ply))
Expand Down

0 comments on commit 6511a37

Please sign in to comment.