mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-22 01:56:58 +08:00
Merge branch 'master' of https://github.com/nodchip/Stockfish into sf-nnue-nodchip
This commit is contained in:
139
src/evaluate.cpp
139
src/evaluate.cpp
@@ -22,6 +22,7 @@
|
||||
#include <cassert>
|
||||
#include <cstring> // For std::memset
|
||||
#include <iomanip>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
#include "bitboard.h"
|
||||
@@ -29,6 +30,7 @@
|
||||
#include "material.h"
|
||||
#include "pawns.h"
|
||||
#include "thread.h"
|
||||
#include "eval/nnue/evaluate_nnue.h"
|
||||
|
||||
namespace Trace {
|
||||
|
||||
@@ -877,9 +879,11 @@ namespace {
|
||||
/// evaluate() is the evaluator for the outer world. It returns a static
|
||||
/// evaluation of the position from the point of view of the side to move.
|
||||
|
||||
#if !defined(EVAL_NNUE)
|
||||
Value Eval::evaluate(const Position& pos) {
|
||||
return Evaluation<NO_TRACE>(pos).value();
|
||||
}
|
||||
#endif // defined(EVAL_NNUE)
|
||||
|
||||
|
||||
/// trace() is like evaluate(), but instead of returning a value, it returns
|
||||
@@ -924,3 +928,138 @@ std::string Eval::trace(const Position& pos) {
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
#if defined(EVAL_NNUE) || defined(EVAL_LEARN)
|
||||
namespace Eval {
|
||||
ExtBonaPiece kpp_board_index[PIECE_NB] = {
|
||||
{ BONA_PIECE_ZERO, BONA_PIECE_ZERO },
|
||||
{ f_pawn, e_pawn },
|
||||
{ f_knight, e_knight },
|
||||
{ f_bishop, e_bishop },
|
||||
{ f_rook, e_rook },
|
||||
{ f_queen, e_queen },
|
||||
{ f_king, e_king },
|
||||
{ BONA_PIECE_ZERO, BONA_PIECE_ZERO },
|
||||
|
||||
// <20><><EFBFBD>肩<EFBFBD>猩<EFBFBD><E78CA9><EFBFBD>ꍇ<EFBFBD>Bf<42><66>e<EFBFBD><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ւ<EFBFBD><D682><EFBFBD><EFBFBD>B
|
||||
{ BONA_PIECE_ZERO, BONA_PIECE_ZERO },
|
||||
{ e_pawn, f_pawn },
|
||||
{ e_knight, f_knight },
|
||||
{ e_bishop, f_bishop },
|
||||
{ e_rook, f_rook },
|
||||
{ e_queen, f_queen },
|
||||
{ e_king, f_king },
|
||||
{ BONA_PIECE_ZERO, BONA_PIECE_ZERO }, // <20><><EFBFBD>̐<EFBFBD><CC90><EFBFBD><EFBFBD>͂Ȃ<CD82>
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ŕێ<C595><DB8E><EFBFBD><EFBFBD>Ă<EFBFBD><C482><EFBFBD>pieceListFw[]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BonaPiece<63>ł<EFBFBD><C582>邩<EFBFBD><E982A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B
|
||||
// <20><> : <20>f<EFBFBD>o<EFBFBD>b<EFBFBD>O<EFBFBD>p<EFBFBD>B<EFBFBD>x<EFBFBD><78><EFBFBD>B
|
||||
bool EvalList::is_valid(const Position& pos)
|
||||
{
|
||||
std::set<PieceNumber> piece_numbers;
|
||||
for (Square sq = SQ_A1; sq != SQUARE_NB; ++sq) {
|
||||
auto piece_number = piece_no_of_board(sq);
|
||||
if (piece_number == PIECE_NUMBER_NB) {
|
||||
continue;
|
||||
}
|
||||
assert(!piece_numbers.count(piece_number));
|
||||
piece_numbers.insert(piece_number);
|
||||
}
|
||||
|
||||
for (int i = 0; i < length(); ++i)
|
||||
{
|
||||
BonaPiece fw = pieceListFw[i];
|
||||
// <20><><EFBFBD><EFBFBD>fw<66><77><EFBFBD>{<7B><><EFBFBD>ɑ<EFBFBD><C991>݂<EFBFBD><DD82>邩<EFBFBD><E982A9>Position<6F>N<EFBFBD><4E><EFBFBD>X<EFBFBD>̂ق<CC82><D982>ɒ<EFBFBD><C992>ׂɍs<C98D><73><EFBFBD>B
|
||||
|
||||
if (fw == Eval::BONA_PIECE_ZERO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// <20>͈͊O
|
||||
if (!(0 <= fw && fw < fe_end))
|
||||
return false;
|
||||
|
||||
// <20>Տ<EFBFBD><D58F>̋<EFBFBD><CC8B>Ȃ̂ł<CC82><C582>̋<CC8B>{<7B><><EFBFBD>ɑ<EFBFBD><C991>݂<EFBFBD><DD82>邩<EFBFBD><E982A9><EFBFBD>ׂɂ<D782><C982><EFBFBD><EFBFBD>B
|
||||
for (Piece pc = NO_PIECE; pc < PIECE_NB; ++pc)
|
||||
{
|
||||
auto pt = type_of(pc);
|
||||
if (pt == NO_PIECE || pt == 7) // <20><><EFBFBD>݂<EFBFBD><DD82>Ȃ<EFBFBD><C882><EFBFBD>
|
||||
continue;
|
||||
|
||||
// <20><>pc<70><63>BonaPiece<63>̊J<CC8A>n<EFBFBD>ԍ<EFBFBD>
|
||||
auto s = BonaPiece(kpp_board_index[pc].fw);
|
||||
if (s <= fw && fw < s + SQUARE_NB)
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><C282><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̂ł<CC82><C582>̋sq<73>̒n<CC92>_<EFBFBD>ɂ<EFBFBD><C982>邩<EFBFBD>ׂ<F092B282><D782>B
|
||||
Square sq = (Square)(fw - s);
|
||||
Piece pc2 = pos.piece_on(sq);
|
||||
|
||||
if (pc2 != pc)
|
||||
return false;
|
||||
|
||||
goto Found;
|
||||
}
|
||||
}
|
||||
// <20><><EFBFBD>̂<EFBFBD><CC82><EFBFBD><EFBFBD>݂<EFBFBD><DD82>Ȃ<EFBFBD><C882><EFBFBD><EFBFBD>ł<EFBFBD><C582><EFBFBD><EFBFBD><EFBFBD>..
|
||||
return false;
|
||||
Found:;
|
||||
}
|
||||
|
||||
// Validate piece_no_list_board
|
||||
for (auto sq = SQUARE_ZERO; sq < SQUARE_NB; ++sq) {
|
||||
Piece expected_piece = pos.piece_on(sq);
|
||||
PieceNumber piece_number = piece_no_list_board[sq];
|
||||
if (piece_number == PIECE_NUMBER_NB) {
|
||||
assert(expected_piece == NO_PIECE);
|
||||
if (expected_piece != NO_PIECE) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
BonaPiece bona_piece_white = pieceListFw[piece_number];
|
||||
Piece actual_piece;
|
||||
for (actual_piece = NO_PIECE; actual_piece < PIECE_NB; ++actual_piece) {
|
||||
if (kpp_board_index[actual_piece].fw == BONA_PIECE_ZERO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (kpp_board_index[actual_piece].fw <= bona_piece_white
|
||||
&& bona_piece_white < kpp_board_index[actual_piece].fw + SQUARE_NB) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert(actual_piece != PIECE_NB);
|
||||
if (actual_piece == PIECE_NB) {
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(actual_piece == expected_piece);
|
||||
if (actual_piece != expected_piece) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Square actual_square = static_cast<Square>(
|
||||
bona_piece_white - kpp_board_index[actual_piece].fw);
|
||||
assert(sq == actual_square);
|
||||
if (sq != actual_square) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif // defined(EVAL_NNUE) || defined(EVAL_LEARN)
|
||||
|
||||
#if !defined(EVAL_NNUE)
|
||||
namespace Eval {
|
||||
void evaluate_with_no_return(const Position& pos) {}
|
||||
void update_weights(uint64_t epoch, const std::array<bool, 4> & freeze) {}
|
||||
void init_grad(double eta1, uint64_t eta_epoch, double eta2, uint64_t eta2_epoch, double eta3) {}
|
||||
void add_grad(Position& pos, Color rootColor, double delt_grad, const std::array<bool, 4> & freeze) {}
|
||||
void save_eval(std::string suffix) {}
|
||||
double get_eta() { return 0.0; }
|
||||
}
|
||||
#endif // defined(EVAL_NNUE)
|
||||
|
||||
Reference in New Issue
Block a user