Page 2 of 2

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Tue Sep 11, 2018 6:38 pm
by maksimKorzh
Thanks, Marco! Everything is clear now :D Now I need to dive into the work :D

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Tue Sep 11, 2018 7:23 pm
by elcabesa
good work, and good luck

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 10:17 am
by maksimKorzh
I'm stuck with collecting mating scored moves, please help

Code: Select all

static inline int NegaMaxSearch(int alpha, int beta, CHESSBOARD *board, SEARCH *info, int depth)
{
	int bestScore = 0;
	int bestMove = 0;
	int legalMoves = 0;
	
	info->nodes++;

	if(InCheck(board, side))
		depth++;

	if(!depth)
		return QuiescenceSearch(alpha, beta, board, info);
		
	MOVELIST list[1];
	GenerateMoves(board, list);
	
	for(int moveNum = 0; moveNum < list->moveCount; ++moveNum)
	{
		CHESSBOARD boardStored[1];
		boardStored[0] = board[0];
		
		if(!MakeMove(board, list->moves[moveNum].move, all))
			continue;
		
		legalMoves++;
		int score = -NegaMaxSearch(-beta, -alpha, board, info, depth - 1);
		TakeBack(board, boardStored);
		
		if(score >= beta)
			return beta; // fail hard beta-cutoff, mating score 100000 is cut here
			
		if(score > alpha)
		{
			alpha = score;
			
			bestScore = alpha;
			bestMove = list->moves[moveNum].move;
			
			info->bestScore = bestScore;			
			info->bestMove = bestMove;
		}
	}
		
	if(!legalMoves)
	{
		if(InCheck(board, side))
			return -100000; // on checkmate
			
		else
			return 0; // on stalemate
	}
	
	else if(legalMoves)
	{
		info->bestScore = bestScore;			
		info->bestMove = bestMove;
	}
	
	return alpha;
}
if there are legal moves everything works well, but when returning -100000 on mate - on depth 1 it returns the right move, but increasing the depth leads to returning any move but mating one. I have no idea why plese help. Thanks in advance.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 10:38 am
by WinPooh
> static inline int QuiescenceSearch

Since this function recursively calls itself, I doubt it can be inlined.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 11:02 am
by elcabesa
Maybe I'm wrong but in you don't handle mate in quiescence search and also when in check in quiescent search you only test capture move... what if the only option is to escape instead of capturing?
Usually a quiescence search when in check shall test all the moves, not only captures.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 7:23 pm
by hgm
It depends on how you score the position. If you return (at least) the static evaluation even when in check, this is only slightly wrong. In effect you then make the assumption that one of the non-captures in general will be able to solve the problem (i.e. evade the check). This is the common case, as the overwhelming majority of checks aren't checkmates. Just like when your Queen is attacked you assume that you will be able to rescue it if you have the move.

The catch is that for Queens can escape threats much easier than Kings, as they are so much more mobile. Weak pieces don't have that advantage to the King, but threats on those are much rarer, as they are usually protected, and even if not, can be easily protected. Which would deter most attackers. The King is unique in that it is very valuable despite its low mobility, so the stand-pat assumption fails more often for Kings than for any other piece. This is why it deserves special treatment.

But the benefits are not huge, as this special treatment can be quite expensive. You have to detect check in QS, and you have to extend when that happens.

What would be wrong is to return -INF when in check and none of the captures solve it. Then the engine would imagine all kind of mates at the horizon tjat in reality are almost never there.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 7:46 pm
by Sven
elcabesa wrote: Fri Sep 14, 2018 11:02 am Maybe I'm wrong but in you don't handle mate in quiescence search and also when in check in quiescent search you only test capture move... what if the only option is to escape instead of capturing?
Usually a quiescence search when in check shall test all the moves, not only captures.
When using check extension you never run into that problem at all since you never even call quiescence search when being in check. But even without check extension you can write your quiescence search roughly like this:

Code: Select all

int qsearch(int alpha, int beta)
{
    if (isInCheck()) return fullSearch(alpha, beta, 1);
    ...
which is only a small overhead (for a non-world-class engine at least).

My engine Jumbo already detects being in check at the parent node by detecting moves that give check before making them on the board, and calls the appropriate search function depending on that information.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Fri Sep 14, 2018 10:29 pm
by cdani
Sven wrote: Fri Sep 14, 2018 7:46 pm When using check extension you never run into that problem at all since you never even call quiescence search when being in check.
A few months ago Andscacs didn't went to quiesce in check. I changed it and was a slight improvement. I search all the evasions, so is like normal search but simplified.

Re: Minimalist UCI chess engine written by self learner from scratch

Posted: Sat Sep 15, 2018 12:02 pm
by Sven
cdani wrote: Fri Sep 14, 2018 10:29 pm
Sven wrote: Fri Sep 14, 2018 7:46 pm When using check extension you never run into that problem at all since you never even call quiescence search when being in check.
A few months ago Andscacs didn't went to quiesce in check. I changed it and was a slight improvement. I search all the evasions, so is like normal search but simplified.
Recently I implemented an evasion generator for Jumbo as well, very small improvement here too.