Minimalist UCI chess engine written by self learner from scratch

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
maksimKorzh
Posts: 52
Joined: Sat Sep 08, 2018 3:37 pm
Location: Ukraine
Full name: Maksim Korzh
Contact:

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

Post by maksimKorzh » Tue Sep 11, 2018 4:38 pm

Thanks, Marco! Everything is clear now :D Now I need to dive into the work :D


maksimKorzh
Posts: 52
Joined: Sat Sep 08, 2018 3:37 pm
Location: Ukraine
Full name: Maksim Korzh
Contact:

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

Post by maksimKorzh » Fri Sep 14, 2018 8:17 am

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.

User avatar
WinPooh
Posts: 230
Joined: Fri Mar 17, 2006 7:01 am
Contact:

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

Post by WinPooh » Fri Sep 14, 2018 8:38 am

> static inline int QuiescenceSearch

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

elcabesa
Posts: 732
Joined: Sun May 23, 2010 11:32 am
Contact:

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

Post by elcabesa » Fri Sep 14, 2018 9: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.

User avatar
hgm
Posts: 22582
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

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

Post by hgm » Fri Sep 14, 2018 5:23 pm

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.

Sven
Posts: 3697
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

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

Post by Sven » Fri Sep 14, 2018 5:46 pm

elcabesa wrote:
Fri Sep 14, 2018 9: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.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

User avatar
cdani
Posts: 2065
Joined: Sat Jan 18, 2014 9:24 am
Location: Andorra
Contact:

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

Post by cdani » Fri Sep 14, 2018 8:29 pm

Sven wrote:
Fri Sep 14, 2018 5: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.

Sven
Posts: 3697
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

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

Post by Sven » Sat Sep 15, 2018 10:02 am

cdani wrote:
Fri Sep 14, 2018 8:29 pm
Sven wrote:
Fri Sep 14, 2018 5: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.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

Post Reply