Skip to content
Merged
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
53 changes: 31 additions & 22 deletions open_spiel/examples/count_all_states.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "open_spiel/abseil-cpp/absl/container/flat_hash_set.h"
#include "open_spiel/abseil-cpp/absl/strings/str_format.h"
#include "open_spiel/algorithms/get_all_states.h"
#include "open_spiel/algorithms/get_all_histories.h"
#include "open_spiel/canonical_game_strings.h"
#include "open_spiel/spiel.h"
#include "open_spiel/spiel_utils.h"
Expand All @@ -25,37 +25,46 @@
// Counts all states and prints them to the console.
int main(int argc, char** argv) {
for (std::string_view game_name :
{std::string("kuhn_poker"), std::string("leduc_poker"),
std::string("liars_dice"), open_spiel::TurnBasedGoofspielGameString(4),
{std::string("tic_tac_toe"), std::string("kuhn_poker"),
std::string("leduc_poker"), std::string("liars_dice"),
open_spiel::TurnBasedGoofspielGameString(4),
open_spiel::TurnBasedGoofspielGameString(5),
open_spiel::TurnBasedGoofspielGameString(6)}) {
std::shared_ptr<const open_spiel::Game> game =
open_spiel::LoadGame(std::string(game_name));
std::map<std::string, std::unique_ptr<open_spiel::State>> all_states =
open_spiel::algorithms::GetAllStates(*game, /*depth_limit=*/-1,
/*include_terminals=*/true,
/*include_chance_states=*/true);
absl::flat_hash_set<std::string> all_infostates;
const int num_histories = all_states.size();
int num_terminal_states = 0;
std::vector<std::unique_ptr<open_spiel::State>> all_histories =
open_spiel::algorithms::GetAllHistories(*game, /*depth_limit=*/-1,
/*include_terminals=*/true,
/*include_chance_states=*/true);
absl::flat_hash_set<std::string> nonterminal_states;
absl::flat_hash_set<std::string> terminal_states;
const int num_histories = all_histories.size();
int num_terminal_histories = 0;
int num_chance_nodes = 0;
// TODO: Fix counting of information states for some games after having a
// GetAllHistories. Right now the counting of information states will not
// be correct for perfect information games. See this issue for details:
// https://github.com/deepmind/open_spiel/issues/401
for (const auto& [_, state] : all_states) {
for (const auto& state : all_histories) {
if (state->CurrentPlayer() >= 0) {
all_infostates.insert(state->InformationStateString());
if (game->GetType().information ==
open_spiel::GameType::Information::kPerfectInformation) {
nonterminal_states.insert(state->ToString());
} else {
nonterminal_states.insert(state->InformationStateString());
}
}
if (state->IsTerminal()) {
++num_terminal_histories;
terminal_states.insert(state->ToString());
}
if (state->IsTerminal()) ++num_terminal_states;
if (state->IsChanceNode()) ++num_chance_nodes;
}
const int num_infostates = all_infostates.size();
const int num_nonterminal_states = nonterminal_states.size();
const int num_terminal_states = terminal_states.size();
std::cout << absl::StreamFormat(
"Game: %s, num_histories: %i, num_terminals: %i, "
"num_chance: %i, num_infostates: %i",
game_name, num_histories, num_terminal_states,
num_chance_nodes, num_infostates)
"Game: %s, num_histories: %i, num_terminal_histories: %i, "
"num_chance_nodes: %i, num_nonterminal_states: %i, "
"num_terminal_states: %i",
game_name, num_histories, num_terminal_histories,
num_chance_nodes, num_nonterminal_states,
num_terminal_states)
<< std::endl;
}
}