def __setstate__(self, data): """Support for unpickling.""" # TODO(author11) It should be possible to do this in the C++ layer instead game, state = pyspiel.deserialize_game_and_state(data) self.__init__(game) for h in state.full_history(): self.apply_action(h.action)
def serialize_deserialize(self, game, state): # OpenSpiel native serialization ser_str = pyspiel.serialize_game_and_state(game, state) new_game, new_state = pyspiel.deserialize_game_and_state(ser_str) self.assertEqual(str(game), str(new_game)) self.assertEqual(str(state), str(new_state)) # Pickle serialization + deserialization (of the state). pickled_state = pickle.dumps(state) unpickled_state = pickle.loads(pickled_state) self.assertEqual(str(state), str(unpickled_state))
def test_pickle(self): """Checks pickling and unpickling works.""" game = pyspiel.load_game("python_tic_tac_toe") state = game.new_initial_state() for a in [4, 2, 3, 7]: state.apply_action(a) ser_str = pyspiel.serialize_game_and_state(game, state) new_game, new_state = pyspiel.deserialize_game_and_state(ser_str) self.assertEqual(str(game), str(new_game)) self.assertEqual(str(state), str(new_state)) pickled_state = pickle.dumps(state) unpickled_state = pickle.loads(pickled_state) self.assertEqual(str(state), str(unpickled_state))
def serialize_deserialize(self, game, state): ser_str = pyspiel.serialize_game_and_state(game, state) new_game, new_state = pyspiel.deserialize_game_and_state(ser_str) self.assertEqual(str(game), str(new_game)) self.assertEqual(str(state), str(new_state))