about king attack

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Daniel Anulliero
Posts: 759
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

about king attack

Post by Daniel Anulliero »

I want to implement some king attack eval in my chess program Isa
Computing each pieces can attack a square adjacent to the ennemy king .Value weighted by val piece and number of attackers (very classical approach ! :lol: )
But my problem is the tapered eval :
I have an op and eg score (just like a lot of engine , very classical too !) , and my problem is :
If I have the attack value for op and eg stages , Isa attack too early in the games and lose...
If I compute the attack value only for eg , it play too passively and lose...
-I tried same weights for op and eg , did'nt work
-I tried small weight for op and big weight fro eg , did'nt work...
-I tried big weight for eg only , did'nt work...

Any lights ?
PK
Posts: 893
Joined: Mon Jan 15, 2007 11:23 am
Location: Warsza

Re: about king attack

Post by PK »

In Rodent I add king attack value to total score, but switch it off if the attacking side has no queen. Please note that if Your function is sensitive to the number of attackers (which You say it is), then it scales automatically, because piece exchanges tend to decrease firepower. This also explains why adding attack value to endgame score only is counterproductive - attacker needs some wood on the board, so we don't want to award going to the endgame while attacking (conversion of advantage is another matter, but it depends on accurate endgame evaluation).

There are more question to answer. Instead of using just squares adjacent to enemy king, You may want to include also three squares facing enemy position. You may even want to cheat a bit, and ask Your program to evaluate attacks against black king on g8 even though it is on h8.

If You have problems with getting this right, then start by implementing something simple (Fruit or Bobcat formulas come to mind, as well as a sample specification from chessprogramming wiki) and then try to beat it.
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: about king attack

Post by stegemma »

In Satana, I count the squares attacked by any piece near to the opponent king and I multiply that number by a value (very low) then I sum the result to the position score. This let the pieces move toward the opponent king and seems to works good in any game stage.
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
User avatar
hgm
Posts: 27796
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: about king attack

Post by hgm »

In 'Simple' I am implementing a slightly more general method. In the innermost loop of the mailbox move generator I added a statement

hits += nearKing[to - xKing];

where nearKing[] is an offsetted array (so that it can handle negative indexes) that contains contains 1 for squares next to the origin, and 0 elsewhere. 'hits' is set to 0 whenever you start generating moves for a new piece, so that once you are done with this piece, it contains the total number of moves of that piece that intersect the opponent's King neighborhood.

At that point (which is already infrequently executed) 'hits' is multiplied with a weight for that piece, (currently I use 4 for Q, 1 for minors, but the weight[] table could be initialized any way you want), and added to a variable 'seige', so that (in its lower bits) it will record the weighted attacks count. (I also keep track there of the normal mobility contribution of that piece by how much the length of the move list has grown.) In addition, when hits is non-zero, I add a piece-specific ID bit (obtained from another table id[piece]) to the high-order part of seige, to count the number of attacks of pieces of that type.

seige += weight[piece]*hits + (id[piece] & -hits);

(-hits acts like a mask that kills the ID bit if the piece in question does not attack; -0 = 0, while any negative number would have all the high bits set to 1.) The ID's are chosen so that the various piece types don't bite each other, e.g. minors get 0x100, Rooks 0x800, Queens 0x2000, so that there can be up to 7 minors and 3 Rooks before you get any confusion. In the evaluation the upper part of seige is then later used to index a lookup table to scale the safety according to the the exact combination of material that is involved in the attack:

(seige & 0xFF) * kingSafety[seige >> 8 & 255]

where kingSafety[] is a 256-byte table that can again be initialized in any way you want.

The whole thing can again be scaled by game phase, but I think it should drop off much faster than the usual eg / op linear interpolation. Once the opponent is down to 3 minors or 2 Rooks this kind of safety becomes almost completely unimportant, and when he just lacks a Queen it is already of minor importance (as Pawel pointed out).
User avatar
hgm
Posts: 27796
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: about king attack

Post by hgm »

