Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add searchmoves functionality #677

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lc0/src/chess/uciloop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const std::unordered_map<std::string, std::unordered_set<std::string>>
{{"position"}, {"fen", "startpos", "moves"}},
{{"go"},
{"infinite", "wtime", "btime", "winc", "binc", "movestogo", "depth",
"nodes", "movetime"}},
"nodes", "movetime", "searchmoves"}},
{{"start"}, {}},
{{"stop"}, {}},
{{"quit"}, {}},
Expand Down Expand Up @@ -138,6 +138,10 @@ bool UciLoop::DispatchCommand(
}
go_params.infinite = true;
}
if (ContainsKey(params, "searchmoves")) {
go_params.searchmoves =
StrSplitAtWhitespace(GetOrEmpty(params, "searchmoves"));
}
#define OPTION(x) \
if (ContainsKey(params, #x)) { \
go_params.x = std::stoi(GetOrEmpty(params, #x)); \
Expand Down Expand Up @@ -211,4 +215,4 @@ void UciLoop::SendInfo(const ThinkingInfo& info) {
SendResponse(res);
}

} // namespace lczero
} // namespace lczero
3 changes: 2 additions & 1 deletion lc0/src/chess/uciloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct GoParams {
int nodes = -1;
std::int64_t movetime = -1;
bool infinite = false;
std::vector<std::string> searchmoves;
};

class UciLoop {
Expand Down Expand Up @@ -78,4 +79,4 @@ class UciLoop {
std::ofstream debug_log_;
};

} // namespace lczero
} // namespace lczero
8 changes: 8 additions & 0 deletions lc0/src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ SearchLimits EngineController::PopulateSearchLimits(int /*ply*/, bool is_black,
limits.time_ms = params.movetime;
int64_t time = (is_black ? params.btime : params.wtime);
limits.infinite = params.infinite;
if (!params.searchmoves.empty()) {
limits.searchmoves.reserve(params.searchmoves.size());
bool color = is_black;
for (const auto& move : params.searchmoves) {
limits.searchmoves.emplace_back(move, color);
color = !color;
}
}
if (params.infinite || time < 0) return limits;
int increment = std::max(int64_t(0), is_black ? params.binc : params.winc);

Expand Down
2 changes: 1 addition & 1 deletion lc0/src/mcts/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,4 @@ void NodeTree::DeallocateTree() {
current_head_ = nullptr;
}

} // namespace lczero
} // namespace lczero
4 changes: 2 additions & 2 deletions lc0/src/mcts/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Node {
// Returns whether a node has children.
bool HasChildren() const { return child_ != nullptr; }

// Returns move from the point of new of player BEFORE the position.
// Returns move from the point of view of player BEFORE the position.
Move GetMove() const { return move_; }

// Returns move, with optional flip (false == player BEFORE the position).
Expand Down Expand Up @@ -210,4 +210,4 @@ class NodeTree {
Node* gamebegin_node_ = nullptr;
PositionHistory history_;
};
} // namespace lczero
} // namespace lczero
9 changes: 9 additions & 0 deletions lc0/src/mcts/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ Node* Search::PickNodeToExtend(Node* node, PositionHistory* history) {
? -node->GetQ(0, kExtraVirtualLoss)
: -node->GetQ(0, kExtraVirtualLoss) -
kFpuReduction * std::sqrt(node->GetVisitedPolicy());

for (Node* iter : node->Children()) {
if (is_root_node) {
// If there's no chance to catch up the currently best node with
Expand All @@ -652,6 +653,14 @@ Node* Search::PickNodeToExtend(Node* node, PositionHistory* history) {
remaining_playouts_ < best_node_n - iter->GetNStarted()) {
continue;
}
// If searchmoves was sent, restrict the search only in that moves
if (!limits_.searchmoves.empty() &&
std::find(limits_.searchmoves.begin(),
limits_.searchmoves.end(),
iter->GetMove()) ==
limits_.searchmoves.end()) {
continue;
}
++possible_moves;
}
float Q = iter->GetQ(parent_q, kExtraVirtualLoss);
Expand Down
1 change: 1 addition & 0 deletions lc0/src/mcts/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct SearchLimits {
std::int64_t playouts = -1;
std::int64_t time_ms = -1;
bool infinite = false;
MoveList searchmoves;
};

class Search {
Expand Down