TT with Null Move Cuts A PV Node/Line

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: TT with Null Move Cuts A PV Node/Line

Post by Evert »

bob wrote: When you recurse with those values, you can never do the "if (value > alpha && value < beta) because beta = alpha+1 99+% of the time, and therefore if value > alpha, then it is at least == beta and for fail-soft it could be even higher.

But along the PV, where beta != alpha+1, you search the first move at such a node with the normal alpha/beta window, then you search the remainder of the moves at that ply with alpha, alpha+1. On occasion, here, you CAN get the value > alpha & value < beta, because the window you just failed high on was alpha, alpha+1, and you want to re-search with alpha,beta to see if it still fails high or if you get a real score back.
I'm not entirely sure I understand the < beta in that test.

Without it, any move that improves alpha (against expectations) with a null-window is subjected to a research - in other words, we don't necessarily trust the result. Seems fair.

With that addition, we're saying that we want an exact score if possible - but if the returned score suggests that the move will cause a cut-off, we don't bother and simply trust the score as given. In that sense, the <beta should be a simple speed optimisation that prevents an unnecessary research that is going to fail high. Is that the idea?

If my understanding is correct, in a fail-hard environment the returned score can never be larger than alpha+1, so the condition alpha < score < beta is equivalent to score>alpha && beta !=alpha+1, right?
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: TT with Null Move Cuts A PV Node/Line

Post by Sven »

Evert wrote:
bob wrote: When you recurse with those values, you can never do the "if (value > alpha && value < beta) because beta = alpha+1 99+% of the time, and therefore if value > alpha, then it is at least == beta and for fail-soft it could be even higher.

But along the PV, where beta != alpha+1, you search the first move at such a node with the normal alpha/beta window, then you search the remainder of the moves at that ply with alpha, alpha+1. On occasion, here, you CAN get the value > alpha & value < beta, because the window you just failed high on was alpha, alpha+1, and you want to re-search with alpha,beta to see if it still fails high or if you get a real score back.
I'm not entirely sure I understand the < beta in that test.

Without it, any move that improves alpha (against expectations) with a null-window is subjected to a research - in other words, we don't necessarily trust the result. Seems fair.

With that addition, we're saying that we want an exact score if possible - but if the returned score suggests that the move will cause a cut-off, we don't bother and simply trust the score as given. In that sense, the <beta should be a simple speed optimisation that prevents an unnecessary research that is going to fail high. Is that the idea?

If my understanding is correct, in a fail-hard environment the returned score can never be larger than alpha+1, so the condition alpha < score < beta is equivalent to score>alpha && beta !=alpha+1, right?
Yes, but this is not sufficient to say whether the "&& score < beta" part of the PVS re-search condition is
1) correct and required for the algorithm to work at all,
2) correct and required for efficiency,
3) correct but redundant, or
4) incorrect.

Now I could write a lot of text again but instead I prefer to show some code (see below). After everyone has read it carefully, I would really like to see a well-written answer to my "1, 2, 3, or 4?" question above ... (My preferred answer is "2" but currently I refrain from another textual explanation of it.)

Please note, the context of my code example below is fail-hard. After clarifying the topic for fail-hard we can move on to fail-soft, or to a generalized answer. My feeling is that the answer for fail-soft is "2" as well but I did not dig into it yet.

Depending on the level of agreement on this issue it might be necessary to change the code example in the CPW article about PVS, or not ...

So here are my two functions "A" (without score < beta) and "B" (with score < beta added). So which statement 1, 2, 3, 4 of the list above applies to "B"?

Code: Select all

