Trouble with IID

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Trouble with IID

Post by outAtime »

I'm having some difficulty because I'm not sure how to check if the hash contains a best move or not.

Code: Select all

long int chk_hash(int alpha, int beta, int depth, int *type, move_s *move) {

	/* see what info we can get from our hash tables for the current
	 position.  This could be a value we can return, a suggested move, or
	 even just a warning not to use null search in this position */

	hash_s *hash_p;
	long int score;
	s_int h_depth, flag;
	d_long hash;

	*type = no_info;
	*move = dummy;

	/* lookup our hash: */
	hash_p = hash_table + (hash_mask & cur_pos.x1);
	hash = hash_p->hash;
	if (hash.x1 == cur_pos.x1 && hash.x2 == cur_pos.x2) {
		/* get info from the hash: */
		*move = hash_p->move;
		score = hash_p->score;
		h_depth = hash_p->depth;
		flag = hash_p->flag;

		/* adjust our score if it is a mate score: */
		if (abs(score) > INF - 100) {
			if (score > 0)
				score -= (ply);
			else
				score += (ply);
		}

		/* see if we should avoid null moves: */
		if &#40;h_depth >= depth - null_red && score < beta && flag == u_bound&#41;
			*type = avoid_null;

		/* check what info we can get from our hash&#58; */
		if &#40;h_depth >= depth&#41; &#123;
			switch &#40;flag&#41; &#123;
			case u_bound&#58;
				if &#40;score <= alpha&#41; &#123;
					*type = u_bound;
					return &#40;alpha&#41;;
				&#125;
				break;
			case l_bound&#58;
				if &#40;score >= beta&#41; &#123;
					*type = l_bound;
					return &#40;beta&#41;;
				&#125;
				break;
			case exact&#58;
				*type = exact;
				return &#40;score&#41;;
				break;
			&#125;
		&#125;
	&#125;

	return &#40;0&#41;;

&#125;
also after the IID search would I return chk_hash(alpha, beta, depth, &h_type, &h_move)? I have the feeling I should only be returning just the move, but again having difficulty with implementation. I can write (if h_type == no_info) but cant seem to figure out something for the _move. Thanks for any help.
outAtime
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Trouble with IID

Post by Daniel Shawul »

I am not sure what your question is but you should check if you have a move from the tt all the time.
Whatever move you have in tt is the best so far, so no need for further checks. The score is checked
against current bounds (alpha & beta) because we don't want to prematurely stop search (hash cutoff)
for a suboptimal move searched with a lower bound. But the move is use for _move ordering_, so no severe penalties there.

Especially in IID where you do shallow depth search first, you need to search the best move first
otherwise the point of doing shallow depth search will be lost. I don't think you need to make any changes
to your hash table for IID to work. It will work even without the tt if you save the best move from the shallow
search elsewhere and search it first.
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Trouble with IID

Post by outAtime »

hmmm... ok well the hash tables are checked in search:

Code: Select all

/* see what info we can get from our hash table&#58; */
	h_score = chk_hash&#40;alpha, beta, depth, &h_type, &h_move&#41;;
	if &#40;h_type != no_info&#41; &#123;
		switch &#40;h_type&#41; &#123;
		case exact&#58;
			return &#40;h_score&#41;;
		case u_bound&#58;
			return &#40;h_score&#41;;
		case l_bound&#58;
			return &#40;h_score&#41;;
		case avoid_null&#58;
			do_null = FALSE;
			break;
		default&#58;
			break;
		&#125;
	&#125;

	temp_hash = cur_pos;
	ep_temp = ep_square;
	i_alpha = alpha;
but you are mentioning saving the best move from the IID search so maybe I should do:

Code: Select all

/* Internal iterative deepening */
    if (    depth >= 5
        &&  h_type == no_info;
        &&  &#40;pv_node || (!in_check&#40;))))
    &#123;
	    int d;
		int best_move = dummy;
		int bestIID = -INF;
		
        d = &#40;pv_node ? depth - 2 &#58; depth / 2&#41;;

        do_null = FALSE;
        bestIID = search&#40;alpha, beta, d, FALSE, TRUE&#41;;
        do_null = TRUE;
		
		best_move = bestIID;
		best_move = moves&#91;i&#93;;
		
        return chk_hash&#40;alpha, beta, depth, &h_type, &h_move&#41;;
        
    &#125;
	 
still not sure about the (h_type == no_info) bit or if this is what you meant... this still dosen't look quite like anything else ive seen, not to say that is bad, but just not sure.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Trouble with IID

Post by outAtime »

This has to be wrong i get compiler errors:

Code: Select all

/* Internal iterative deepening */
    if (    depth >= 5
        &&  h_type == no_info
        &&  &#40;pv_node || (!in_check&#40;))))
    &#123;
	    int d;
		move_s moves&#91;MOVE_BUFF&#93;, best_move = dummy;
		int bestIID = -INF;
		
        d = &#40;pv_node ? depth - 2 &#58; depth / 2&#41;;

        do_null = FALSE;
        bestIID = search&#40;alpha, beta, d, FALSE, TRUE&#41;;
        do_null = TRUE;
		
		best_move = bestIID;
		best_move = moves&#91;i&#93;;
		
        return chk_hash&#40;alpha, beta, depth, &h_type, &h_move&#41;;
        
    &#125;
in best_move = bestIID I get incompatable types in assignment error, so im missing something here..
outAtime
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Trouble with IID

Post by Daniel Shawul »

I think we are miscomunicating :)
First of all there shouldn't be a return in your IID implementation. The point of it is to get a move to search first, so once you do the shallow search you move along.
Second forget the hypothetical example I gave with doing IID without the hash table as that seems to cause more confusion.
All you have to do get the move from the tt, and check if it is good enough . Otherwise do a shallow depth IID.
So the only condition , if any, you should apply on the use of the hash move is check depth with which it was searched. Something like

Code: Select all

if&#40;h_depth >= current_depth - 3&#41;
*move = hash_p->move; 
You have that already without the depth check which is also fine.
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Trouble with IID

Post by outAtime »

ok, so from what i can gather you are suggesting i do something so simple as:

Code: Select all

	/* Internal iterative deepening */
    &#123;
	    int d;
				
        d = &#40;pv_node ? depth - 2 &#58; depth / 2&#41;;
        		
        do_null = FALSE;
        search&#40;alpha, beta, d, FALSE, TRUE&#41;;
        do_null = TRUE;        
    &#125;
seems almost too simple... what am I missing here? nothing? Is this the implementation you suggest? Maybe I still need something like what you mentioned about checking if the hash move is good enough... do I still need that? How would i check it? Thanks for your help.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Trouble with IID

Post by outAtime »

I still think I should be saving the result of this search in move_ordering or something otherwise... hmm, never mind ill wait for the reply. Thanks again.
outAtime
User avatar
hgm
Posts: 27796
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Trouble with IID

Post by hgm »

Inmicro-Max I do bascally this:

Code: Select all

Search&#40;depth&#41;
&#123;
    d = 0;
    if&#40;HashHit&#40;) == OK&#41; &#123;
        d = hash.depth;
        bestScore = hash.score;
    &#125;
    while&#40;d < depth&#41; &#123;
        d = NextDepth&#40;d&#41;;
        bestScore = -INF;
        for&#40;ALL_MOVES&#41; &#123;
            score = -Search&#40;d-1&#41;;
            if&#40;score<bestScore&#41; bestScore = score;
        &#125;
    &#125;
    hash.depth = depth;
    hash.score=bestScore;
    return bestScore;
&#125;
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Trouble with IID

Post by outAtime »

ok, that looks more like the implementations im used to seeing. Im still waiting to find out Shawul's idea of IID with no return... he suggests I dont need to return anything or have any conditions... I guess I still dont know :)

Sloppy does:

Code: Select all

/* Get the best move from the hash table.
   If not successfull, return NULLMOVE.  */
U32
get_hash_move&#40;U64 key&#41;
&#123;
	Hash *hash = &hash_table&#91;key % settings.hash_size&#93;;

	if &#40;hash->key == key&#41;
		return hash->best;
	return NULLMOVE;
&#125;

/* Probe the hash table for a score and the best move.
   If not successfull, return VAL_NONE.  */
int
probe_hash&#40;int depth, int alpha, int beta, U64 key, U32 *best_move, int ply&#41;
&#123;
	Hash *hash;

	ASSERT&#40;2, best_move != NULL&#41;;

	hash = &hash_table&#91;key % settings.hash_size&#93;;
	if &#40;hash->key == key&#41; &#123;
		*best_move = hash->best;
		if (&#40;int&#41;hash->depth >= depth&#41; &#123;
			int val = val_from_hash&#40;hash->val, ply&#41;;

			if &#40;hash->flag == H_EXACT&#41;
				return val;
			if &#40;hash->flag == H_ALPHA&#41; &#123;
				if &#40;val <= alpha&#41;
					return alpha;
				if &#40;val < beta&#41;
					return VAL_AVOID_NULL;
			&#125; else if &#40;hash->flag == H_BETA && val >= beta&#41;
				return beta;
		&#125;
	&#125;

	return VAL_NONE;
&#125;
so there is a separate function there to detect if hash has a best_move...
i was having difficulty trying to translate that idea so thought id post to see what else I could do, or if I was even on the right track... I can call h_type but cant call anything it seems from hash.c having to do with *move etc.. so im just tring to find out another way, or how to do it.
In my search PV has the highest move_ordering and seems alot of progams using IID order hash first so that may be another question. Thanks.
outAtime
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Trouble with IID

Post by Daniel Shawul »


ok, that looks more like the implementations im used to seeing. Im still waiting to find out Shawul's idea of IID with no return... he suggests I dont need to return anything or have any conditions... I guess I still dont know
Please stop the drama stanley.
You should try to comprehend what is posted if you are really here for that.
And don't insist on someone responding to your post. This is a public forum and you get help from anyone.
In the rare case that you really are a confused soul which needed help, I was referring to the bold faced text below. Anybody will tell you that is absurd.

Code: Select all

/* Internal iterative deepening */ 
    if (    depth >= 5 
        &&  h_type == no_info 
        &&  &#40;pv_node || (!in_check&#40;)))) 
    &#123; 
       int d; 
      move_s moves&#91;MOVE_BUFF&#93;, best_move = dummy; 
      int bestIID = -INF; 
       
        d = &#40;pv_node ? depth - 2 &#58; depth / 2&#41;; 

        do_null = FALSE; 
        bestIID = search&#40;alpha, beta, d, FALSE, TRUE&#41;; 
        do_null = TRUE; 
       
      best_move = bestIID; 
      best_move = moves&#91;i&#93;; 
       
        &#91;b&#93; return chk_hash&#40;alpha, beta, depth, &h_type, &h_move&#41;; &#91;/b&#93; 
        
    &#125; 
And no, I am not replying to your post again, so don't bother replying to this post.