Skip to content

Commit 6a79d5d

Browse files
Rocky640zamar
authored andcommitted
Correct Pawn Trace Score + Code Clean up
No functional change Resolves #542
1 parent 3e4fed3 commit 6a79d5d

File tree

4 files changed

+81
-80
lines changed

4 files changed

+81
-80
lines changed

src/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const vector<string> Defaults = {
7575
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
7676

7777
// 7-man positions
78-
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
78+
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124" // Draw
7979
};
8080

8181
} // namespace

src/endgame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace {
3939
70, 50, 30, 20, 20, 30, 50, 70,
4040
80, 60, 40, 30, 30, 40, 60, 80,
4141
90, 70, 60, 50, 50, 60, 70, 90,
42-
100, 90, 80, 70, 70, 80, 90, 100,
42+
100, 90, 80, 70, 70, 80, 90, 100
4343
};
4444

4545
// Table used to drive the king towards a corner square of the

src/evaluate.cpp

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ namespace {
111111
enum { PawnStructure, PassedPawns, Space, KingSafety };
112112

113113
const struct Weight { int mg, eg; } Weights[] = {
114-
{214, 203}, {193, 262}, {47, 0}, {330, 0}
115-
};
114+
{214, 203}, {193, 262}, {47, 0}, {330, 0} };
116115

117116
Score operator*(Score s, const Weight& w) {
118117
return make_score(mg_value(s) * w.mg / 256, eg_value(s) * w.eg / 256);
@@ -156,19 +155,26 @@ namespace {
156155
{ S( 8, 2), S(13, 4) } // Bishops
157156
};
158157

159-
// Threat[minor/rook][attacked PieceType] contains
158+
// RookOnFile[semiopen/open] contains bonuses for each rook when there is no
159+
// friendly pawn on the rook file.
160+
const Score RookOnFile[2] = { S(19, 10), S(43, 21) };
161+
162+
// ThreatBySafePawn[PieceType] contains bonuses according to which piece
163+
// type is attacked by a pawn which is protected or not attacked.
164+
const Score ThreatBySafePawn[PIECE_TYPE_NB] = {
165+
S(0, 0), S(0, 0), S(176, 139), S(131, 127), S(217, 218), S(203, 215) };
166+
167+
// Threat[by minor/by rook][attacked PieceType] contains
160168
// bonuses according to which piece type attacks which one.
161169
// Attacks on lesser pieces which are pawn defended are not considered.
162-
const Score Threat[2][PIECE_TYPE_NB] = {
163-
{ S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72, 107), S(48,118) }, // Minor attacks
164-
{ S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48) } // Rook attacks
170+
const Score Threat[][PIECE_TYPE_NB] = {
171+
{ S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72,107), S(48,118) }, // by Minor
172+
{ S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48) } // by Rook
165173
};
166174

167-
// ThreatenedByPawn[PieceType] contains a penalty according to which piece
168-
// type is attacked by a pawn.
169-
const Score ThreatenedByPawn[PIECE_TYPE_NB] = {
170-
S(0, 0), S(0, 0), S(176, 139), S(131, 127), S(217, 218), S(203, 215)
171-
};
175+
// ThreatByKing[on one/on many] contains bonuses for King attacks on
176+
// pawns or pieces which are not pawn defended.
177+
const Score ThreatByKing[2] = { S(3, 62), S(9, 138) };
172178

173179
// Passed[mg/eg][Rank] contains midgame and endgame bonuses for passed pawns.
174180
// We don't use a Score because we process the two components independently.
@@ -177,27 +183,22 @@ namespace {
177183
{ V(7), V(14), V(37), V(63), V(134), V(189) }
178184
};
179185

