Skip to content

Commit

Permalink
Simplify registering a single_tensor observer for a game
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 512028396
Change-Id: I581542eba9d254d70d55c42ea92ad2057a70d77f
  • Loading branch information
DeepMind Technologies Ltd authored and lanctot committed Feb 28, 2023
1 parent 4d674e4 commit edca9e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
11 changes: 1 addition & 10 deletions open_spiel/games/leduc_poker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,7 @@ std::string StatelessActionToString(Action action) {
}
}

// Provides the observations / infostates as defined on the state
// as a single tensor.
std::shared_ptr<Observer> MakeSingleTensorObserver(
const Game& game, absl::optional<IIGObservationType> iig_obs_type,
const GameParameters& params) {
return std::shared_ptr<Observer>(game.MakeBuiltInObserver(iig_obs_type));
}

ObserverRegisterer single_tensor(
kGameType.short_name, "single_tensor", MakeSingleTensorObserver);
RegisterSingleTensorObserver single_tensor(kGameType.short_name);
} // namespace

// The Observer class is responsible for creating representations of the game
Expand Down
12 changes: 12 additions & 0 deletions open_spiel/observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,18 @@ void ObserverRegisterer::RegisterObserver(const std::string& game_name,
observers()[key] = creator;
}

std::shared_ptr<Observer> MakeSingleTensorObserver(
const Game& game, absl::optional<IIGObservationType> iig_obs_type,
const GameParameters& params) {
return std::shared_ptr<Observer>(game.MakeBuiltInObserver(iig_obs_type));
}

RegisterSingleTensorObserver::RegisterSingleTensorObserver(
const std::string& game_name) {
ObserverRegisterer single_tensor(game_name, "single_tensor",
MakeSingleTensorObserver);
}

std::shared_ptr<Observer> ObserverRegisterer::CreateByName(
const std::string& observer_name,
const Game& game,
Expand Down
16 changes: 16 additions & 0 deletions open_spiel/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ class ObserverRegisterer {
}
};

// Registers an observer named "single_tensor" which falls back to
// state.observation_tensor or state.information_state_tensor (which generate a
// single tensor).
//
// Note that one cannot pass empty ObservationParams to
// game->MakeObserver(...) to achieve the same behavior in general:
// leduc, goofspiel and many other games will generate multiple tensors in that
// case.
//
// Use:
// RegisterSingleTensorObserver single_tensor(kGameType.short_name);
class RegisterSingleTensorObserver {
public:
RegisterSingleTensorObserver(const std::string& game_name);
};

// Pure function that creates a tensor from an observer. Slower than using an
// Observation, but threadsafe. This is useful when you cannot keep an
// Observation around to use multiple times.
Expand Down

0 comments on commit edca9e1

Please sign in to comment.