Skip to content

Commit

Permalink
Merge official-stockfish/master
Browse files Browse the repository at this point in the history
bench: 3559377
  • Loading branch information
ianfab committed Jul 27, 2019
2 parents ee19d0c + 9d3a2ec commit a447f63
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 117 deletions.
11 changes: 10 additions & 1 deletion Readme.md
Expand Up @@ -96,7 +96,16 @@ Currently, Stockfish has the following UCI options:
Leave at 1 for best performance.

* #### Skill Level
Lower the Skill Level in order to make Stockfish play weaker.
Lower the Skill Level in order to make Stockfish play weaker (see also UCI_LimitStrength).
Internally, MultiPV is enabled, and with a certain probability depending on the Skill Level a
weaker move will be played.

* #### UCI_LimitStrength
Enable weaker play aiming for an Elo rating as set by UCI_Elo. This option overrides Skill Level.

* #### UCI_Elo
If enabled by UCI_LimitStrength, aim for an engine strength of the given Elo.
This Elo rating has been calibrated at a time control of 60s+0.6s and anchored to CCRL 40/4.

* #### Move Overhead
Assume a time delay of x ms due to network and GUI overheads. This is useful to
Expand Down
6 changes: 5 additions & 1 deletion src/Makefile
Expand Up @@ -139,6 +139,8 @@ endif
ifeq ($(ARCH),ppc-64)
arch = ppc64
bits = 64
popcnt = yes
prefetch = yes
endif


Expand Down Expand Up @@ -328,7 +330,9 @@ endif

