2011-11-12 20:44:06 +01:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2015-02-01 01:46:09 +01:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2018-12-03 20:38:40 +01:00
|
|
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
#include <algorithm>
|
2011-11-12 20:44:06 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "bitboard.h"
|
|
|
|
#include "pawns.h"
|
|
|
|
#include "position.h"
|
2015-02-01 01:46:09 +01:00
|
|
|
#include "thread.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
#define V Value
|
2011-11-12 20:44:06 +01:00
|
|
|
#define S(mg, eg) make_score(mg, eg)
|
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
// Pawn penalties
|
|
|
|
constexpr Score Backward = S( 9, 24);
|
|
|
|
constexpr Score Doubled = S(11, 56);
|
|
|
|
constexpr Score Isolated = S( 5, 15);
|
2016-05-30 20:56:34 +02:00
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
// Connected pawn bonus by opposed, phalanx, #support and rank
|
|
|
|
Score Connected[2][2][3][RANK_NB];
|
2016-05-30 20:56:34 +02:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
// Strength of pawn shelter for our king by [distance from edge][rank].
|
|
|
|
// RANK_1 = 0 is used for files where we have no pawn, or pawn is behind our king.
|
|
|
|
constexpr Value ShelterStrength[int(FILE_NB) / 2][RANK_NB] = {
|
|
|
|
{ V( -6), V( 81), V( 93), V( 58), V( 39), V( 18), V( 25) },
|
|
|
|
{ V(-43), V( 61), V( 35), V(-49), V(-29), V(-11), V( -63) },
|
|
|
|
{ V(-10), V( 75), V( 23), V( -2), V( 32), V( 3), V( -45) },
|
|
|
|
{ V(-39), V(-13), V(-29), V(-52), V(-48), V(-67), V(-166) }
|
2016-11-01 19:40:58 +01:00
|
|
|
};
|
2015-02-01 01:46:09 +01:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
// Danger of enemy pawns moving toward our king by [distance from edge][rank].
|
|
|
|
// RANK_1 = 0 is used for files where the enemy has no pawn, or their pawn
|
|
|
|
// is behind our king.
|
|
|
|
constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = {
|
|
|
|
{ V( 89), V(107), V(123), V(93), V(57), V( 45), V( 51) },
|
|
|
|
{ V( 44), V(-18), V(123), V(46), V(39), V( -7), V( 23) },
|
|
|
|
{ V( 4), V( 52), V(162), V(37), V( 7), V(-14), V( -2) },
|
|
|
|
{ V(-10), V(-14), V( 90), V(15), V( 2), V( -7), V(-16) }
|
2016-11-01 19:40:58 +01:00
|
|
|
};
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
#undef S
|
|
|
|
#undef V
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
template<Color Us>
|
2013-08-20 20:02:33 +02:00
|
|
|
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
|
|
|
constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2016-05-30 20:56:34 +02:00
|
|
|
Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
|
2017-09-10 10:30:09 +02:00
|
|
|
Bitboard lever, leverPush;
|
2013-05-03 19:03:42 +02:00
|
|
|
Square s;
|
2017-09-10 10:30:09 +02:00
|
|
|
bool opposed, backward;
|
2015-02-01 01:46:09 +01:00
|
|
|
Score score = SCORE_ZERO;
|
2015-10-23 22:58:14 +02:00
|
|
|
const Square* pl = pos.squares<PAWN>(Us);
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
Bitboard ourPawns = pos.pieces( Us, PAWN);
|
2013-08-20 20:02:33 +02:00
|
|
|
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
|
|
|
|
2018-02-01 18:45:14 +01:00
|
|
|
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0;
|
2013-08-20 20:02:33 +02:00
|
|
|
e->semiopenFiles[Us] = 0xFF;
|
2016-11-01 19:40:58 +01:00
|
|
|
e->kingSquares[Us] = SQ_NONE;
|
2018-12-03 20:38:40 +01:00
|
|
|
e->pawnAttacks[Us] = pawn_attacks_bb<Us>(ourPawns);
|
2016-05-30 20:56:34 +02:00
|
|
|
e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares);
|
2013-08-20 20:02:33 +02:00
|
|
|
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
// Loop through all pawns of the current color and score each pawn
|
|
|
|
while ((s = *pl++) != SQ_NONE)
|
|
|
|
{
|
|
|
|
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
|
|
|
|
2014-10-12 14:38:00 +02:00
|
|
|
File f = file_of(s);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2016-11-01 19:40:58 +01:00
|
|
|
e->semiopenFiles[Us] &= ~(1 << f);
|
2015-12-27 23:25:14 +01:00
|
|
|
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// Flag the pawn
|
2017-09-10 10:30:09 +02:00
|
|
|
opposed = theirPawns & forward_file_bb(Us, s);
|
2016-05-30 20:56:34 +02:00
|
|
|
stoppers = theirPawns & passed_pawn_mask(Us, s);
|
2017-09-10 10:30:09 +02:00
|
|
|
lever = theirPawns & PawnAttacks[Us][s];
|
|
|
|
leverPush = theirPawns & PawnAttacks[Us][s + Up];
|
|
|
|
doubled = ourPawns & (s - Up);
|
2016-05-30 20:56:34 +02:00
|
|
|
neighbours = ourPawns & adjacent_files_bb(f);
|
|
|
|
phalanx = neighbours & rank_bb(s);
|
|
|
|
supported = neighbours & rank_bb(s - Up);
|
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
// A pawn is backward when it is behind all pawns of the same color
|
|
|
|
// on the adjacent files and cannot be safely advanced.
|
|
|
|
backward = !(ourPawns & pawn_attack_span(Them, s + Up))
|
|
|
|
&& (stoppers & (leverPush | (s + Up)));
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
// Passed pawns will be properly scored in evaluation because we need
|
2017-09-10 10:30:09 +02:00
|
|
|
// full attack info to evaluate them. Include also not passed pawns
|
|
|
|
// which could become passed after one or two pawn pushes when are
|
|
|
|
// not attacked more times than defended.
|
|
|
|
if ( !(stoppers ^ lever ^ leverPush)
|
2018-12-03 20:38:40 +01:00
|
|
|
&& popcount(supported) >= popcount(lever) - 1
|
2017-09-10 10:30:09 +02:00
|
|
|
&& popcount(phalanx) >= popcount(leverPush))
|
2013-05-03 19:03:42 +02:00
|
|
|
e->passedPawns[Us] |= s;
|
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
else if ( stoppers == SquareBB[s + Up]
|
|
|
|
&& relative_rank(Us, s) >= RANK_5)
|
|
|
|
{
|
|
|
|
b = shift<Up>(supported) & ~theirPawns;
|
|
|
|
while (b)
|
|
|
|
if (!more_than_one(theirPawns & PawnAttacks[Us][pop_lsb(&b)]))
|
|
|
|
e->passedPawns[Us] |= s;
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// Score this pawn
|
2017-09-10 10:30:09 +02:00
|
|
|
if (supported | phalanx)
|
2018-02-01 18:45:14 +01:00
|
|
|
score += Connected[opposed][bool(phalanx)][popcount(supported)][relative_rank(Us, s)];
|
2017-09-10 10:30:09 +02:00
|
|
|
|
|
|
|
else if (!neighbours)
|
2018-02-01 18:45:14 +01:00
|
|
|
score -= Isolated, e->weakUnopposed[Us] += !opposed;
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
else if (backward)
|
2018-02-01 18:45:14 +01:00
|
|
|
score -= Backward, e->weakUnopposed[Us] += !opposed;
|
2015-10-23 22:58:14 +02:00
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
if (doubled && !supported)
|
2016-05-30 20:56:34 +02:00
|
|
|
score -= Doubled;
|
2013-05-03 19:03:42 +02:00
|
|
|
}
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
return score;
|
2013-05-03 19:03:42 +02:00
|
|
|
}
|
2013-08-20 20:02:33 +02:00
|
|
|
|
|
|
|
} // namespace
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
namespace Pawns {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
/// Pawns::init() initializes some tables needed by evaluation. Instead of using
|
|
|
|
/// hard-coded tables, when makes sense, we prefer to calculate them with a formula
|
|
|
|
/// to reduce independent parameters and to allow easier tuning and better insight.
|
|
|
|
|
2016-11-01 19:40:58 +01:00
|
|
|
void init() {
|
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
static constexpr int Seed[RANK_NB] = { 0, 13, 24, 18, 65, 100, 175, 330 };
|
2015-02-01 01:46:09 +01:00
|
|
|
|
|
|
|
for (int opposed = 0; opposed <= 1; ++opposed)
|
|
|
|
for (int phalanx = 0; phalanx <= 1; ++phalanx)
|
2017-09-10 10:30:09 +02:00
|
|
|
for (int support = 0; support <= 2; ++support)
|
2015-10-23 22:58:14 +02:00
|
|
|
for (Rank r = RANK_2; r < RANK_8; ++r)
|
|
|
|
{
|
2017-09-10 10:30:09 +02:00
|
|
|
int v = 17 * support;
|
|
|
|
v += (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed;
|
|
|
|
|
|
|
|
Connected[opposed][phalanx][support][r] = make_score(v, v * (r - 2) / 4);
|
2015-10-23 22:58:14 +02:00
|
|
|
}
|
2015-02-01 01:46:09 +01:00
|
|
|
}
|
|
|
|
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
/// Pawns::probe() looks up the current position's pawns configuration in
|
|
|
|
/// the pawns hash table. It returns a pointer to the Entry if the position
|
|
|
|
/// is found. Otherwise a new Entry is computed and stored there, so we don't
|
|
|
|
/// have to recompute all when the same pawns configuration occurs again.
|
|
|
|
|
|
|
|
Entry* probe(const Position& pos) {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
Key key = pos.pawn_key();
|
2015-02-01 01:46:09 +01:00
|
|
|
Entry* e = pos.this_thread()->pawnsTable[key];
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
if (e->key == key)
|
|
|
|
return e;
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
e->key = key;
|
2018-12-03 20:38:40 +01:00
|
|
|
e->scores[WHITE] = evaluate<WHITE>(pos, e);
|
|
|
|
e->scores[BLACK] = evaluate<BLACK>(pos, e);
|
2016-09-18 00:05:29 +02:00
|
|
|
e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
|
2018-12-03 20:38:40 +01:00
|
|
|
e->asymmetry = popcount( (e->passedPawns[WHITE] | e->passedPawns[BLACK])
|
|
|
|
| (e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK]));
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
return e;
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
/// Entry::evaluate_shelter() calculates the shelter bonus and the storm
|
|
|
|
/// penalty for a king, looking at the king file and the two closest files.
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2011-11-12 20:44:06 +01:00
|
|
|
template<Color Us>
|
2018-12-03 20:38:40 +01:00
|
|
|
Value Entry::evaluate_shelter(const Position& pos, Square ksq) {
|
2015-02-01 01:46:09 +01:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
|
|
|
constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
|
|
|
|
constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB);
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq);
|
2013-08-20 20:02:33 +02:00
|
|
|
Bitboard ourPawns = b & pos.pieces(Us);
|
2012-06-04 18:25:51 +02:00
|
|
|
Bitboard theirPawns = b & pos.pieces(Them);
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
Value safety = (shift<Down>(theirPawns) & (FileABB | FileHBB) & BlockRanks & ksq) ?
|
|
|
|
Value(374) : Value(5);
|
|
|
|
|
|
|
|
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
2018-02-01 18:45:14 +01:00
|
|
|
for (File f = File(center - 1); f <= File(center + 1); ++f)
|
2011-11-12 20:44:06 +01:00
|
|
|
{
|
2013-11-30 20:12:34 +01:00
|
|
|
b = ourPawns & file_bb(f);
|
2018-12-03 20:38:40 +01:00
|
|
|
int ourRank = b ? relative_rank(Us, backmost_sq(Us, b)) : 0;
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2017-09-10 10:30:09 +02:00
|
|
|
b = theirPawns & file_bb(f);
|
2018-12-03 20:38:40 +01:00
|
|
|
int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0;
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2018-02-01 18:45:14 +01:00
|
|
|
int d = std::min(f, ~f);
|
2018-12-03 20:38:40 +01:00
|
|
|
safety += ShelterStrength[d][ourRank];
|
|
|
|
safety -= (ourRank && (ourRank == theirRank - 1)) ? 66 * (theirRank == RANK_3)
|
|
|
|
: UnblockedStorm[d][theirRank];
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
return safety;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
/// Entry::do_king_safety() calculates a bonus for king safety. It is called only
|
|
|
|
/// when king square changes, which is about 20% of total king_safety() calls.
|
2012-06-04 18:25:51 +02:00
|
|
|
|
|
|
|
template<Color Us>
|
2018-12-03 20:38:40 +01:00
|
|
|
Score Entry::do_king_safety(const Position& pos) {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
Square ksq = pos.square<KING>(Us);
|
2011-11-12 20:44:06 +01:00
|
|
|
kingSquares[Us] = ksq;
|
2014-05-31 14:23:03 +02:00
|
|
|
castlingRights[Us] = pos.can_castle(Us);
|
2015-10-23 22:58:14 +02:00
|
|
|
int minKingPawnDistance = 0;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
Bitboard pawns = pos.pieces(Us, PAWN);
|
|
|
|
if (pawns)
|
2018-12-03 20:38:40 +01:00
|
|
|
while (!(DistanceRingBB[ksq][++minKingPawnDistance] & pawns)) {}
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
Value bonus = evaluate_shelter<Us>(pos, ksq);
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
// If we can castle use the bonus after the castling if it is bigger
|
2018-12-03 20:38:40 +01:00
|
|
|
if (pos.can_castle(Us | KING_SIDE))
|
|
|
|
bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_G1)));
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2018-12-03 20:38:40 +01:00
|
|
|
if (pos.can_castle(Us | QUEEN_SIDE))
|
|
|
|
bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1)));
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
return make_score(bonus, -16 * minKingPawnDistance);
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit template instantiation
|
2018-12-03 20:38:40 +01:00
|
|
|
template Score Entry::do_king_safety<WHITE>(const Position& pos);
|
|
|
|
template Score Entry::do_king_safety<BLACK>(const Position& pos);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
} // namespace Pawns
|