Engine sacrificing pieces!

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Anton
Posts: 3549
Joined: Sun Mar 26, 2006 5:53 pm

Engine sacrificing pieces!

Post by Anton »

Forgive my stupidity, I am a programming /chess programming newbie.

I started off by coding a move generator, and in order to test and debug the move generator, I spliced it into FirstChess, I also added piece square tables to the evaluation.

The problem is this, when the engine plays a game, it is as if it is playing "losers" chess, for example as white, it first move is c2-c3 followed by Qb3 then Qxb7??. if playing against itself, both sides display this suicidal behavior.

The search function is identical to the search function found in firstchess, that is where I suspect the problem is, however after hours of scratching my head, I am unable to work out why it is doing this. I am posting this in the hope that there is an obvious reason for this type of behavior.

Anton.
smatovic
Posts: 2645
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Engine sacrificing pieces!

Post by smatovic »

The problem is this, when the engine plays a game, it is as if it is playing "losers" chess, for example as white, it first move is c2-c3 followed by Qb3 then Qxb7??. if playing against itself, both sides display this suicidal behavior.
I dont know firstchess, but it sounds that you forgot to negate the score value somewhere.

--
Srdja
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Engine sacrificing pieces!

Post by Evert »

That, or it's not doing a quiescence search and doesn't see that the pawn is defended.
mike_bike_kite
Posts: 98
Joined: Tue Jul 26, 2011 12:18 am
Location: London

Re: Engine sacrificing pieces!

Post by mike_bike_kite »

Do a one ply search and print the score for each move. If it's picking the worst scoring move then it's the part of your code that compares scores. If it just does it for one side then you're missing some negation for the opposing side. If instead it all looks ok at one ply then does the program extend on captures and is this part going wrong.

It's obviously important to have good debugging that you can switch on and off whenever you need it.
Anton
Posts: 3549
Joined: Sun Mar 26, 2006 5:53 pm

Re: Engine sacrificing pieces!

Post by Anton »

Using the code below, it was playing the worst scoring move for _both_ sides. When I instead return "-score" for white, and "score" for black, the moves the engine plays make a lot more sense, to be honest, I am not sure why it works when I return a negative value for white, I think the next step is to rip out the "Firstchess" search function and replace it with a vanilla alphabeta.

Thanks for the suggestions guys, and yes, a debugging framework is a great idea, the coding / debugging ratio is around 5 / 95 at the moment

Code: Select all

        if (colour[i] == WHITE)
							
                   case KNIGHT:   
                       score += ((value_piece[piece[i]]) + w_knight_pcsq[i]);
       
                if (colour[i] == BLACK)

                   case KNIGHT:   
                      score -= ((value_piece[piece[i]]) + b_knight_pcsq[i]);

              if (side == WHITE)
                   return score;
		       else return -score;
smatovic
Posts: 2645
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Engine sacrificing pieces!

Post by smatovic »

When I instead return "-score" for white, and "score" for black, the moves the engine plays make a lot more sense,
hmm, thats the way negamax-scoring works.

The evaluation returns a value from -INF to + INF, - is good for black and + is good for white. But if you want to compare in a negamax Framework the numbers you have to negate the value for black to work only with positive numbers.

--
Srdja
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Engine sacrificing pieces!

Post by Evert »

To be a little bit clearer: a positive score is good for the side-to-move, a negative score is good for the "other" side.
In practice it seems easiest to do the evaluation from one side's point of view (say white's) and then flip the sign if it happens to be black's turn.
NaltaP312
Posts: 56
Joined: Wed Oct 29, 2008 1:06 pm
Full name: Marc Paule

Re: Engine sacrificing pieces!

Post by NaltaP312 »

firstchess is available here
http://alumnos.elo.utfsm.cl/~dmartin/pr ... rstchess.c
Danasah is based on.
Now i don't what it is better to begin :
8x8
16x16 like fruit
0x88 like faile
User avatar
phhnguyen
Posts: 1434
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: Engine sacrificing pieces!

Post by phhnguyen »

Can you try to search little bit deeper? Say 6 or 7 instead of default 5?
(In main function, change the line:
max_depth = 6;
)

I think the main problem is the search depth is too small.
Anton
Posts: 3549
Joined: Sun Mar 26, 2006 5:53 pm

Re: Engine sacrificing pieces!

Post by Anton »

phhnguyen wrote:Can you try to search little bit deeper? Say 6 or 7 instead of default 5?
(In main function, change the line:
max_depth = 6;
)

I think the main problem is the search depth is too small.
Thanks for your replies, The problem was in the way I implemented the evaluation, the Firstchess code was not at fault. Sorry for any confusion caused.

Since fixing the suicidal behavior, Sweetleaf now plays something resembling chess, and won it's first game against the weakest opponent I could find, Saitek team-mate chess computer playing on it's weakest setting :D

[Event "-"]
[Site "-"]
[Date "2011.09.24"]
[Round "-"]
[White "Sweetleaf_014a"]
[Black "Saitek_Team-Mate_L1"]
[Result "1-0"]

1.e4 c5 2.Bc4 Nf6 3.e5 d5 4.Be2 Nfd7 5.e6 fxe6 6.Bb5 Nc6 7.Ba4 Nde5 8.Qh5+
Ng6 9.Nf3 Bd7 10.Ng5 Nd4 11.Nxh7 Bxa4 12.Qxg6+ Kd7 13.b3 Bc6 14.c3 Nf5 15.
Ng5 Rh6 16.Qf7 Nd6 17.Qg8 e5 18.Nf3 Rh5 19.g4 Rh6 20.Nxe5+ Kc7 21.d4 Rf6 22.Bg5 cxd4
23.cxd4 Rc8 24.Nc3 Ne4 25.Nxc6 Rxc6 26.Bf4+ Kd7 27.Qxd5+ Nd6 28.Bxd6 exd6
29.Qf5+ Ke8 30.Qg6+ Kd7 31.Qf5+ Kc7 32.Nd5+ Kb8 33.Kd1 Be7 34.Rg1 Bf6 35.
Qe4 Qa5 36.g5 Bd8 37.a4 Bb6 38.b4 Qa6 39.b5 Qxa4+ 40.Rxa4 Rc1+ 41.Kd2 Rxg1
42.Qe7 Bc7 43.b6 Bxb6 44.Nxb6 axb6 45.Qxd6+ Rc7 46.Rc4 Rxg5 47.Qxc7+ Ka7
48.Rb4 b5 49.Rb3 b4 50.Rxb4 Rb5 51.Ra4+ Ra5 52.Rxa5# 1-0
Yves Catineau wrote: Now i don't what it is better to begin :
8x8
16x16 like fruit
0x88 like faile
I chose 8x8 to begin because of it's simplicity, and because I am using pre-computed move arrays, I don't have to worry about checking if the pieces are out of bounds, bullet-proof but slow.