Return perf data from ShardokAIClient (#4274)

* capture the metrics in ShardokAIClient

* clean up logging

* Address PR review comments

- Replace macro with constexpr bool for performance logging
- Add documentation comments for CommandChoiceResults struct
- Use if constexpr instead of preprocessor directives

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
2025-07-16 14:29:15 -07:00
committed by GitHub
co-authored by Claude
parent bf1b87612c
commit a9d41b59fd
5 changed files with 79 additions and 48 deletions
@@ -78,6 +78,7 @@ auto IterativeDeepeningAI::IterativeSearch(
int currentDepth = 1;
size_t previousBestCommand = 0; // Track best command from previous depth
size_t evaluatedCountAtHighestDepth = 0;
// Main iterative deepening loop
while ((currentDepth == 1 || !IsTimeExpired(timeBudget)) && currentDepth <= maxDepth) {
@@ -122,15 +123,9 @@ auto IterativeDeepeningAI::IterativeSearch(
}
}
if (evaluatedCount < commands.size()) {
printf("ID AI: Depth %d - evaluated %d/%zu commands\n",
currentDepth,
evaluatedCount,
commands.size());
}
// Find the best command at current depth and check if it changed
if (evaluatedCount > 0) {
evaluatedCountAtHighestDepth = evaluatedCount;
size_t currentBestCommand = 0;
ScoreValue currentBestScore = -std::numeric_limits<ScoreValue>::infinity();
@@ -217,13 +212,8 @@ auto IterativeDeepeningAI::IterativeSearch(
result.searchCompleted = result.minimumDepthCompleted;
result.timeUsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - startTime);
#if DEBUG_ITERATIVE_DEEPENING_TIMINGS
printf("ID AI: Search complete - achieved depth %d for best command %zu (score %.2f)\n",
result.depthAchieved,
result.bestCommandIndex,
result.bestScore);
#endif
result.availableCommandCount = commands.size();
result.commandCountEvaluated = evaluatedCountAtHighestDepth;
return result;
}
@@ -298,6 +288,8 @@ auto IterativeDeepeningAI::SearchAllCommandsAtDepth(
result.depthAchieved = depth;
result.searchCompleted = true;
result.minimumDepthCompleted = true;
result.availableCommandCount = bestResult.availableCommandCount;
result.commandCountEvaluated = bestResult.commandCountEvaluated;
// For the best command, use the actual score
// For others, use a slightly lower score (this is a simplification for Phase 2)
@@ -327,6 +319,8 @@ auto IterativeDeepeningAI::SearchCommandAtDepthWithEngine(
result.depthAchieved = depth;
result.searchCompleted = true;
result.minimumDepthCompleted = true;
result.availableCommandCount = commands.size();
result.commandCountEvaluated = 1; // We're evaluating just this command
if (commandIndex >= commands.size()) {
result.bestScore = 0.0;
@@ -32,6 +32,8 @@ public:
std::chrono::milliseconds timeUsed;
bool minimumDepthCompleted;
bool searchCompleted;
size_t availableCommandCount;
size_t commandCountEvaluated;
SearchResult()
: bestCommandIndex(0),
@@ -39,7 +41,9 @@ public:
depthAchieved(0),
timeUsed(0),
minimumDepthCompleted(false),
searchCompleted(false) {}
searchCompleted(false),
availableCommandCount(0),
commandCountEvaluated(0) {}
};
IterativeDeepeningAI(
@@ -21,11 +21,11 @@
namespace shardok {
static constexpr bool kDebugTimings = true;
using net::eagle0::shardok::api::ActionResultView;
using net::eagle0::shardok::api::GameStateView;
static constexpr bool kPerformanceLogging = true;
void ApplyUpdate(GameStateView &currentView, const ActionResultView &update) {}
auto RoundsRemaining(const GameSettingsSPtr &settings, const GameStateView &gsv) -> int {
@@ -63,7 +63,7 @@ void CheckCommand(const CommandProto &realDescriptor, const CommandProto &guesse
auto ShardokAIClient::StandardChooseCommandIndex(
const GameSettingsSPtr &settings,
const GameStateW &guessedState,
const vector<CommandProto> &realAvailableCommands) const -> size_t {
const vector<CommandProto> &realAvailableCommands) const -> CommandChoiceResults {
const auto settingsGetter = settings->GetGetter();
const auto guessedEngine = ShardokEngine(settings, guessedState);
@@ -101,13 +101,33 @@ auto ShardokAIClient::StandardChooseCommandIndex(
auto search_result =
iterativeAI.IterativeSearch(settings, guessedState, realAvailableCommands, timeBudget);
return search_result.bestCommandIndex;
CommandChoiceResults result{};
result.chosenIndex = search_result.bestCommandIndex;
result.availableCommandCount = search_result.availableCommandCount;
result.depthAchieved = search_result.depthAchieved;
result.commandCountEvaluated = search_result.commandCountEvaluated;
if constexpr (kPerformanceLogging) {
if (result.commandCountEvaluated < result.availableCommandCount) {
printf("ID AI: Depth %d - evaluated %lu/%zu commands\n",
result.depthAchieved,
result.commandCountEvaluated,
result.availableCommandCount);
}
printf("ID AI: Search complete - achieved depth %d for best command %zu\n",
result.depthAchieved,
result.chosenIndex);
fflush(stdout);
}
return result;
}
auto ShardokAIClient::LateRoundAttackerChooseCommandIndex(
const GameSettingsSPtr &settings,
const GameStateW &guessedState,
const vector<CommandProto> &realAvailableCommands) const -> size_t {
const vector<CommandProto> &realAvailableCommands) const -> CommandChoiceResults {
if (const auto dismissCommand = std::ranges::find_if(
realAvailableCommands,
[](const net::eagle0::shardok::api::CommandDescriptor &cmd) {
@@ -116,14 +136,20 @@ auto ShardokAIClient::LateRoundAttackerChooseCommandIndex(
dismissCommand == realAvailableCommands.end()) {
return StandardChooseCommandIndex(settings, guessedState, realAvailableCommands);
} else {
return static_cast<size_t>(std::distance(realAvailableCommands.begin(), dismissCommand));
CommandChoiceResults results{};
results.chosenIndex =
static_cast<size_t>(std::distance(realAvailableCommands.begin(), dismissCommand));
results.availableCommandCount = realAvailableCommands.size();
results.depthAchieved = 1; // Simple heuristic choice
results.commandCountEvaluated = 1; // Only evaluated one command type
return results;
}
}
auto ShardokAIClient::FinalRoundAttackerChooseCommandIndex(
const GameSettingsSPtr &settings,
const GameStateW &guessedState,
const vector<CommandProto> &realAvailableCommands) const -> size_t {
const vector<CommandProto> &realAvailableCommands) const -> CommandChoiceResults {
if (const auto fleeCommand = std::ranges::find_if(
realAvailableCommands,
[](const net::eagle0::shardok::api::CommandDescriptor &cmd) {
@@ -132,33 +158,39 @@ auto ShardokAIClient::FinalRoundAttackerChooseCommandIndex(
fleeCommand == realAvailableCommands.end()) {
return LateRoundAttackerChooseCommandIndex(settings, guessedState, realAvailableCommands);
} else {
return static_cast<size_t>(std::distance(realAvailableCommands.begin(), fleeCommand));
CommandChoiceResults results{};
results.chosenIndex =
static_cast<size_t>(std::distance(realAvailableCommands.begin(), fleeCommand));
results.availableCommandCount = realAvailableCommands.size();
results.depthAchieved = 1; // Simple heuristic choice
results.commandCountEvaluated = 1; // Only evaluated one command type
return results;
}
}
auto ShardokAIClient::ChooseCommandIndex(
const GameSettingsSPtr &settings,
const GameStateView &gsv,
const vector<CommandProto> &realAvailableCommands) const -> size_t {
const vector<CommandProto> &realAvailableCommands) const -> CommandChoiceResults {
static int typeChosenCount[net::eagle0::shardok::common::CommandType_MAX + 1];
static int totalChoices = 0;
size_t chosenIndex;
CommandChoiceResults results{};
const auto guessedState = GameStateGuesser::GuessedState(playerId, settings->GetGetter(), gsv);
if (const int roundsRemaining = RoundsRemaining(settings, gsv);
!isDefender && roundsRemaining <= 1) {
chosenIndex =
results =
FinalRoundAttackerChooseCommandIndex(settings, guessedState, realAvailableCommands);
} else if (!isDefender && roundsRemaining <= 3) {
chosenIndex =
results =
LateRoundAttackerChooseCommandIndex(settings, guessedState, realAvailableCommands);
} else {
chosenIndex = StandardChooseCommandIndex(settings, guessedState, realAvailableCommands);
results = StandardChooseCommandIndex(settings, guessedState, realAvailableCommands);
}
const auto chosenType = realAvailableCommands[chosenIndex].type();
const auto chosenType = realAvailableCommands[results.chosenIndex].type();
typeChosenCount[static_cast<int>(chosenType)]++;
totalChoices++;
@@ -179,12 +211,11 @@ auto ShardokAIClient::ChooseCommandIndex(
printf("\n\n");
}
return chosenIndex;
return results;
}
auto ShardokAIClient::ChooseCommandIndex(const ShardokEngine &engine) const -> size_t {
const auto startTimeMicros = CurrentTimeMicros();
auto ShardokAIClient::ChooseCommandIndex(const ShardokEngine &engine) const
-> CommandChoiceResults {
if (const auto &availableCommands = engine.GetAvailableCommandProtos(playerId, false);
availableCommands.empty()) {
printf("no commands for player %d\n", playerId);
@@ -194,15 +225,8 @@ auto ShardokAIClient::ChooseCommandIndex(const ShardokEngine &engine) const -> s
const auto &settings = engine.GetGameSettings();
const auto &gsv = engine.GetGameStateView(GetPlayerId());
const size_t chosenIndex = ChooseCommandIndex(settings, gsv, availableCommands);
const auto elapsedMicros = CurrentTimeMicros() - startTimeMicros;
if (kDebugTimings) {
std::cerr << "Milliseconds to choose command index: " << elapsedMicros / 1000
<< std::endl;
}
return chosenIndex;
const auto results = ChooseCommandIndex(settings, gsv, availableCommands);
return results;
}
}
@@ -21,6 +21,14 @@ namespace shardok {
using VictoryCondition = net::eagle0::shardok::storage::fb::VictoryCondition;
/// Results from AI command selection, including performance metrics.
struct CommandChoiceResults {
size_t chosenIndex; ///< Index of the chosen command in the available commands list
size_t availableCommandCount; ///< Total number of commands that were available to choose from
int depthAchieved; ///< Maximum search depth reached for the best command
size_t commandCountEvaluated; ///< Number of commands evaluated at the highest achieved depth
};
//
// A ShardokGameClient representing an AI player.
//
@@ -37,19 +45,19 @@ private:
[[nodiscard]] auto StandardChooseCommandIndex(
const GameSettingsSPtr& settings,
const GameStateW& guessedState,
const vector<CommandProto>& realAvailableCommands) const -> size_t;
const vector<CommandProto>& realAvailableCommands) const -> CommandChoiceResults;
[[nodiscard]] auto LateRoundAttackerChooseCommandIndex(
const GameSettingsSPtr& settings,
const GameStateW& guessedState,
const vector<CommandProto>& realAvailableCommands) const -> size_t;
const vector<CommandProto>& realAvailableCommands) const -> CommandChoiceResults;
[[nodiscard]] auto FinalRoundAttackerChooseCommandIndex(
const GameSettingsSPtr& settings,
const GameStateW& guessedState,
const vector<CommandProto>& realAvailableCommands) const -> size_t;
const vector<CommandProto>& realAvailableCommands) const -> CommandChoiceResults;
[[nodiscard]] auto ChooseCommandIndex(
const GameSettingsSPtr& settings,
const net::eagle0::shardok::api::GameStateView& gsv,
const vector<CommandProto>& realAvailableCommands) const -> size_t;
const vector<CommandProto>& realAvailableCommands) const -> CommandChoiceResults;
public:
explicit ShardokAIClient(
@@ -61,7 +69,8 @@ public:
[[nodiscard]] auto GetPlayerId() const -> PlayerId { return playerId; }
[[nodiscard]] auto ChooseCommandIndex(const ShardokEngine& engine) const -> size_t;
[[nodiscard]] auto ChooseCommandIndex(const ShardokEngine& engine) const
-> CommandChoiceResults;
};
} // namespace shardok
@@ -129,7 +129,7 @@ auto ShardokGameController::LockedCheckOneAICommand() -> bool {
const PlayerId currentPid = engine->GetCurrentPlayerId();
if (const shared_ptr<ShardokAIClient> currentPlayerClient = LockedAIClientForPid(currentPid)) {
const int index = currentPlayerClient->ChooseCommandIndex(*engine);
const int index = currentPlayerClient->ChooseCommandIndex(*engine).chosenIndex;
engine->PostCommand(currentPid, index);
LockedNotifyClients();