Skip to content

Commit

Permalink
- history statsに関して添字をStockfishから入れ替えている件、search.cppにコメントをかなり追加した。
Browse files Browse the repository at this point in the history
  - 棋力に影響する変更はしてない。(つもり)
  • Loading branch information
yaneurao committed Oct 18, 2023
1 parent 09b05fd commit 2792338
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 16 deletions.
95 changes: 87 additions & 8 deletions source/engine/yaneuraou-engine/yaneuraou-search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ void search_thread_init(Thread* th, Stack* ss , Move pv[])
for (int i = 7; i > 0; --i)
{
(ss - i)->continuationHistory = &th->continuationHistory[0][0][SQ_ZERO][NO_PIECE]; // Use as a sentinel
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。
(ss - i)->staticEval = VALUE_NONE;
}

Expand Down Expand Up @@ -1619,6 +1623,11 @@ namespace {
{
int penalty = -stat_bonus(depth);
thisThread->mainHistory[from_to(ttMove)][us] << penalty;
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。

update_continuation_histories(ss, pos.moved_piece_after(ttMove), to_sq(ttMove), penalty);
}
}
Expand Down Expand Up @@ -1840,6 +1849,10 @@ namespace {
// この右辺の↑係数、調整すべきだろうけども、4 Eloのところ調整しても…みたいな意味はある。

thisThread->mainHistory[from_to((ss-1)->currentMove)][~us] << bonus;
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
}

// Set up the improvement variable, which is the difference between the current
Expand Down Expand Up @@ -1913,6 +1926,11 @@ namespace {
&& !( !ttCapture
&& ttMove
&& thisThread->mainHistory[from_to(ttMove)][us] < 989))
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。


