more stages in movepicker | bench 1468577 | bench 1468577
This commit is contained in:
1 parent
dbd56e0c21
commit
f23a59730b
5 files changed
+456
-340
No files matched your search
+39
-133
@@ -1,155 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "search.h"
|
||||
#include <array>
|
||||
#include "board.h"
|
||||
#include "movegen.h"
|
||||
|
||||
#include <limits>
|
||||
namespace SEARCH {
|
||||
|
||||
using namespace SEARCH;
|
||||
class SearchContext;
|
||||
|
||||
enum class PickStage {
|
||||
TTMove,
|
||||
GenNoisy,
|
||||
GoodNoisy,
|
||||
Killer,
|
||||
GenQuiet,
|
||||
Quiet,
|
||||
BadNoisy,
|
||||
Finished
|
||||
};
|
||||
|
||||
template <MovegenMode mode>
|
||||
class MovePicker {
|
||||
private:
|
||||
enum Stage {
|
||||
TT,
|
||||
Rest,
|
||||
End
|
||||
};
|
||||
|
||||
Board& board;
|
||||
SearchContext* ctx;
|
||||
Move ttMove;
|
||||
int ply;
|
||||
Stage stage;
|
||||
|
||||
int index;
|
||||
bool generated;
|
||||
|
||||
int scores[MAX_MOVES];
|
||||
|
||||
public:
|
||||
MovePicker(Board& b, SearchContext* c, int p, Move tt)
|
||||
: board(b), ctx(c), ttMove(tt), ply(p), stage(TT), index(0), generated(false)
|
||||
{
|
||||
}
|
||||
MovePicker(Board &board, SearchContext *ctx, int ply, Move ttMove);
|
||||
|
||||
Move Next() {
|
||||
while (true) {
|
||||
switch (stage) {
|
||||
|
||||
case TT: {
|
||||
stage = Rest;
|
||||
|
||||
if (ttMove && board.IsPseudoLegal(ttMove))
|
||||
return ttMove;
|
||||
break;
|
||||
}
|
||||
|
||||
case Rest: {
|
||||
|
||||
if (!generated) {
|
||||
generated = true;
|
||||
|
||||
MOVEGEN::GenerateMoves<mode>(board, true);
|
||||
|
||||
ScoreAllMoves();
|
||||
index = 0;
|
||||
}
|
||||
|
||||
while (index < board.currentMoveIndex) {
|
||||
int idx = FindNext();
|
||||
Move m = board.moveList[idx];
|
||||
|
||||
if (m == ttMove)
|
||||
continue;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
stage = End;
|
||||
break;
|
||||
}
|
||||
|
||||
case End:
|
||||
return Move();
|
||||
}
|
||||
}
|
||||
}
|
||||
Move Next();
|
||||
|
||||
private:
|
||||
Board &board;
|
||||
SearchContext *ctx;
|
||||
int ply;
|
||||
|
||||
int ScoreMove(Move& move) {
|
||||
Move ttMove;
|
||||
Move killerMove;
|
||||
|
||||
if (move.IsCapture()) {
|
||||
PickStage stage;
|
||||
|
||||
int attackerType = board.GetPieceType(move.MoveFrom());
|
||||
int targetType = board.GetPieceType(move.MoveTo());
|
||||
int noisyStart = 0;
|
||||
int noisyEnd = 0;
|
||||
int goodNoisyEnd = 0;
|
||||
|
||||
if (move.GetFlags() == epCapture)
|
||||
targetType = Pawn;
|
||||
int quietStart = 0;
|
||||
int quietEnd = 0;
|
||||
|
||||
int capthistScore =
|
||||
ctx->capthist[board.sideToMove][attackerType][targetType][move.MoveTo()];
|
||||
int noisyCursor = 0;
|
||||
int badNoisyCursor = 0;
|
||||
int quietCursor = 0;
|
||||
|
||||
return 50000 * SEE(board, move, seeOrderingThreshold)
|
||||
+ (100 * targetType - attackerType + 105)
|
||||
+ capthistScore;
|
||||
}
|
||||
std::array<int, MAX_MOVES> scores{};
|
||||
|
||||
if (ctx->killerMoves[ply] == move)
|
||||
return 41000;
|
||||
void ScoreNoisyRange();
|
||||
|
||||
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
|
||||
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
|
||||
void ScoreQuietRange();
|
||||
|
||||
int historyScore =
|
||||
ctx->history[board.sideToMove]
|
||||
[move.MoveFrom()]
|
||||
[move.MoveTo()]
|
||||
[sourceThreatened]
|
||||
[targetThreatened];
|
||||
int ScoreNoisyMove(Move move);
|
||||
int ScoreQuietMove(Move move);
|
||||
|
||||
int conthistScore = 0;
|
||||
Move PopBest(int &cursor, int end);
|
||||
};
|
||||
|
||||
if (ply > 0) {
|
||||
conthistScore = ctx->conthist.GetNPly(board, move, ctx, ply, 1);
|
||||
|
||||
if (ply > 1)
|
||||
conthistScore += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
|
||||
}
|
||||
|
||||
return 20000 + historyScore + conthistScore;
|
||||
}
|
||||
|
||||
void ScoreAllMoves() {
|
||||
int moveCount = board.currentMoveIndex;
|
||||
for (int i = 0; i < moveCount; i++)
|
||||
scores[i] = ScoreMove(board.moveList[i]);
|
||||
}
|
||||
|
||||
int FindNext() {
|
||||
const auto toU64 = [](int s) {
|
||||
int64_t widened = s;
|
||||
widened -= std::numeric_limits<int32_t>::min();
|
||||
return static_cast<uint64_t>(widened) << 32;
|
||||
};
|
||||
|
||||
int moveCount = board.currentMoveIndex;
|
||||
|
||||
uint64_t best = toU64(scores[index]) | static_cast<uint64_t>(256 - index);
|
||||
for (int i = index + 1; i < moveCount; i++) {
|
||||
uint64_t curr = toU64(scores[i]) | static_cast<uint64_t>(256 - i);
|
||||
if (curr > best)
|
||||
best = curr;
|
||||
}
|
||||
|
||||
int bestIdx = 256 - static_cast<int>(best & 0xFFFFFFFF);
|
||||
|
||||
if (bestIdx != index) {
|
||||
std::swap(board.moveList[index], board.moveList[bestIdx]);
|
||||
std::swap(scores[index], scores[bestIdx]);
|
||||
}
|
||||
|
||||
return index++;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user