Skip to content

Conversation

@BartekCupial
Copy link

Summary

This PR adds the ability to save and load game state for environments. This allows games to be paused and resumed from the saved state.

This leverages how save scumming can be done, as described here https://nethackwiki.com/wiki/Save_scumming
basically all the files from HACKDIR are copied to separate directory. Then if someone wants to load the game it's enough to copy back the files and create new nethack game. Unfortunately game has to be turned off and on to do so.

Disclaimer: I am not sure if my changes in _new_dl don't break something else, I don't really understand what is happening there and why.

Example usage (from added test)

    def test_save_and_load(self, env_name, rollout_len):
        """Tests rollout_len steps (or until termination) of random policy."""
        with tempfile.TemporaryDirectory() as gamesavedir:
            env = gym.make(env_name, gamesavedir=gamesavedir)

            obs = env.reset()
            for _ in range(rollout_len):
                action = env.action_space.sample()
                obs, _, done, _ = env.step(action)
                if done:
                    obs = env.reset()

            env.save()

            env = gym.make(env_name, gameloaddir=gamesavedir)
            obsload = env.reset()

            assert (obsload["blstats"] == obs["blstats"]).all()
            assert (obsload["glyphs"] == obs["glyphs"]).all()

Use cases

I used this to evaluate trained agents starting from different levels in the dungeon. For example Sokoban https://nethackwiki.com/wiki/Sokoban. I thought I could share my code and contribute back to the community.

@BartekCupial BartekCupial force-pushed the save_and_load_game2 branch from 373e1df to 84c6e77 Compare May 20, 2024 06:55
@StephenOman StephenOman added the enhancement New feature or request label Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants