如何编译和运行皮卡鱼

运行pikafish后输入d命令:

Screenshot_20251009_190900

rook:车; knight:马; bishop:象; advisor:士; king:将; cannon:炮; pawn:卒。红棋用大写字母。红方叫白方。

棋盘可以看作9x10的交叉点,也可看作方格(square).

对应代码是position.cpp里的std::ostream& operator<<(std::ostream& os, const Position& pos),不是bitboard.cpp里的pretty,后者没人调用。

先看types.h。enum Square : int8_t { SQ_A0 ... SQ_I9, SQUARE_NB   = 90 } NB是number的意思:…的个数。

enum Direction : int8_t { EAST = 1, NORTH = 9, SOUTH = -NORTH } 上北下南,左西右东。

// Initializes the position object with the given FEN string.
Position& Position::set(const string& fenStr, StateInfo* si) {
  unsigned char      token;
  size_t             idx;
  Square             sq = SQ_A9;
  std::istringstream ss(fenStr);
  while ((ss >> token) && !isspace(token)) {
    if (isdigit(token)) sq += (token - '0') * EAST; // Advance the given number of files
    else if (token == '/')  sq += 2 * SOUTH;
    else if ((idx = PieceToChar.find(token)) != string::npos) put_piece(Piece(idx), sq);
  }
}

过度花哨了。* 1怎么说?stringstream包string包char*?

Piece是棋子。File是列,rank是行,和row首字母都是r. rank有军衔的意思,可联想兵到底线升变。

constexpr std::string_view PieceToChar(" RACPNBK racpnbk");

string_view是C++17引入的轻量级字符串视图类,定义于<string_view>中‌。它提供对字符串数据的只读访问,仅存储指针和长度信息。

① 这个函数没必要优化。

② 最快的是像ctype.h那样放在T [256]的表里,T是char好还是int好?还是自己操作位?

Position::put_piece(Piece pc, Square s) {
  board[s] = pc;
  byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
  byColorBB[color_of(pc)] |= s;
  pieceCount[pc]++;
}

enum Piece std::int8_t {...}
Piece board[SQUARE_NB];

typedef __uint128_t Bitboard;
// 实际为using Bitboard = ...,处理Linux和Windows不同的情况。Linux下啥头文件都不用包含。

enum PieceType std::int8_t { ROOK, ..., PIECE_TYPE_NB = 8 }
Bitboard byTypeBB[PIECE_TYPE_NB];

enum Color : int8_t { WHITE, BLACK, COLOR_NB = 2 };
Bitboard byColorBB[COLOR_NB];

每方有7种、16个棋子。补充type_of和color_of:

constexpr PieceType type_of(Piece pc) { return PieceType(pc & 7); }

constexpr Color color_of(Piece pc) {
  assert(pc != NO_PIECE);
  return Color(pc >> 3);
}

与GNU chess相比:

  • 虽然我都看不懂,但感觉皮卡鱼赏心悦目,可读性强。在misc.cpp里作者提到了"Our fancy logging facility"; 也许极个别别的地方也有点fancy.
  • 搜索部分比较简明。NNUE的online部分1000来行。
  • 速度极快的xxHash还有汇编写的Huffman解码可以先不看。Meta有基础研究科学家,腾讯呢?
  • 皮卡鱼(基于Stockfish)棋力强。
  • 带数据。43M的pikafish.nnue是压缩后,7z一点都再压缩不动。Top CPU Contributors里有个Contributors for training data generation with >10,000,000 positions generated, as of 2023-02-16.  不是说拿1千万个局面训练的,是1千万是上榜条件(157人上榜),榜首kaka是452524769438/1e8 > 4525。Contributors to Fishtest with >10,000 CPU hours, as of 2023-07-31. 榜首浮生若梦5029923 (574.2年?)。也许43M的是精简版。

 

posted on 2025-10-09 20:18  华容道专家  阅读(14)  评论(0)    收藏  举报