I can add that the move generator also keeps track of 'pseudo-captures', i.e. moves to squares occupied by a piece of your own: this means the move protects the piece, and the number of protectors for that square is then incremented before rejecting the move and going on with the next move direction. But I am thinking of ectually extending this pseudo-capture handling to also generate X-ray attacks: if the the pseudo-capture of a slider hits on a piece that can slide in the same direction, it could make a special continuation of the ray scan to also count the intersection of the X-ray attack with the King neighborhood in the 'seige' variable. And account for X-ray protectors when you again hit an own piece. (It wouldn't generate any real moves, so it would not contribute to mobility.)
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: about king attack

Post by jdart »

Another problem is, if you evaluate king safety in the endgame, you may discourage activating the king, which can be very important. In the endgame the king should advance toward the center or farther if possible.

So you have to scale down king safety at least in the late endgame.

--Jon
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

In the endgame

Post by sje »

jdart wrote:Another problem is, if you evaluate king safety in the endgame, you may discourage activating the king, which can be very important. In the endgame the king should advance toward the center or farther if possible.
I would add that if any pawns are left on the board in an endgame, then a king should be rewarded for staying close to the centroid of all pawns. The centroid is easily calculated and can be stored as part of a pawn structure evaluation entry in the pawn structure transposition table. This was noted in the Chess 4.x report some four decades ago and it helps greatly with searching.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: about king attack

Post by Ferdy »

Daniel Anulliero wrote:I want to implement some king attack eval in my chess program Isa
Computing each pieces can attack a square adjacent to the ennemy king .Value weighted by val piece and number of attackers (very classical approach ! :lol: )
This does not always work, see the ff. diagram to illustrate the point
[d]6k1/3p4/2nN2p1/6N1/8/3b4/1B4P1/6K1 w - - 0 1
White's king attack looks good but it is not. Number of attackers may be high, but if it attacks same square (Ngf7 and Ndf7) and that square when occupied does not check the king (f7 sq), then it just mislead the engine evaluation.

Another example is this.
[d]5bk1/R7/6p1/6N1/8/4r3/1B4P1/6K1 w - - 0 1
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: about king attack

Post by stegemma »

Ferdy wrote:
Daniel Anulliero wrote:I want to implement some king attack eval in my chess program Isa
Computing each pieces can attack a square adjacent to the ennemy king .Value weighted by val piece and number of attackers (very classical approach ! :lol: )
This does not always work, see the ff. diagram to illustrate the point
[d]6k1/3p4/2nN2p1/6N1/8/3b4/1B4P1/6K1 w - - 0 1
White's king attack looks good but it is not. Number of attackers may be high, but if it attacks same square (Ngf7 and Ndf7) and that square when occupied does not check the king (f7 sq), then it just mislead the engine evaluation.

Another example is this.
[d]5bk1/R7/6p1/6N1/8/4r3/1B4P1/6K1 w - - 0 1
I think that is good if a piece attacks a square near to the king even if from that square it cannot directly give check. Controlling that square maybe another piece can mate and with just a few moves even the attacking piece can do it. "Attacks" for me means to move toward the enemy king, not necessarily to give check.

In the first diagram, for sample, white can play Ne8...
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: about king attack

Post by Kempelen »

Ferdy wrote:
Daniel Anulliero wrote:I want to implement some king attack eval in my chess program Isa
Computing each pieces can attack a square adjacent to the ennemy king .Value weighted by val piece and number of attackers (very classical approach ! :lol: )
This does not always work, see the ff. diagram to illustrate the point
[d]6k1/3p4/2nN2p1/6N1/8/3b4/1B4P1/6K1 w - - 0 1
White's king attack looks good but it is not. Number of attackers may be high, but if it attacks same square (Ngf7 and Ndf7) and that square when occupied does not check the king (f7 sq), then it just mislead the engine evaluation.

Another example is this.
[d]5bk1/R7/6p1/6N1/8/4r3/1B4P1/6K1 w - - 0 1
Altought what you point it is true, it is not so easy. Those examples are without queens. When queens on the board, those attacks usually are letal. I bet it is hard to find examples with queens.
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/