int PVS_FailHard_A&#40;int alpha, int beta, ...) &#123;
    ...
    ASSERT&#40;alpha < beta&#41;;
    bool isZeroWindow = &#40;beta == alpha+1&#41;; // not part of algorithm
    bool isFirstMove = true;
    for &#40;all moves&#41; &#123;
        int score;
        make&#40;);
        if &#40;isFirstMove&#41; &#123;
            isFirstMove = false;
            score = -PVS_FailHard_A&#40;-beta, -alpha, ...);
        &#125; else &#123;
            score = -PVS_FailHard_A&#40;-&#40;alpha+1&#41;, -alpha, ...);
            if &#40;score > alpha&#41; &#123;
                ASSERT&#40;score == alpha+1&#41;;
                ASSERT&#40;!isZeroWindow || score == beta&#41;;
                //
                // !!!!!! Note 1&#58; If "!isZeroWindow" then score < beta. This will
                // !!!!!! implicitly avoid a useless re-search which would occur in
                // !!!!!! case of score >= beta &#40;the latter would fail high the same
                // !!!!!! way as the original search did&#41;.
                //
                // !!!!!! Note 2&#58; If "isZeroWindow" then score == beta so here we
                // !!!!!! perform a useless re-search with the same bounds as before
                // !!!!!! since beta == alpha+1.
                //
                score = -PVS_FailHard_A&#40;-beta, -alpha, ...);
            &#125;
        &#125;
        unmake&#40;);
        if &#40;score >= beta&#41; &#123;
            return beta;
        &#125;
        if &#40;score > alpha&#41; &#123;
            alpha = score;
        &#125;
    &#125;
    return alpha;
&#125;



int PVS_FailHard_B&#40;int alpha, int beta, ...) &#123;
    ...
    ASSERT&#40;alpha < beta&#41;;
    bool isZeroWindow = &#40;beta == alpha+1&#41;; // not part of algorithm
    bool isFirstMove = true;
    for &#40;all moves&#41; &#123;
        int score;
        make&#40;);
        if &#40;isFirstMove&#41; &#123;
            isFirstMove = false;
            score = -PVS_FailHard_B&#40;-beta, -alpha, ...);
        &#125; else &#123;
            score = -PVS_FailHard_B&#40;-&#40;alpha+1&#41;, -alpha, ...);
            if &#40;score > alpha && score < beta&#41; &#123;
                ASSERT&#40;score == alpha+1&#41;;
                ASSERT&#40;!isZeroWindow&#41;;
                //
                // !!!!!! Note 1&#58; Here we explicitly avoid a useless re-search,
                // !!!!!! see note 1 in PVS_FailHard_A&#40;).
                //
                // !!!!!! Note 2&#58; A re-search will only be performed with bounds
                // !!!!!! different from the previous ones.
                //
                score = -PVS_FailHard_B&#40;-beta, -alpha, ...);
            &#125;
        &#125;
        unmake&#40;);
        if &#40;score >= beta&#41; &#123;
            return beta;
        &#125;
        if &#40;score > alpha&#41; &#123;
            alpha = score;
        &#125;
    &#125;
    return alpha;
&#125;
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: TT with Null Move Cuts A PV Node/Line

Post by xmas79 »

My understanding is that the correct answer is "2". Your notes catch every case and explain well the behaviour of the search. It seems obvious to me that we are interested in a research only if the potential score is between alpha and beta. Two cases arise:
1) if the zw search fails low ---> the move is not good. No need to research.
2) if the zw search fails high ---> the move is very good. Research or not research?
2a) if we are not on a PV node then alpha = beta + 1, so the score is not between alpha and beta. It can be either alpha or beta: alpha -> no good -> no research.... Beta -> good, but research will fail the same way as the research bounds are the same.
2b) if we are on a PV node then alpha != beta + 1, so the score can be between alpha and beta (score <= beta). So the research is must to understand if we are still failing high or we have an exact score.

Since this code doesn't know if we are at a PV node (alpha != beta + 1) or not, the "score < beta" is required to exclude the research in the 2a) case.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: TT with Null Move Cuts A PV Node/Line

Post by bob »

Evert wrote:
bob wrote: When you recurse with those values, you can never do the "if (value > alpha && value < beta) because beta = alpha+1 99+% of the time, and therefore if value > alpha, then it is at least == beta and for fail-soft it could be even higher.