### 3.6 popcnt
ifeq ($(popcnt),yes)
ifeq ($(comp),icc)
ifeq ($(arch),ppc64)
CXXFLAGS += -DUSE_POPCNT
else ifeq ($(comp),icc)
CXXFLAGS += -msse3 -DUSE_POPCNT
else
CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
Expand Down
2 changes: 1 addition & 1 deletion src/bitboard.cpp
Expand Up @@ -376,7 +376,7 @@ void Bitboards::init() {
init_magics(BishopTable, BishopMagics, BishopDirections);
#endif

for (Color c = WHITE; c <= BLACK; ++c)
for (Color c : { WHITE, BLACK })
for (PieceType pt = PAWN; pt <= KING; ++pt)
{
const PieceInfo* pi = pieceMap.find(pt)->second;
Expand Down
43 changes: 16 additions & 27 deletions src/evaluate.cpp
Expand Up @@ -131,20 +131,13 @@ namespace {

// PassedRank[Rank] contains a bonus according to the rank of a passed pawn
constexpr Score PassedRank[RANK_NB] = {
S(0, 0), S(5, 18), S(12, 23), S(10, 31), S(57, 62), S(163, 167), S(271, 250)
};

// PassedFile[File] contains a bonus according to the file of a passed pawn
constexpr Score PassedFile[FILE_NB] = {
S( -1, 7), S( 0, 9), S(-9, -8), S(-30,-14),
S(-30,-14), S(-9, -8), S( 0, 9), S( -1, 7)
S(0, 0), S(10, 28), S(17, 33), S(15, 41), S(62, 72), S(168, 177), S(276, 260)
};

// KingProximity contains a penalty according to distance from king
constexpr Score KingProximity = S(1, 3);

// Assorted bonuses and penalties
constexpr Score AttacksOnSpaceArea = S( 4, 0);
constexpr Score BishopPawns = S( 3, 7);
constexpr Score CorneredBishop = S( 50, 50);
constexpr Score FlankAttacks = S( 8, 0);
Expand All @@ -153,7 +146,8 @@ namespace {
constexpr Score KnightOnQueen = S( 16, 12);
constexpr Score LongDiagonalBishop = S( 45, 0);
constexpr Score MinorBehindPawn = S( 18, 3);
constexpr Score Outpost = S( 36, 12);
constexpr Score Outpost = S( 18, 6);
constexpr Score PassedFile = S( 11, 8);
constexpr Score PawnlessFlank = S( 17, 95);
constexpr Score RestrictedPiece = S( 7, 7);
constexpr Score RookOnPawn = S( 10, 32);
Expand Down Expand Up @@ -349,10 +343,10 @@ namespace {
// Bonus if piece is on an outpost square or can reach one
bb = OutpostRanks & attackedBy[Us][PAWN] & ~pe->pawn_attacks_span(Them);
if (bb & s)
score += Outpost * (Pt == KNIGHT ? 2 : 1);
score += Outpost * (Pt == KNIGHT ? 4 : 2);

else if (bb & b & ~pos.pieces(Us))
score += Outpost / (Pt == KNIGHT ? 1 : 2);
score += Outpost * (Pt == KNIGHT ? 2 : 1);

// Knight and Bishop bonus for being right behind a pawn
if (shift<Down>(pos.pieces(PAWN)) & s)
Expand Down Expand Up @@ -674,7 +668,7 @@ namespace {
b &= ~attackedBy[Them][PAWN] & safe;

// Bonus for safe pawn threats on the next move
b = (pawn_attacks_bb<Us>(b) | shift<Up>(shift<Up>(pos.pieces(Us, SHOGI_PAWN)))) & pos.pieces(Them);
b = (pawn_attacks_bb<Us>(b) | shift<Up>(shift<Up>(pos.pieces(Us, SHOGI_PAWN)))) & nonPawnEnemies;
score += ThreatByPawnPush * popcount(b);

// Our safe or protected pawns
Expand Down Expand Up @@ -718,7 +712,7 @@ namespace {
return pos.count<KING>(c) ? std::min(distance(pos.square<KING>(c), s), 5) : 5;
};

Bitboard b, bb, squaresToQueen, defendedSquares, unsafeSquares;
Bitboard b, bb, squaresToQueen, unsafeSquares;
Score score = SCORE_ZERO;

b = pe->passed_pawns(Us);
Expand All @@ -730,12 +724,13 @@ namespace {
assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));

int r = std::max(RANK_8 - std::max(pos.promotion_rank() - relative_rank(Us, s, pos.max_rank()), 0), 0);
File f = file_of(s);

Score bonus = PassedRank[r];

if (r > RANK_3)
{
int w = (r-2) * (r-2) + 2;
int w = 5 * r - 13;
Square blockSq = s + Up;

// Skip bonus for antichess variants
Expand All @@ -753,16 +748,13 @@ namespace {
// If the pawn is free to advance, then increase the bonus
if (pos.empty(blockSq))
{
defendedSquares = squaresToQueen = forward_file_bb(Us, s);
squaresToQueen = forward_file_bb(Us, s);
unsafeSquares = passed_pawn_span(Us, s);

bb = forward_file_bb(Them, s) & pos.pieces(ROOK, QUEEN);

if (!(pos.pieces(Us) & bb))
defendedSquares &= attackedBy[Us][ALL_PIECES];

if (!(pos.pieces(Them) & bb))
unsafeSquares &= attackedBy[Them][ALL_PIECES] | pos.pieces(Them);
unsafeSquares &= attackedBy[Them][ALL_PIECES];

// If there are no enemy attacks on passed pawn span, assign a big bonus.
// Otherwise assign a smaller bonus if the path to queen is not attacked
Expand All @@ -772,8 +764,8 @@ namespace {
!(unsafeSquares & blockSq) ? 9 :
0 ;

// Assign a larger bonus if the block square is defended.
if (defendedSquares & blockSq)
// Assign a larger bonus if the block square is defended
if ((pos.pieces(Us) & bb) || (attackedBy[Us][ALL_PIECES] & blockSq))
k += 5;

bonus += make_score(k * w, k * w);
Expand All @@ -783,10 +775,10 @@ namespace {
// Scale down bonus for candidate passers which need more than one
// pawn push to become passed, or have a pawn in front of them.
if ( !pos.pawn_passed(Us, s + Up)
|| (pos.pieces(PAWN) & forward_file_bb(Us, s)))
|| (pos.pieces(PAWN) & (s + Up)))
bonus = bonus / 2;

score += bonus + PassedFile[file_of(s)];
score += bonus - PassedFile * std::min(f, File(pos.max_file() - f));
}

// Scale by maximum promotion piece value
Expand Down Expand Up @@ -862,13 +854,10 @@ namespace {
safe = behind & ~attackedBy[Them][ALL_PIECES];
behind = 0;
}

int bonus = popcount(safe) + popcount(behind & safe);
int bonus = popcount(safe) + popcount(behind & safe & ~attackedBy[Them][ALL_PIECES]);
int weight = pos.count<ALL_PIECES>(Us) - 1;
Score score = make_score(bonus * weight * weight / 16, 0);

score -= AttacksOnSpaceArea * popcount(attackedBy[Them][ALL_PIECES] & behind & safe);

if (T)
Trace::add(SPACE, Us, score);

Expand Down
6 changes: 3 additions & 3 deletions src/material.cpp
Expand Up @@ -164,14 +164,14 @@ Entry* probe(const Position& pos) {
if ((e->evaluationFunction = Endgames::probe<Value>(key)) != nullptr)
return e;

for (Color c = WHITE; c <= BLACK; ++c)
for (Color c : { WHITE, BLACK })
if (is_KFsPsK(pos, c))
{
e->evaluationFunction = &EvaluateKFsPsK[c];
return e;
}

for (Color c = WHITE; c <= BLACK; ++c)
for (Color c : { WHITE, BLACK })
if (is_KXK(pos, c))
{
e->evaluationFunction = &EvaluateKXK[c];
Expand All @@ -191,7 +191,7 @@ Entry* probe(const Position& pos) {
// We didn't find any specialized scaling function, so fall back on generic
// ones that refer to more than one material distribution. Note that in this
// case we don't return after setting the function.
for (Color c = WHITE; c <= BLACK; ++c)
for (Color c : { WHITE, BLACK })
{
if (is_KBPsK(pos, c))
e->scalingFunction[c] = &ScaleKBPsK[c];
Expand Down
4 changes: 2 additions & 2 deletions src/movepick.cpp
Expand Up @@ -111,8 +111,8 @@ void MovePicker::score() {

for (auto& m : *this)
if (Type == CAPTURES)
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))] / 8;
m.value = int(PieceValue[MG][pos.piece_on(to_sq(m))]) * 6
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))];

else if (Type == QUIETS)
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
Expand Down
78 changes: 40 additions & 38 deletions src/pawns.cpp
Expand Up @@ -32,11 +32,11 @@ namespace {
#define S(mg, eg) make_score(mg, eg)

// Pawn penalties
constexpr Score Backward = S( 9, 24);
constexpr Score Doubled = S(11, 56);
constexpr Score Isolated = S( 5, 15);
constexpr Score WeakUnopposed = S( 13, 27);
constexpr Score Attacked2Unsupported = S( 0, 20);
constexpr Score Backward = S( 9, 24);
constexpr Score Doubled = S(11, 56);
constexpr Score Isolated = S( 5, 15);
constexpr Score WeakLever = S( 0, 56);
constexpr Score WeakUnopposed = S(13, 27);

// Connected pawn bonus
constexpr int Connected[RANK_NB] = { 0, 7, 8, 12, 29, 48, 86 };
Expand Down Expand Up @@ -73,22 +73,19 @@ namespace {
Bitboard b, neighbours, stoppers, doubled, support, phalanx;
Bitboard lever, leverPush;
Square s;
bool opposed, backward;
bool opposed, backward, passed;
Score score = SCORE_ZERO;
const Square* pl = pos.squares<PAWN>(Us);

Bitboard ourPawns = pos.pieces( Us, PAWN);
Bitboard theirPawns = pos.pieces(Them, PAWN);

Bitboard doubleAttackThem = pawn_double_attacks_bb<Them>(theirPawns);

e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0;
e->kingSquares[Us] = SQ_NONE;
e->pawnAttacks[Us] = pawn_attacks_bb<Us>(ourPawns);

// Unsupported enemy pawns attacked twice by us
score += Attacked2Unsupported * popcount( theirPawns
& pawn_double_attacks_bb<Us>(ourPawns)
& ~pawn_attacks_bb<Them>(theirPawns));

// Loop through all pawns of the current color and score each pawn
while ((s = *pl++) != SQ_NONE)
{
Expand All @@ -108,30 +105,28 @@ namespace {
phalanx = neighbours & rank_bb(s);
support = r > RANK_1 ? neighbours & rank_bb(s - Up) : Bitboard(0);

// A pawn is backward when it is behind all pawns of the same color
// on the adjacent files and cannot be safely advanced.
backward = relative_rank(Them, s, pos.max_rank()) > RANK_1
&& !(ourPawns & pawn_attack_span(Them, s + Up))
// A pawn is backward when it is behind all pawns of the same color on
// the adjacent files and cannot safely advance. Phalanx and isolated
// pawns will be excluded when the pawn is scored.
backward = !(neighbours & forward_ranks_bb(Them, s))
&& is_ok(s + Up)
&& (stoppers & (leverPush | (s + Up)));

// Passed pawns will be properly scored in evaluation because we need
// 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) ||
(!(stoppers ^ leverPush) && popcount(phalanx) >= popcount(leverPush)))
// A pawn is passed if one of the three following conditions is true:
// (a) there is no stoppers except some levers
// (b) the only stoppers are the leverPush, but we outnumber them
// (c) there is only one front stopper which can be levered.
passed = !(stoppers ^ lever)
|| ( !(stoppers ^ leverPush)
&& popcount(phalanx) >= popcount(leverPush))
|| ( is_ok(s + Up) && stoppers == square_bb(s + Up) && r >= RANK_5
&& (shift<Up>(support) & ~(theirPawns | doubleAttackThem)));

// Passed pawns will be properly scored later in evaluation when we have
// full attack info.
if (passed)
e->passedPawns[Us] |= s;

else if ( relative_rank(Them, s, pos.max_rank()) > RANK_1
&& stoppers == square_bb(s + Up)
&& r >= RANK_5)
{
b = shift<Up>(support) & ~theirPawns;
while (b)
if (!more_than_one(theirPawns & PseudoAttacks[Us][PAWN][pop_lsb(&b)]))
e->passedPawns[Us] |= s;
}

// Score this pawn
if (support | phalanx)
{
Expand All @@ -141,6 +136,7 @@ namespace {
v = std::max(v, popcount(support | phalanx) * 50) / (opposed ? 2 : 1);
score += make_score(v, v * (r - 2) / 4);
}

else if (!neighbours)
score -= Isolated * (1 + 2 * pos.must_capture()) + WeakUnopposed * int(!opposed);

Expand All @@ -151,6 +147,12 @@ namespace {
score -= Doubled;
}

// Penalize the unsupported and non passed pawns attacked twice by the enemy
b = ourPawns
& doubleAttackThem
& ~(e->pawnAttacks[Us] | e->passedPawns[Us]);
score -= WeakLever * popcount(b);

// Double pawn evaluation if there are no non-pawn pieces
if (pos.count<ALL_PIECES>(Us) == pos.count<PAWN>(Us))
score = score * 2;
Expand Down Expand Up @@ -205,13 +207,13 @@ Entry* probe(const Position& pos) {
template<Color Us>
void Entry::evaluate_shelter(const Position& pos, Square ksq, Score& shelter) {

constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);

Bitboard b = pos.pieces(PAWN, SHOGI_PAWN) & ~forward_ranks_bb(Them, ksq);
Bitboard ourPawns = b & pos.pieces(Us);
Bitboard theirPawns = b & pos.pieces(Them);

Value bonus[] = { Value(5), Value(5) };
Score bonus = make_score(5, 5);

File center = clamp(file_of(ksq), FILE_B, File(pos.max_file() - 1));
for (File f = File(center - 1); f <= File(center + 1); ++f)
Expand All @@ -223,16 +225,16 @@ void Entry::evaluate_shelter(const Position& pos, Square ksq, Score& shelter) {
Rank theirRank = b ? relative_rank(Us, frontmost_sq(Them, b), pos.max_rank()) : RANK_1;

int d = std::min(std::min(f, File(pos.max_file() - f)), FILE_D);
bonus[MG] += ShelterStrength[d][ourRank] * (1 + (pos.captures_to_hand() && ourRank <= RANK_2));
bonus += make_score(ShelterStrength[d][ourRank], 0) * (1 + (pos.captures_to_hand() && ourRank <= RANK_2));

if (ourRank && (ourRank == theirRank - 1))
bonus[MG] -= 82 * (theirRank == RANK_3), bonus[EG] -= 82 * (theirRank == RANK_3);
bonus -= make_score(82 * (theirRank == RANK_3), 82 * (theirRank == RANK_3));
else
bonus[MG] -= UnblockedStorm[d][theirRank];
bonus -= make_score(UnblockedStorm[d][theirRank], 0);
}

if (bonus[MG] > mg_value(shelter))
shelter = make_score(bonus[MG], bonus[EG]);
if (mg_value(bonus) > mg_value(shelter))
shelter = bonus;
}


Expand Down

0 comments on commit a447f63

Please sign in to comment.