Skip to content

Commit

Permalink
Add observation vectors to Go.
Browse files Browse the repository at this point in the history
Fixes: #37

PiperOrigin-RevId: 268947042
Change-Id: I2e19df43bfb3fffd53d21aa876d695553908cd3d
  • Loading branch information
DeepMind Technologies Ltd authored and lanctot committed Sep 13, 2019
1 parent c210cf3 commit 9a08488
Show file tree
Hide file tree
Showing 3 changed files with 1,007 additions and 2 deletions.
25 changes: 24 additions & 1 deletion open_spiel/games/go.cc
Expand Up @@ -38,7 +38,7 @@ const GameType kGameType{
/*provides_information_state=*/true,
/*provides_information_state_as_normalized_vector=*/false,
/*provides_observation=*/true,
/*provides_observation_as_normalized_vector=*/false,
/*provides_observation_as_normalized_vector=*/true,
/*parameter_specification=*/
{
{"komi", GameType::ParameterSpec{GameParameter::Type::kDouble, false}},
Expand Down Expand Up @@ -94,6 +94,29 @@ std::string GoState::Observation(int player) const {
return ToString();
}

void GoState::ObservationAsNormalizedVector(int player,
std::vector<double>* values) const {
SPIEL_CHECK_GE(player, 0);
SPIEL_CHECK_LT(player, num_players_);

int num_cells = board_.board_size() * board_.board_size();
values->resize(num_cells * (CellStates() + 1));
std::fill(values->begin(), values->end(), 0.);

// Add planes: black, white, empty.
int cell = 0;
for (GoPoint p : BoardPoints(board_.board_size())) {
int color_val = static_cast<int>(board_.PointColor(p));
(*values)[num_cells * color_val + cell] = 1.0;
++cell;
}
SPIEL_CHECK_EQ(cell, num_cells);

// Add a fourth binary plane for komi (whether white is to play).
std::fill(values->begin() + (CellStates() * num_cells), values->end(),
(to_play_ == GoColor::kWhite ? 1.0 : 0.0));
}

std::vector<Action> GoState::LegalActions() const {
std::vector<Action> actions{};
for (GoPoint p : BoardPoints(board_.board_size())) {
Expand Down
12 changes: 12 additions & 0 deletions open_spiel/games/go.h
Expand Up @@ -42,6 +42,7 @@ namespace go {
constexpr int NumPlayers() { return 2; }
constexpr double LossUtility() { return -1; }
constexpr double WinUtility() { return 1; }
constexpr int CellStates() { return 3; } // Black, white, empty.

// Go can only end in a draw when using a round komi.
// We also treat superko as a draw.
Expand Down Expand Up @@ -75,6 +76,11 @@ class GoState : public State {
std::string InformationState(int player) const override;
std::string Observation(int player) const override;

// Four planes: black, white, empty, and a bias plane of bits indicating komi
// (whether white is to play).
void ObservationAsNormalizedVector(
int player, std::vector<double>* values) const override;

std::vector<double> Returns() const override;

std::unique_ptr<State> Clone() const override;
Expand Down Expand Up @@ -121,6 +127,12 @@ class GoGame : public Game {
return std::unique_ptr<State>(new GoState(board_size_, komi_, handicap_));
}

std::vector<int> ObservationNormalizedVectorShape() const override {
// Planes: black, white, empty, and a bias plane indicating komi (whether
// white is to play).
return {CellStates() + 1, board_size_, board_size_};
}

int NumPlayers() const override { return go::NumPlayers(); }

double MinUtility() const override { return LossUtility(); }
Expand Down

0 comments on commit 9a08488

Please sign in to comment.