Engine seems to wait to make good moves until later.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Mikerada6
Posts: 3
Joined: Sun Jun 19, 2016 1:31 am

Engine seems to wait to make good moves until later.

Post by Mikerada6 »

I am trying to set up my first engine and i am giving it the following position

r1bqkb1r/pppppppp/2n4n/6P1/3PP3/2PBBP2/PP1NN2P/R2Q1RK1 w q - 0 1

Code: Select all

   +---+---+---+---+---+---+---+---+
8  |*r*|   |*b*|*q*|*k*|*b*|   |*r*|
   +---+---+---+---+---+---+---+---+
7  |*p*|*p*|*p*|*p*|*p*|*p*|*p*|*p*|
   +---+---+---+---+---+---+---+---+
6  |   |   |*n*|   |   |   |   |*n*|
   +---+---+---+---+---+---+---+---+
5  |   |   |   |   |   |   |(P)|   |
   +---+---+---+---+---+---+---+---+
4  |   |   |   |(P)|(P)|   |   |   |
   +---+---+---+---+---+---+---+---+
3  |   |   |(P)|(B)|(B)|(P)|   |   |
   +---+---+---+---+---+---+---+---+
2  |(P)|(P)|   |(N)|(N)|   |   |(P)|
   +---+---+---+---+---+---+---+---+
1  |(R)|   |   |(Q)|   |(R)|(K)|   |
   +---+---+---+---+---+---+---+---+
     a   b   c   d   e   f   g   h 

The best move is g5h6. However when I run alphaBeta to any depth farther than 2 the engine seems to want to wait to make that move and tries to make set up moves first. The results of different depths are given here:

Code: Select all

info depth 1 time 21 nodes 39 NPS 1857.14 pv  g5h6  score cp 598
info depth 2 time 64 nodes 223 NPS 3484.38 pv  g5h6  c6d4 score cp 350
info depth 3 time 503 nodes 4903 NPS 9747.51 pv g1g2 a7a6  g5h6  score cp 608
info depth 4 time 1719 nodes 33801 NPS 19663.18 pv g1g2 d7d6  g5h6  g7h6 score cp 338
info depth 5 time 35468 nodes 1356307 NPS 38240.30 pv g1g2 h6f5 e4f5 e7e5 h2h3 score cp 538
info depth 6 time 167660 nodes 6574103 NPS 39210.92 pv g1g2 h6g8 g2h3 a7a6 d1c2 a6a5 score cp 129
info depth 7 time 2537624 nodes 97183274 NPS 38296.96 pv g1g2 a7a6 g2h3 a6a5 h3g3 a5a4 g5h6 score cp 589
Any idea why it would want to wait to make this move?
AndrewGrant
Posts: 1754
Joined: Tue Apr 19, 2016 6:08 am
Location: U.S.A
Full name: Andrew Grant

Re: Engine seems to wait to make good moves until later.

Post by AndrewGrant »

First guess for this would have to be your Trans Table, assuming you have one. I would imagine this is the issue.

How are you getting the best move out of the search? If it's not trans, maybe you are returning the wrong move.

