Skip to content

Commit

Permalink
Fix behavior of max^n MCTS with regards to backpropagation in games w…
Browse files Browse the repository at this point in the history
…ith chance, reflecting the same recent fix from the Python implementation.

PiperOrigin-RevId: 461358712
Change-Id: Iba965e1843cc67ca3ebca73cbbe0f893bafd6eb9
  • Loading branch information
lanctot committed Jul 18, 2022
1 parent 51b1122 commit 328d6a8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions open_spiel/algorithms/mcts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "open_spiel/abseil-cpp/absl/time/clock.h"
#include "open_spiel/abseil-cpp/absl/time/time.h"
#include "open_spiel/spiel.h"
#include "open_spiel/spiel_globals.h"
#include "open_spiel/spiel_utils.h"

namespace open_spiel {
Expand Down Expand Up @@ -348,10 +349,10 @@ std::unique_ptr<State> MCTSBot::ApplyTreePolicy(
}

std::unique_ptr<SearchNode> MCTSBot::MCTSearch(const State& state) {
Player player_id = state.CurrentPlayer();
nodes_ = 1;
gc_limit_ = MIN_GC_LIMIT;
auto root = std::make_unique<SearchNode>(kInvalidAction, player_id, 1);
auto root = std::make_unique<SearchNode>(kInvalidAction,
state.CurrentPlayer(), 1);
std::vector<SearchNode*> visit_path;
std::vector<double> returns;
visit_path.reserve(64);
Expand All @@ -373,12 +374,18 @@ std::unique_ptr<SearchNode> MCTSBot::MCTSearch(const State& state) {
}

// Propagate values back.
for (auto it = visit_path.rbegin(); it != visit_path.rend(); ++it) {
SearchNode* node = *it;
while (!visit_path.empty()) {
int decision_node_idx = visit_path.size() - 1;
SearchNode* node = visit_path[decision_node_idx];

node->total_reward +=
returns[node->player == kChancePlayerId ? player_id : node->player];
// If it's a chance node, find the parent player id.
while (visit_path[decision_node_idx]->player == kChancePlayerId) {
decision_node_idx--;
}

node->total_reward += returns[visit_path[decision_node_idx]->player];
node->explore_count += 1;
visit_path.pop_back();

// Back up solved results as well.
if (solved && !node->children.empty()) {
Expand Down

0 comments on commit 328d6a8

Please sign in to comment.