Skip to content

Commit

Permalink
Swaps std::uniform_int_distribution for absl::uniform_int_distribution.
Browse files Browse the repository at this point in the history
This should be 2x faster.

PiperOrigin-RevId: 272676089
Change-Id: I78f5053d56e3b2e07bee4cca76918e25101e73c6
  • Loading branch information
DeepMind Technologies Ltd authored and open_spiel@google.com committed Oct 7, 2019
1 parent e6f4d94 commit b56449a
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion open_spiel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ add_subdirectory (abseil-cpp)
add_library(open_spiel_core OBJECT ${OPEN_SPIEL_CORE_FILES})
target_include_directories (open_spiel_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} abseil-cpp)
link_libraries(open_spiel_core absl::container absl::strings absl::str_format
absl::time absl::optional)
absl::time absl::optional absl::random_random)

# Just the minimal base library: no games.
set (OPEN_SPIEL_CORE_OBJECTS $<TARGET_OBJECTS:open_spiel_core>)
Expand Down
4 changes: 3 additions & 1 deletion open_spiel/algorithms/mcts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <limits>
#include <random>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"

namespace open_spiel {
namespace algorithms {
namespace {
Expand Down Expand Up @@ -130,7 +132,7 @@ double RandomRolloutEvaluator::evaluate(const State& state) const {
working_state->ApplyAction(action);
} else {
auto actions = working_state->LegalActions();
std::uniform_int_distribution<int> dist(0, actions.size() - 1);
absl::uniform_int_distribution<int> dist(0, actions.size() - 1);
int index = dist(rng_);
working_state->ApplyAction(actions[index]);
}
Expand Down
3 changes: 2 additions & 1 deletion open_spiel/contrib/tf_trajectories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <random>
#include <vector>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/abseil-cpp/absl/strings/str_join.h"
#include "third_party/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorMap.h"
#include "open_spiel/spiel_utils.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ void TFBatchTrajectoryRecorder::GetNextStatesUniform() {
for (int b = 0; b < batch_size_; ++b) {
if (!terminal_flags_[b]) {
std::vector<Action> actions = states_[b]->LegalActions();
std::uniform_int_distribution<> dist(0, actions.size() - 1);
absl::uniform_int_distribution<> dist(0, actions.size() - 1);
Action action = actions[dist(rng_)];
states_[b]->ApplyAction(action);
SampleChance(b);
Expand Down
5 changes: 3 additions & 2 deletions open_spiel/examples/example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory>
#include <random>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/spiel.h"
#include "open_spiel/spiel_utils.h"

Expand Down Expand Up @@ -120,7 +121,7 @@ int main(int argc, char** argv) {
PrintLegalActions(*state, player, actions);
}

std::uniform_int_distribution<> dis(0, actions.size() - 1);
absl::uniform_int_distribution<> dis(0, actions.size() - 1);
open_spiel::Action action = actions[dis(rng)];
joint_action.push_back(action);
std::cerr << "player " << player << " chose "
Expand Down Expand Up @@ -149,7 +150,7 @@ int main(int argc, char** argv) {
PrintLegalActions(*state, player, actions);
}

std::uniform_int_distribution<> dis(0, actions.size() - 1);
absl::uniform_int_distribution<> dis(0, actions.size() - 1);
auto action = actions[dis(rng)];
std::cerr << "chose action: " << state->ActionToString(player, action)
<< std::endl;
Expand Down
5 changes: 3 additions & 2 deletions open_spiel/examples/matrix_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <memory>
#include <random>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/matrix_game.h"
#include "open_spiel/spiel.h"

Expand Down Expand Up @@ -57,10 +58,10 @@ int main(int argc, char** argv) {
std::vector<open_spiel::Action> col_actions = state->LegalActions(1);

open_spiel::Action row_action =
row_actions[std::uniform_int_distribution<int>(
row_actions[absl::uniform_int_distribution<int>(
0, row_actions.size() - 1)(rng)];
open_spiel::Action col_action =
col_actions[std::uniform_int_distribution<int>(
col_actions[absl::uniform_int_distribution<int>(
0, col_actions.size() - 1)(rng)];

std::cerr << "Joint action is: (" << state->ActionToString(0, row_action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <random>
#include <string>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/spiel.h"

namespace open_spiel {
Expand Down Expand Up @@ -63,7 +64,7 @@ void SimulateGames(std::mt19937* rng, const Game& game, State* sim_state,

std::vector<Action> actions;
actions = sim_state->LegalActions(p);
std::uniform_int_distribution<> dis(0, actions.size() - 1);
absl::uniform_int_distribution<> dis(0, actions.size() - 1);
Action action = actions[dis(*rng)];
joint_action.push_back(action);
std::cout << "player " << p << " chose "
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/bridge_uncontested_bidding.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Deal {
Deal() { std::iota(std::begin(cards_), std::end(cards_), 0); }
void Shuffle(std::mt19937* rng, int begin = 0, int end = kNumCards) {
for (int i = begin; i < end - 1; ++i) {
// We don't use std::uniform_int_distribution because it behaves
// We don't use absl::uniform_int_distribution because it behaves
// differently in different versions of C++, and we want reproducible
// tests.
int j = i + (*rng)() % (end - i);
Expand Down
6 changes: 4 additions & 2 deletions open_spiel/games/chess/chess_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <random>
#include <string>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"

namespace open_spiel {
namespace chess_common {

Expand Down Expand Up @@ -116,7 +118,7 @@ class ZobristTable {

ZobristTable(Generator::result_type seed) {
Generator generator(seed);
std::uniform_int_distribution<Generator::result_type> dist;
absl::uniform_int_distribution<Generator::result_type> dist;
data_.reserve(InnerDim);
for (std::size_t i = 0; i < InnerDim; ++i) {
data_.emplace_back(dist(generator));
Expand All @@ -139,7 +141,7 @@ class ZobristTable<T, InnerDim> {

ZobristTable(Generator::result_type seed) : data_(InnerDim) {
Generator generator(seed);
std::uniform_int_distribution<T> dist;
absl::uniform_int_distribution<T> dist;
for (auto& field : data_) {
field = dist(generator);
}
Expand Down
1 change: 1 addition & 0 deletions open_spiel/games/go/go_board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <iomanip>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/games/chess/chess_common.h"
#include "open_spiel/spiel_utils.h"

Expand Down
3 changes: 2 additions & 1 deletion open_spiel/spiel_bots.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/spiel_utils.h"

namespace open_spiel {
Expand All @@ -33,7 +34,7 @@ class UniformRandomBot : public Bot {
const double p = 1.0 / num_legal_actions;
for (auto action : legal_actions) policy.emplace_back(action, p);
int selection =
std::uniform_int_distribution<int>(0, num_legal_actions - 1)(rng_);
absl::uniform_int_distribution<int>(0, num_legal_actions - 1)(rng_);
return std::make_pair(policy, legal_actions[selection]);
}

Expand Down
9 changes: 5 additions & 4 deletions open_spiel/tests/basic_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <set>
#include <string>

#include "open_spiel/abseil-cpp/absl/random/uniform_int_distribution.h"
#include "open_spiel/abseil-cpp/absl/time/clock.h"
#include "open_spiel/game_transforms/turn_based_simultaneous_game.h"
#include "open_spiel/spiel.h"
Expand Down Expand Up @@ -218,7 +219,7 @@ int RandomSimulationFast(std::mt19937* rng, const Game& game, bool verbose) {
for (int p = 0; p < game.NumPlayers(); p++) {
std::vector<Action> actions;
actions = state->LegalActions(p);
std::uniform_int_distribution<> dis(0, actions.size() - 1);
std::uniform_int_distribution<int> dis(0, actions.size() - 1);
Action action = actions[dis(*rng)];
joint_action.push_back(action);
if (verbose) {
Expand All @@ -231,7 +232,7 @@ int RandomSimulationFast(std::mt19937* rng, const Game& game, bool verbose) {
} else {
// Sample an action uniformly.
std::vector<Action> actions = state->LegalActions();
std::uniform_int_distribution<> dis(0, actions.size() - 1);
std::uniform_int_distribution<int> dis(0, actions.size() - 1);
Action action = actions[dis(*rng)];
if (verbose) {
int p = state->CurrentPlayer();
Expand Down Expand Up @@ -348,7 +349,7 @@ void RandomSimulation(std::mt19937* rng, const Game& game, bool undo) {
for (auto p = Player{0}; p < game.NumPlayers(); p++) {
std::vector<Action> actions;
actions = state->LegalActions(p);
std::uniform_int_distribution<> dis(0, actions.size() - 1);
std::uniform_int_distribution<int> dis(0, actions.size() - 1);
Action action = actions[dis(*rng)];
joint_action.push_back(action);
if (p == 0) {
Expand Down Expand Up @@ -394,7 +395,7 @@ void RandomSimulation(std::mt19937* rng, const Game& game, bool undo) {
SPIEL_CHECK_TRUE(actions.empty());
else
SPIEL_CHECK_FALSE(actions.empty());
std::uniform_int_distribution<> dis(0, actions.size() - 1);
std::uniform_int_distribution<int> dis(0, actions.size() - 1);
Action action = actions[dis(*rng)];

std::cout << "chose action: " << state->ActionToString(player, action)
Expand Down

0 comments on commit b56449a

Please sign in to comment.