Modify hash probing code to pvs

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Necromancer
Posts: 33
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Modify hash probing code to pvs

Post 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!
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Modify hash probing code to pvs

Post 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
Necromancer
Posts: 33
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Re: Modify hash probing code to pvs

Post 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.
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Modify hash probing code to pvs

Post 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.
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: Modify hash probing code to pvs

Post 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.
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Modify hash probing code to pvs

Post 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
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Modify hash probing code to pvs

Post by jdart »

If that is a bad design, then Stockfish has a bad design.

--Jon
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Modify hash probing code to pvs

Post by hgm »

Not my fault.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Modify hash probing code to pvs

Post 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...
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: Modify hash probing code to pvs

Post 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.