Skip to content

Commit

Permalink
Improved ordering : using ttMove and pvMove
Browse files Browse the repository at this point in the history
  • Loading branch information
ppipelin committed Jul 30, 2024
1 parent 6e82bf9 commit 74f7cab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ C++ chess engine

| Rank | Name | Elo | +/- | Games | Score | Draw |
| ---- | ------------- | ----- | --- | ----- | ----- | ----- |
| 1 | radiance_3.0 | 242 | 25 | 960 | 80.1% | 10.6% |
| 2 | radiance_2.4 | 226 | 24 | 960 | 78.6% | 12.7% |
| 3 | radiance_2.3 | 169 | 22 | 960 | 72.6% | 14.8% |
| 4 | radiance_2.2 | -5 | 20 | 960 | 49.2% | 18.0% |
| 5 | radiance_2.1 | -8 | 20 | 960 | 48.9% | 17.8% |
| 6 | radiance_2.0 | -44 | 20 | 960 | 43.7% | 16.6% |
| 7 | radiance_1.5 | -178 | 23 | 960 | 26.5% | 10.4% |
| 8 | radiance_1.4 | -1002 | nan | 960 | 0.3% | 0.0% |
| 1 | radiance_3.1 | 334 | 23 | 1600 | 87.2% | 8.6% |
| 2 | radiance_3.0 | 196 | 18 | 1600 | 75.5% | 10.1% |
| 3 | radiance_2.4 | 195 | 18 | 1600 | 75.4% | 12.3% |
| 4 | radiance_2.3 | 143 | 17 | 1600 | 69.5% | 11.8% |
| 5 | radiance_2.2 | -23 | 16 | 1600 | 46.7% | 12.2% |
| 6 | radiance_2.1 | -51 | 16 | 1600 | 42.8% | 10.8% |
| 7 | radiance_2.0 | -70 | 16 | 1600 | 40.1% | 10.1% |
| 8 | radiance_1.5 | -343 | 26 | 1600 | 12.2% | 0.0% |
| 9 | radiance_1.4 | -881 | 126 | 1600 | 0.6% | 0.0% |

_I'm radiant!_
23 changes: 16 additions & 7 deletions include/SearchMaterialistNegamax.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class SearchMaterialistNegamax : virtual public Search
else
{
Search::generateMoveList(b, moveList, /*legalOnly=*/ true, /*onlyCapture=*/ false);
Search::orderMoves(b, moveList);
Search::orderMoves(b, moveList, (rootMoves[0].pv.size() > ss->ply) ? rootMoves[0].pv[ss->ply] : cMove());
}

for (const cMove &move : moveList)
Expand All @@ -126,6 +126,7 @@ class SearchMaterialistNegamax : virtual public Search

ss->currentMove = move;

Key key = b.m_s->materialKey;
// 16. Make the move
b.movePiece(move, s);

Expand All @@ -139,11 +140,11 @@ class SearchMaterialistNegamax : virtual public Search
else
{
#ifdef transposition
auto it = transpositionTable.find(b.m_s->materialKey);
auto it = transpositionTable.find(key);
const bool found = it != transpositionTable.end();
if (found && it->second.second > depth - 1)
if (found && std::get<1>(it->second) > depth - 1)
{
score = it->second.first;
score = std::get<0>(it->second);
if (score > VALUE_MATE_IN_MAX_DEPTH)
score -= ss->ply;
else if (score < VALUE_MATED_IN_MAX_DEPTH)
Expand Down Expand Up @@ -187,9 +188,9 @@ class SearchMaterialistNegamax : virtual public Search
if (score != VALUE_DRAW)
{
if (!found)
transpositionTable[b.m_s->materialKey] = std::pair<Value, UInt>(score, depth - 1);
else if (it->second.second <= depth - 1)
it->second = std::pair<Value, UInt>(score, depth - 1);
transpositionTable[key] = std::tuple<Value, UInt, cMove>(score, depth - 1, move);
else if (std::get<1>(it->second) <= depth - 1)
it->second = std::tuple<Value, UInt, cMove>(score, depth - 1, move);
}
#endif
}
Expand Down Expand Up @@ -242,7 +243,15 @@ class SearchMaterialistNegamax : virtual public Search

// Fail high
if (score >= beta)
{
auto it = transpositionTable.find(key);
const bool found = it != transpositionTable.end();
if (!found)
transpositionTable[key] = std::tuple<Value, UInt, cMove>(score, depth - 1, move);
else if (std::get<1>(it->second) <= depth - 1)
it->second = std::tuple<Value, UInt, cMove>(score, depth - 1, move);
break;
}
else
alpha = score; // Update alpha! Always alpha < beta
}
Expand Down
37 changes: 32 additions & 5 deletions include/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inline TimePoint now()
}

namespace {
std::unordered_map<Key, std::pair<Value, UInt>> transpositionTable;
std::unordered_map<Key, std::tuple<Value, UInt, cMove>> transpositionTable;
TimePoint remaining = 0;
}

Expand Down Expand Up @@ -334,20 +334,47 @@ class Search

struct MoveComparator
{
MoveComparator(BoardParser &b) : b(b) {};
MoveComparator(const BoardParser &b, const cMove &move1, const cMove &move2) : b(b), move1(move1), move2(move2) {};

bool operator() (const cMove &m1, const cMove &m2) const
{
if (move1 != cMove())
{
if (move1 == m1)
return true;
else if (move1 == m2)
return false;
}
if (move2 != cMove())
{
if (move2 == m1)
return true;
else if (move2 == m2)
return false;
}
return (m1.isCapture() && m1.getFlags() != 0x5 ? Int((*b.boardParsed())[m1.getTo()]->value()) - Int((*b.boardParsed())[m1.getFrom()]->value()) : 0) >
(m2.isCapture() && m2.getFlags() != 0x5 ? Int((*b.boardParsed())[m2.getTo()]->value()) - Int((*b.boardParsed())[m2.getFrom()]->value()) : 0);
}

BoardParser &b;
const BoardParser &b;
const cMove &move1;
const cMove &move2;
};

static void orderMoves(BoardParser &b, std::vector<cMove> &moveList)
static void orderMoves(const BoardParser &b, std::vector<cMove> &moveList, cMove pvMove = cMove())
{
std::sort(moveList.begin(), moveList.end(), MoveComparator(b));
// Search pvMove is in movelist. Speed up comparision if not.
if (pvMove != cMove() && std::find(moveList.begin(), moveList.end(), pvMove) == moveList.end())
pvMove = cMove();

auto it = transpositionTable.find(b.m_s->materialKey);
const bool found = it != transpositionTable.end();
cMove ttMove = found ? std::get<2>(it->second) : cMove();
// Search ttMove is in movelist. Speed up comparision if not.
if (ttMove != cMove() && std::find(moveList.begin(), moveList.end(), ttMove) == moveList.end())
ttMove = cMove();

std::sort(moveList.begin(), moveList.end(), MoveComparator(b, pvMove, ttMove));
}

/**
Expand Down

0 comments on commit 74f7cab

Please sign in to comment.