From 6511a374d94c6683ee8fe47637ce2e9f24b8508a Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Wed, 16 Apr 2014 15:06:08 +0200 Subject: chess-polyglot.el: Ship default book file. Also, move some customisation settings from chess-uci to chess-polyglot. --- .gitignore | 1 - Makefile.am | 1 + chess-polyglot.bin | Bin 0 -> 100000 bytes chess-polyglot.el | 38 +++++++++++++++++++++++++++++++++++--- chess-uci.el | 36 +++++++++--------------------------- 5 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 chess-polyglot.bin diff --git a/.gitignore b/.gitignore index b7a468e..e80ebbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.aux -*.bin *.cp *.cps *.elc diff --git a/Makefile.am b/Makefile.am index 0ccb316..309c759 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/chess-polyglot.bin b/chess-polyglot.bin new file mode 100644 index 0000000..07a2df6 Binary files /dev/null and b/chess-polyglot.bin differ diff --git a/chess-polyglot.el b/chess-polyglot.el index b23e0ab..af1767b 100644 --- a/chess-polyglot.el +++ b/chess-polyglot.el @@ -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)) @@ -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) @@ -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 diff --git a/chess-uci.el b/chess-uci.el index e421f71..ff2ec51 100644 --- a/chess-uci.el +++ b/chess-uci.el @@ -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.") @@ -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) @@ -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)) -- cgit v1.2.3