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 2 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
11 changes: 6 additions & 5 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 @@ -133,11 +133,12 @@ bool UciLoop::DispatchCommand(
} else if (command == "go") {
GoParams go_params;
if (ContainsKey(params, "infinite")) {
if (!GetOrEmpty(params, "infinite").empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will return back.

throw Exception("Unexpected token " + GetOrEmpty(params, "infinite"));
}
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 +212,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
1 change: 1 addition & 0 deletions lc0/src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ SearchLimits EngineController::PopulateSearchLimits(int /*ply*/, bool is_black,
limits.visits = params.nodes;
limits.time_ms = params.movetime;
int64_t time = (is_black ? params.btime : params.wtime);
limits.searchmoves = params.searchmoves;
limits.infinite = params.infinite;
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
10 changes: 10 additions & 0 deletions lc0/src/mcts/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ Node* Search::PickNodeToExtend(Node* node, PositionHistory* history) {
? -node->GetQ(0, kExtraVirtualLoss)
: -node->GetQ(0, kExtraVirtualLoss) -
kFpuReduction * std::sqrt(node->GetVisitedPolicy());

bool root_color = played_history_.IsBlackToMove();
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 +654,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(root_color).as_string()) ==
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to do vice versa, convert searchmoves from string to Move (somewhere before getting into Search class) and then compare.
For example, promotions can be encoded both as "e7e8q" and "e7e8Q", and I'm not sure how knight promotion works too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

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;
std::vector<std::string> searchmoves;
};

class Search {
Expand Down