But along the PV, where beta != alpha+1, you search the first move at such a node with the normal alpha/beta window, then you search the remainder of the moves at that ply with alpha, alpha+1. On occasion, here, you CAN get the value > alpha & value < beta, because the window you just failed high on was alpha, alpha+1, and you want to re-search with alpha,beta to see if it still fails high or if you get a real score back.
I'm not entirely sure I understand the < beta in that test.

Without it, any move that improves alpha (against expectations) with a null-window is subjected to a research - in other words, we don't necessarily trust the result. Seems fair.

With that addition, we're saying that we want an exact score if possible - but if the returned score suggests that the move will cause a cut-off, we don't bother and simply trust the score as given. In that sense, the <beta should be a simple speed optimisation that prevents an unnecessary research that is going to fail high. Is that the idea?

If my understanding is correct, in a fail-hard environment the returned score can never be larger than alpha+1, so the condition alpha < score < beta is equivalent to score>alpha && beta !=alpha+1, right?
No, because if ANY move exceeds alpha, it normally is >= beta since most searches have a null-window passed in. Only on PV-type nodes do you have a non-null-window, and only on those nodes can value be > alpha AND < beta, because on most nodes if it is > alpha it is = to beta thanks to the null-window.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: TT with Null Move Cuts A PV Node/Line

Post by bob »

xmas79 wrote:My understanding is that the correct answer is "2". Your notes catch every case and explain well the behaviour of the search. It seems obvious to me that we are interested in a research only if the potential score is between alpha and beta. Two cases arise:
1) if the zw search fails low ---> the move is not good. No need to research.
2) if the zw search fails high ---> the move is very good. Research or not research?
2a) if we are not on a PV node then alpha = beta + 1, so the score is not between alpha and beta. It can be either alpha or beta: alpha -> no good -> no research.... Beta -> good, but research will fail the same way as the research bounds are the same.
2b) if we are on a PV node then alpha != beta + 1, so the score can be between alpha and beta (score <= beta). So the research is must to understand if we are still failing high or we have an exact score.

Since this code doesn't know if we are at a PV node (alpha != beta + 1) or not, the "score < beta" is required to exclude the research in the 2a) case.
Yes. As I said, this takes a bit of thought to get your head around it. Did me too when I first saw this back in '78. But once the light comes on, it is intuitively obvious to the casual observer. :)
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: TT with Null Move Cuts A PV Node/Line

Post by Evert »

bob wrote: No, because if ANY move exceeds alpha, it normally is >= beta since most searches have a null-window passed in. Only on PV-type nodes do you have a non-null-window, and only on those nodes can value be > alpha AND < beta, because on most nodes if it is > alpha it is = to beta thanks to the null-window.
I'm not sure how that's different from what I said?
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: TT with Null Move Cuts A PV Node/Line

Post by Evert »

Sven Schüle wrote: Yes, but this is not sufficient to say whether the "&& score < beta" part of the PVS re-search condition is
1) correct and required for the algorithm to work at all,
2) correct and required for efficiency,
3) correct but redundant, or
4) incorrect.

Now I could write a lot of text again but instead I prefer to show some code (see below). After everyone has read it carefully, I would really like to see a well-written answer to my "1, 2, 3, or 4?" question above ... (My preferred answer is "2" but currently I refrain from another textual explanation of it.)

Please note, the context of my code example below is fail-hard. After clarifying the topic for fail-hard we can move on to fail-soft, or to a generalized answer. My feeling is that the answer for fail-soft is "2" as well but I did not dig into it yet.
Well, for fail-soft it's of course entirely possible to return a score > beta even from a null-window search with (alpha, alpha+1). That's assuming we're in a PV node where beta > alpha+1, otherwise there's nothing to do.
The question is whether you'd trust such an out-of-bounds score in a fail-soft framework. However, if you don't there's probably little point in doing fail-soft at all and if you do it's not so obvious why you couldn't just trust the returned score.