// 29462の根拠はよくわからないが、VALUE_TB_WIN_IN_MAX_PLY より少し小さい値にしたいようだ。
// そこまではfutility pruningで枝刈りして良いと言うことなのだろう。
Expand Down Expand Up @@ -1957,6 +1975,10 @@ namespace {
// null moveなので、王手はかかっていなくて駒取りでもない。
// よって、continuationHistory[0(王手かかってない)][0(駒取りではない)][SQ_ZERO][NO_PIECE]
ss->continuationHistory = &thisThread->continuationHistory[0][0][SQ_ZERO][NO_PIECE];
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。

pos.do_null_move(st);

Expand Down Expand Up @@ -2078,6 +2100,10 @@ namespace {
[true ] // captureOrPromotion
[to_sq(move) ]
[pos.moved_piece_after(move)];
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。

pos.do_move(move, st);

Expand Down Expand Up @@ -2147,11 +2173,14 @@ namespace {
nullptr , (ss - 4)->continuationHistory ,
nullptr , (ss - 6)->continuationHistory };

// 直前の指し手で動かした(移動後の)駒 : やねうら王独自
Piece prevPc = pos.piece_on(prevSq);

// 1手前の指し手(1手前のtoとPiece)に対応するよさげな応手を統計情報から取得。
Move countermove = prevSq != SQ_NONE ? thisThread->counterMoves[prevSq][prevPc] : MOVE_NONE;
// 1手前がnull moveの時prevSq == SQ_NONEになるのでこのケースは除外する。
Move countermove = prevSq != SQ_NONE ? thisThread->counterMoves[prevSq][pos.piece_on(prevSq)] : MOVE_NONE;
// ※ ↑counterMovesに関して
// Stockfishは [pc][to]の順
// やねうら王は [to][pc]の順
// なので注意。


MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory,
&captureHistory,
Expand Down Expand Up @@ -2328,14 +2357,20 @@ namespace {
int history = (*contHist[0])[to_sq(move)][movedPiece]
+ (*contHist[1])[to_sq(move)][movedPiece]
+ (*contHist[3])[to_sq(move)][movedPiece];
// contHist[][]はStockfishと逆順なので注意。
// ↑contHistに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。

if (lmrDepth < PARAM_PRUNING_BY_HISTORY_DEPTH/*6*/
&& history < -3875 * (depth - 1))
continue;

history += 2 * thisThread->mainHistory[from_to(move)][ us];
// mainHistory[][]もStockfishと逆順なので注意。
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。

lmrDepth += history / 5793;
lmrDepth = std::max(lmrDepth, -2);
Expand Down Expand Up @@ -2526,6 +2561,10 @@ namespace {
&& move == ttMove
&& move == ss->killers[0]
&& (*contHist[0])[to_sq(move)][movedPiece] >= PARAM_QUIET_TT_EXTENSION /*4194*/)
// ↑contHistに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。
extension = 1;
}

Expand Down Expand Up @@ -2563,6 +2602,10 @@ namespace {
[capture ]
[to_sq(move)]
[movedPiece ];
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。

// -----------------------
// Step 16. Make the move
Expand Down Expand Up @@ -2640,9 +2683,17 @@ namespace {
// 【計測資料 11.】statScoreの計算でcontHist[3]も調べるかどうか。
// contHist[5]も/2とかで入れたほうが良いのでは…。誤差か…?
ss->statScore = 2 * thisThread->mainHistory[from_to(move)][us]
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
+ (*contHist[0])[to_sq(move)][movedPiece]
+ (*contHist[1])[to_sq(move)][movedPiece]
+ (*contHist[3])[to_sq(move)][movedPiece]
// ↑contHistに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。
- 3848;

// Decrease/increase reduction for moves with a good/bad history (~25 Elo)
Expand Down Expand Up @@ -2981,6 +3032,10 @@ namespace {
int bonus = (depth > 6) + (PvNode || cutNode) + (bestValue < alpha - PARAM_COUNTERMOVE_FAILLOW_MARGIN /*653*/) + ((ss-1)->moveCount > 11);
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth) * bonus);
thisThread->mainHistory[from_to((ss-1)->currentMove)][~us] << stat_bonus(depth) * bonus / 2;
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
}

// 将棋ではtable probe使っていないのでmaxValue関係ない。
Expand Down Expand Up @@ -3475,6 +3530,10 @@ namespace {
if ( !capture
&& (*contHist[0])[to_sq(move)][pos.moved_piece_after(move)] < 0
&& (*contHist[1])[to_sq(move)][pos.moved_piece_after(move)] < 0)
// ↑contHistに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。
continue;


Expand All @@ -3495,6 +3554,10 @@ namespace {
[capture ]
[to_sq(move) ]
[pos.moved_piece_after(move)];
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。

quietCheckEvasions += !capture && ss->inCheck;

Expand Down Expand Up @@ -3705,7 +3768,10 @@ namespace {
for (int i = 0; i < quietCount; ++i)
{
thisThread->mainHistory[from_to(quietsSearched[i])][us] << -bestMoveBonus;
// StockfishはmainHistory↑は、[Color][from_to]の順なので注意。
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。

update_continuation_histories(ss, pos.moved_piece_after(quietsSearched[i]), to_sq(quietsSearched[i]), -bestMoveBonus);
}
Expand All @@ -3714,7 +3780,8 @@ namespace {
// Increase stats for the best move in case it was a capture move
captured = type_of(pos.piece_on(to_sq(bestMove)));
captureHistory[to_sq(bestMove)][moved_piece][captured] << quietMoveBonus;
// ※ StockfishはcaptureHistory↑は[pc][to][captured]の順なので注意。
// ※ StockfishはcaptureHistory↑は[pc][to][captured]の順
// やねうら王は、 [to][pc][captured]の順なので注意。
}

// Extra penalty for a quiet early move that was not a TT move or
Expand Down Expand Up @@ -3758,6 +3825,10 @@ namespace {
break;
if (is_ok((ss - i)->currentMove))
(*(ss - i)->continuationHistory)[to][pc] << bonus;
// ↑continuationHistoryに関して
// Stockfishは、 [inCheck][capture][pc][sq]の順
// やねうら王は、[inCheck][capture][sq][pc]の順
// なので注意。
}
}

Expand All @@ -3784,6 +3855,10 @@ namespace {

Thread* thisThread = pos.this_thread();
thisThread->mainHistory[from_to(move)][us] << bonus;
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
update_continuation_histories(ss, pos.moved_piece_after(move), to_sq(move), bonus);

// Update countermove history
Expand All @@ -3792,6 +3867,10 @@ namespace {
// 直前に移動させた升(その升に移動させた駒がある。今回の指し手はcaptureではないはずなので)
Square prevSq = to_sq((ss - 1)->currentMove);
thisThread->counterMoves[prevSq][pos.piece_on(prevSq)] = move;
// ※ ↑counterMovesに関して
// Stockfishは [pc][to]の順
// やねうら王は [to][pc]の順
// なので注意。
}
}

Expand Down
17 changes: 17 additions & 0 deletions source/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,19 @@ void MovePicker::score()
PieceType moved_piece = type_of(pos.moved_piece_before(m));

m.value = (*mainHistory)[from_to(m)][pos.side_to_move()]
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
+ 2 * (*continuationHistory[0])[movedSq][movedPiece]
+ (*continuationHistory[1])[movedSq][movedPiece]
+ (*continuationHistory[3])[movedSq][movedPiece]
+ (*continuationHistory[5])[movedSq][movedPiece]
// ↑continuationHistoryに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。

// 移動元の駒が安い駒で当たりになっている場合、移動させることでそれを回避できるなら価値を上げておく。
#if 0
+ (threatened & from_sq(m) ?
Expand Down Expand Up @@ -336,8 +345,16 @@ void MovePicker::score()
else
// 捕獲しない指し手に関してはhistoryの値の順番
m.value = (*mainHistory)[from_to(m)][pos.side_to_move()]
// ↑mainHistoryに関して
// Stockfishは [c][from_to]の順
// やねうら王は[from_to][c]の順
// なので注意。
+ 2 * (*continuationHistory[0])[to_sq(m)][pos.moved_piece_after(m)]
- (1 << 28);
// ↑continuationHistoryに関して
// Stockfishは、 [pc][sq]の順
// やねうら王は、[sq][pc]の順
// なので注意。

}
}
Expand Down
17 changes: 10 additions & 7 deletions source/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,32 @@ enum StatsType { NoCaptures, Captures };
/// (~11 elo)
// ButterflyHistoryは、 現在の探索中にquietな指し手がどれくらい成功/失敗したかを記録し、
// reductionと指し手オーダリングの決定のために用いられる。
// 添字は[from_to][color]の順。
// cf. http://chessprogramming.wikispaces.com/Butterfly+Boards
// 簡単に言うと、fromの駒をtoに移動させることに対するhistory。
// やねうら王では、ここで用いられるfromは、駒打ちのときに特殊な値になっていて、盤上のfromとは区別される。
// そのため、(SQ_NB + 7)まで移動元がある。
// ※ Stockfishとは、添字の順番を入れ替えてあるので注意。
// やねうら王では、添字は[from_to][color]の順。
using ButterflyHistory = Stats<int16_t, 7183, int(SQ_NB + 7) * int(SQ_NB) , COLOR_NB>;

/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
/// move, see www.chessprogramming.org/Countermove_Heuristic
// CounterMoveHistoryは、直前の指し手の[to][piece]によってindexされるcounter moves(応手)を格納する。
// CounterMoveHistoryは、直前の指し手の[piece][to]によってindexされるcounter moves(応手)を格納する。
/// cf. http://chessprogramming.wikispaces.com/Countermove+Heuristic
// ※ Stockfishとは、添字の順番を入れ替えてあるので注意。
// やねうら王では、[to][piece]の順。
using CounterMoveHistory = Stats<Move, NOT_USED, SQ_NB , PIECE_NB>;

/// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
// CapturePieceToHistoryは、指し手の[to][piece][captured piece type]で示される。
// ※ Stockfishとは、添字の順番を変更してあるので注意。
// Stockfishでは、[piece][to][captured piece type]の順。
// CapturePieceToHistoryは、指し手の [piece][to][captured piece type]で示される。
// ※ やねうら王ではStockfishとは、添字の順番を変更してあるので注意。
// やねうら王では、[to][piece][captured piece type]の順。
using CapturePieceToHistory = Stats<int16_t, 10692, SQ_NB, PIECE_NB , PIECE_TYPE_NB>;

/// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
/// PieceToHistoryは、ButterflyHistoryに似たものだが、指し手の[to][piece]で示される。
/// PieceToHistoryは、ButterflyHistoryに似たものだが、指し手の[piece][to]で示される。
// ※ Stockfishとは、添字の順番を入れ替えてあるので注意。
// Stockfishでは[piece][to]の順。
// やねうら王では[to][piece]の順。
using PieceToHistory = Stats<int16_t, 29952, SQ_NB , PIECE_NB>;

/// ContinuationHistory is the combined history of a given pair of moves, usually
Expand All @@ -135,6 +136,8 @@ using PieceToHistory = Stats<int16_t, 29952, SQ_NB , PIECE_NB>;
// 普通、1手前によって与えられる現在の指し手(によるcombined history)
// このnested history tableは、ButterflyBoardsの代わりに、PieceToHistoryをベースとしている。
// ※ Stockfishとは、添字の順番を入れ替えてあるので注意。
// Stockfishでは、 [pc][to]の順。
//  やねうら王では、[to][pc]の順。
using ContinuationHistory = Stats<PieceToHistory, NOT_USED, SQ_NB , PIECE_NB>;


Expand Down
4 changes: 4 additions & 0 deletions source/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ void Thread::clear()
// Tweak history initialization : https://github.com/official-stockfish/Stockfish/commit/7d44b43b3ceb2eebc756709432a0e291f885a1d2

for (auto& to : continuationHistory[inCheck][c])
// ↑Stockfishでは [pc][to]の順、
// やねうら王では[to][pc]の順だから
// ここではStockfishではpcが来るのが正しいのだが、なぜか
// Stockfishのソースコード、変数名がtoになっている。
for (auto& h : to)
h->fill(-71);

Expand Down
2 changes: 1 addition & 1 deletion source/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Thread

// コア数が多いか、長い持ち時間においては、ContinuationHistoryもスレッドごとに確保したほうが良いらしい。
// cf. https://github.com/official-stockfish/Stockfish/commit/5c58d1f5cb4871595c07e6c2f6931780b5ac05b5
// 添字の[2][2]は、[inCheck(王手がかかっているか)][captureOrPawnPromotion]
// 添字の[2][2]は、[inCheck(王手がかかっているか)][capture]
// → この改造、レーティングがほぼ上がっていない。悪い改造のような気がする。
ContinuationHistory continuationHistory[2][2];

Expand Down

0 comments on commit 2792338

Please sign in to comment.