Computer Chess Club Archives


Search

Terms

Messages

Subject: Symbolic: Opening book move selection

Author: Steven Edwards

Date: 09:14:20 03/01/04


Here's Symbolic's opening book move selection source.  I hope I''m not giving
Lisp a bad reputation with all of my confusing code.


(defun WLDTotalFromBookNode (MyBookNode)
    "Calculate the win/lose/draw total from a book node"
    (let*
        (
            (WLDCount (getprop MyBookNode 'BookWLD))
            (WinCount (first WLDCount))
            (LoseCount (second WLDCount))
            (DrawCount (third WLDCount))
            (WLDTotal (+ WinCount LoseCount DrawCount))
        )
        WLDTotal))


(defun BookExpectationFromBookNode (MyBookNode)
    "Calculate the winning rate from a book node"
    (let*
        (
            (WLDCount (getprop MyBookNode 'BookWLD))
            (WinCount (first WLDCount))
            (LoseCount (second WLDCount))
            (TotalWLCount (+ WinCount LoseCount))
            (Expectation (/ (float (- WinCount LoseCount)) (float
TotalWLCount)))
        )
        Expectation))


(defun BookNodeExpectationTriples (MyBookNodes)
    "Return a list of triples of book nodes/expectation/WLDTotals"
    (let ((NodeExpectations nil))
        (dolist (BookNode MyBookNodes)
            (setf NodeExpectations
                (cons
                    (list
                        BookNode
                        (BookExpectationFromBookNode BookNode)
                        (WLDTotalFromBookNode BookNode))
                    NodeExpectations)))
        NodeExpectations))


(defun NodeProbabilityPairs (MyBookNodeExpTriples)
    "Return a list of pairs of node/probabilites from a list of
node/expectation/WLDTotals"
    (let ((SumOfExpCountProd 0.0) (ProbabilityPairs nil))
        (dolist (Triple MyBookNodeExpTriples)
            (setf SumOfExpCountProd
                (+ SumOfExpCountProd (* (second Triple) (third Triple)))))
        (dolist (Triple MyBookNodeExpTriples)
            (setf ProbabilityPairs
                (cons
                    (list
                        (first Triple)
                        (/ (* (second Triple) (third Triple))
SumOfExpCountProd))
                    ProbabilityPairs)))
        ProbabilityPairs))


(defun FilterDecentBookNodeExpectationTriples (MyBookNodeExpTriples)
    "Filter out non-positive expectation book node expecation triples"
    (let ((DecentBookNodeExpTriples nil))
        (dolist (DecentBookNodeExpTriple MyBookNodeExpTriples)
            (if (> (second DecentBookNodeExpTriple) 0.0)
                (setf DecentBookNodeExpTriples
                    (cons DecentBookNodeExpTriple DecentBookNodeExpTriples))))
        DecentBookNodeExpTriples))


(defun CollectBookSubNodes (MyNode)
    "Return a list of book subnodes from a node"
    (ExplodeNode MyNode)
    (let ((BookNodes nil))
        (dolist (SubNode (getprop MyNode 'SubNodes))
            (if (getprop SubNode 'IsBook)
                (setf BookNodes (cons SubNode BookNodes))))
        BookNodes))


(defun DecentBookMove (MyNode)
    "Return a decent book move from a node, if any"
    (let*
        (
            (SelMove nil)
            (BookSubNodes (CollectBookSubNodes MyNode))
            (NodeExpectationTriples (BookNodeExpectationTriples BookSubNodes))
            (DecentTriples (FilterDecentBookNodeExpectationTriples
NodeExpectationTriples))
            (NodeProbPairs (NodeProbabilityPairs DecentTriples))
        )
        (if NodeProbPairs
            (let ((SelectProb (random-unit)) (SumProb 0.0) (CurrPair nil))
                (dowhile (not SelMove)
                    (setf CurrPair (first NodeProbPairs))
                    (setf NodeProbPairs (rest NodeProbPairs))
                    (if (null? NodeProbPairs)
                        (setf SelMove (getprop (first CurrPair) 'PriorMove))
                        (progn
                            (setf SumProb (+ SumProb (second CurrPair)))
                            (if (< SelectProb SumProb)
                                (setf SelMove (getprop (first CurrPair)
'PriorMove))))))))
        SelMove))



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.