Page 1 of 2

Modify hash probing code to pvs

Posted: Mon Dec 05, 2016 1:51 pm
by Necromancer
Hello all,

My first engine Tunguska is playing at 2100 ELO I believe, but before releasing it here, I want to try PVS + aspiration (and some tweaks to the eval, which is very simple by now).

To do this, I'm looking at the CPW code at https://github.com/nescitus/cpw-engine/ ... search.cpp.

Since I don't fully understand some details, I got confused at the hash probing code.
Here's my current code:

Code: Select all

//calling the hashprobe
int score = -INFINITE;
int pvMove = Move::NO_MOVE;

if( HashTable::probeHashEntry(board, &pvMove, &score, alpha, beta, depth)){
	board.hashTable->cut++;
	return score;
}

Code: Select all

//Hash probe implementation
bool HashTable::probeHashEntry(Board& board, int *move, int *score, int alpha, int beta, int depth) {
	int index = (int)(board.zKey & board.hashTable->numEntries_1);

	if(board.hashTable->table[index].zKey == board.zKey) {
		*move = board.hashTable->table[index].move;

		if(board.hashTable->table[index].depth >= depth){
			board.hashTable->hit++;

			*score = board.hashTable->table[index].score;
			if(*score > ISMATE) 
				*score -= board.ply;
            else if&#40;*score < -ISMATE&#41; 
            	*score += board.ply;

            switch&#40;board.hashTable->table&#91;index&#93;.flags&#41; &#123;
                assert&#40;*score >= -Search&#58;&#58;INFINITE && *score <= Search&#58;&#58;INFINITE&#41;;

                case HFALPHA&#58; 
	            	if&#40;*score <= alpha&#41; &#123;
	                	*score = alpha;
	                	return true;
	                &#125;
	                break;
                case HFBETA&#58; 
                	if&#40;*score >= beta&#41; &#123;
                    	*score = beta;
                    	return true;
                    &#125;
                    break;
                case HFEXACT&#58;
                    return true;
                    break;
                default&#58; assert&#40;false&#41;; 
                break;
            &#125;
		&#125;
	&#125;
	return false;
&#125;
And here's CPW code:

Code: Select all

// in pv nodes we return only in case of an exact hash hit
if (&#40;val = tt_probe&#40;depth, alpha, beta, &tt_move_index&#41;) != INVALID&#41; &#123;
	if (!is_pv || &#40;val > alpha && val < beta&#41;) &#123;
		//mate val correction ommited
		return val;
	&#125;
&#125;
How do I modify my code? I don't get that !is_pv part.

Note: my search and hashtable code are taken fron the 'Vice' engine. I'm still absorving how all this works!

Thank you!

Re: Modify hash probing code to pvs

Posted: Mon Dec 05, 2016 6:27 pm
by jdart
I believe the idea is that in a typical engine non-PV nodes are searched with different extensions and reductions, and so the score from a non-PV node is less trustworthy than a score from a PV node. For this reason a hash hit from a non-PV node is not allowed to cut off a PV node. Since most nodes are not PV nodes, this restriction does not cost much.

--Jon

Re: Modify hash probing code to pvs

Posted: Mon Dec 05, 2016 7:03 pm
by Necromancer
Hum, it make more sense now. So the condition below is enough?

Code: Select all

if&#40;!HashTable&#58;&#58;probeHashEntry&#40;board, &pvMove, &score, alpha, beta, depth&#41;)&#123;
	if (!is_pv&#41;&#123; //
		board.hashTable->cut++;
		return score;
	&#125;
&#125;
Or do I need that extra bit?

Code: Select all

 //...
 if (!is_pv || &#40;score > alpha && score < beta&#41;)&#123;
	board.hashTable->cut++;
		return score;
 &#125;
Thank you.

Re: Modify hash probing code to pvs

Posted: Mon Dec 05, 2016 9:45 pm
by hgm
jdart wrote:I believe the idea is that in a typical engine non-PV nodes are searched with different extensions and reductions, and so the score from a non-PV node is less trustworthy than a score from a PV node. For this reason a hash hit from a non-PV node is not allowed to cut off a PV node. Since most nodes are not PV nodes, this restriction does not cost much.
That is pretty bad design. The draft of an entry is supposed to determine if the data in it is of sufficient quality to satisfy the current search request. If search results from PV nodes and non-PV nodes are not of the same quality, they should never be stored with the same draft.

Re: Modify hash probing code to pvs

Posted: Mon Dec 05, 2016 10:55 pm
by syzygy
hgm wrote:
jdart wrote:I believe the idea is that in a typical engine non-PV nodes are searched with different extensions and reductions, and so the score from a non-PV node is less trustworthy than a score from a PV node. For this reason a hash hit from a non-PV node is not allowed to cut off a PV node. Since most nodes are not PV nodes, this restriction does not cost much.
That is pretty bad design. The draft of an entry is supposed to determine if the data in it is of sufficient quality to satisfy the current search request. If search results from PV nodes and non-PV nodes are not of the same quality, they should never be stored with the same draft.
Or you just don't take TT cutoffs in PV nodes.

Re: Modify hash probing code to pvs

Posted: Tue Dec 06, 2016 1:12 am
by jdart
The second condition allows cutoff in a PV node if the score is in bounds. That does not make sense to me (given my earlier comments about the node types).

As with everything, testing and verifying that a change actually makes a positive difference is always a good idea.

--Jon

Re: Modify hash probing code to pvs

Posted: Tue Dec 06, 2016 1:12 am
by jdart
If that is a bad design, then Stockfish has a bad design.

--Jon

Re: Modify hash probing code to pvs

Posted: Tue Dec 06, 2016 8:53 am
by hgm
Not my fault.

Re: Modify hash probing code to pvs

Posted: Tue Dec 06, 2016 8:54 am
by hgm
syzygy wrote:Or you just don't take TT cutoffs in PV nodes.
Or you just don't use a TT at all...

Re: Modify hash probing code to pvs

Posted: Tue Dec 06, 2016 8:58 am
by syzygy
hgm wrote:
syzygy wrote:Or you just don't take TT cutoffs in PV nodes.
Or you just don't use a TT at all...
There is a difference in terms of Elo.