180-
// PassedFile[File] contains a bonus according to the file of a passed pawn.
181-
const Score PassedFile[] = {
182-
S( 12, 10), S( 3, 10), S( 1, -8), S(-27, -12),
183-
S(-27, -12), S( 1, -8), S( 3, 10), S( 12, 10)
186+
// PassedFile[File] contains a bonus according to the file of a passed pawn
187+
const Score PassedFile[FILE_NB] = {
188+
S( 12, 10), S( 3, 10), S( 1, -8), S(-27,-12),
189+
S(-27,-12), S( 1, -8), S( 3, 10), S( 12, 10)
184190
};
185191

186-
const Score ThreatenedByHangingPawn = S(70, 63);
187-
188192
// Assorted bonuses and penalties used by evaluation
189-
const Score KingOnOne = S( 3, 62);
190-
const Score KingOnMany = S( 9,138);
191-
const Score RookOnPawn = S( 7, 27);
192-
const Score RookOnOpenFile = S(43, 21);
193-
const Score RookOnSemiOpenFile = S(19, 10);
194-
const Score BishopPawns = S( 8, 12);
195-
const Score MinorBehindPawn = S(16, 0);
196-
const Score TrappedRook = S(92, 0);
197-
const Score Unstoppable = S( 0, 20);
198-
const Score Hanging = S(48, 28);
199-
const Score PawnAttackThreat = S(31, 19);
200-
const Score Checked = S(20, 20);
193+
const Score MinorBehindPawn = S(16, 0);
194+
const Score BishopPawns = S( 8, 12);
195+
const Score RookOnPawn = S( 7, 27);
196+
const Score TrappedRook = S(92, 0);
197+
const Score Checked = S(20, 20);
198+
const Score ThreatByHangingPawn = S(70, 63);
199+
const Score Hanging = S(48, 28);
200+
const Score ThreatByPawnPush = S(31, 19);
201+
const Score Unstoppable = S( 0, 20);
201202

202203
// Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
203204
// a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only
@@ -343,15 +344,15 @@ namespace {
343344
{
344345
Bitboard alignedPawns = pos.pieces(Them, PAWN) & PseudoAttacks[ROOK][s];
345346
if (alignedPawns)
346-
score += popcount<Max15>(alignedPawns) * RookOnPawn;
347+
score += RookOnPawn * popcount<Max15>(alignedPawns);
347348
}
348349

349350
// Bonus when on an open or semi-open file
350351
if (ei.pi->semiopen_file(Us, file_of(s)))
351-
score += ei.pi->semiopen_file(Them, file_of(s)) ? RookOnOpenFile : RookOnSemiOpenFile;
352+
score += RookOnFile[!!ei.pi->semiopen_file(Them, file_of(s))];
352353

353354
// Penalize when trapped by the king, even more if king cannot castle
354-
if (mob <= 3 && !ei.pi->semiopen_file(Us, file_of(s)))
355+
else if (mob <= 3)
355356
{
356357
Square ksq = pos.square<KING>(Us);
357358

@@ -506,10 +507,10 @@ namespace {
506507
safeThreats = (shift_bb<Right>(b) | shift_bb<Left>(b)) & weak;
507508

508509
if (weak ^ safeThreats)
509-
score += ThreatenedByHangingPawn;
510+
score += ThreatByHangingPawn;
510511

511512
while (safeThreats)
512-
score += ThreatenedByPawn[type_of(pos.piece_on(pop_lsb(&safeThreats)))];
513+
score += ThreatBySafePawn[type_of(pos.piece_on(pop_lsb(&safeThreats)))];
513514
}
514515

515516
// Non-pawn enemies defended by a pawn
@@ -537,7 +538,7 @@ namespace {
537538

538539
b = weak & ei.attackedBy[Us][KING];
539540
if (b)
540-
score += more_than_one(b) ? KingOnMany : KingOnOne;
541+
score += ThreatByKing[more_than_one(b)];
541542
}
542543

543544
// Bonus if some pawns can safely push and attack an enemy piece
@@ -553,7 +554,7 @@ namespace {
553554
& ~ei.attackedBy[Us][PAWN];
554555

555556
if (b)
556-
score += popcount<Max15>(b) * PawnAttackThreat;
557+
score += ThreatByPawnPush * popcount<Max15>(b);
557558

558559
if (DoTrace)
559560
Trace::add(THREAT, Us, score);
@@ -811,10 +812,10 @@ Value Eval::evaluate(const Position& pos) {
811812
{
812813
Bitboard b;
813814
if ((b = ei.pi->passed_pawns(WHITE)) != 0)
814-
score += int(relative_rank(WHITE, frontmost_sq(WHITE, b))) * Unstoppable;
815+
score += Unstoppable * int(relative_rank(WHITE, frontmost_sq(WHITE, b)));
815816

816817
if ((b = ei.pi->passed_pawns(BLACK)) != 0)
817-
score -= int(relative_rank(BLACK, frontmost_sq(BLACK, b))) * Unstoppable;
818+
score -= Unstoppable * int(relative_rank(BLACK, frontmost_sq(BLACK, b)));
818819
}
819820

820821
// Evaluate space for both sides, only during opening
@@ -839,7 +840,7 @@ Value Eval::evaluate(const Position& pos) {
839840
{
840841
Trace::add(MATERIAL, pos.psq_score());
841842
Trace::add(IMBALANCE, ei.me->imbalance());
842-
Trace::add(PAWN, ei.pi->pawns_score());
843+
Trace::add(PAWN, ei.pi->pawns_score() * Weights[PawnStructure]);
843844
Trace::add(MOBILITY, mobility[WHITE], mobility[BLACK]);
844845
Trace::add(SPACE, evaluate_space<WHITE>(pos, ei) * Weights[Space]
845846
, evaluate_space<BLACK>(pos, ei) * Weights[Space]);

src/pawns.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,59 +31,60 @@ namespace {
3131
#define V Value
3232
#define S(mg, eg) make_score(mg, eg)
3333

34-
// Doubled pawn penalty by file
35-
const Score Doubled[FILE_NB] = {
36-
S(13, 43), S(20, 48), S(23, 48), S(23, 48),
37-
S(23, 48), S(23, 48), S(20, 48), S(13, 43) };
38-
3934
// Isolated pawn penalty by opposed flag and file
4035
const Score Isolated[2][FILE_NB] = {
41-
{ S(37, 45), S(54, 52), S(60, 52), S(60, 52),
42-
S(60, 52), S(60, 52), S(54, 52), S(37, 45) },
43-
{ S(25, 30), S(36, 35), S(40, 35), S(40, 35),
44-
S(40, 35), S(40, 35), S(36, 35), S(25, 30) } };
36+
{ S(37, 45), S(54, 52), S(60, 52), S(60, 52),
37+
S(60, 52), S(60, 52), S(54, 52), S(37, 45) },
38+
{ S(25, 30), S(36, 35), S(40, 35), S(40, 35),
39+
S(40, 35), S(40, 35), S(36, 35), S(25, 30) } };
4540

4641
// Backward pawn penalty by opposed flag
4742
const Score Backward[2] = { S(67, 42), S(49, 24) };
43+
44+
// Unsupported pawn penalty, for pawns which are neither isolated or backward
45+
const Score Unsupported = S(20, 10);
4846

4947
// Connected pawn bonus by opposed, phalanx, twice supported and rank
5048
Score Connected[2][2][2][RANK_NB];
49+
50+
// Doubled pawn penalty by file
51+
const Score Doubled[FILE_NB] = {
52+
S(13, 43), S(20, 48), S(23, 48), S(23, 48),
53+
S(23, 48), S(23, 48), S(20, 48), S(13, 43) };
5154

52-
// Levers bonus by rank
55+
// Lever bonus by rank
5356
const Score Lever[RANK_NB] = {
54-
S( 0, 0), S( 0, 0), S(0, 0), S(0, 0),
55-
S(20,20), S(40,40), S(0, 0), S(0, 0) };
56-
57-
// Unsupported pawn penalty
58-
const Score UnsupportedPawnPenalty = S(20, 10);
57+
S( 0, 0), S( 0, 0), S(0, 0), S(0, 0),
58+
S(20, 20), S(40, 40), S(0, 0), S(0, 0) };
5959

60+
// Center bind bonus, when two pawns controls the same central square
6061
const Score CenterBind = S(16, 0);
6162

6263
// Weakness of our pawn shelter in front of the king by [distance from edge][rank]
6364
const Value ShelterWeakness[][RANK_NB] = {
64-
{ V( 97), V(21), V(26), V(51), V(87), V( 89), V( 99) },
65-
{ V(120), V( 0), V(28), V(76), V(88), V(103), V(104) },
66-
{ V(101), V( 7), V(54), V(78), V(77), V( 92), V(101) },
67-
{ V( 80), V(11), V(44), V(68), V(87), V( 90), V(119) } };
65+
{ V( 97), V(21), V(26), V(51), V(87), V( 89), V( 99) },
66+
{ V(120), V( 0), V(28), V(76), V(88), V(103), V(104) },
67+
{ V(101), V( 7), V(54), V(78), V(77), V( 92), V(101) },
68+
{ V( 80), V(11), V(44), V(68), V(87), V( 90), V(119) } };
6869

6970
// Danger of enemy pawns moving toward our king by [type][distance from edge][rank]
7071
const Value StormDanger[][4][RANK_NB] = {
71-
{ { V( 0), V( 67), V( 134), V(38), V(32) },
72-
{ V( 0), V( 57), V( 139), V(37), V(22) },
73-
{ V( 0), V( 43), V( 115), V(43), V(27) },
74-
{ V( 0), V( 68), V( 124), V(57), V(32) } },
75-
{ { V(20), V( 43), V( 100), V(56), V(20) },
76-
{ V(23), V( 20), V( 98), V(40), V(15) },
77-
{ V(23), V( 39), V( 103), V(36), V(18) },
78-
{ V(28), V( 19), V( 108), V(42), V(26) } },
79-
{ { V( 0), V( 0), V( 75), V(14), V( 2) },
80-
{ V( 0), V( 0), V( 150), V(30), V( 4) },
81-
{ V( 0), V( 0), V( 160), V(22), V( 5) },
82-
{ V( 0), V( 0), V( 166), V(24), V(13) } },
83-
{ { V( 0), V(-283), V(-281), V(57), V(31) },
84-
{ V( 0), V( 58), V( 141), V(39), V(18) },
85-
{ V( 0), V( 65), V( 142), V(48), V(32) },
86-
{ V( 0), V( 60), V( 126), V(51), V(19) } } };
72+
{ { V( 0), V( 67), V( 134), V(38), V(32) },
73+
{ V( 0), V( 57), V( 139), V(37), V(22) },
74+
{ V( 0), V( 43), V( 115), V(43), V(27) },
75+
{ V( 0), V( 68), V( 124), V(57), V(32) } },
76+
{ { V(20), V( 43), V( 100), V(56), V(20) },
77+
{ V(23), V( 20), V( 98), V(40), V(15) },
78+
{ V(23), V( 39), V( 103), V(36), V(18) },
79+
{ V(28), V( 19), V( 108), V(42), V(26) } },
80+
{ { V( 0), V( 0), V( 75), V(14), V( 2) },
81+
{ V( 0), V( 0), V( 150), V(30), V( 4) },
82+
{ V( 0), V( 0), V( 160), V(22), V( 5) },
83+
{ V( 0), V( 0), V( 166), V(24), V(13) } },
84+
{ { V( 0), V(-283), V(-281), V(57), V(31) },
85+
{ V( 0), V( 58), V( 141), V(39), V(18) },
86+
{ V( 0), V( 65), V( 142), V(48), V(32) },
87+
{ V( 0), V( 60), V( 126), V(51), V(19) } } };
8788

8889
// Max bonus for king safety. Corresponds to start position with all the pawns
8990
// in front of the king and no enemy pawn on the horizon.
@@ -167,7 +168,7 @@ namespace {
167168
assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
168169

169170
// Passed pawns will be properly scored in evaluation because we need
170-
// full attack info to evaluate passed pawns. Only the frontmost passed
171+
// full attack info to evaluate them. Only the frontmost passed
171172
// pawn on each file is considered a true passed pawn.
172173
if (passed && !doubled)
173174
e->passedPawns[Us] |= s;
@@ -180,7 +181,7 @@ namespace {
180181
score -= Backward[opposed];
181182

182183
else if (!supported)
183-
score -= UnsupportedPawnPenalty;
184+
score -= Unsupported;
184185

185186
if (connected)
186187
score += Connected[opposed][!!phalanx][more_than_one(supported)][relative_rank(Us, s)];
@@ -195,9 +196,8 @@ namespace {
195196
b = e->semiopenFiles[Us] ^ 0xFF;
196197
e->pawnSpan[Us] = b ? int(msb(b) - lsb(b)) : 0;
197198

198-
// Center binds: Two pawns controlling the same central square
199199
b = shift_bb<Right>(ourPawns) & shift_bb<Left>(ourPawns) & CenterBindMask;
200-
score += popcount<Max15>(b) * CenterBind;
200+
score += CenterBind * popcount<Max15>(b);
201201

202202
return score;
203203
}

0 commit comments

Comments
 (0)