Anyway, at a PV node knowing that a move improves alpha is not enough to cause a cut-off even if we ignore needing an exact score for the node. If you're in a fail-hard framework, you need the verification search for that because you have no other information than "improves alpha". In fail soft you possibly do.

I'm starting to think that in a fail-hard framework, the answer is 1, whereas in fail-soft it may be 3. However, PV nodes are rare and it's possible that getting them wrong now still means we get them right on the next iteration, so it may not actually matter much either way...
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: TT with Null Move Cuts A PV Node/Line

Post by xmas79 »

Anyway, at a PV node knowing that a move improves alpha is not enough to cause a cut-off even if we ignore needing an exact score for the node. If you're in a fail-hard framework, you need the verification search for that because you have no other information than "improves alpha". In fail soft you possibly do.
Why not? Improving alpha above beta means you are outside the window, and indeed all you need is knowing you improved alpha within the bounds or above beta... You will ignore the needing of an exact score only in non PV nodes.
I'm starting to think that in a fail-hard framework, the answer is 1, whereas in fail-soft it may be 3. However, PV nodes are rare and it's possible that getting them wrong now still means we get them right on the next iteration, so it may not actually matter much either way...
In FH without the "&& score < beta" all will happen to you is a research on the same node that will fail in the same way as you will search it with the same bounds. The algorithm will always work, it will only be inefficient as you will search twice the same node with the same bound, giving you nothing.
In FS the thing is the same. You need to avoid research with the same bounds.

Also notice that generally speaking the "&& score < beta" will guard you against a useless research in non PV nodes. PV nodes are only affected when have a very good move which give you a score above beta. Then without the code you will trigger a research and still fail high, with you won't research and still fail high. IMHO.
xmas79
Posts: 286
Joined: Mon Jun 03, 2013 7:05 pm
Location: Italy

Re: TT with Null Move Cuts A PV Node/Line

Post by xmas79 »

Yes. As I said, this takes a bit of thought to get your head around it. Did me too when I first saw this back in '78. But once the light comes on, it is intuitively obvious to the casual observer. :)
That demonstrates that I'm doing my homeworks... I'm not touchy, but yes, I'm only aiming at saying in the not so near future of the year 2054: "Yes. As I said, this takes a bit of thought to get your head around it. Did me too when I first saw this back in '14. But once the light comes on, it is intuitively obvious to the casual observer. :)" :D
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: TT with Null Move Cuts A PV Node/Line

Post by Evert »

xmas79 wrote:
Anyway, at a PV node knowing that a move improves alpha is not enough to cause a cut-off even if we ignore needing an exact score for the node. If you're in a fail-hard framework, you need the verification search for that because you have no other information than "improves alpha". In fail soft you possibly do.
Why not? Improving alpha above beta means you are outside the window,
Note that I said "improve alpha", not "improve alpha above beta". If you do that you can take the cut-off immediately (if you trust the score). But in a fail hard environment the score of a node searched with a null-window is either alpha, or alpha+1. At a PV node > alpha does NOT imply >= beta since beta>alpha+1.
and indeed all you need is knowing you improved alpha within the bounds or above beta…
But that's the point: you don't know if you're above beta. You probably also want to know by how much you've improved alpha, since you need an exact score eventually.
In FH without the "&& score < beta" all will happen to you is a research on the same node that will fail in the same way as you will search it with the same bounds. The algorithm will always work, it will only be inefficient as you will search twice the same node with the same bound, giving you nothing.
In FS the thing is the same. You need to avoid research with the same bounds.
Note that I'm talking in the context of PV nodes here, so beta>alpha+1. Clearly there is never any point in calling the search again with the same bounds (but it shouldn't hurt much either since the position is in the TT).
Also notice that generally speaking the "&& score < beta" will guard you against a useless research in non PV nodes.
That's what I said.