eveluate: Position * 改为 const Position &

暂时去掉 UCI 代码。
This commit is contained in:
Calcitem 2020-09-06 12:21:30 +08:00
parent 69451b47f3
commit 490983195d
7 changed files with 44 additions and 41 deletions

View File

@ -52,14 +52,14 @@ class Evaluation
{
public:
Evaluation() = delete;
explicit Evaluation(Position *p) : pos(p)
explicit Evaluation(const Position &p) : pos(p)
{
}
Evaluation &operator=(const Evaluation &) = delete;
Value value();
private:
Position *pos;
const Position &pos;
};
// Evaluation::value() is the main function of the class. It computes the various
@ -74,25 +74,25 @@ Value Evaluation::value()
int nPiecesOnBoardDiff;
int pieceCountNeedRemove;
switch (pos->phase) {
switch (pos.phase) {
case PHASE_READY:
break;
case PHASE_PLACING:
nPiecesInHandDiff = pos->pieceCountInHand[BLACK] - pos->pieceCountInHand[WHITE];
nPiecesInHandDiff = pos.pieceCountInHand[BLACK] - pos.pieceCountInHand[WHITE];
value += nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND;
nPiecesOnBoardDiff = pos->pieceCountOnBoard[BLACK] - pos->pieceCountOnBoard[WHITE];
nPiecesOnBoardDiff = pos.pieceCountOnBoard[BLACK] - pos.pieceCountOnBoard[WHITE];
value += nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD;
switch (pos->action) {
switch (pos.action) {
case ACTION_SELECT:
case ACTION_PLACE:
break;
case ACTION_REMOVE:
pieceCountNeedRemove = (pos->sideToMove == BLACK) ?
pos->pieceCountNeedRemove : -(pos->pieceCountNeedRemove);
pieceCountNeedRemove = (pos.sideToMove == BLACK) ?
pos.pieceCountNeedRemove : -(pos.pieceCountNeedRemove);
value += pieceCountNeedRemove * VALUE_EACH_PIECE_PLACING_NEEDREMOVE;
break;
default:
@ -102,21 +102,21 @@ Value Evaluation::value()
break;
case PHASE_MOVING:
value = pos->pieceCountOnBoard[BLACK] * VALUE_EACH_PIECE_ONBOARD -
pos->pieceCountOnBoard[WHITE] * VALUE_EACH_PIECE_ONBOARD;
value = pos.pieceCountOnBoard[BLACK] * VALUE_EACH_PIECE_ONBOARD -
pos.pieceCountOnBoard[WHITE] * VALUE_EACH_PIECE_ONBOARD;
#ifdef EVALUATE_MOBILITY
value += pos->get_mobility_diff(position->turn, position->pieceCountInHand[BLACK], position->pieceCountInHand[WHITE], false) * 10;
value += pos.get_mobility_diff(position->turn, position->pieceCountInHand[BLACK], position->pieceCountInHand[WHITE], false) * 10;
#endif /* EVALUATE_MOBILITY */
switch (pos->action) {
switch (pos.action) {
case ACTION_SELECT:
case ACTION_PLACE:
break;
case ACTION_REMOVE:
pieceCountNeedRemove = (pos->sideToMove == BLACK) ?
pos->pieceCountNeedRemove : -(pos->pieceCountNeedRemove);
pieceCountNeedRemove = (pos.sideToMove == BLACK) ?
pos.pieceCountNeedRemove : -(pos.pieceCountNeedRemove);
value += pieceCountNeedRemove * VALUE_EACH_PIECE_MOVING_NEEDREMOVE;
break;
default:
@ -126,23 +126,23 @@ Value Evaluation::value()
break;
case PHASE_GAMEOVER:
if (pos->pieceCountOnBoard[BLACK] + pos->pieceCountOnBoard[WHITE] >=
if (pos.pieceCountOnBoard[BLACK] + pos.pieceCountOnBoard[WHITE] >=
RANK_NB * FILE_NB) {
if (rule.isBlackLosebutNotDrawWhenBoardFull) {
value -= VALUE_MATE;
} else {
value = VALUE_DRAW;
}
} else if (pos->action == ACTION_SELECT &&
pos->is_all_surrounded() &&
} else if (pos.action == ACTION_SELECT &&
pos.is_all_surrounded() &&
rule.isLoseButNotChangeTurnWhenNoWay) {
Value delta = pos->sideToMove == BLACK ? -VALUE_MATE : VALUE_MATE;
Value delta = pos.sideToMove == BLACK ? -VALUE_MATE : VALUE_MATE;
value += delta;
}
else if (pos->pieceCountOnBoard[BLACK] < rule.nPiecesAtLeast) {
else if (pos.pieceCountOnBoard[BLACK] < rule.nPiecesAtLeast) {
value -= VALUE_MATE;
} else if (pos->pieceCountOnBoard[WHITE] < rule.nPiecesAtLeast) {
} else if (pos.pieceCountOnBoard[WHITE] < rule.nPiecesAtLeast) {
value += VALUE_MATE;
}
@ -152,7 +152,7 @@ Value Evaluation::value()
break;
}
if (pos->sideToMove == WHITE) {
if (pos.sideToMove == WHITE) {
value = -value;
}
@ -165,7 +165,7 @@ Value Evaluation::value()
/// 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.
Value Eval::evaluate(Position *pos)
Value Eval::evaluate(const Position &pos)
{
#ifdef ALPHABETA_AI
return Evaluation(pos).value();
@ -176,17 +176,17 @@ Value Eval::evaluate(Position *pos)
/// a string (suitable for outputting to stdout) that contains the detailed
/// descriptions and values of each evaluation term. Useful for debugging.
std::string Eval::trace(Position *pos)
std::string Eval::trace(const Position &pos)
{
#if 0
std::memset(scores, 0, sizeof(scores));
// TODO
//pos->this_thread()->contempt = 0 // TODO: SCORE_ZERO; // Reset any dynamic contempt
//pos.this_thread()->contempt = 0 // TODO: SCORE_ZERO; // Reset any dynamic contempt
Value v = Evaluation(pos)->value();
v = pos->side_to_move() == WHITE ? v : -v; // Trace scores are from white's point of view
v = pos.side_to_move() == WHITE ? v : -v; // Trace scores are from white's point of view
std::stringstream ss;
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2)
@ -207,6 +207,6 @@ std::string Eval::trace(Position *pos)
return ss.str();
#endif
pos = pos;
//pos = pos;
return "";
}

View File

@ -28,9 +28,9 @@ class Position;
namespace Eval {
std::string trace(Position *pos);
std::string trace(const Position &pos);
Value evaluate(Position *pos);
Value evaluate(const Position &pos);
};
#endif // #ifndef EVALUATE_H_INCLUDED

View File

@ -1469,7 +1469,7 @@ Square Position::polar_to_square(File file, Rank rank)
return static_cast<Square>(file * RANK_NB + rank - 1);
}
Color Position::color_on(Square s)
Color Position::color_on(Square s) const
{
return Color((board[s] & 0x30) >> PLAYER_SHIFT);
}
@ -1637,7 +1637,7 @@ void Position::surrounded_pieces_count(Square s, int &nOurPieces, int &nTheirPie
}
}
bool Position::is_all_surrounded()
bool Position::is_all_surrounded() const
{
// Full
if (pieceCountOnBoard[BLACK] + pieceCountOnBoard[WHITE] >= RANK_NB * FILE_NB)

View File

@ -77,7 +77,7 @@ public:
// Position representation
Piece piece_on(Square s) const;
Color color_on(Square s);
Color color_on(Square s) const;
bool empty(Square s) const;
template<PieceType Pt> int count(Color c) const;
@ -155,7 +155,7 @@ public:
int surrounded_empty_squares_count(Square s, bool includeFobidden);
void surrounded_pieces_count(Square s, int &nOurPieces, int &nTheirPieces, int &nBanned, int &nEmpty);
bool is_all_surrounded();
bool is_all_surrounded() const;
static void square_to_polar(Square s, File &file, Rank &rank);
static Square polar_to_square(File file, Rank rank);

View File

@ -786,7 +786,7 @@ Value search(Position *pos, Stack<Position> &ss, Depth depth, Depth originDepth,
if (unlikely(pos->phase == PHASE_GAMEOVER) || // TODO: Deal with hash and requiredQuit
depth <= 0) {
bestValue = Eval::evaluate(pos);
bestValue = Eval::evaluate(*pos);
// For win quickly
if (bestValue > 0) {

View File

@ -37,6 +37,8 @@ using namespace std;
extern vector<string> setup_bench(Position*, istream&);
#if 0
namespace {
// FEN string of the initial position, normal mill game
@ -48,7 +50,7 @@ namespace {
// or the starting position ("startpos") and then makes the moves given in the
// following move list ("moves").
void position(Position* pos, istringstream& is, StateListPtr& states) {
void position(Position& pos, istringstream& is, StateListPtr& states) {
Move m;
string token, fen;
@ -74,7 +76,7 @@ namespace {
{
states->emplace_back();
//pos.do_move(m, states->back()); // TODO
pos->do_move(m);
pos.do_move(m);
}
}
@ -166,10 +168,10 @@ namespace {
nodes += Threads.nodes_searched();
}
else
sync_cout << "\n" << Eval::trace(&pos) << sync_endl;
sync_cout << "\n" << Eval::trace(pos) << sync_endl;
}
else if (token == "setoption") setoption(is);
else if (token == "position") position(&pos, is, states);
else if (token == "position") position(pos, is, states);
else if (token == "ucinewgame") { Search::clear(); elapsed = now(); } // Search::clear() may take some while
}
@ -230,7 +232,7 @@ void UCI::loop(int argc, char* argv[]) {
else if (token == "setoption") setoption(is);
else if (token == "go") go(pos, is, states);
else if (token == "position") position(&pos, is, states);
else if (token == "position") position(pos, is, states);
else if (token == "ucinewgame") Search::clear();
else if (token == "isready") sync_cout << "readyok" << sync_endl;
@ -239,7 +241,7 @@ void UCI::loop(int argc, char* argv[]) {
else if (token == "flip") pos.flip();
else if (token == "bench") bench(pos, is, states);
else if (token == "d") sync_cout << &pos << sync_endl;
else if (token == "eval") sync_cout << Eval::trace(&pos) << sync_endl;
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
else if (token == "compiler") sync_cout << compiler_info() << sync_endl;
else
sync_cout << "Unknown command: " << cmd << sync_endl;
@ -302,7 +304,7 @@ string UCI::move(Move m) {
/// UCI::to_move() converts a string representing a move in coordinate notation
/// (g1f3, a7a8q) to the corresponding legal Move, if any.
Move UCI::to_move(Position* pos, string& str) {
Move UCI::to_move(const Position& pos, string& str) {
if (str.length() == 5) // Junior could send promotion piece in uppercase
str[4] = char(tolower(str[4]));
@ -313,3 +315,4 @@ Move UCI::to_move(Position* pos, string& str) {
return MOVE_NONE;
}
#endif

View File

@ -73,7 +73,7 @@ void loop(int argc, char* argv[]);
std::string value(Value v);
std::string square(Square s);
std::string move(Move m);
std::string pv(Position* pos, Depth depth, Value alpha, Value beta);
std::string pv(Position& pos, Depth depth, Value alpha, Value beta);
Move to_move(Position* pos, std::string& str);
} // namespace UCI