help with negamaxed version of korfs rbfms

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

smatovic
Posts: 2576
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

help with negamaxed version of korfs rbfms

Post by smatovic »

Heyho, i could need some help with an negamaxed version of Korfs and Chickerings Recursive-Best-First-MiniMax-Search

MiniMax-Version:

Code: Select all

BFMAX (Node, Alpha, Beta)
    FOR each Child[i] of Node
        M[i] := Evaluation(Child[i] >
        IF M[i] > Beta return M[i]
    SORT Child[i] and M[i] in decreasing order
    IF only one child, M[2] := -infinity
    WHILE Alpha <= M&#91;i&#93; <= Beta
        M&#91;l&#93; &#58;= BFMIN&#40;Child&#91;l&#93;,max&#40;Alpha,M&#91;2&#93;),Beta&#41;
        insert Child&#91;l&#93; and  M&#91;l&#93; in sorted order
    return M&#91;l&#93;

BFMIN &#40;Node, Alpha, Beta&#41;
    FOR each Child&#91;i&#93; of Node
        M&#91;i&#93; &#58;= Evaluation&#40;Child&#91;i&#93;)
        IF M&#91;i&#93; < Alpha return M&#91;i&#93;
    SORT Child&#91;i&#93; and M&#91;i&#93; in increasing order
    IF only one child, M&#91;2&#93; &#58;= infinity
    WHILE Alpha <= MCI&#93; <= Beta
        MC&#91;1&#93; &#58;= BFMAX&#40;Child&#91;l&#93;,Alpha,min&#40;Beta,M&#91;2&#93;))
        insert Child&#91;l&#93; and M&#91;l&#93; in sorted order
    return M&#91;l&#93;
Is this negamaxed version correct??

Code: Select all

BFnega&#40;Node, Alpha,Beta&#41;
    FOR each Child&#91;i&#93; of Node
        M&#91;i&#93; &#58;= Evaluation&#40;Child&#91;i&#93; >
        IF M&#91;i&#93; > Beta return M&#91;i&#93;
    SORT Child&#91;i&#93; and M&#91;i&#93; in decreasing order
    IF only one child, M&#91;2&#93; &#58;= -infinity
    WHILE Alpha <= M&#91;i&#93; <= Beta
        M&#91;l&#93; &#58;= -BFMIN&#40;Child&#91;l&#93;,-Beta, -&#40;max&#40;Alpha,M&#91;2&#93;)))
        insert Child&#91;l&#93; and  M&#91;l&#93; in sorted order
    return M&#91;l&#93;
Thanks,
Srdja
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

Almost. The recursive call must be M[1] := -BFnega(...) instead of -BFMIN(...).

Also in both BFMAX() and BFnega() functions there is a dubious ">" character instead of the expected closing round bracket.

In BFMIN() "WHILE Alpha <= MCI] <= Beta" must be "WHILE Alpha <= M[1] <= Beta", and "MC[1] := ..." must be "M[1] := ...".

Most of these small differences to the original version are probably from scanning.

Sven
smatovic
Posts: 2576
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: help with negamaxed version of korfs rbfms

Post by smatovic »

Thanks Sven,

i am trying to get an iterative version running, maybe you could take a look on this too?
Somehow it hangs in a loop i guess...

Code: Select all


    // iterative version in pseudo code

    depth = 1;
    alpha&#91;0&#93; = -INF;
    beta&#91;0&#93;  =  INF;
    node = root;
    n = node.children; // children is -1 when unexplored


    while&#40;n > 0&#41; &#123;

        // select best child and score
        score = -INF
        for each node.child as child &#123;
            tmp = -child.score
            if &#40;tmp > score&#41; &#123;
                score = tmp;
                best = child;
            &#125;
        &#125;
        node.score = score;

        // check beta
        if ( score > beta&#91;depth-1&#93;) &#123;
            // go back in tree
            node = node.parent;
            depth--;
        &#125;
        else &#123;           
            // get second best score
            score = -INF
            for each node.child as child &#123;

                if &#40;child == best&#41;
                    continue;

                tmp = -child.score
                if &#40;tmp > score&#41; &#123;
                    score = tmp;
                &#125;
            &#125;

            // set alpha
            alpha&#91;depth&#93; = -beta&#91;depth-1&#93;;
            // set beta
            beta&#91;depth&#93;  = &#40;score > alpha&#91;depth-1&#93;)? -score &#58; -alpha&#91;depth-1&#93;;

            depth++;
            node = best;
        &#125;
        n = node.children;
    &#125;
    
--
Srdja
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

Not sure, but I could imagine that you restart the parent node search from scratch after a beta cutoff since you don't remember which children were already processed.

I also do not understand your pre-loop initialization. Your comment "children is -1 when unexplored" would prevent the "while(n>0)" loop from even starting, since -1 is not >0 ?? So it would mean that the root node is not "unexplored" in the beginning.

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

I guess you also need something like childIndex[depth] running within the range [0 .. node.children - 1] for each depth. Within the loop you might further need to distinguish between "processing after depth++" (enter a new subtree) and "processing after depth--" (select next sibling of parent).

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

Sven Schüle wrote:Almost. The recursive call must be M[1] := -BFnega(...) instead of -BFMIN(...).

Also in both BFMAX() and BFnega() functions there is a dubious ">" character instead of the expected closing round bracket.

In BFMIN() "WHILE Alpha <= MCI] <= Beta" must be "WHILE Alpha <= M[1] <= Beta", and "MC[1] := ..." must be "M[1] := ...".

Most of these small differences to the original version are probably from scanning.

Sven
I just found another difference: in both BFMAX() and BFnega(), "WHILE Alpha <= M <= Beta" must be "WHILE Alpha <= M[1] <= Beta".

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

Sven Schüle wrote:Not sure, but I could imagine that you restart the parent node search from scratch after a beta cutoff since you don't remember which children were already processed.
It seems that is the inherent overhead of the algorithm, and it should not cause any hanging in a loop. So maybe there is something else around ...

I currently wonder how you realize the sorting that was present in the recursive version.

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: help with negamaxed version of korfs rbfms

Post by Sven »

Sven Schüle wrote:
Sven Schüle wrote:Not sure, but I could imagine that you restart the parent node search from scratch after a beta cutoff since you don't remember which children were already processed.
It seems that is the inherent overhead of the algorithm, and it should not cause any hanging in a loop. So maybe there is something else around ...

I currently wonder how you realize the sorting that was present in the recursive version.

Sven
Even the recursive version does not always terminate. If your Evaluation() never returns a value outside of the Alpha/Beta window given at the root node then it will loop forever. I quickly implemented your BFnega() function above to check this. It is possible that the iterative version suffers from the same problem.

Btw, the way how scores are checked against Alpha/Beta looks dubious, already in the original document. A score is inside the window if it is > Alpha and < Beta, the authors used some >=/<= that appears to be wrong. But that is more a minor issue compared to the "termination" problem.

The algorithm also does not mention how to stop at a maximum depth.

Sven
smatovic
Posts: 2576
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: help with negamaxed version of korfs rbfms

Post by smatovic »

i think i got it now, i forgot to check for alpha

Code: Select all

                

        // check beta
        if ( score > beta&#91;depth-1&#93; || score < alpha&#91;depth-1&#93; ) &#123;
            // go back in tree
            node = node.parent;
            depth--;
        &#125; 

--
Srdja