Skip to content

Commit

Permalink
Merge remote-tracking branch 'ddugovic/master' into ddugovic
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Oct 7, 2017
2 parents a487595 + a30d658 commit e1e76da
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 86 deletions.
42 changes: 29 additions & 13 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@

namespace {

const Bitboard LongDiagonals = 0x8142241818244281ULL; // A1..H8 | H1..A8
const Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;

const Bitboard KingFlank[FILE_NB] = {
QueenSide, QueenSide, QueenSide, CenterFiles, CenterFiles, KingSide, KingSide, KingSide
};

namespace Trace {

enum Tracing {NO_TRACE, TRACE};
Expand Down Expand Up @@ -317,8 +327,8 @@ namespace {
// supported by a pawn. If the minor piece occupies an outpost square
// then score is doubled.
const Score Outpost[][2] = {
{ S(22, 6), S(33, 9) }, // Knight
{ S( 9, 2), S(14, 4) } // Bishop
{ S(22, 6), S(36,12) }, // Knight
{ S( 9, 2), S(15, 5) } // Bishop
};

// RookOnFile[semiopen/open] contains bonuses for each rook when there is no
Expand Down Expand Up @@ -367,8 +377,8 @@ namespace {
#endif
#ifdef HORDE
{
{ V(5), V( 5), V(31), V(73), V(166), V(252) },
{ V(7), V(14), V(38), V(73), V(166), V(252) }
{ V(-66), V(-25), V( 66), V(68), V( 72), V(250) },
{ V( 10), V( 7), V(-12), V(81), V(210), V(258) }
},
#endif
#ifdef KOTH
Expand Down Expand Up @@ -474,6 +484,7 @@ namespace {
// Assorted bonuses and penalties used by evaluation
const Score MinorBehindPawn = S( 16, 0);
const Score BishopPawns = S( 8, 12);
const Score LongRangedBishop = S( 22, 0);
const Score RookOnPawn = S( 8, 24);
const Score TrappedRook = S( 92, 0);
const Score WeakQueen = S( 50, 10);
Expand Down Expand Up @@ -513,6 +524,7 @@ namespace {
const Score ThreatBySafePawn = S(182,175);
const Score ThreatByRank = S( 16, 3);
const Score Hanging = S( 48, 27);
const Score WeakUnopposedPawn = S( 5, 25);
const Score ThreatByPawnPush = S( 38, 22);
const Score HinderPassedPawn = S( 7, 0);
const Score TrappedBishopA1H1 = S( 50, 50);
Expand Down Expand Up @@ -764,10 +776,18 @@ namespace {
&& (pos.pieces(PAWN) & (s + pawn_push(Us))))
score += MinorBehindPawn;

// Penalty for pawns on the same color square as the bishop
if (Pt == BISHOP)
{
// Penalty for pawns on the same color square as the bishop
score -= BishopPawns * pe->pawns_on_same_color_squares(Us, s);

// Bonus for bishop on a long diagonal without pawns in the center
if ( (LongDiagonals & s)
&& !(attackedBy[Them][PAWN] & s)
&& !(Center & PseudoAttacks[BISHOP][s] & pos.pieces(PAWN)))
score += LongRangedBishop;
}

// An important Chess960 pattern: A cornered bishop blocked by a friendly
// pawn diagonally in front of it is a very serious problem, especially
// when that pawn is also blocked.
Expand Down Expand Up @@ -822,14 +842,6 @@ namespace {

// evaluate_king() assigns bonuses and penalties to a king of a given color

const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;

const Bitboard KingFlank[FILE_NB] = {
QueenSide, QueenSide, QueenSide, CenterFiles, CenterFiles, KingSide, KingSide, KingSide
};

template<Tracing T> template<Color Us>
Score Evaluation<T>::evaluate_king() {

Expand Down Expand Up @@ -1176,6 +1188,10 @@ namespace {
score += ThreatByKing[more_than_one(b)];
}

// Bonus for opponent unopposed weak pawns
if (pos.pieces(Us, ROOK, QUEEN))
score += WeakUnopposedPawn * pe->weak_unopposed(Them);

// Find squares where our pawns can push on the next move
b = shift<Up>(pos.pieces(Us, PAWN)) & ~pos.pieces();
b |= shift<Up>(b & TRank3BB) & ~pos.pieces();
Expand Down
23 changes: 7 additions & 16 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace {
if (pos.count_in_hand<Pt>(Us))
{
if (Checks)
return moveList;
b &= pos.check_squares(Pt);
while (b)
*moveList++ = make_drop(pop_lsb(&b), make_piece(Us, Pt));
}
Expand Down Expand Up @@ -216,15 +216,6 @@ namespace {
b2 |= dc2;
}
}
#ifdef CRAZYHOUSE
// Do not require drops to be check (unless already required by target)
if (V == CRAZYHOUSE_VARIANT && pos.count_in_hand<PAWN>(Us))
{
Bitboard b = (Type == EVASIONS ? emptySquares & target : emptySquares) & ~(Rank1BB | Rank8BB);
if (Type != QUIET_CHECKS)
moveList = generate_drops<Us, PAWN, false>(pos, moveList, b);
}
#endif

while (b1)
{
Expand Down Expand Up @@ -373,22 +364,22 @@ namespace {
const bool Checks = Type == QUIET_CHECKS;

moveList = generate_pawn_moves<V, Us, Type>(pos, moveList, target);
moveList = generate_moves<V, KNIGHT, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, BISHOP, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, ROOK, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, QUEEN, Checks>(pos, moveList, Us, target);
#ifdef CRAZYHOUSE
if (V == CRAZYHOUSE_VARIANT && Type != CAPTURES &&
(pos.count_in_hand<ALL_PIECES>(Us) - pos.count_in_hand<PAWN>(Us)))
if (V == CRAZYHOUSE_VARIANT && Type != CAPTURES && pos.count_in_hand<ALL_PIECES>(Us))
{
Bitboard b = Type == EVASIONS ? target ^ pos.checkers() :
Type == NON_EVASIONS ? target ^ pos.pieces(~Us) : target;
moveList = generate_drops<Us, PAWN, Checks>(pos, moveList, b & ~(Rank1BB | Rank8BB));
moveList = generate_drops<Us, KNIGHT, Checks>(pos, moveList, b);
moveList = generate_drops<Us, BISHOP, Checks>(pos, moveList, b);
moveList = generate_drops<Us, ROOK, Checks>(pos, moveList, b);
moveList = generate_drops<Us, QUEEN, Checks>(pos, moveList, b);
}
#endif
moveList = generate_moves<V, KNIGHT, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, BISHOP, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, ROOK, Checks>(pos, moveList, Us, target);
moveList = generate_moves<V, QUEEN, Checks>(pos, moveList, Us, target);

#ifdef ANTI
if (V == ANTI_VARIANT)
Expand Down
74 changes: 39 additions & 35 deletions src/pawns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,67 @@ namespace {
#define V Value
#define S(mg, eg) make_score(mg, eg)

// Isolated pawn penalty by opposed flag
const Score Isolated[VARIANT_NB][2] = {
{ S(27, 30), S(13, 18) },
// Isolated pawn penalty
const Score Isolated[VARIANT_NB] = {
S(13, 18),
#ifdef ANTI
{ S(50, 80), S(54, 69) },
S(54, 69),
#endif
#ifdef ATOMIC
{ S(27, 28), S(24, 14) },
S(24, 14),
#endif
#ifdef CRAZYHOUSE
{ S(45, 40), S(30, 27) },
S(30, 27),
#endif
#ifdef HORDE
{ S(59, 42), S(16, 38) },
S(16, 38),
#endif
#ifdef KOTH
{ S(45, 40), S(30, 27) },
S(30, 27),
#endif
#ifdef LOSERS
{ S(50, 80), S(54, 69) },
S(54, 69),
#endif
#ifdef RACE
{},
S(0, 0),
#endif
#ifdef RELAY
{ S(45, 40), S(30, 27) },
S(30, 27),
#endif
#ifdef THREECHECK
{ S(45, 40), S(30, 27) },
S(30, 27),
#endif
};

// Backward pawn penalty by opposed flag
const Score Backward[VARIANT_NB][2] = {
{ S(40, 26), S(24, 12) },
// Backward pawn penalty
const Score Backward[VARIANT_NB] = {
S(24, 12),
#ifdef ANTI
{ S(64, 25), S(26, 50) },
S(26, 50),
#endif
#ifdef ATOMIC
{ S(48, 21), S(35, 15) },
S(35, 15),
#endif
#ifdef CRAZYHOUSE
{ S(56, 33), S(41, 19) },
S(41, 19),
#endif
#ifdef HORDE
{ S(44, 24), S(78, 14) },
S(78, 14),
#endif
#ifdef KOTH
{ S(56, 33), S(41, 19) },
S(41, 19),
#endif
#ifdef LOSERS
{ S(64, 25), S(26, 50) },
S(26, 50),
#endif
#ifdef RACE
{},
S(0, 0),
#endif
#ifdef RELAY
{ S(56, 33), S(41, 19) },
S(41, 19),
#endif
#ifdef THREECHECK
{ S(56, 33), S(41, 19) },
S(41, 19),
#endif
};

Expand Down Expand Up @@ -229,14 +229,14 @@ namespace {
#endif
#ifdef THREECHECK
{
{ { V(105), V( 1), V(22), V( 52), V(86), V( 89), V( 98) }, // Not On King file
{ V(116), V( 3), V(55), V(109), V(81), V( 97), V( 99) },
{ V(121), V(23), V(69), V( 93), V(58), V( 88), V(112) },
{ V( 94), V(11), V(52), V( 67), V(90), V( 85), V(112) } },
{ { V(105), V( 1), V(22), V( 52), V(86), V( 89), V( 98) }, // On King file
{ V(116), V( 3), V(55), V(109), V(81), V( 97), V( 99) },
{ V(121), V(23), V(69), V( 93), V(58), V( 88), V(112) },
{ V( 94), V(11), V(52), V( 67), V(90), V( 85), V(112) } }
{ { V(140), V( 11), V(38), V( 26), V( 99), V( 94), V( 95) }, // Not On King file
{ V(104), V( 14), V(28), V(128), V( 86), V(107), V(115) },
{ V(144), V( 59), V(89), V( 97), V( 39), V( 85), V(114) },
{ V(103), V( 24), V(76), V( 96), V(115), V( 98), V(127) } },
{ { V(115), V(-16), V(13), V( 38), V(115), V( 76), V( 92) }, // On King file
{ V(166), V( 20), V(51), V(111), V( 98), V(113), V(114) },
{ V(102), V( 29), V(76), V( 75), V( 60), V( 99), V( 96) },
{ V( 89), V( 18), V(44), V(112), V( 77), V(114), V(115) } }
},
#endif
};
Expand Down Expand Up @@ -440,7 +440,7 @@ namespace {
Bitboard ourPawns = pos.pieces( Us, PAWN);
Bitboard theirPawns = pos.pieces(Them, PAWN);

e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0;
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0;
e->semiopenFiles[Us] = 0xFF;
e->kingSquares[Us] = SQ_NONE;
e->pawnAttacks[Us] = shift<Right>(ourPawns) | shift<Left>(ourPawns);
Expand Down Expand Up @@ -533,10 +533,10 @@ namespace {
score += Connected[pos.variant()][opposed][!!phalanx][popcount(supported)][relative_rank(Us, s)];

else if (!neighbours)
score -= Isolated[pos.variant()][opposed];
score -= Isolated[pos.variant()], e->weakUnopposed[Us] += !opposed;

else if (backward)
score -= Backward[pos.variant()][opposed];
score -= Backward[pos.variant()], e->weakUnopposed[Us] += !opposed;

#ifdef HORDE
if (doubled && (!supported || pos.is_horde()))
Expand Down Expand Up @@ -688,6 +688,10 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));

#ifdef CRAZYHOUSE
if (pos.is_house())
return make_score(bonus, bonus);
#endif
return make_score(bonus, -16 * minKingPawnDistance);
}

Expand Down
2 changes: 2 additions & 0 deletions src/pawns.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct Entry {
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
int weak_unopposed(Color c) const { return weakUnopposed[c]; }
int pawn_asymmetry() const { return asymmetry; }
int open_files() const { return openFiles; }

Expand Down Expand Up @@ -71,6 +72,7 @@ struct Entry {
Bitboard pawnAttacksSpan[COLOR_NB];
Square kingSquares[COLOR_NB];
Score kingSafety[COLOR_NB];
int weakUnopposed[COLOR_NB];
int castlingRights[COLOR_NB];
int semiopenFiles[COLOR_NB];
int pawnsOnSquares[COLOR_NB][COLOR_NB]; // [color][light/dark squares]
Expand Down
9 changes: 4 additions & 5 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Position& Position::set(const string& fenStr, bool isChess960, Variant v, StateI
}
#endif

// Convert from fullmove starting from 1 to ply starting from 0,
// Convert from fullmove starting from 1 to gamePly starting from 0,
// handle also common incorrect FEN with fullmove = 0.
gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);

Expand Down Expand Up @@ -1840,11 +1840,10 @@ bool Position::is_draw(int ply) const {
{
stp = stp->previous->previous;

// At root position ply is 1, so return a draw score if a position
// repeats once earlier but strictly after the root, or repeats twice
// before or at the root.
// Return a draw score if a position repeats once earlier but strictly
// after the root, or repeats twice before or at the root.
if ( stp->key == st->key
&& ++cnt + (ply - 1 > i) == 2)
&& ++cnt + (ply > i) == 2)
return true;
}

Expand Down
Loading

0 comments on commit e1e76da

Please sign in to comment.