mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Removed a bunch of unrelated files
This commit is contained in:
parent
0c3bec7e23
commit
748749dd4a
9 changed files with 1 additions and 2583 deletions
|
@ -34,9 +34,7 @@ BINDIR = $(PREFIX)/bin
|
|||
PGOBENCH = ./$(EXE) bench 16 1 1 default time
|
||||
|
||||
### Object files
|
||||
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
||||
material.o misc.o movegen.o movepick.o pawns.o position.o \
|
||||
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
|
||||
OBJS = main.o bitboard.o
|
||||
|
||||
### ==========================================================================
|
||||
### Section 2. High-level Configuration
|
||||
|
|
|
@ -1,175 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "uci.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
const char* Defaults[] = {
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
|
||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14",
|
||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14",
|
||||
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
|
||||
"r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13",
|
||||
"r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16",
|
||||
"4r1k1/r1q2ppp/ppp2n2/4P3/5Rb1/1N1BQ3/PPP3PP/R5K1 w - - 1 17",
|
||||
"2rqkb1r/ppp2p2/2npb1p1/1N1Nn2p/2P1PP2/8/PP2B1PP/R1BQK2R b KQ - 0 11",
|
||||
"r1bq1r1k/b1p1npp1/p2p3p/1p6/3PP3/1B2NN2/PP3PPP/R2Q1RK1 w - - 1 16",
|
||||
"3r1rk1/p5pp/bpp1pp2/8/q1PP1P2/b3P3/P2NQRPP/1R2B1K1 b - - 6 22",
|
||||
"r1q2rk1/2p1bppp/2Pp4/p6b/Q1PNp3/4B3/PP1R1PPP/2K4R w - - 2 18",
|
||||
"4k2r/1pb2ppp/1p2p3/1R1p4/3P4/2r1PN2/P4PPP/1R4K1 b - - 3 22",
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
||||
"6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
|
||||
"3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
|
||||
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1",
|
||||
"8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
|
||||
"7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
|
||||
"8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
|
||||
"8/1p3pp1/7p/5P1P/2k3P1/8/2K2P2/8 w - - 0 1",
|
||||
"8/pp2r1k1/2p1p3/3pP2p/1P1P1P1P/P5KR/8/8 w - - 0 1",
|
||||
"8/3p4/p1bk3p/Pp6/1Kp1PpPp/2P2P1P/2P5/5B2 b - - 0 1",
|
||||
"5k2/7R/4P2p/5K2/p1r2P1p/8/8/8 b - - 0 1",
|
||||
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
|
||||
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
|
||||
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
|
||||
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1",
|
||||
|
||||
// 5-man positions
|
||||
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
|
||||
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
|
||||
"8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
|
||||
|
||||
// 6-man positions
|
||||
"8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
|
||||
"8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
|
||||
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
|
||||
|
||||
// 7-man positions
|
||||
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
|
||||
/// of positions for a given limit each. There are five parameters: the
|
||||
/// transposition table size, the number of search threads that should
|
||||
/// be used, the limit value spent for each position (optional, default is
|
||||
/// depth 13), an optional file name where to look for positions in FEN
|
||||
/// format (defaults are the positions defined above) and the type of the
|
||||
/// limit value: depth (default), time in secs or number of nodes.
|
||||
|
||||
void benchmark(const Position& current, istream& is) {
|
||||
|
||||
string token;
|
||||
Search::LimitsType limits;
|
||||
vector<string> fens;
|
||||
|
||||
// Assign default values to missing arguments
|
||||
string ttSize = (is >> token) ? token : "16";
|
||||
string threads = (is >> token) ? token : "1";
|
||||
string limit = (is >> token) ? token : "13";
|
||||
string fenFile = (is >> token) ? token : "default";
|
||||
string limitType = (is >> token) ? token : "depth";
|
||||
|
||||
Options["Hash"] = ttSize;
|
||||
Options["Threads"] = threads;
|
||||
TT.clear();
|
||||
|
||||
if (limitType == "time")
|
||||
limits.movetime = 1000 * atoi(limit.c_str()); // movetime is in ms
|
||||
|
||||
else if (limitType == "nodes")
|
||||
limits.nodes = atoi(limit.c_str());
|
||||
|
||||
else if (limitType == "mate")
|
||||
limits.mate = atoi(limit.c_str());
|
||||
|
||||
else
|
||||
limits.depth = atoi(limit.c_str());
|
||||
|
||||
if (fenFile == "default")
|
||||
fens.assign(Defaults, Defaults + 37);
|
||||
|
||||
else if (fenFile == "current")
|
||||
fens.push_back(current.fen());
|
||||
|
||||
else
|
||||
{
|
||||
string fen;
|
||||
ifstream file(fenFile.c_str());
|
||||
|
||||
if (!file.is_open())
|
||||
{
|
||||
cerr << "Unable to open file " << fenFile << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
while (getline(file, fen))
|
||||
if (!fen.empty())
|
||||
fens.push_back(fen);
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
uint64_t nodes = 0;
|
||||
Search::StateStackPtr st;
|
||||
Time::point elapsed = Time::now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); ++i)
|
||||
{
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
||||
|
||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||
|
||||
if (limitType == "perft")
|
||||
nodes += Search::perft<true>(pos, limits.depth * ONE_PLY);
|
||||
|
||||
else
|
||||
{
|
||||
Threads.start_thinking(pos, limits, st);
|
||||
Threads.wait_for_think_finished();
|
||||
nodes += Search::RootPos.nodes_searched();
|
||||
}
|
||||
}
|
||||
|
||||
elapsed = std::max(Time::now() - elapsed, Time::point(1)); // Avoid a 'divide by zero'
|
||||
|
||||
dbg_print(); // Just before to exit
|
||||
|
||||
cerr << "\n==========================="
|
||||
<< "\nTotal time (ms) : " << elapsed
|
||||
<< "\nNodes searched : " << nodes
|
||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||
}
|
175
src/bitbase.cpp
175
src/bitbase.cpp
|
@ -1,175 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7
|
||||
const unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
||||
|
||||
// Each uint32_t stores results of 32 positions, one per bit
|
||||
uint32_t KPKBitbase[MAX_INDEX / 32];
|
||||
|
||||
// A KPK bitbase index is an integer in [0, IndexMax] range
|
||||
//
|
||||
// Information is mapped in a way that minimizes the number of iterations:
|
||||
//
|
||||
// bit 0- 5: white king square (from SQ_A1 to SQ_H8)
|
||||
// bit 6-11: black king square (from SQ_A1 to SQ_H8)
|
||||
// bit 12: side to move (WHITE or BLACK)
|
||||
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
||||
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
|
||||
unsigned index(Color us, Square bksq, Square wksq, Square psq) {
|
||||
return wksq | (bksq << 6) | (us << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
|
||||
}
|
||||
|
||||
enum Result {
|
||||
INVALID = 0,
|
||||
UNKNOWN = 1,
|
||||
DRAW = 2,
|
||||
WIN = 4
|
||||
};
|
||||
|
||||
inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||
|
||||
struct KPKPosition {
|
||||
|
||||
KPKPosition(unsigned idx);
|
||||
operator Result() const { return result; }
|
||||
Result classify(const std::vector<KPKPosition>& db)
|
||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||
|
||||
private:
|
||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||
|
||||
Color us;
|
||||
Square bksq, wksq, psq;
|
||||
Result result;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||
|
||||
assert(file_of(wpsq) <= FILE_D);
|
||||
|
||||
unsigned idx = index(us, bksq, wksq, wpsq);
|
||||
return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
|
||||
}
|
||||
|
||||
|
||||
void Bitbases::init() {
|
||||
|
||||
unsigned idx, repeat = 1;
|
||||
std::vector<KPKPosition> db;
|
||||
db.reserve(MAX_INDEX);
|
||||
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
db.push_back(KPKPosition(idx));
|
||||
|
||||
// Iterate through the positions until none of the unknown positions can be
|
||||
// changed to either wins or draws (15 cycles needed).
|
||||
while (repeat)
|
||||
for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
|
||||
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
|
||||
|
||||
// Map 32 results into one KPKBitbase[] entry
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
if (db[idx] == WIN)
|
||||
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
KPKPosition::KPKPosition(unsigned idx) {
|
||||
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
|
||||
result = UNKNOWN;
|
||||
|
||||
// Check if two pieces are on the same square or if a king can be captured
|
||||
if ( distance(wksq, bksq) <= 1
|
||||
|| wksq == psq
|
||||
|| bksq == psq
|
||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
||||
result = INVALID;
|
||||
|
||||
else if (us == WHITE)
|
||||
{
|
||||
// Immediate win if a pawn can be promoted without getting captured
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( distance(bksq, psq + DELTA_N) > 1
|
||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
||||
result = WIN;
|
||||
}
|
||||
// Immediate draw if it is a stalemate or a king captures undefended pawn
|
||||
else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
||||
result = DRAW;
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||
|
||||
// White to Move: If one move leads to a position classified as WIN, the result
|
||||
// of the current position is WIN. If all moves lead to positions classified
|
||||
// as DRAW, the current position is classified as DRAW, otherwise the current
|
||||
// position is classified as UNKNOWN.
|
||||
//
|
||||
// Black to Move: If one move leads to a position classified as DRAW, the result
|
||||
// of the current position is DRAW. If all moves lead to positions classified
|
||||
// as WIN, the position is classified as WIN, otherwise the current position is
|
||||
// classified as UNKNOWN.
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
||||
|
||||
while (b)
|
||||
r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
|
||||
: db[index(Them, pop_lsb(&b), wksq, psq)];
|
||||
|
||||
if (Us == WHITE && rank_of(psq) < RANK_7)
|
||||
{
|
||||
Square s = psq + DELTA_N;
|
||||
r |= db[index(BLACK, bksq, wksq, s)]; // Single push
|
||||
|
||||
if (rank_of(psq) == RANK_2 && s != wksq && s != bksq)
|
||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
||||
}
|
||||
|
||||
if (Us == WHITE)
|
||||
return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
else
|
||||
return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
}
|
||||
|
||||
} // namespace
|
857
src/endgame.cpp
857
src/endgame.cpp
|
@ -1,857 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "endgame.h"
|
||||
#include "movegen.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace {
|
||||
|
||||
// Table used to drive the king towards the edge of the board
|
||||
// in KX vs K and KQ vs KR endgames.
|
||||
const int PushToEdges[SQUARE_NB] = {
|
||||
100, 90, 80, 70, 70, 80, 90, 100,
|
||||
90, 70, 60, 50, 50, 60, 70, 90,
|
||||
80, 60, 40, 30, 30, 40, 60, 80,
|
||||
70, 50, 30, 20, 20, 30, 50, 70,
|
||||
70, 50, 30, 20, 20, 30, 50, 70,
|
||||
80, 60, 40, 30, 30, 40, 60, 80,
|
||||
90, 70, 60, 50, 50, 60, 70, 90,
|
||||
100, 90, 80, 70, 70, 80, 90, 100,
|
||||
};
|
||||
|
||||
// Table used to drive the king towards a corner square of the
|
||||
// right color in KBN vs K endgames.
|
||||
const int PushToCorners[SQUARE_NB] = {
|
||||
200, 190, 180, 170, 160, 150, 140, 130,
|
||||
190, 180, 170, 160, 150, 140, 130, 140,
|
||||
180, 170, 155, 140, 140, 125, 140, 150,
|
||||
170, 160, 140, 120, 110, 140, 150, 160,
|
||||
160, 150, 140, 110, 120, 140, 160, 170,
|
||||
150, 140, 125, 140, 140, 155, 170, 180,
|
||||
140, 130, 140, 150, 160, 170, 180, 190,
|
||||
130, 140, 150, 160, 170, 180, 190, 200
|
||||
};
|
||||
|
||||
// Tables used to drive a piece towards or away from another piece
|
||||
const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
||||
const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
||||
return pos.non_pawn_material(c) == npm && pos.count<PAWN>(c) == pawnsCnt;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Map the square as if strongSide is white and strongSide's only pawn
|
||||
// is on the left half of the board.
|
||||
Square normalize(const Position& pos, Color strongSide, Square sq) {
|
||||
|
||||
assert(pos.count<PAWN>(strongSide) == 1);
|
||||
|
||||
if (file_of(pos.list<PAWN>(strongSide)[0]) >= FILE_E)
|
||||
sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1
|
||||
|
||||
if (strongSide == BLACK)
|
||||
sq = ~sq;
|
||||
|
||||
return sq;
|
||||
}
|
||||
|
||||
// Get the material key of Position out of the given endgame key code
|
||||
// like "KBPKN". The trick here is to first forge an ad-hoc FEN string
|
||||
// and then let a Position object do the work for us.
|
||||
Key key(const string& code, Color c) {
|
||||
|
||||
assert(code.length() > 0 && code.length() < 8);
|
||||
assert(code[0] == 'K');
|
||||
|
||||
string sides[] = { code.substr(code.find('K', 1)), // Weak
|
||||
code.substr(0, code.find('K', 1)) }; // Strong
|
||||
|
||||
std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
|
||||
|
||||
string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
|
||||
+ sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
|
||||
|
||||
return Position(fen, false, NULL).material_key();
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
void delete_endgame(const typename M::value_type& p) { delete p.second; }
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
/// Endgames members definitions
|
||||
|
||||
Endgames::Endgames() {
|
||||
|
||||
add<KPK>("KPK");
|
||||
add<KNNK>("KNNK");
|
||||
add<KBNK>("KBNK");
|
||||
add<KRKP>("KRKP");
|
||||
add<KRKB>("KRKB");
|
||||
add<KRKN>("KRKN");
|
||||
add<KQKP>("KQKP");
|
||||
add<KQKR>("KQKR");
|
||||
|
||||
add<KNPK>("KNPK");
|
||||
add<KNPKB>("KNPKB");
|
||||
add<KRPKR>("KRPKR");
|
||||
add<KRPKB>("KRPKB");
|
||||
add<KBPKB>("KBPKB");
|
||||
add<KBPKN>("KBPKN");
|
||||
add<KBPPKB>("KBPPKB");
|
||||
add<KRPPKRP>("KRPPKRP");
|
||||
}
|
||||
|
||||
Endgames::~Endgames() {
|
||||
|
||||
for_each(m1.begin(), m1.end(), delete_endgame<M1>);
|
||||
for_each(m2.begin(), m2.end(), delete_endgame<M2>);
|
||||
}
|
||||
|
||||
template<EndgameType E>
|
||||
void Endgames::add(const string& code) {
|
||||
|
||||
map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
|
||||
map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
|
||||
}
|
||||
|
||||
|
||||
/// Mate with KX vs K. This function is used to evaluate positions with
|
||||
/// king and plenty of material vs a lone king. It simply gives the
|
||||
/// attacking side a bonus for driving the defending king towards the edge
|
||||
/// of the board, and for keeping the distance between the two kings small.
|
||||
template<>
|
||||
Value Endgame<KXK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
assert(!pos.checkers()); // Eval is never called when in check
|
||||
|
||||
// Stalemate detection with lone king
|
||||
if (pos.side_to_move() == weakSide && !MoveList<LEGAL>(pos).size())
|
||||
return VALUE_DRAW;
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
|
||||
Value result = pos.non_pawn_material(strongSide)
|
||||
+ pos.count<PAWN>(strongSide) * PawnValueEg
|
||||
+ PushToEdges[loserKSq]
|
||||
+ PushClose[distance(winnerKSq, loserKSq)];
|
||||
|
||||
if ( pos.count<QUEEN>(strongSide)
|
||||
|| pos.count<ROOK>(strongSide)
|
||||
||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
|
||||
||(pos.count<BISHOP>(strongSide) > 1 && opposite_colors(pos.list<BISHOP>(strongSide)[0],
|
||||
pos.list<BISHOP>(strongSide)[1])))
|
||||
result += VALUE_KNOWN_WIN;
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// Mate with KBN vs K. This is similar to KX vs K, but we have to drive the
|
||||
/// defending king towards a corner square of the right color.
|
||||
template<>
|
||||
Value Endgame<KBNK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, KnightValueMg + BishopValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
|
||||
// kbnk_mate_table() tries to drive toward corners A1 or H8. If we have a
|
||||
// bishop that cannot reach the above squares, we flip the kings in order
|
||||
// to drive the enemy toward corners A8 or H1.
|
||||
if (opposite_colors(bishopSq, SQ_A1))
|
||||
{
|
||||
winnerKSq = ~winnerKSq;
|
||||
loserKSq = ~loserKSq;
|
||||
}
|
||||
|
||||
Value result = VALUE_KNOWN_WIN
|
||||
+ PushClose[distance(winnerKSq, loserKSq)]
|
||||
+ PushToCorners[loserKSq];
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KP vs K. This endgame is evaluated with the help of a bitbase.
|
||||
template<>
|
||||
Value Endgame<KPK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, VALUE_ZERO, 1));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
if (!Bitbases::probe(wksq, psq, bksq, us))
|
||||
return VALUE_DRAW;
|
||||
|
||||
Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(psq));
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KP. This is a somewhat tricky endgame to evaluate precisely without
|
||||
/// a bitbase. The function below returns drawish scores when the pawn is
|
||||
/// far advanced with support of the king, while the attacking king is far
|
||||
/// away.
|
||||
template<>
|
||||
Value Endgame<KRKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square wksq = relative_square(strongSide, pos.king_square(strongSide));
|
||||
Square bksq = relative_square(strongSide, pos.king_square(weakSide));
|
||||
Square rsq = relative_square(strongSide, pos.list<ROOK>(strongSide)[0]);
|
||||
Square psq = relative_square(strongSide, pos.list<PAWN>(weakSide)[0]);
|
||||
|
||||
Square queeningSq = make_square(file_of(psq), RANK_1);
|
||||
Value result;
|
||||
|
||||
// If the stronger side's king is in front of the pawn, it's a win
|
||||
if (wksq < psq && file_of(wksq) == file_of(psq))
|
||||
result = RookValueEg - distance(wksq, psq);
|
||||
|
||||
// If the weaker side's king is too far from the pawn and the rook,
|
||||
// it's a win.
|
||||
else if ( distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
|
||||
&& distance(bksq, rsq) >= 3)
|
||||
result = RookValueEg - distance(wksq, psq);
|
||||
|
||||
// If the pawn is far advanced and supported by the defending king,
|
||||
// the position is drawish
|
||||
else if ( rank_of(bksq) <= RANK_3
|
||||
&& distance(bksq, psq) == 1
|
||||
&& rank_of(wksq) >= RANK_4
|
||||
&& distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
|
||||
result = Value(80) - 8 * distance(wksq, psq);
|
||||
|
||||
else
|
||||
result = Value(200) - 8 * ( distance(wksq, psq + DELTA_S)
|
||||
- distance(bksq, psq + DELTA_S)
|
||||
- distance(psq, queeningSq));
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KB. This is very simple, and always returns drawish scores. The
|
||||
/// score is slightly bigger when the defending king is close to the edge.
|
||||
template<>
|
||||
Value Endgame<KRKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Value result = Value(PushToEdges[pos.king_square(weakSide)]);
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KN. The attacking side has slightly better winning chances than
|
||||
/// in KR vs KB, particularly if the king and the knight are far apart.
|
||||
template<>
|
||||
Value Endgame<KRKN>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square bksq = pos.king_square(weakSide);
|
||||
Square bnsq = pos.list<KNIGHT>(weakSide)[0];
|
||||
Value result = Value(PushToEdges[bksq] + PushAway[distance(bksq, bnsq)]);
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KP. In general, this is a win for the stronger side, but there are a
|
||||
/// few important exceptions. A pawn on 7th rank and on the A,C,F or H files
|
||||
/// with a king positioned next to it can be a draw, so in that case, we only
|
||||
/// use the distance between the kings.
|
||||
template<>
|
||||
Value Endgame<KQKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square pawnSq = pos.list<PAWN>(weakSide)[0];
|
||||
|
||||
Value result = Value(PushClose[distance(winnerKSq, loserKSq)]);
|
||||
|
||||
if ( relative_rank(weakSide, pawnSq) != RANK_7
|
||||
|| distance(loserKSq, pawnSq) != 1
|
||||
|| !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
|
||||
result += QueenValueEg - PawnValueEg;
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KR. This is almost identical to KX vs K: We give the attacking
|
||||
/// king a bonus for having the kings close together, and for forcing the
|
||||
/// defending king towards the edge. If we also take care to avoid null move for
|
||||
/// the defending side in the search, this is usually sufficient to win KQ vs KR.
|
||||
template<>
|
||||
Value Endgame<KQKR>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
|
||||
Value result = QueenValueEg
|
||||
- RookValueEg
|
||||
+ PushToEdges[loserKSq]
|
||||
+ PushClose[distance(winnerKSq, loserKSq)];
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// Some cases of trivial draws
|
||||
template<> Value Endgame<KNNK>::operator()(const Position&) const { return VALUE_DRAW; }
|
||||
|
||||
|
||||
/// KB and one or more pawns vs K. It checks for draws with rook pawns and
|
||||
/// a bishop of the wrong color. If such a draw is detected, SCALE_FACTOR_DRAW
|
||||
/// is returned. If not, the return value is SCALE_FACTOR_NONE, i.e. no scaling
|
||||
/// will be used.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(pos.non_pawn_material(strongSide) == BishopValueMg);
|
||||
assert(pos.count<PAWN>(strongSide) >= 1);
|
||||
|
||||
// No assertions about the material of weakSide, because we want draws to
|
||||
// be detected even when the weaker side has some pawns.
|
||||
|
||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||
File pawnFile = file_of(pos.list<PAWN>(strongSide)[0]);
|
||||
|
||||
// All pawns are on a single rook file ?
|
||||
if ( (pawnFile == FILE_A || pawnFile == FILE_H)
|
||||
&& !(pawns & ~file_bb(pawnFile)))
|
||||
{
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square queeningSq = relative_square(strongSide, make_square(pawnFile, RANK_8));
|
||||
Square kingSq = pos.king_square(weakSide);
|
||||
|
||||
if ( opposite_colors(queeningSq, bishopSq)
|
||||
&& distance(queeningSq, kingSq) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
|
||||
// If all the pawns are on the same B or G file, then it's potentially a draw
|
||||
if ( (pawnFile == FILE_B || pawnFile == FILE_G)
|
||||
&& !(pos.pieces(PAWN) & ~file_bb(pawnFile))
|
||||
&& pos.non_pawn_material(weakSide) == 0
|
||||
&& pos.count<PAWN>(weakSide) >= 1)
|
||||
{
|
||||
// Get weakSide pawn that is closest to the home rank
|
||||
Square weakPawnSq = backmost_sq(weakSide, pos.pieces(weakSide, PAWN));
|
||||
|
||||
Square strongKingSq = pos.king_square(strongSide);
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
|
||||
// There's potential for a draw if our pawn is blocked on the 7th rank,
|
||||
// the bishop cannot attack it or they only have one pawn left
|
||||
if ( relative_rank(strongSide, weakPawnSq) == RANK_7
|
||||
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
|
||||
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
|
||||
{
|
||||
int strongKingDist = distance(weakPawnSq, strongKingSq);
|
||||
int weakKingDist = distance(weakPawnSq, weakKingSq);
|
||||
|
||||
// It's a draw if the weak king is on its back two ranks, within 2
|
||||
// squares of the blocking pawn and the strong king is not
|
||||
// closer. (I think this rule only fails in practically
|
||||
// unreachable positions such as 5k1K/6p1/6P1/8/8/3B4/8/8 w
|
||||
// and positions where qsearch will immediately correct the
|
||||
// problem such as 8/4k1p1/6P1/1K6/3B4/8/8/8 w)
|
||||
if ( relative_rank(strongSide, weakKingSq) >= RANK_7
|
||||
&& weakKingDist <= 2
|
||||
&& weakKingDist <= strongKingDist)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
}
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KR and one or more pawns. It tests for fortress draws with a rook on
|
||||
/// the third rank defended by a pawn.
|
||||
template<>
|
||||
ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(pos.count<ROOK>(weakSide) == 1);
|
||||
assert(pos.count<PAWN>(weakSide) >= 1);
|
||||
|
||||
Square kingSq = pos.king_square(weakSide);
|
||||
Square rsq = pos.list<ROOK>(weakSide)[0];
|
||||
|
||||
if ( relative_rank(weakSide, kingSq) <= RANK_2
|
||||
&& relative_rank(weakSide, pos.king_square(strongSide)) >= RANK_4
|
||||
&& relative_rank(weakSide, rsq) == RANK_3
|
||||
&& ( pos.pieces(weakSide, PAWN)
|
||||
& pos.attacks_from<KING>(kingSq)
|
||||
& pos.attacks_from<PAWN>(rsq, strongSide)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KRP vs KR. This function knows a handful of the most important classes of
|
||||
/// drawn positions, but is far from perfect. It would probably be a good idea
|
||||
/// to add more knowledge in the future.
|
||||
///
|
||||
/// It would also be nice to rewrite the actual code for this function,
|
||||
/// which is mostly copied from Glaurung 1.x, and isn't very pretty.
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square wrsq = normalize(pos, strongSide, pos.list<ROOK>(strongSide)[0]);
|
||||
Square wpsq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square brsq = normalize(pos, strongSide, pos.list<ROOK>(weakSide)[0]);
|
||||
|
||||
File f = file_of(wpsq);
|
||||
Rank r = rank_of(wpsq);
|
||||
Square queeningSq = make_square(f, RANK_8);
|
||||
int tempo = (pos.side_to_move() == strongSide);
|
||||
|
||||
// If the pawn is not too far advanced and the defending king defends the
|
||||
// queening square, use the third-rank defence.
|
||||
if ( r <= RANK_5
|
||||
&& distance(bksq, queeningSq) <= 1
|
||||
&& wksq <= SQ_H5
|
||||
&& (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// The defending side saves a draw by checking from behind in case the pawn
|
||||
// has advanced to the 6th rank with the king behind.
|
||||
if ( r == RANK_6
|
||||
&& distance(bksq, queeningSq) <= 1
|
||||
&& rank_of(wksq) + tempo <= RANK_6
|
||||
&& (rank_of(brsq) == RANK_1 || (!tempo && distance(file_of(brsq), f) >= 3)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
if ( r >= RANK_6
|
||||
&& bksq == queeningSq
|
||||
&& rank_of(brsq) == RANK_1
|
||||
&& (!tempo || distance(wksq, wpsq) >= 2))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
|
||||
// and the black rook is behind the pawn.
|
||||
if ( wpsq == SQ_A7
|
||||
&& wrsq == SQ_A8
|
||||
&& (bksq == SQ_H7 || bksq == SQ_G7)
|
||||
&& file_of(brsq) == FILE_A
|
||||
&& (rank_of(brsq) <= RANK_3 || file_of(wksq) >= FILE_D || rank_of(wksq) <= RANK_5))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// If the defending king blocks the pawn and the attacking king is too far
|
||||
// away, it's a draw.
|
||||
if ( r <= RANK_5
|
||||
&& bksq == wpsq + DELTA_N
|
||||
&& distance(wksq, wpsq) - tempo >= 2
|
||||
&& distance(wksq, brsq) - tempo >= 2)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// Pawn on the 7th rank supported by the rook from behind usually wins if the
|
||||
// attacking king is closer to the queening square than the defending king,
|
||||
// and the defending king cannot gain tempi by threatening the attacking rook.
|
||||
if ( r == RANK_7
|
||||
&& f != FILE_A
|
||||
&& file_of(wrsq) == f
|
||||
&& wrsq != queeningSq
|
||||
&& (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
|
||||
&& (distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo))
|
||||
return ScaleFactor(SCALE_FACTOR_MAX - 2 * distance(wksq, queeningSq));
|
||||
|
||||
// Similar to the above, but with the pawn further back
|
||||
if ( f != FILE_A
|
||||
&& file_of(wrsq) == f
|
||||
&& wrsq < wpsq
|
||||
&& (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
|
||||
&& (distance(wksq, wpsq + DELTA_N) < distance(bksq, wpsq + DELTA_N) - 2 + tempo)
|
||||
&& ( distance(bksq, wrsq) + tempo >= 3
|
||||
|| ( distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo
|
||||
&& (distance(wksq, wpsq + DELTA_N) < distance(bksq, wrsq) + tempo))))
|
||||
return ScaleFactor( SCALE_FACTOR_MAX
|
||||
- 8 * distance(wpsq, queeningSq)
|
||||
- 2 * distance(wksq, queeningSq));
|
||||
|
||||
// If the pawn is not far advanced and the defending king is somewhere in
|
||||
// the pawn's path, it's probably a draw.
|
||||
if (r <= RANK_4 && bksq > wpsq)
|
||||
{
|
||||
if (file_of(bksq) == file_of(wpsq))
|
||||
return ScaleFactor(10);
|
||||
if ( distance<File>(bksq, wpsq) == 1
|
||||
&& distance(wksq, bksq) > 2)
|
||||
return ScaleFactor(24 - 2 * distance(wksq, bksq));
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
// Test for a rook pawn
|
||||
if (pos.pieces(PAWN) & (FileABB | FileHBB))
|
||||
{
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Square bsq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
||||
Rank rk = relative_rank(strongSide, psq);
|
||||
Square push = pawn_push(strongSide);
|
||||
|
||||
// If the pawn is on the 5th rank and the pawn (currently) is on
|
||||
// the same color square as the bishop then there is a chance of
|
||||
// a fortress. Depending on the king position give a moderate
|
||||
// reduction or a stronger one if the defending king is near the
|
||||
// corner but not trapped there.
|
||||
if (rk == RANK_5 && !opposite_colors(bsq, psq))
|
||||
{
|
||||
int d = distance(psq + 3 * push, ksq);
|
||||
|
||||
if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push))
|
||||
return ScaleFactor(24);
|
||||
else
|
||||
return ScaleFactor(48);
|
||||
}
|
||||
|
||||
// When the pawn has moved to the 6th rank we can be fairly sure
|
||||
// it's drawn if the bishop attacks the square in front of the
|
||||
// pawn from a reasonable distance and the defending king is near
|
||||
// the corner
|
||||
if ( rk == RANK_6
|
||||
&& distance(psq + 2 * push, ksq) <= 1
|
||||
&& (PseudoAttacks[BISHOP][bsq] & (psq + push))
|
||||
&& distance<File>(bsq, psq) >= 2)
|
||||
return ScaleFactor(8);
|
||||
}
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
/// KRPP vs KRP. There is just a single rule: if the stronger side has no passed
|
||||
/// pawns and the defending king is actively placed, the position is drawish.
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 1));
|
||||
|
||||
Square wpsq1 = pos.list<PAWN>(strongSide)[0];
|
||||
Square wpsq2 = pos.list<PAWN>(strongSide)[1];
|
||||
Square bksq = pos.king_square(weakSide);
|
||||
|
||||
// Does the stronger side have a passed pawn?
|
||||
if (pos.pawn_passed(strongSide, wpsq1) || pos.pawn_passed(strongSide, wpsq2))
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2));
|
||||
|
||||
if ( distance<File>(bksq, wpsq1) <= 1
|
||||
&& distance<File>(bksq, wpsq2) <= 1
|
||||
&& relative_rank(strongSide, bksq) > r)
|
||||
{
|
||||
switch (r) {
|
||||
case RANK_2: return ScaleFactor(10);
|
||||
case RANK_3: return ScaleFactor(10);
|
||||
case RANK_4: return ScaleFactor(15);
|
||||
case RANK_5: return ScaleFactor(20);
|
||||
case RANK_6: return ScaleFactor(40);
|
||||
default: assert(false);
|
||||
}
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// K and two or more pawns vs K. There is just a single rule here: If all pawns
|
||||
/// are on the same rook file and are blocked by the defending king, it's a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(pos.non_pawn_material(strongSide) == VALUE_ZERO);
|
||||
assert(pos.count<PAWN>(strongSide) >= 2);
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
||||
|
||||
// If all pawns are ahead of the king, on a single rook file and
|
||||
// the king is within one file of the pawns, it's a draw.
|
||||
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
|
||||
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
||||
&& distance<File>(ksq, psq) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KBP vs KB. There are two rules: if the defending king is somewhere along the
|
||||
/// path of the pawn, and the square of the king is not of the same color as the
|
||||
/// stronger side's bishop, it's a draw. If the two bishops have opposite color,
|
||||
/// it's almost always a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square weakBishopSq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
|
||||
// Case 1: Defending king blocks the pawn, and cannot be driven away
|
||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||
&& relative_rank(strongSide, pawnSq) < relative_rank(strongSide, weakKingSq)
|
||||
&& ( opposite_colors(weakKingSq, strongBishopSq)
|
||||
|| relative_rank(strongSide, weakKingSq) <= RANK_6))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// Case 2: Opposite colored bishops
|
||||
if (opposite_colors(strongBishopSq, weakBishopSq))
|
||||
{
|
||||
// We assume that the position is drawn in the following three situations:
|
||||
//
|
||||
// a. The pawn is on rank 5 or further back.
|
||||
// b. The defending king is somewhere in the pawn's path.
|
||||
// c. The defending bishop attacks some square along the pawn's path,
|
||||
// and is at least three squares away from the pawn.
|
||||
//
|
||||
// These rules are probably not perfect, but in practice they work
|
||||
// reasonably well.
|
||||
|
||||
if (relative_rank(strongSide, pawnSq) <= RANK_5)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
{
|
||||
Bitboard path = forward_bb(strongSide, pawnSq);
|
||||
|
||||
if (path & pos.pieces(weakSide, KING))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
||||
&& distance(weakBishopSq, pawnSq) >= 3)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KBPP vs KB. It detects a few basic draws with opposite-colored bishops
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square wbsq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square bbsq = pos.list<BISHOP>(weakSide)[0];
|
||||
|
||||
if (!opposite_colors(wbsq, bbsq))
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Square psq1 = pos.list<PAWN>(strongSide)[0];
|
||||
Square psq2 = pos.list<PAWN>(strongSide)[1];
|
||||
Rank r1 = rank_of(psq1);
|
||||
Rank r2 = rank_of(psq2);
|
||||
Square blockSq1, blockSq2;
|
||||
|
||||
if (relative_rank(strongSide, psq1) > relative_rank(strongSide, psq2))
|
||||
{
|
||||
blockSq1 = psq1 + pawn_push(strongSide);
|
||||
blockSq2 = make_square(file_of(psq2), rank_of(psq1));
|
||||
}
|
||||
else
|
||||
{
|
||||
blockSq1 = psq2 + pawn_push(strongSide);
|
||||
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
|
||||
}
|
||||
|
||||
switch (distance<File>(psq1, psq2))
|
||||
{
|
||||
case 0:
|
||||
// Both pawns are on the same file. It's an easy draw if the defender firmly
|
||||
// controls some square in the frontmost pawn's path.
|
||||
if ( file_of(ksq) == file_of(blockSq1)
|
||||
&& relative_rank(strongSide, ksq) >= relative_rank(strongSide, blockSq1)
|
||||
&& opposite_colors(ksq, wbsq))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
case 1:
|
||||
// Pawns on adjacent files. It's a draw if the defender firmly controls the
|
||||
// square in front of the frontmost pawn's path, and the square diagonally
|
||||
// behind this square on the file of the other pawn.
|
||||
if ( ksq == blockSq1
|
||||
&& opposite_colors(ksq, wbsq)
|
||||
&& ( bbsq == blockSq2
|
||||
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakSide, BISHOP))
|
||||
|| distance(r1, r2) >= 2))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
else if ( ksq == blockSq2
|
||||
&& opposite_colors(ksq, wbsq)
|
||||
&& ( bbsq == blockSq1
|
||||
|| (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(weakSide, BISHOP))))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
default:
|
||||
// The pawns are not on the same file or adjacent files. No scaling.
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// KBP vs KN. There is a single rule: If the defending king is somewhere along
|
||||
/// the path of the pawn, and the square of the king is not of the same color as
|
||||
/// the stronger side's bishop, it's a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPKN>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
|
||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||
&& relative_rank(strongSide, pawnSq) < relative_rank(strongSide, weakKingSq)
|
||||
&& ( opposite_colors(weakKingSq, strongBishopSq)
|
||||
|| relative_rank(strongSide, weakKingSq) <= RANK_6))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KNP vs K. There is a single rule: if the pawn is a rook pawn on the 7th rank
|
||||
/// and the defending king prevents the pawn from advancing, the position is drawn.
|
||||
template<>
|
||||
ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, KnightValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
|
||||
if (pawnSq == SQ_A7 && distance(SQ_A8, weakKingSq) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KNP vs KB. If knight can block bishop from taking pawn, it's a win.
|
||||
/// Otherwise the position is drawn.
|
||||
template<>
|
||||
ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square bishopSq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
|
||||
// King needs to get close to promoting pawn to prevent knight from blocking.
|
||||
// Rules for this are very tricky, so just approximate.
|
||||
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
||||
return ScaleFactor(distance(weakKingSq, pawnSq));
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KP vs KP. This is done by removing the weakest side's pawn and probing the
|
||||
/// KP vs K bitbase: If the weakest side has a draw without the pawn, it probably
|
||||
/// has at least a draw with the pawn as well. The exception is when the stronger
|
||||
/// side's pawn is far advanced and not on a rook file; in this case it is often
|
||||
/// possible to win (e.g. 8/4k3/3p4/3P4/6K1/8/8/8 w - - 0 1).
|
||||
template<>
|
||||
ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, VALUE_ZERO, 1));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
// If the pawn has advanced to the fifth rank or further, and is not a
|
||||
// rook pawn, it's too dangerous to assume that it's at least a draw.
|
||||
if (rank_of(psq) >= RANK_5 && file_of(psq) != FILE_A)
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw,
|
||||
// it's probably at least a draw even with the pawn.
|
||||
return Bitbases::probe(wksq, psq, bksq, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
||||
}
|
121
src/endgame.h
121
src/endgame.h
|
@ -1,121 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifndef ENDGAME_H_INCLUDED
|
||||
#define ENDGAME_H_INCLUDED
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
/// EndgameType lists all supported endgames
|
||||
|
||||
enum EndgameType {
|
||||
|
||||
// Evaluation functions
|
||||
|
||||
KNNK, // KNN vs K
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
KRKP, // KR vs KP
|
||||
KRKB, // KR vs KB
|
||||
KRKN, // KR vs KN
|
||||
KQKP, // KQ vs KP
|
||||
KQKR, // KQ vs KR
|
||||
|
||||
|
||||
// Scaling functions
|
||||
SCALE_FUNS,
|
||||
|
||||
KBPsK, // KB and pawns vs K
|
||||
KQKRPs, // KQ vs KR and pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPKB, // KRP vs KB
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // K and pawns vs K
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KNPK, // KNP vs K
|
||||
KNPKB, // KNP vs KB
|
||||
KPKP // KP vs KP
|
||||
};
|
||||
|
||||
|
||||
/// Endgame functions can be of two types depending on whether they return a
|
||||
/// Value or a ScaleFactor. Type eg_fun<int>::type returns either ScaleFactor
|
||||
/// or Value depending on whether the template parameter is 0 or 1.
|
||||
|
||||
template<int> struct eg_fun { typedef Value type; };
|
||||
template<> struct eg_fun<1> { typedef ScaleFactor type; };
|
||||
|
||||
|
||||
/// Base and derived templates for endgame evaluation and scaling functions
|
||||
|
||||
template<typename T>
|
||||
struct EndgameBase {
|
||||
|
||||
virtual ~EndgameBase() {}
|
||||
virtual Color strong_side() const = 0;
|
||||
virtual T operator()(const Position&) const = 0;
|
||||
};
|
||||
|
||||
|
||||
template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
|
||||
struct Endgame : public EndgameBase<T> {
|
||||
|
||||
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
|
||||
Color strong_side() const { return strongSide; }
|
||||
T operator()(const Position&) const;
|
||||
|
||||
private:
|
||||
const Color strongSide, weakSide;
|
||||
};
|
||||
|
||||
|
||||
/// The Endgames class stores the pointers to endgame evaluation and scaling
|
||||
/// base objects in two std::map typedefs. We then use polymorphism to invoke
|
||||
/// the actual endgame function by calling its virtual operator().
|
||||
|
||||
class Endgames {
|
||||
|
||||
typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
|
||||
typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
|
||||
|
||||
M1 m1;
|
||||
M2 m2;
|
||||
|
||||
M1& map(M1::mapped_type) { return m1; }
|
||||
M2& map(M2::mapped_type) { return m2; }
|
||||
|
||||
template<EndgameType E> void add(const std::string& code);
|
||||
|
||||
public:
|
||||
Endgames();
|
||||
~Endgames();
|
||||
|
||||
template<typename T> T probe(Key key, T& eg)
|
||||
{ return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
|
||||
};
|
||||
|
||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
905
src/evaluate.cpp
905
src/evaluate.cpp
|
@ -1,905 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring> // For std::memset
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include "bitcount.h"
|
||||
#include "evaluate.h"
|
||||
#include "material.h"
|
||||
#include "pawns.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Struct EvalInfo contains various information computed and collected
|
||||
// by the evaluation functions.
|
||||
struct EvalInfo {
|
||||
|
||||
// Pointers to material and pawn hash table entries
|
||||
Material::Entry* mi;
|
||||
Pawns::Entry* pi;
|
||||
|
||||
// attackedBy[color][piece type] is a bitboard representing all squares
|
||||
// attacked by a given color and piece type, attackedBy[color][ALL_PIECES]
|
||||
// contains all squares attacked by the given color.
|
||||
Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB];
|
||||
|
||||
// kingRing[color] is the zone around the king which is considered
|
||||
// by the king safety evaluation. This consists of the squares directly
|
||||
// adjacent to the king, and the three (or two, for a king on an edge file)
|
||||
// squares two ranks in front of the king. For instance, if black's king
|
||||
// is on g8, kingRing[BLACK] is a bitboard containing the squares f8, h8,
|
||||
// f7, g7, h7, f6, g6 and h6.
|
||||
Bitboard kingRing[COLOR_NB];
|
||||
|
||||
// kingAttackersCount[color] is the number of pieces of the given color
|
||||
// which attack a square in the kingRing of the enemy king.
|
||||
int kingAttackersCount[COLOR_NB];
|
||||
|
||||
// kingAttackersWeight[color] is the sum of the "weight" of the pieces of the
|
||||
// given color which attack a square in the kingRing of the enemy king. The
|
||||
// weights of the individual piece types are given by the elements in the
|
||||
// KingAttackWeights array.
|
||||
int kingAttackersWeight[COLOR_NB];
|
||||
|
||||
// kingAdjacentZoneAttacksCount[color] is the number of attacks to squares
|
||||
// directly adjacent to the king of the given color. Pieces which attack
|
||||
// more than one square are counted multiple times. For instance, if black's
|
||||
// king is on g8 and there's a white knight on g5, this knight adds
|
||||
// 2 to kingAdjacentZoneAttacksCount[BLACK].
|
||||
int kingAdjacentZoneAttacksCount[COLOR_NB];
|
||||
|
||||
Bitboard pinnedPieces[COLOR_NB];
|
||||
};
|
||||
|
||||
namespace Tracing {
|
||||
|
||||
enum Terms { // First 8 entries are for PieceType
|
||||
MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, TOTAL, TERMS_NB
|
||||
};
|
||||
|
||||
Score scores[COLOR_NB][TERMS_NB];
|
||||
EvalInfo ei;
|
||||
ScaleFactor sf;
|
||||
|
||||
double to_cp(Value v);
|
||||
void write(int idx, Color c, Score s);
|
||||
void write(int idx, Score w, Score b = SCORE_ZERO);
|
||||
void print(std::stringstream& ss, const char* name, int idx);
|
||||
std::string do_trace(const Position& pos);
|
||||
}
|
||||
|
||||
// Evaluation weights, indexed by evaluation term
|
||||
enum { Mobility, PawnStructure, PassedPawns, Space, KingSafety };
|
||||
const struct Weight { int mg, eg; } Weights[] = {
|
||||
{289, 344}, {233, 201}, {221, 273}, {46, 0}, {321, 0}
|
||||
};
|
||||
|
||||
#define V(v) Value(v)
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// MobilityBonus[PieceType][attacked] contains bonuses for middle and end
|
||||
// game, indexed by piece type and number of attacked squares not occupied by
|
||||
// friendly pieces.
|
||||
const Score MobilityBonus[][32] = {
|
||||
{}, {},
|
||||
{ S(-65,-50), S(-42,-30), S(-9,-10), S( 3, 0), S(15, 10), S(27, 20), // Knights
|
||||
S( 37, 28), S( 42, 31), S(44, 33) },
|
||||
{ S(-52,-47), S(-28,-23), S( 6, 1), S(20, 15), S(34, 29), S(48, 43), // Bishops
|
||||
S( 60, 55), S( 68, 63), S(74, 68), S(77, 72), S(80, 75), S(82, 77),
|
||||
S( 84, 79), S( 86, 81) },
|
||||
{ S(-47,-53), S(-31,-26), S(-5, 0), S( 1, 16), S( 7, 32), S(13, 48), // Rooks
|
||||
S( 18, 64), S( 22, 80), S(26, 96), S(29,109), S(31,115), S(33,119),
|
||||
S( 35,122), S( 36,123), S(37,124) },
|
||||
{ S(-42,-40), S(-28,-23), S(-5, -7), S( 0, 0), S( 6, 10), S(11, 19), // Queens
|
||||
S( 13, 29), S( 18, 38), S(20, 40), S(21, 41), S(22, 41), S(22, 41),
|
||||
S( 22, 41), S( 23, 41), S(24, 41), S(25, 41), S(25, 41), S(25, 41),
|
||||
S( 25, 41), S( 25, 41), S(25, 41), S(25, 41), S(25, 41), S(25, 41),
|
||||
S( 25, 41), S( 25, 41), S(25, 41), S(25, 41) }
|
||||
};
|
||||
|
||||
// Outpost[PieceType][Square] contains bonuses for knights and bishops outposts,
|
||||
// indexed by piece type and square (from white's point of view).
|
||||
const Value Outpost[][SQUARE_NB] = {
|
||||
{// A B C D E F G H
|
||||
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Knights
|
||||
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
|
||||
V(0), V(0), V(4), V(8), V(8), V(4), V(0), V(0),
|
||||
V(0), V(4),V(17),V(26),V(26),V(17), V(4), V(0),
|
||||
V(0), V(8),V(26),V(35),V(35),V(26), V(8), V(0),
|
||||
V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0) },
|
||||
{
|
||||
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Bishops
|
||||
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
|
||||
V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0),
|
||||
V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0),
|
||||
V(0),V(10),V(21),V(21),V(21),V(21),V(10), V(0),
|
||||
V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0) }
|
||||
};
|
||||
|
||||
// Threat[defended/weak][minor/major attacking][attacked PieceType] contains
|
||||
// bonuses according to which piece type attacks which one.
|
||||
const Score Threat[][2][PIECE_TYPE_NB] = {
|
||||
{ { S(0, 0), S( 0, 0), S(19, 37), S(24, 37), S(44, 97), S(35,106) }, // Defended Minor
|
||||
{ S(0, 0), S( 0, 0), S( 9, 14), S( 9, 14), S( 7, 14), S(24, 48) } }, // Defended Major
|
||||
{ { S(0, 0), S( 0,32), S(33, 41), S(31, 50), S(41,100), S(35,104) }, // Weak Minor
|
||||
{ S(0, 0), S( 0,27), S(26, 57), S(26, 57), S(0 , 43), S(23, 51) } } // Weak Major
|
||||
};
|
||||
|
||||
// ThreatenedByPawn[PieceType] contains a penalty according to which piece
|
||||
// type is attacked by an enemy pawn.
|
||||
const Score ThreatenedByPawn[] = {
|
||||
S(0, 0), S(0, 0), S(87, 118), S(84, 122), S(114, 203), S(121, 217)
|
||||
};
|
||||
|
||||
// Assorted bonuses and penalties used by evaluation
|
||||
const Score KingOnOne = S( 2, 58);
|
||||
const Score KingOnMany = S( 6,125);
|
||||
const Score RookOnPawn = S( 7, 27);
|
||||
const Score RookOnOpenFile = S(43, 21);
|
||||
const Score RookOnSemiOpenFile = S(19, 10);
|
||||
const Score BishopPawns = S( 8, 12);
|
||||
const Score MinorBehindPawn = S(16, 0);
|
||||
const Score TrappedRook = S(92, 0);
|
||||
const Score Unstoppable = S( 0, 20);
|
||||
const Score Hanging = S(31, 26);
|
||||
|
||||
// Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
|
||||
// a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only
|
||||
// happen in Chess960 games.
|
||||
const Score TrappedBishopA1H1 = S(50, 50);
|
||||
|
||||
#undef S
|
||||
#undef V
|
||||
|
||||
// SpaceMask[Color] contains the area of the board which is considered
|
||||
// by the space evaluation. In the middlegame, each side is given a bonus
|
||||
// based on how many squares inside this area are safe and available for
|
||||
// friendly minor pieces.
|
||||
const Bitboard SpaceMask[] = {
|
||||
(FileCBB | FileDBB | FileEBB | FileFBB) & (Rank2BB | Rank3BB | Rank4BB),
|
||||
(FileCBB | FileDBB | FileEBB | FileFBB) & (Rank7BB | Rank6BB | Rank5BB)
|
||||
};
|
||||
|
||||
// King danger constants and variables. The king danger scores are looked-up
|
||||
// in KingDanger[]. Various little "meta-bonuses" measuring the strength
|
||||
// of the enemy attack are added up into an integer, which is used as an
|
||||
// index to KingDanger[].
|
||||
//
|
||||
// KingAttackWeights[PieceType] contains king attack weights by piece type
|
||||
const int KingAttackWeights[] = { 0, 0, 6, 2, 5, 5 };
|
||||
|
||||
// Bonuses for enemy's safe checks
|
||||
const int QueenContactCheck = 92;
|
||||
const int RookContactCheck = 68;
|
||||
const int QueenCheck = 50;
|
||||
const int RookCheck = 36;
|
||||
const int BishopCheck = 7;
|
||||
const int KnightCheck = 14;
|
||||
|
||||
// KingDanger[attackUnits] contains the actual king danger weighted
|
||||
// scores, indexed by a calculated integer number.
|
||||
Score KingDanger[512];
|
||||
|
||||
// apply_weight() weighs score 's' by weight 'w' trying to prevent overflow
|
||||
Score apply_weight(Score s, const Weight& w) {
|
||||
return make_score(mg_value(s) * w.mg / 256, eg_value(s) * w.eg / 256);
|
||||
}
|
||||
|
||||
|
||||
// init_eval_info() initializes king bitboards for given color adding
|
||||
// pawn attacks. To be done at the beginning of the evaluation.
|
||||
|
||||
template<Color Us>
|
||||
void init_eval_info(const Position& pos, EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square Down = (Us == WHITE ? DELTA_S : DELTA_N);
|
||||
|
||||
ei.pinnedPieces[Us] = pos.pinned_pieces(Us);
|
||||
|
||||
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
|
||||
ei.attackedBy[Us][ALL_PIECES] = ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
|
||||
|
||||
// Init king safety tables only if we are going to use them
|
||||
if (pos.non_pawn_material(Us) >= QueenValueMg)
|
||||
{
|
||||
ei.kingRing[Them] = b | shift_bb<Down>(b);
|
||||
b &= ei.attackedBy[Us][PAWN];
|
||||
ei.kingAttackersCount[Us] = b ? popcount<Max15>(b) : 0;
|
||||
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
|
||||
}
|
||||
else
|
||||
ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0;
|
||||
}
|
||||
|
||||
|
||||
// evaluate_outpost() evaluates bishop and knight outpost squares
|
||||
|
||||
template<PieceType Pt, Color Us>
|
||||
Score evaluate_outpost(const Position& pos, const EvalInfo& ei, Square s) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
assert (Pt == BISHOP || Pt == KNIGHT);
|
||||
|
||||
// Initial bonus based on square
|
||||
Value bonus = Outpost[Pt == BISHOP][relative_square(Us, s)];
|
||||
|
||||
// Increase bonus if supported by pawn, especially if the opponent has
|
||||
// no minor piece which can trade with the outpost piece.
|
||||
if (bonus && (ei.attackedBy[Us][PAWN] & s))
|
||||
{
|
||||
if ( !pos.pieces(Them, KNIGHT)
|
||||
&& !(squares_of_color(s) & pos.pieces(Them, BISHOP)))
|
||||
bonus += bonus + bonus / 2;
|
||||
else
|
||||
bonus += bonus / 2;
|
||||
}
|
||||
|
||||
return make_score(bonus * 2, bonus / 2);
|
||||
}
|
||||
|
||||
|
||||
// evaluate_pieces() assigns bonuses and penalties to the pieces of a given color
|
||||
|
||||
template<PieceType Pt, Color Us, bool Trace>
|
||||
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score* mobility, Bitboard* mobilityArea) {
|
||||
|
||||
Bitboard b;
|
||||
Square s;
|
||||
Score score = SCORE_ZERO;
|
||||
|
||||
const PieceType NextPt = (Us == WHITE ? Pt : PieceType(Pt + 1));
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square* pl = pos.list<Pt>(Us);
|
||||
|
||||
ei.attackedBy[Us][Pt] = 0;
|
||||
|
||||
while ((s = *pl++) != SQ_NONE)
|
||||
{
|
||||
// Find attacked squares, including x-ray attacks for bishops and rooks
|
||||
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(Us, QUEEN))
|
||||
: Pt == ROOK ? attacks_bb< ROOK>(s, pos.pieces() ^ pos.pieces(Us, ROOK, QUEEN))
|
||||
: pos.attacks_from<Pt>(s);
|
||||
|
||||
if (ei.pinnedPieces[Us] & s)
|
||||
b &= LineBB[pos.king_square(Us)][s];
|
||||
|
||||
ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][Pt] |= b;
|
||||
|
||||
if (b & ei.kingRing[Them])
|
||||
{
|
||||
ei.kingAttackersCount[Us]++;
|
||||
ei.kingAttackersWeight[Us] += KingAttackWeights[Pt];
|
||||
Bitboard bb = b & ei.attackedBy[Them][KING];
|
||||
if (bb)
|
||||
ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb);
|
||||
}
|
||||
|
||||
if (Pt == QUEEN)
|
||||
b &= ~( ei.attackedBy[Them][KNIGHT]
|
||||
| ei.attackedBy[Them][BISHOP]
|
||||
| ei.attackedBy[Them][ROOK]);
|
||||
|
||||
int mob = Pt != QUEEN ? popcount<Max15>(b & mobilityArea[Us])
|
||||
: popcount<Full >(b & mobilityArea[Us]);
|
||||
|
||||
mobility[Us] += MobilityBonus[Pt][mob];
|
||||
|
||||
// Decrease score if we are attacked by an enemy pawn. The remaining part
|
||||
// of threat evaluation must be done later when we have full attack info.
|
||||
if (ei.attackedBy[Them][PAWN] & s)
|
||||
score -= ThreatenedByPawn[Pt];
|
||||
|
||||
if (Pt == BISHOP || Pt == KNIGHT)
|
||||
{
|
||||
// Bonus for outpost square
|
||||
if (!(pos.pieces(Them, PAWN) & pawn_attack_span(Us, s)))
|
||||
score += evaluate_outpost<Pt, Us>(pos, ei, s);
|
||||
|
||||
// Bonus when behind a pawn
|
||||
if ( relative_rank(Us, s) < RANK_5
|
||||
&& (pos.pieces(PAWN) & (s + pawn_push(Us))))
|
||||
score += MinorBehindPawn;
|
||||
|
||||
// Penalty for pawns on same color square of bishop
|
||||
if (Pt == BISHOP)
|
||||
score -= BishopPawns * ei.pi->pawns_on_same_color_squares(Us, s);
|
||||
|
||||
// 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.
|
||||
if ( Pt == BISHOP
|
||||
&& pos.is_chess960()
|
||||
&& (s == relative_square(Us, SQ_A1) || s == relative_square(Us, SQ_H1)))
|
||||
{
|
||||
Square d = pawn_push(Us) + (file_of(s) == FILE_A ? DELTA_E : DELTA_W);
|
||||
if (pos.piece_on(s + d) == make_piece(Us, PAWN))
|
||||
score -= !pos.empty(s + d + pawn_push(Us)) ? TrappedBishopA1H1 * 4
|
||||
: pos.piece_on(s + d + d) == make_piece(Us, PAWN) ? TrappedBishopA1H1 * 2
|
||||
: TrappedBishopA1H1;
|
||||
}
|
||||
}
|
||||
|
||||
if (Pt == ROOK)
|
||||
{
|
||||
// Bonus for aligning with enemy pawns on the same rank/file
|
||||
if (relative_rank(Us, s) >= RANK_5)
|
||||
{
|
||||
Bitboard alignedPawns = pos.pieces(Them, PAWN) & PseudoAttacks[ROOK][s];
|
||||
if (alignedPawns)
|
||||
score += popcount<Max15>(alignedPawns) * RookOnPawn;
|
||||
}
|
||||
|
||||
// Bonus when on an open or semi-open file
|
||||
if (ei.pi->semiopen_file(Us, file_of(s)))
|
||||
score += ei.pi->semiopen_file(Them, file_of(s)) ? RookOnOpenFile : RookOnSemiOpenFile;
|
||||
|
||||
// Penalize when trapped by the king, even more if king cannot castle
|
||||
if (mob <= 3 && !ei.pi->semiopen_file(Us, file_of(s)))
|
||||
{
|
||||
Square ksq = pos.king_square(Us);
|
||||
|
||||
if ( ((file_of(ksq) < FILE_E) == (file_of(s) < file_of(ksq)))
|
||||
&& (rank_of(ksq) == rank_of(s) || relative_rank(Us, ksq) == RANK_1)
|
||||
&& !ei.pi->semiopen_side(Us, file_of(ksq), file_of(s) < file_of(ksq)))
|
||||
score -= (TrappedRook - make_score(mob * 22, 0)) * (1 + !pos.can_castle(Us));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Pt, Us, score);
|
||||
|
||||
// Recursively call evaluate_pieces() of next piece type until KING excluded
|
||||
return score - evaluate_pieces<NextPt, Them, Trace>(pos, ei, mobility, mobilityArea);
|
||||
}
|
||||
|
||||
template<>
|
||||
Score evaluate_pieces<KING, WHITE, false>(const Position&, EvalInfo&, Score*, Bitboard*) { return SCORE_ZERO; }
|
||||
template<>
|
||||
Score evaluate_pieces<KING, WHITE, true>(const Position&, EvalInfo&, Score*, Bitboard*) { return SCORE_ZERO; }
|
||||
|
||||
|
||||
// evaluate_king() assigns bonuses and penalties to a king of a given color
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
Score evaluate_king(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard undefended, b, b1, b2, safe;
|
||||
int attackUnits;
|
||||
const Square ksq = pos.king_square(Us);
|
||||
|
||||
// King shelter and enemy pawns storm
|
||||
Score score = ei.pi->king_safety<Us>(pos, ksq);
|
||||
|
||||
// Main king safety evaluation
|
||||
if (ei.kingAttackersCount[Them])
|
||||
{
|
||||
// Find the attacked squares around the king which have no defenders
|
||||
// apart from the king itself
|
||||
undefended = ei.attackedBy[Them][ALL_PIECES]
|
||||
& ei.attackedBy[Us][KING]
|
||||
& ~( ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
|
||||
| ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
|
||||
| ei.attackedBy[Us][QUEEN]);
|
||||
|
||||
// Initialize the 'attackUnits' variable, which is used later on as an
|
||||
// index into the KingDanger[] array. The initial value is based on the
|
||||
// number and types of the enemy's attacking pieces, the number of
|
||||
// attacked and undefended squares around our king and the quality of
|
||||
// the pawn shelter (current 'score' value).
|
||||
attackUnits = std::min(77, ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them])
|
||||
+ 10 * ei.kingAdjacentZoneAttacksCount[Them]
|
||||
+ 19 * popcount<Max15>(undefended)
|
||||
+ 9 * (ei.pinnedPieces[Us] != 0)
|
||||
- mg_value(score) * 63 / 512
|
||||
- !pos.count<QUEEN>(Them) * 60;
|
||||
|
||||
// Analyse the enemy's safe queen contact checks. Firstly, find the
|
||||
// undefended squares around the king reachable by the enemy queen...
|
||||
b = undefended & ei.attackedBy[Them][QUEEN] & ~pos.pieces(Them);
|
||||
if (b)
|
||||
{
|
||||
// ...and then remove squares not supported by another enemy piece
|
||||
b &= ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT]
|
||||
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK];
|
||||
|
||||
if (b)
|
||||
attackUnits += QueenContactCheck * popcount<Max15>(b);
|
||||
}
|
||||
|
||||
// Analyse the enemy's safe rook contact checks. Firstly, find the
|
||||
// undefended squares around the king reachable by the enemy rooks...
|
||||
b = undefended & ei.attackedBy[Them][ROOK] & ~pos.pieces(Them);
|
||||
|
||||
// Consider only squares where the enemy's rook gives check
|
||||
b &= PseudoAttacks[ROOK][ksq];
|
||||
|
||||
if (b)
|
||||
{
|
||||
// ...and then remove squares not supported by another enemy piece
|
||||
b &= ( ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT]
|
||||
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
|
||||
|
||||
if (b)
|
||||
attackUnits += RookContactCheck * popcount<Max15>(b);
|
||||
}
|
||||
|
||||
// Analyse the enemy's safe distance checks for sliders and knights
|
||||
safe = ~(ei.attackedBy[Us][ALL_PIECES] | pos.pieces(Them));
|
||||
|
||||
b1 = pos.attacks_from<ROOK >(ksq) & safe;
|
||||
b2 = pos.attacks_from<BISHOP>(ksq) & safe;
|
||||
|
||||
// Enemy queen safe checks
|
||||
b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
|
||||
if (b)
|
||||
attackUnits += QueenCheck * popcount<Max15>(b);
|
||||
|
||||
// Enemy rooks safe checks
|
||||
b = b1 & ei.attackedBy[Them][ROOK];
|
||||
if (b)
|
||||
attackUnits += RookCheck * popcount<Max15>(b);
|
||||
|
||||
// Enemy bishops safe checks
|
||||
b = b2 & ei.attackedBy[Them][BISHOP];
|
||||
if (b)
|
||||
attackUnits += BishopCheck * popcount<Max15>(b);
|
||||
|
||||
// Enemy knights safe checks
|
||||
b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
|
||||
if (b)
|
||||
attackUnits += KnightCheck * popcount<Max15>(b);
|
||||
|
||||
// Finally, extract the king danger score from the KingDanger[]
|
||||
// array and subtract the score from evaluation.
|
||||
score -= KingDanger[std::max(std::min(attackUnits, 399), 0)];
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(KING, Us, score);
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
// evaluate_threats() assigns bonuses according to the type of attacking piece
|
||||
// and the type of attacked one.
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
Score evaluate_threats(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
enum { Defended, Weak };
|
||||
enum { Minor, Major };
|
||||
|
||||
Bitboard b, weak, defended;
|
||||
Score score = SCORE_ZERO;
|
||||
|
||||
// Non-pawn enemies defended by a pawn
|
||||
defended = (pos.pieces(Them) ^ pos.pieces(Them, PAWN))
|
||||
& ei.attackedBy[Them][PAWN];
|
||||
|
||||
// Add a bonus according to the kind of attacking pieces
|
||||
if (defended)
|
||||
{
|
||||
b = defended & (ei.attackedBy[Us][KNIGHT] | ei.attackedBy[Us][BISHOP]);
|
||||
while (b)
|
||||
score += Threat[Defended][Minor][type_of(pos.piece_on(pop_lsb(&b)))];
|
||||
|
||||
b = defended & (ei.attackedBy[Us][ROOK]);
|
||||
while (b)
|
||||
score += Threat[Defended][Major][type_of(pos.piece_on(pop_lsb(&b)))];
|
||||
}
|
||||
|
||||
// Enemies not defended by a pawn and under our attack
|
||||
weak = pos.pieces(Them)
|
||||
& ~ei.attackedBy[Them][PAWN]
|
||||
& ei.attackedBy[Us][ALL_PIECES];
|
||||
|
||||
// Add a bonus according to the kind of attacking pieces
|
||||
if (weak)
|
||||
{
|
||||
b = weak & (ei.attackedBy[Us][KNIGHT] | ei.attackedBy[Us][BISHOP]);
|
||||
while (b)
|
||||
score += Threat[Weak][Minor][type_of(pos.piece_on(pop_lsb(&b)))];
|
||||
|
||||
b = weak & (ei.attackedBy[Us][ROOK] | ei.attackedBy[Us][QUEEN]);
|
||||
while (b)
|
||||
score += Threat[Weak][Major][type_of(pos.piece_on(pop_lsb(&b)))];
|
||||
|
||||
b = weak & ~ei.attackedBy[Them][ALL_PIECES];
|
||||
if (b)
|
||||
score += Hanging * popcount<Max15>(b);
|
||||
|
||||
b = weak & ei.attackedBy[Us][KING];
|
||||
if (b)
|
||||
score += more_than_one(b) ? KingOnMany : KingOnOne;
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Tracing::THREAT, Us, score);
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
// evaluate_passed_pawns() evaluates the passed pawns of the given color
|
||||
|
||||
template<Color Us, bool Trace>
|
||||
Score evaluate_passed_pawns(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard b, squaresToQueen, defendedSquares, unsafeSquares;
|
||||
Score score = SCORE_ZERO;
|
||||
|
||||
b = ei.pi->passed_pawns(Us);
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_lsb(&b);
|
||||
|
||||
assert(pos.pawn_passed(Us, s));
|
||||
|
||||
int r = relative_rank(Us, s) - RANK_2;
|
||||
int rr = r * (r - 1);
|
||||
|
||||
// Base bonus based on rank
|
||||
Value mbonus = Value(17 * rr), ebonus = Value(7 * (rr + r + 1));
|
||||
|
||||
if (rr)
|
||||
{
|
||||
Square blockSq = s + pawn_push(Us);
|
||||
|
||||
// Adjust bonus based on the king's proximity
|
||||
ebonus += distance(pos.king_square(Them), blockSq) * 5 * rr
|
||||
- distance(pos.king_square(Us ), blockSq) * 2 * rr;
|
||||
|
||||
// If blockSq is not the queening square then consider also a second push
|
||||
if (relative_rank(Us, blockSq) != RANK_8)
|
||||
ebonus -= distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr;
|
||||
|
||||
// If the pawn is free to advance, then increase the bonus
|
||||
if (pos.empty(blockSq))
|
||||
{
|
||||
// If there is a rook or queen attacking/defending the pawn from behind,
|
||||
// consider all the squaresToQueen. Otherwise consider only the squares
|
||||
// in the pawn's path attacked or occupied by the enemy.
|
||||
defendedSquares = unsafeSquares = squaresToQueen = forward_bb(Us, s);
|
||||
|
||||
Bitboard bb = forward_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from<ROOK>(s);
|
||||
|
||||
if (!(pos.pieces(Us) & bb))
|
||||
defendedSquares &= ei.attackedBy[Us][ALL_PIECES];
|
||||
|
||||
if (!(pos.pieces(Them) & bb))
|
||||
unsafeSquares &= ei.attackedBy[Them][ALL_PIECES] | pos.pieces(Them);
|
||||
|
||||
// If there aren't any enemy attacks, assign a big bonus. Otherwise
|
||||
// assign a smaller bonus if the block square isn't attacked.
|
||||
int k = !unsafeSquares ? 15 : !(unsafeSquares & blockSq) ? 9 : 0;
|
||||
|
||||
// If the path to queen is fully defended, assign a big bonus.
|
||||
// Otherwise assign a smaller bonus if the block square is defended.
|
||||
if (defendedSquares == squaresToQueen)
|
||||
k += 6;
|
||||
|
||||
else if (defendedSquares & blockSq)
|
||||
k += 4;
|
||||
|
||||
mbonus += k * rr, ebonus += k * rr;
|
||||
}
|
||||
else if (pos.pieces(Us) & blockSq)
|
||||
mbonus += rr * 3 + r * 2 + 3, ebonus += rr + r * 2;
|
||||
} // rr != 0
|
||||
|
||||
if (pos.count<PAWN>(Us) < pos.count<PAWN>(Them))
|
||||
ebonus += ebonus / 4;
|
||||
|
||||
score += make_score(mbonus, ebonus);
|
||||
}
|
||||
|
||||
if (Trace)
|
||||
Tracing::write(Tracing::PASSED, Us, apply_weight(score, Weights[PassedPawns]));
|
||||
|
||||
// Add the scores to the middlegame and endgame eval
|
||||
return apply_weight(score, Weights[PassedPawns]);
|
||||
}
|
||||
|
||||
|
||||
// evaluate_space() computes the space evaluation for a given side. The
|
||||
// space evaluation is a simple bonus based on the number of safe squares
|
||||
// available for minor pieces on the central four files on ranks 2--4. Safe
|
||||
// squares one, two or three squares behind a friendly pawn are counted
|
||||
// twice. Finally, the space bonus is multiplied by a weight. The aim is to
|
||||
// improve play on game opening.
|
||||
template<Color Us>
|
||||
Score evaluate_space(const Position& pos, const EvalInfo& ei) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
// Find the safe squares for our pieces inside the area defined by
|
||||
// SpaceMask[]. A square is unsafe if it is attacked by an enemy
|
||||
// pawn, or if it is undefended and attacked by an enemy piece.
|
||||
Bitboard safe = SpaceMask[Us]
|
||||
& ~pos.pieces(Us, PAWN)
|
||||
& ~ei.attackedBy[Them][PAWN]
|
||||
& (ei.attackedBy[Us][ALL_PIECES] | ~ei.attackedBy[Them][ALL_PIECES]);
|
||||
|
||||
// Find all squares which are at most three squares behind some friendly pawn
|
||||
Bitboard behind = pos.pieces(Us, PAWN);
|
||||
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
|
||||
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
|
||||
|
||||
// Since SpaceMask[Us] is fully on our half of the board
|
||||
assert(unsigned(safe >> (Us == WHITE ? 32 : 0)) == 0);
|
||||
|
||||
// Count safe + (behind & safe) with a single popcount
|
||||
int bonus = popcount<Full>((Us == WHITE ? safe << 32 : safe >> 32) | (behind & safe));
|
||||
int weight = pos.count<KNIGHT>(Us) + pos.count<BISHOP>(Us)
|
||||
+ pos.count<KNIGHT>(Them) + pos.count<BISHOP>(Them);
|
||||
|
||||
return make_score(bonus * weight * weight, 0);
|
||||
}
|
||||
|
||||
|
||||
// do_evaluate() is the evaluation entry point, called directly from evaluate()
|
||||
|
||||
template<bool Trace>
|
||||
Value do_evaluate(const Position& pos) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
EvalInfo ei;
|
||||
Score score, mobility[2] = { SCORE_ZERO, SCORE_ZERO };
|
||||
|
||||
// Initialize score by reading the incrementally updated scores included
|
||||
// in the position object (material + piece square tables).
|
||||
// Score is computed from the point of view of white.
|
||||
score = pos.psq_score();
|
||||
|
||||
// Probe the material hash table
|
||||
ei.mi = Material::probe(pos);
|
||||
score += ei.mi->imbalance();
|
||||
|
||||
// If we have a specialized evaluation function for the current material
|
||||
// configuration, call it and return.
|
||||
if (ei.mi->specialized_eval_exists())
|
||||
return ei.mi->evaluate(pos);
|
||||
|
||||
// Probe the pawn hash table
|
||||
ei.pi = Pawns::probe(pos);
|
||||
score += apply_weight(ei.pi->pawns_score(), Weights[PawnStructure]);
|
||||
|
||||
// Initialize attack and king safety bitboards
|
||||
init_eval_info<WHITE>(pos, ei);
|
||||
init_eval_info<BLACK>(pos, ei);
|
||||
|
||||
ei.attackedBy[WHITE][ALL_PIECES] |= ei.attackedBy[WHITE][KING];
|
||||
ei.attackedBy[BLACK][ALL_PIECES] |= ei.attackedBy[BLACK][KING];
|
||||
|
||||
// Do not include in mobility squares protected by enemy pawns or occupied by our pawns or king
|
||||
Bitboard mobilityArea[] = { ~(ei.attackedBy[BLACK][PAWN] | pos.pieces(WHITE, PAWN, KING)),
|
||||
~(ei.attackedBy[WHITE][PAWN] | pos.pieces(BLACK, PAWN, KING)) };
|
||||
|
||||
// Evaluate pieces and mobility
|
||||
score += evaluate_pieces<KNIGHT, WHITE, Trace>(pos, ei, mobility, mobilityArea);
|
||||
score += apply_weight(mobility[WHITE] - mobility[BLACK], Weights[Mobility]);
|
||||
|
||||
// Evaluate kings after all other pieces because we need complete attack
|
||||
// information when computing the king safety evaluation.
|
||||
score += evaluate_king<WHITE, Trace>(pos, ei)
|
||||
- evaluate_king<BLACK, Trace>(pos, ei);
|
||||
|
||||
// Evaluate tactical threats, we need full attack information including king
|
||||
score += evaluate_threats<WHITE, Trace>(pos, ei)
|
||||
- evaluate_threats<BLACK, Trace>(pos, ei);
|
||||
|
||||
// Evaluate passed pawns, we need full attack information including king
|
||||
score += evaluate_passed_pawns<WHITE, Trace>(pos, ei)
|
||||
- evaluate_passed_pawns<BLACK, Trace>(pos, ei);
|
||||
|
||||
// If both sides have only pawns, score for potential unstoppable pawns
|
||||
if (!pos.non_pawn_material(WHITE) && !pos.non_pawn_material(BLACK))
|
||||
{
|
||||
Bitboard b;
|
||||
if ((b = ei.pi->passed_pawns(WHITE)) != 0)
|
||||
score += int(relative_rank(WHITE, frontmost_sq(WHITE, b))) * Unstoppable;
|
||||
|
||||
if ((b = ei.pi->passed_pawns(BLACK)) != 0)
|
||||
score -= int(relative_rank(BLACK, frontmost_sq(BLACK, b))) * Unstoppable;
|
||||
}
|
||||
|
||||
// Evaluate space for both sides, only during opening
|
||||
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
|
||||
{
|
||||
Score s = evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei);
|
||||
score += apply_weight(s, Weights[Space]);
|
||||
}
|
||||
|
||||
// Scale winning side if position is more drawish than it appears
|
||||
Color strongSide = eg_value(score) > VALUE_DRAW ? WHITE : BLACK;
|
||||
ScaleFactor sf = ei.mi->scale_factor(pos, strongSide);
|
||||
|
||||
// If we don't already have an unusual scale factor, check for certain
|
||||
// types of endgames, and use a lower scale for those.
|
||||
if ( ei.mi->game_phase() < PHASE_MIDGAME
|
||||
&& (sf == SCALE_FACTOR_NORMAL || sf == SCALE_FACTOR_ONEPAWN))
|
||||
{
|
||||
if (pos.opposite_bishops())
|
||||
{
|
||||
// Endgame with opposite-colored bishops and no other pieces (ignoring pawns)
|
||||
// is almost a draw, in case of KBP vs KB is even more a draw.
|
||||
if ( pos.non_pawn_material(WHITE) == BishopValueMg
|
||||
&& pos.non_pawn_material(BLACK) == BishopValueMg)
|
||||
sf = more_than_one(pos.pieces(PAWN)) ? ScaleFactor(32) : ScaleFactor(8);
|
||||
|
||||
// Endgame with opposite-colored bishops, but also other pieces. Still
|
||||
// a bit drawish, but not as drawish as with only the two bishops.
|
||||
else
|
||||
sf = ScaleFactor(50 * sf / SCALE_FACTOR_NORMAL);
|
||||
}
|
||||
// Endings where weaker side can place his king in front of the opponent's
|
||||
// pawns are drawish.
|
||||
else if ( abs(eg_value(score)) <= BishopValueEg
|
||||
&& ei.pi->pawn_span(strongSide) <= 1
|
||||
&& !pos.pawn_passed(~strongSide, pos.king_square(~strongSide)))
|
||||
sf = ei.pi->pawn_span(strongSide) ? ScaleFactor(56) : ScaleFactor(38);
|
||||
}
|
||||
|
||||
// Interpolate between a middlegame and a (scaled by 'sf') endgame score
|
||||
Value v = mg_value(score) * int(ei.mi->game_phase())
|
||||
+ eg_value(score) * int(PHASE_MIDGAME - ei.mi->game_phase()) * sf / SCALE_FACTOR_NORMAL;
|
||||
|
||||
v /= int(PHASE_MIDGAME);
|
||||
|
||||
// In case of tracing add all single evaluation contributions for both white and black
|
||||
if (Trace)
|
||||
{
|
||||
Tracing::write(Tracing::MATERIAL, pos.psq_score());
|
||||
Tracing::write(Tracing::IMBALANCE, ei.mi->imbalance());
|
||||
Tracing::write(PAWN, ei.pi->pawns_score());
|
||||
Tracing::write(Tracing::MOBILITY, apply_weight(mobility[WHITE], Weights[Mobility])
|
||||
, apply_weight(mobility[BLACK], Weights[Mobility]));
|
||||
Tracing::write(Tracing::SPACE, apply_weight(evaluate_space<WHITE>(pos, ei), Weights[Space])
|
||||
, apply_weight(evaluate_space<BLACK>(pos, ei), Weights[Space]));
|
||||
Tracing::write(Tracing::TOTAL, score);
|
||||
Tracing::ei = ei;
|
||||
Tracing::sf = sf;
|
||||
}
|
||||
|
||||
return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo;
|
||||
}
|
||||
|
||||
|
||||
// Tracing function definitions
|
||||
|
||||
double Tracing::to_cp(Value v) { return double(v) / PawnValueEg; }
|
||||
|
||||
void Tracing::write(int idx, Color c, Score s) { scores[c][idx] = s; }
|
||||
|
||||
void Tracing::write(int idx, Score w, Score b) {
|
||||
|
||||
write(idx, WHITE, w);
|
||||
write(idx, BLACK, b);
|
||||
}
|
||||
|
||||
void Tracing::print(std::stringstream& ss, const char* name, int idx) {
|
||||
|
||||
Score wScore = scores[WHITE][idx];
|
||||
Score bScore = scores[BLACK][idx];
|
||||
|
||||
switch (idx) {
|
||||
case MATERIAL: case IMBALANCE: case PAWN: case TOTAL:
|
||||
ss << std::setw(15) << name << " | --- --- | --- --- | "
|
||||
<< std::setw(5) << to_cp(mg_value(wScore - bScore)) << " "
|
||||
<< std::setw(5) << to_cp(eg_value(wScore - bScore)) << " \n";
|
||||
break;
|
||||
default:
|
||||
ss << std::setw(15) << name << " | " << std::noshowpos
|
||||
<< std::setw(5) << to_cp(mg_value(wScore)) << " "
|
||||
<< std::setw(5) << to_cp(eg_value(wScore)) << " | "
|
||||
<< std::setw(5) << to_cp(mg_value(bScore)) << " "
|
||||
<< std::setw(5) << to_cp(eg_value(bScore)) << " | "
|
||||
<< std::setw(5) << to_cp(mg_value(wScore - bScore)) << " "
|
||||
<< std::setw(5) << to_cp(eg_value(wScore - bScore)) << " \n";
|
||||
}
|
||||
}
|
||||
|
||||
std::string Tracing::do_trace(const Position& pos) {
|
||||
|
||||
std::memset(scores, 0, sizeof(scores));
|
||||
|
||||
Value v = do_evaluate<true>(pos);
|
||||
v = pos.side_to_move() == WHITE ? v : -v; // White's point of view
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2)
|
||||
<< " Eval term | White | Black | Total \n"
|
||||
<< " | MG EG | MG EG | MG EG \n"
|
||||
<< "----------------+-------------+-------------+-------------\n";
|
||||
|
||||
print(ss, "Material", MATERIAL);
|
||||
print(ss, "Imbalance", IMBALANCE);
|
||||
print(ss, "Pawns", PAWN);
|
||||
print(ss, "Knights", KNIGHT);
|
||||
print(ss, "Bishops", BISHOP);
|
||||
print(ss, "Rooks", ROOK);
|
||||
print(ss, "Queens", QUEEN);
|
||||
print(ss, "Mobility", MOBILITY);
|
||||
print(ss, "King safety", KING);
|
||||
print(ss, "Threats", THREAT);
|
||||
print(ss, "Passed pawns", PASSED);
|
||||
print(ss, "Space", SPACE);
|
||||
|
||||
ss << "----------------+-------------+-------------+-------------\n";
|
||||
print(ss, "Total", TOTAL);
|
||||
|
||||
ss << "\nTotal Evaluation: " << to_cp(v) << " (white side)\n";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
namespace Eval {
|
||||
|
||||
/// evaluate() is the main evaluation function. It returns a static evaluation
|
||||
/// of the position always from the point of view of the side to move.
|
||||
|
||||
Value evaluate(const Position& pos) {
|
||||
return do_evaluate<false>(pos);
|
||||
}
|
||||
|
||||
|
||||
/// trace() is like evaluate(), but instead of returning a value, it returns
|
||||
/// a string (suitable for outputting to stdout) that contains the detailed
|
||||
/// descriptions and values of each evaluation term. It's mainly used for
|
||||
/// debugging.
|
||||
std::string trace(const Position& pos) {
|
||||
return Tracing::do_trace(pos);
|
||||
}
|
||||
|
||||
|
||||
/// init() computes evaluation weights, usually at startup
|
||||
|
||||
void init() {
|
||||
|
||||
const double MaxSlope = 7.5;
|
||||
const double Peak = 1280;
|
||||
double t = 0.0;
|
||||
|
||||
for (int i = 1; i < 400; ++i)
|
||||
{
|
||||
t = std::min(Peak, std::min(0.025 * i * i, t + MaxSlope));
|
||||
KingDanger[i] = apply_weight(make_score(int(t), 0), Weights[KingSafety]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Eval
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifndef EVALUATE_H_INCLUDED
|
||||
#define EVALUATE_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
|
||||
class Position;
|
||||
|
||||
namespace Eval {
|
||||
|
||||
const Value Tempo = Value(17); // Must be visible to search
|
||||
|
||||
void init();
|
||||
Value evaluate(const Position& pos);
|
||||
std::string trace(const Position& pos);
|
||||
|
||||
}
|
||||
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
238
src/material.cpp
238
src/material.cpp
|
@ -1,238 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <algorithm> // For std::min
|
||||
#include <cassert>
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "material.h"
|
||||
#include "thread.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
// Polynomial material imbalance parameters
|
||||
|
||||
// pair pawn knight bishop rook queen
|
||||
const int Linear[6] = { 1852, -162, -1122, -183, 249, -154 };
|
||||
|
||||
const int QuadraticOurs[][PIECE_TYPE_NB] = {
|
||||
// OUR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 39, 2 }, // Pawn
|
||||
{ 35, 271, -4 }, // Knight OUR PIECES
|
||||
{ 0, 105, 4, 0 }, // Bishop
|
||||
{ -27, -2, 46, 100, -141 }, // Rook
|
||||
{-177, 25, 129, 142, -137, 0 } // Queen
|
||||
};
|
||||
|
||||
const int QuadraticTheirs[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 37, 0 }, // Pawn
|
||||
{ 10, 62, 0 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 0 }, // Bishop
|
||||
{ 50, 40, 23, -22, 0 }, // Rook
|
||||
{ 98, 105, -39, 141, 274, 0 } // Queen
|
||||
};
|
||||
|
||||
// Endgame evaluation and scaling functions are accessed directly and not through
|
||||
// the function maps because they correspond to more than one material hash key.
|
||||
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
||||
|
||||
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
||||
Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
|
||||
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
||||
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
||||
|
||||
// Helper templates used to detect a given material distribution
|
||||
template<Color Us> bool is_KXK(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return !more_than_one(pos.pieces(Them))
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.count<BISHOP>(Us) == 1
|
||||
&& pos.count<PAWN >(Us) >= 1;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KQKRPs(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return !pos.count<PAWN>(Us)
|
||||
&& pos.non_pawn_material(Us) == QueenValueMg
|
||||
&& pos.count<QUEEN>(Us) == 1
|
||||
&& pos.count<ROOK>(Them) == 1
|
||||
&& pos.count<PAWN>(Them) >= 1;
|
||||
}
|
||||
|
||||
/// imbalance() calculates the imbalance by comparing the piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
template<Color Us>
|
||||
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
int bonus = 0;
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||
{
|
||||
if (!pieceCount[Us][pt1])
|
||||
continue;
|
||||
|
||||
int v = Linear[pt1];
|
||||
|
||||
for (int pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
||||
v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticTheirs[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
bonus += pieceCount[Us][pt1] * v;
|
||||
}
|
||||
|
||||
return bonus;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Material {
|
||||
|
||||
/// Material::probe() looks up the current position's material configuration in
|
||||
/// the material 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 material configuration occurs again.
|
||||
|
||||
Entry* probe(const Position& pos) {
|
||||
|
||||
Key key = pos.material_key();
|
||||
Entry* e = pos.this_thread()->materialTable[key];
|
||||
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
e->gamePhase = pos.game_phase();
|
||||
|
||||
// Let's look if we have a specialized evaluation function for this particular
|
||||
// material configuration. Firstly we look for a fixed configuration one, then
|
||||
// for a generic one if the previous search failed.
|
||||
if (pos.this_thread()->endgames.probe(key, e->evaluationFunction))
|
||||
return e;
|
||||
|
||||
if (is_KXK<WHITE>(pos))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[WHITE];
|
||||
return e;
|
||||
}
|
||||
|
||||
if (is_KXK<BLACK>(pos))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[BLACK];
|
||||
return e;
|
||||
}
|
||||
|
||||
// OK, we didn't find any special evaluation function for the current material
|
||||
// configuration. Is there a suitable specialized scaling function?
|
||||
EndgameBase<ScaleFactor>* sf;
|
||||
|
||||
if (pos.this_thread()->endgames.probe(key, sf))
|
||||
{
|
||||
e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned
|
||||
return e;
|
||||
}
|
||||
|
||||
// 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.
|
||||
if (is_KBPsKs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
||||
|
||||
if (is_KBPsKs<BLACK>(pos))
|
||||
e->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
|
||||
|
||||
if (is_KQKRPs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
|
||||
|
||||
else if (is_KQKRPs<BLACK>(pos))
|
||||
e->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
|
||||
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
|
||||
{
|
||||
if (!pos.count<PAWN>(BLACK))
|
||||
{
|
||||
assert(pos.count<PAWN>(WHITE) >= 2);
|
||||
|
||||
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||
}
|
||||
else if (!pos.count<PAWN>(WHITE))
|
||||
{
|
||||
assert(pos.count<PAWN>(BLACK) >= 2);
|
||||
|
||||
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||
}
|
||||
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
||||
{
|
||||
// This is a special case because we set scaling functions
|
||||
// for both colors instead of only one.
|
||||
e->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
|
||||
e->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
|
||||
}
|
||||
}
|
||||
|
||||
// Zero or just one pawn makes it difficult to win, even with a small material
|
||||
// advantage. This catches some trivial draws like KK, KBK and KNK and gives a
|
||||
// drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_b <= BishopValueMg ? 4 : 12);
|
||||
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_w <= BishopValueMg ? 4 : 12);
|
||||
|
||||
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||
|
||||
if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||
|
||||
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
||||
// for the bishop pair "extended piece", which allows us to be more flexible
|
||||
// in defining bishop pair bonuses.
|
||||
const int PieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
||||
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
||||
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
||||
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
||||
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
||||
|
||||
e->value = int16_t((imbalance<WHITE>(PieceCount) - imbalance<BLACK>(PieceCount)) / 16);
|
||||
return e;
|
||||
}
|
||||
|
||||
} // namespace Material
|
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifndef MATERIAL_H_INCLUDED
|
||||
#define MATERIAL_H_INCLUDED
|
||||
|
||||
#include "endgame.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Material {
|
||||
|
||||
/// Material::Entry contains various information about a material configuration.
|
||||
/// It contains a material imbalance evaluation, a function pointer to a special
|
||||
/// endgame evaluation function (which in most cases is NULL, meaning that the
|
||||
/// standard evaluation function will be used), and scale factors.
|
||||
///
|
||||
/// The scale factors are used to scale the evaluation score up or down. For
|
||||
/// instance, in KRB vs KR endgames, the score is scaled down by a factor of 4,
|
||||
/// which will result in scores of absolute value less than one pawn.
|
||||
|
||||
struct Entry {
|
||||
|
||||
Score imbalance() const { return make_score(value, value); }
|
||||
Phase game_phase() const { return gamePhase; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
|
||||
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
|
||||
|
||||
// scale_factor takes a position and a color as input and returns a scale factor
|
||||
// for the given color. We have to provide the position in addition to the color
|
||||
// because the scale factor may also be a function which should be applied to
|
||||
// the position. For instance, in KBP vs K endgames, the scaling function looks
|
||||
// for rook pawns and wrong-colored bishops.
|
||||
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
||||
|
||||
return !scalingFunction[c] || (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE
|
||||
? ScaleFactor(factor[c]) : (*scalingFunction[c])(pos);
|
||||
}
|
||||
|
||||
Key key;
|
||||
int16_t value;
|
||||
uint8_t factor[COLOR_NB];
|
||||
EndgameBase<Value>* evaluationFunction;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
||||
// side (e.g. KPKP, KBPsKs)
|
||||
Phase gamePhase;
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 8192> Table;
|
||||
|
||||
Entry* probe(const Position& pos);
|
||||
|
||||
} // namespace Material
|
||||
|
||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
Loading…
Add table
Reference in a new issue