All else fails, start turning off improvements one at a time. If it's open source and you cant find it, feel free to post some code snippets.
#WeAreAllDraude #JusticeForDraude #RememberDraude #LeptirBigUltra
"Those who can't do, clone instead" - Eduard ( A real life friend, not this forum's Eduard )
Mikerada6
Posts: 3
Joined: Sun Jun 19, 2016 1:31 am

Re: Engine seems to wait to make good moves until later.

Post by Mikerada6 »

here is my very simple alphaBeta. Any help would be greatly appreciated I am a self aught programmer so my skills are not fantastic. Thank you all for the help

Code: Select all

int alphaBetaMax(int alpha, int beta, int depthleft, String input,
			int goal, int[] pv) {
		if (goal == depthleft) {
			System.out.println("info depth " + goal);
		}
		nodeCount++;
		// ArrayList<Integer> moves = board.sortMoves&#40;
		// board.generateMovesNeo&#40;true&#41;, 1&#41;;
		ArrayList<Integer> moves = board.generateMovesNeo&#40;true&#41;;
		//check to see if we can reorder the moves
		if &#40;pv.length > 1 + &#40;goal - depthleft&#41;) &#123;
			int temp = moves.indexOf&#40;pv&#91;goal - depthleft&#93;);
			if &#40;temp > 0&#41; &#123;
				moves.remove&#40;temp&#41;;
				moves.add&#40;0, pv&#91;goal - depthleft&#93;);
			&#125;
		&#125;

		if &#40;depthleft == 0 || moves.size&#40;) == 0&#41; &#123;
			//System.out.print&#40;"Move&#58; " + input&#41;;
			//System.out.println&#40;"\t&#58;\t " + board.getValue&#40;));
			return board.getValue&#40;);
		&#125;
		for &#40;int i = 0; i < moves.size&#40;); i++) &#123;

			if &#40;goal == depthleft&#41; &#123;
				System.out.println&#40;"info Currmove "
						+ UCIBestLine&#40;board.translate&#40;moves.get&#40;i&#41;))
						+ " currmovenumber " + &#40;1 + i&#41;);
			&#125;
			board.move&#40;moves.get&#40;i&#41;);
			String temp = input + " " + board.translate&#40;moves.get&#40;i&#41;);
			int score = alphaBetaMin&#40;alpha, beta, depthleft - 1, temp, goal, pv&#41;;
			board.undo&#40;);
			if &#40;score >= beta&#41;
				return beta; // fail hard beta-cutoff
			if &#40;score > alpha&#41; &#123;
				if (!bestLine.toLowerCase&#40;).contains&#40;temp.toLowerCase&#40;))) &#123;
					bestLine = temp;
					changed = true;
				&#125;
				alpha = score; // alpha acts like max in MiniMax
			&#125;
		&#125;
		return alpha;
	&#125;
AndrewGrant
Posts: 1754
Joined: Tue Apr 19, 2016 6:08 am
Location: U.S.A
Full name: Andrew Grant

Re: Engine seems to wait to make good moves until later.

Post by AndrewGrant »

Two things. I question how you store the Pv. Also, you always return alpha... why if the best move is worth less than alpha?
#WeAreAllDraude #JusticeForDraude #RememberDraude #LeptirBigUltra
"Those who can't do, clone instead" - Eduard ( A real life friend, not this forum's Eduard )
kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: Engine seems to wait to make good moves until later.

Post by kbhearn »

troubling part of that pv is it doesn't want to save the knight in most iterations. if black doesn't want to save the knight with Ng8 then there's no reason you have to take it immediately and if for instance you're lacking quiescent search it might put it off to the last ply in order to save the pawn from being recaptured.

your output certainly look like you don't have quiescent search working yet (the oscillating odd-even scores being a typical sign of that) and if you've started adding reductions of any sort first that could be the cause of some of the oddness as changing the length of a line by a ply could change the score dramaticly.
Mikerada6
Posts: 3
Joined: Sun Jun 19, 2016 1:31 am

Re: Engine seems to wait to make good moves until later.

Post by Mikerada6 »

I have an opposite alphaBeatMin that will return a beta

Code: Select all

int alphaBetaMin&#40;int alpha, int beta, int depthleft, String input,
			int goal, int&#91;&#93; pv&#41; &#123;
		nodeCount++;
		if &#40;goal == depthleft&#41; &#123;
			System.out.println&#40;"info depth " + goal&#41;;
		&#125;
		// ArrayList<Integer> moves = board.sortMoves&#40;
		// board.generateMovesNeo&#40;true&#41;, 1&#41;;
		ArrayList<Integer> moves = board.generateMovesNeo&#40;true&#41;;

		if &#40;pv.length > 1 + &#40;goal - depthleft&#41;) &#123;
			int temp = moves.indexOf&#40;pv&#91;goal - depthleft&#93;);
			if &#40;temp > 0&#41; &#123;
				moves.remove&#40;temp&#41;;
				moves.add&#40;0, pv&#91;goal - depthleft&#93;);
			&#125;
		&#125;

		if &#40;depthleft == 0 || moves.size&#40;) == 0&#41; &#123;
			if &#40;changed&#41; &#123;
				// System.out.println&#40;"info nodes " + nodeCount + " pv"
				// + UCIBestLine&#40;bestLine&#41;);
				changed = false;
			&#125;
			//System.out.print&#40;"Move&#58; " + input&#41;;
			//System.out.println&#40;"\t&#58;\t " + board.getValue&#40;));
			return board.getValue&#40;);
		&#125;
		for &#40;int i = 0; i < moves.size&#40;); i++) &#123;
			if &#40;goal == depthleft&#41; &#123;
				System.out.println&#40;"info Currmove "
						+ UCIBestLine&#40;board.translate&#40;moves.get&#40;i&#41;))
						+ " currmovenumber " + &#40;1 + i&#41;);
			&#125;
			board.move&#40;moves.get&#40;i&#41;);
			String temp = input + " " + board.translate&#40;moves.get&#40;i&#41;);
			int score = alphaBetaMax&#40;alpha, beta, depthleft - 1, temp, goal, pv&#41;;
			board.undo&#40;);
			if &#40;score <= alpha&#41;
				return alpha; // fail hard alpha-cutoff
			if &#40;score < beta&#41; &#123;
				if (!bestLine.toLowerCase&#40;).contains&#40;temp.toLowerCase&#40;))) &#123;
					changed = true;
					bestLine = temp;
				&#125;
				beta = score; // beta acts like min in MiniMax
			&#125;
		&#125;
		return beta;
	&#125;
Daniel Anulliero
Posts: 759
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: Engine seems to wait to make good moves until later.

Post by Daniel Anulliero »

Seems you don't have quiescence search yet . It may be the raison of false move played , but I'm not an expert here :wink:
User avatar
hgm
Posts: 27795
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Engine seems to wait to make good moves until later.

Post by hgm »

Minimax does not care whether you gain something early or late; only the scorein the leaves counts. In this position, however, it seems that blackcan easily save the Knight by withdrawing it to f8. But at 3 ply he does not do that, in the PV, but instead plays a useless move a7a6. So it seems there is something wrong with your search.
konsolas
Posts: 182
Joined: Sun Jun 12, 2016 5:44 pm
Location: London
Full name: Vincent

Re: Engine seems to wait to make good moves until later.

Post by konsolas »

Mikerada6 wrote:I have an opposite alphaBeatMin that will return a beta

Code: Select all

int alphaBetaMin&#40;int alpha, int beta, int depthleft, String input,
			int goal, int&#91;&#93; pv&#41; &#123;
		nodeCount++;
		if &#40;goal == depthleft&#41; &#123;
			System.out.println&#40;"info depth " + goal&#41;;
		&#125;
		// ArrayList<Integer> moves = board.sortMoves&#40;
		// board.generateMovesNeo&#40;true&#41;, 1&#41;;
		ArrayList<Integer> moves = board.generateMovesNeo&#40;true&#41;;

		if &#40;pv.length > 1 + &#40;goal - depthleft&#41;) &#123;
			int temp = moves.indexOf&#40;pv&#91;goal - depthleft&#93;);
			if &#40;temp > 0&#41; &#123;
				moves.remove&#40;temp&#41;;
				moves.add&#40;0, pv&#91;goal - depthleft&#93;);
			&#125;
		&#125;

		if &#40;depthleft == 0 || moves.size&#40;) == 0&#41; &#123;
			if &#40;changed&#41; &#123;
				// System.out.println&#40;"info nodes " + nodeCount + " pv"
				// + UCIBestLine&#40;bestLine&#41;);
				changed = false;
			&#125;
			//System.out.print&#40;"Move&#58; " + input&#41;;
			//System.out.println&#40;"\t&#58;\t " + board.getValue&#40;));
			return board.getValue&#40;);
		&#125;
		for &#40;int i = 0; i < moves.size&#40;); i++) &#123;
			if &#40;goal == depthleft&#41; &#123;
				System.out.println&#40;"info Currmove "
						+ UCIBestLine&#40;board.translate&#40;moves.get&#40;i&#41;))
						+ " currmovenumber " + &#40;1 + i&#41;);
			&#125;
			board.move&#40;moves.get&#40;i&#41;);
			String temp = input + " " + board.translate&#40;moves.get&#40;i&#41;);
			int score = alphaBetaMax&#40;alpha, beta, depthleft - 1, temp, goal, pv&#41;;
			board.undo&#40;);
			if &#40;score <= alpha&#41;
				return alpha; // fail hard alpha-cutoff
			if &#40;score < beta&#41; &#123;
				if (!bestLine.toLowerCase&#40;).contains&#40;temp.toLowerCase&#40;))) &#123;
					changed = true;
					bestLine = temp;
				&#125;
				beta = score; // beta acts like min in MiniMax
			&#125;
		&#125;
		return beta;
	&#125;
It's common for chess engines to use the same alpha-beta search function (not max and min), but instead to pass in -beta and -alpha instead of alpha and beta to a new function. This is simpler to understand and maintain (only 1 search function). I expect there is an obscure problem in one of your search functions that experienced people here (not myself) are not familiar with.