Skip to content

Commit bf46e68

Browse files
committed
refactor: search options struct
1 parent 187fc64 commit bf46e68

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

libbenbot/include/libbenbot/search/Options.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct Options final {
6666
/** Search only this many nodes. Setting this value too low can
6767
really emphasize the effects of our move ordering algorithm.
6868
*/
69-
std::optional<size_t> maxNodes;
69+
size_t maxNodes { std::numeric_limits<size_t>::max() };
7070

7171
/** Restrict the search to only these moves.
7272
If this is empty, all legal moves in the position will be searched.

libbenbot/src/search/Options.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cstddef> // IWYU pragma: keep - for size_t
1818
#include <libbenbot/search/Options.hpp>
1919
#include <libchess/uci/CommandParsing.hpp>
20+
#include <limits>
2021
#include <optional>
2122

2223
namespace ben_bot::search {
@@ -47,35 +48,33 @@ namespace {
4748
void Options::update_from(const chess::uci::GoCommandOptions& goOptions)
4849
{
4950
movesToSearch = goOptions.moves;
50-
maxNodes = goOptions.nodes;
5151
infinite = goOptions.infinite;
5252
mateIn = goOptions.mateIn;
5353

54-
if (goOptions.depth.has_value())
55-
depth = *goOptions.depth;
54+
static constexpr auto MAX = std::numeric_limits<size_t>::max();
5655

57-
// search time
58-
if (goOptions.searchTime.has_value()) {
59-
searchTime = goOptions.searchTime;
60-
} else if (goOptions.infinite) {
61-
searchTime = std::nullopt;
62-
} else {
63-
const bool isWhite = position.is_white_to_move();
56+
depth = goOptions.depth.value_or(MAX);
57+
maxNodes = goOptions.nodes.value_or(MAX);
6458

65-
const auto& timeLeft = isWhite ? goOptions.whiteTimeLeft : goOptions.blackTimeLeft;
59+
searchTime = goOptions.searchTime
60+
.or_else([&goOptions, isWhite = position.is_white_to_move()]() -> std::optional<milliseconds> {
61+
if (not goOptions.infinite) {
62+
const auto& timeLeft = isWhite ? goOptions.whiteTimeLeft : goOptions.blackTimeLeft;
6663

67-
// need to know at least our time remaining in order to calculate search time limit
68-
if (timeLeft.has_value()) {
69-
searchTime = determine_search_time(
70-
*timeLeft,
71-
isWhite ? goOptions.whiteInc : goOptions.blackInc,
72-
goOptions.movesToGo);
73-
}
74-
}
64+
// need to know at least our time remaining in order to calculate search time limit
65+
if (timeLeft.has_value()) {
66+
return determine_search_time(
67+
*timeLeft,
68+
isWhite ? goOptions.whiteInc : goOptions.blackInc,
69+
goOptions.movesToGo);
70+
}
71+
}
7572

76-
searchTime = searchTime.and_then([overhead = moveOverhead](const milliseconds time) {
77-
return std::optional { time - overhead };
78-
});
73+
return std::nullopt;
74+
})
75+
.transform([overhead = moveOverhead](const milliseconds time) {
76+
return time - overhead;
77+
});
7978
}
8079

8180
} // namespace ben_bot::search

libbenbot/src/search/Search.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,8 @@ void Context::search() // NOLINT(readability-function-cognitive-complexity)
345345
}
346346

347347
// if we've hit our node limit, don't do a deeper iteration
348-
if (options.maxNodes.has_value()
349-
and stats.nodesSearched >= *options.maxNodes) {
348+
if (stats.nodesSearched >= options.maxNodes)
350349
break;
351-
}
352350

353351
// if the iteration we just completed took as much or more time than we
354352
// have remaining for the search, then don't start a deeper iteration

0 commit comments

Comments
 (0)