How to use the textworld.play function in textworld

To help you get started, we’ve selected a few textworld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / TextWorld / tests / test_play_generated_games.py View on Github external
quest_length = rng.randint(2, 5)
        quest_breadth = rng.randint(3, 7)
        game_seed = rng.randint(0, 65365)

        with make_temp_directory(prefix="test_play_generated_games") as tmpdir:
            options = textworld.GameOptions()
            options.nb_rooms = world_size
            options.nb_objects = nb_objects
            options.quest_length = quest_length
            options.quest_breadth = quest_breadth
            options.seeds = game_seed
            game_file, game = textworld.make(options, path=tmpdir)

            # Solve the game using WalkthroughAgent.
            agent = textworld.agents.WalkthroughAgent()
            textworld.play(game_file, agent=agent, silent=True)

            # Play the game using RandomAgent and make sure we can always finish the
            # game by following the winning policy.
            env = textworld.start(game_file)

            agent = textworld.agents.RandomCommandAgent()
            agent.reset(env)
            env.compute_intermediate_reward()

            env.seed(4321)
            game_state = env.reset()

            max_steps = 100
            reward = 0
            done = False
            for step in range(max_steps):
github microsoft / TextWorld / tests / test_tw-make.py View on Github external
def test_making_a_game_using_basic_theme():
    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games")
        game_file = pjoin(output_folder, "game_1234.ulx")
        command = ["tw-make", "custom", "--theme", "basic", "--seed", "1234", "--output", game_file]
        assert check_call(command) == 0

        # Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file, agent=agent, silent=True)
github microsoft / TextWorld / tests / test_tw-make.py View on Github external
"tw-coin_collector": ["--level", "1", "--force-entity-numbering"],
        "tw-simple": ["--rewards", "dense", "--goal", "brief"],
    }
    with make_temp_directory(prefix="test_tw-challenge") as tmpdir:
        for challenge in textworld.challenges.CHALLENGES:
            output_folder = pjoin(tmpdir, "gen_games")
            game_file = pjoin(output_folder, challenge + ".ulx")
            command = ["tw-make", challenge, "--seed", "1234", "--output", game_file, "--silent"] + settings[challenge]
            assert check_call(command) == 0

            assert os.path.isdir(output_folder)
            assert os.path.isfile(game_file)

            # Solve the game using WalkthroughAgent.
            agent = textworld.agents.WalkthroughAgent()
            textworld.play(game_file, agent=agent, silent=True)
github microsoft / TextWorld / tests / test_tw-make.py View on Github external
# Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file + ".ulx", agent=agent, silent=True)

    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games", "")
        command = ["tw-make", "custom", "--seed", "1234", "--output", output_folder]
        assert check_call(command) == 0

        assert os.path.isdir(output_folder)
        game_file = glob.glob(pjoin(output_folder, "*.ulx"))[0]

        # Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file, agent=agent, silent=True)

    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games")
        command = ["tw-make", "custom", "--seed", "1234", "--output", output_folder]
        assert check_call(command) == 0

        assert os.path.isfile(output_folder + ".ulx")

        # Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(output_folder + ".ulx", agent=agent, silent=True)
github microsoft / TextWorld / tests / test_tw-make.py View on Github external
# Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file, agent=agent, silent=True)

    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games")
        game_file = pjoin(output_folder, "game_1234")  # Default extension is .ulx
        command = ["tw-make", "custom", "--seed", "1234", "--output", game_file]
        assert check_call(command) == 0

        assert os.path.isdir(output_folder)
        assert os.path.isfile(game_file + ".ulx")

        # Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file + ".ulx", agent=agent, silent=True)

    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games", "")
        command = ["tw-make", "custom", "--seed", "1234", "--output", output_folder]
        assert check_call(command) == 0

        assert os.path.isdir(output_folder)
        game_file = glob.glob(pjoin(output_folder, "*.ulx"))[0]

        # Solve the game using WalkthroughAgent.
        agent = textworld.agents.WalkthroughAgent()
        textworld.play(game_file, agent=agent, silent=True)

    with make_temp_directory(prefix="test_tw-make") as tmpdir:
        output_folder = pjoin(tmpdir, "gen_games")
        command = ["tw-make", "custom", "--seed", "1234", "--output", output_folder]
github microsoft / TextWorld / textworld / generator / maker.py View on Github external
""" Creates a new event using predefined text commands.

        This launches a `textworld.play` session to execute provided commands.

        Args:
            commands: Text commands.

        Returns:
            The resulting event.
        """
        with make_temp_directory() as tmpdir:
            try:
                game_file = self.compile(pjoin(tmpdir, "record_event.ulx"))
                recorder = Recorder()
                agent = textworld.agents.WalkthroughAgent(commands)
                textworld.play(game_file, agent=agent, wrappers=[recorder], silent=True)
            except textworld.agents.WalkthroughDone:
                pass  # Quest is done.

        # Skip "None" actions.
        actions, commands = zip(*[(a, c) for a, c in zip(recorder.actions, commands) if a is not None])
        event = Event(actions=actions, commands=commands)
        return event
github microsoft / TextWorld / textworld / generator / maker.py View on Github external
Args:
            commands: Text commands.
            ask_for_state: If true, the user will be asked to specify
                           which set of facts of the final state are
                           should be true in order to consider the quest
                           as completed.

        Returns:
            The resulting quest.
        """
        with make_temp_directory() as tmpdir:
            try:
                game_file = self.compile(pjoin(tmpdir, "record_quest.ulx"))
                recorder = Recorder()
                agent = textworld.agents.WalkthroughAgent(commands)
                textworld.play(game_file, agent=agent, wrappers=[recorder], silent=True)
            except textworld.agents.WalkthroughDone:
                pass  # Quest is done.

        # Skip "None" actions.
        actions = [action for action in recorder.actions if action is not None]

        # Ask the user which quests have important state, if this is set
        # (if not, we assume the last action contains all the relevant facts)
        winning_facts = None
        if ask_for_state and recorder.last_game_state is not None:
            winning_facts = [user_query.query_for_important_facts(actions=recorder.actions,
                                                                  facts=recorder.last_game_state.state.facts,
                                                                  varinfos=self._working_game.infos)]
        if len(commands) != len(actions):
            unrecognized_commands = [c for c, a in zip(commands, recorder.actions) if a is None]
            raise QuestError("Some of the actions were unrecognized: {}".format(unrecognized_commands))
github xingdi-eric-yuan / TextWorld-Coin-Collector / gym_textworld / gym_textworld / envs / textworld_games_env.py View on Github external
def __init__(self, gamefile, ob_max_length, act_max_length, vocab=None, mode="word"):
        self.gamefile = gamefile
        self.game_env = textworld.play(gamefile)
        self.action_space = text_spaces.Char(max_length=act_max_length)
        self.observation_space = text_spaces.Char(max_length=ob_max_length,
                                                  extra_vocab=[".", ",", "\n"])
        # self.action_space = text_spaces.Word(max_length=8, vocab=vocab)