diff options
| author | Mario Lang <mlang@delysid.org> | 2014-03-28 11:57:29 +0100 |
|---|---|---|
| committer | Mario Lang <mlang@delysid.org> | 2014-03-28 12:14:19 +0100 |
| commit | 6ece68a4a2f5a413f48bb6816b25a41a9376ee3e (patch) | |
| tree | 2ac7ebe01ee50b451ee2e308e1454d5a539a2b86 /chess-polyglot.el | |
| parent | 866b08a1f0c41c54fe398730ed7d3a0e77f400db (diff) | |
Make the strength of book replies configurable.
Diffstat (limited to 'chess-polyglot.el')
| -rw-r--r-- | chess-polyglot.el | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/chess-polyglot.el b/chess-polyglot.el index a58ae8d..a9a0551 100644 --- a/chess-polyglot.el +++ b/chess-polyglot.el @@ -462,23 +462,27 @@ Use `chess-ply-keyword' on elements of the returned list to retrieve them." (when ply (setq plies (nconc plies (list ply)))))))) -(defun chess-polyglot-book-ply (book position) - "If non-nil, a (randomly picked) ply from plies in BOOK for POSITION. -Random distribution is defined by the relative weights of the found plies." - (let* ((plies (chess-polyglot-book-plies book position)) - (random-value (random (cl-reduce - #'+ (mapcar (lambda (ply) - (chess-ply-keyword - ply :polyglot-book-weight)) - plies)))) - (max 0) ply) - (while plies - (let ((min max)) - (setq max (+ max (chess-ply-keyword (car plies) :polyglot-book-weight))) - (if (and (>= random-value min) (< random-value max)) +(defun chess-polyglot-book-ply (book position &optional strength) + "If non-nil a (randomly picked) ply from plies in BOOK for POSITION. +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. +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)) + (cl-assert (and (>= strength 0) (< strength 4))) + (cl-flet ((ply-weight (ply) + (round (expt (chess-ply-keyword ply :polyglot-book-weight) + strength)))) + (let* ((plies (chess-polyglot-book-plies book position)) + (random-value (random (cl-reduce #'+ (mapcar #'ply-weight plies)))) + (max 0) ply) + (while plies + (if (< random-value (setq max (+ max (ply-weight (car plies))))) (setq ply (car plies) plies nil) - (setq plies (cdr plies))))) - ply)) + (setq plies (cdr plies)))) + ply))) (defalias 'chess-polyglot-book-close 'kill-buffer "Close a polyglot book.") |
