Skip to content

Commit

Permalink
Update move ordering algorithm
Browse files Browse the repository at this point in the history
Bench: 1572188
  • Loading branch information
ruicoelhopedro committed Jun 27, 2022
1 parent 3db91a4 commit 069e93a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/MoveOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Move MoveOrder::next_move()
++m_stage;
m_captures = m_position.move_list();
m_position.board().generate_moves(m_captures, MoveGenType::CAPTURES);
sort_moves<true>(m_captures);
partial_sort<true>(m_captures, 0);
m_curr = m_captures.begin();
m_bad_captures = m_captures.end();
}
Expand Down Expand Up @@ -206,7 +206,7 @@ Move MoveOrder::next_move()
++m_stage;
m_moves = MoveList(m_captures.end());
m_position.board().generate_moves(m_moves, MoveGenType::QUIETS);
sort_moves<false>(threshold_moves<false>(m_moves, -1000 * m_depth));
partial_sort<false>(m_moves, -1000 * m_depth);
m_curr = m_moves.begin();
}
else if (m_stage == MoveStage::QUIET)
Expand Down
40 changes: 21 additions & 19 deletions src/MoveOrder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,32 @@ class MoveOrder


template<bool CAPTURES>
MoveList threshold_moves(MoveList& list, int threshold)
void partial_sort(MoveList& list, int threshold)
{
Move* pos = list.begin();
for (auto list_move = list.begin(); list_move != list.end(); list_move++)
// Partial move sorting based on Stockfish's partial insertion sort
Move* sorted_end = list.begin();
for (Move* i = list.begin() + 1; i < list.end(); i++)
{
if (move_score<CAPTURES>(*list_move) > threshold)
// Only sort moves above the threshold
int i_score = move_score<CAPTURES>(*i);
if (i_score > threshold)
{
if (pos != list_move)
std::swap(*pos, *list_move);
pos++;
// Store a copy of the move to assign later
Move curr = *i;

// Little hack: instead of doing the insertion sort from the current position,
// shortcut the entries below threshold. To do so, the current entry is swapped
// with the end of the sorted region. The unsorted entry is set right away,
// while the sorted one is only inserted at the end.
*i = *(++sorted_end);
Move* j = sorted_end;

// Actual insertion sorting
for (; j > list.begin() && i_score > move_score<CAPTURES>(*(j - 1)); j--)
*j = *(j - 1);
*j = curr;
}
}

return MoveList(list.begin(), pos);
}


template<bool CAPTURES>
void sort_moves(MoveList list) const
{
std::sort(list.begin(), list.end(), [this](Move a, Move b)
{
return move_score<CAPTURES>(a) > move_score<CAPTURES>(b);
});
}


Expand Down

0 comments on commit 069e93a

Please sign in to comment.