Esempio n. 1
0
def test_playing_a_game():
    with make_temp_directory(prefix="test_tw-play") as tmpdir:
        options = tw_textlabs.GameOptions()
        options.path = tmpdir
        options.nb_rooms = 5
        options.nb_objects = 10
        options.quest_length = 5
        options.quest_breadth = 4
        options.seeds = 1234
        game_file, _ = tw_textlabs.make(options)

        command = [
            "tw-play", "--max-steps", "100", "--mode", "random", game_file
        ]
        assert check_call(command) == 0

        command = [
            "tw-play", "--max-steps", "100", "--mode", "random-cmd", game_file
        ]
        assert check_call(command) == 0

        command = [
            "tw-play", "--max-steps", "100", "--mode", "walkthrough", game_file
        ]
        assert check_call(command) == 0
Esempio n. 2
0
def test_making_game_is_reproducible_with_seed():
    with make_temp_directory(prefix="test_render_wrapper") as tmpdir:
        options = tw_textlabs.GameOptions()
        options.path = tmpdir
        options.nb_rooms = 2
        options.nb_objects = 20
        options.quest_length = 3
        options.quest_breadth = 3
        options.seeds = 123

        game_file1, game1 = tw_textlabs.make(options)
        options2 = options.copy()
        game_file2, game2 = tw_textlabs.make(options2)
        assert game_file1 == game_file2
        assert game1 == game2
        # Make sure they are not the same Python objects.
        assert id(game1) != id(game2)
Esempio n. 3
0
 def setUp(self):
     self.tmpdir = tempfile.mkdtemp(prefix="test_tw_textlabs")
     options = tw_textlabs.GameOptions()
     options.path = self.tmpdir
     options.nb_rooms = 5
     options.nb_objects = 10
     options.quest_length = 10
     options.seeds = 1234
     self.game_file, self.game = tw_textlabs.make(options)
Esempio n. 4
0
def test_extract_vocab():
    with make_temp_directory(prefix="test_tw-extract") as tmpdir:
        options = tw_textlabs.GameOptions()
        options.path = tmpdir
        options.nb_rooms = 5
        options.nb_objects = 10
        options.quest_length = 5
        options.quest_breadth = 4
        options.seeds = 1234
        game_file1, _ = tw_textlabs.make(options)
        options.seeds = 12345
        game_file2, _ = tw_textlabs.make(options)

        outfile = pjoin(tmpdir, "vocab.txt")
        command = [
            "tw-extract", "vocab", game_file1, game_file2, "--output", outfile
        ]
        stdout = check_output(command).decode()
        assert os.path.isfile(outfile)
        nb_words = len(open(outfile).readlines())
        assert "Extracted {}".format(nb_words) in stdout
Esempio n. 5
0
def test_making_game_with_names_to_exclude():
    g_rng.set_seed(42)

    with make_temp_directory(prefix="test_render_wrapper") as tmpdir:
        options = tw_textlabs.GameOptions()
        options.path = tmpdir
        options.nb_rooms = 2
        options.nb_objects = 20
        options.quest_length = 3
        options.quest_breadth = 3
        options.seeds = 123
        game_file1, game1 = tw_textlabs.make(options)

        options2 = options.copy()
        game1_objects_names = [
            info.name for info in game1.infos.values() if info.name is not None
        ]
        options2.grammar.names_to_exclude = game1_objects_names
        game_file2, game2 = tw_textlabs.make(options2)
        game2_objects_names = [
            info.name for info in game2.infos.values() if info.name is not None
        ]
        assert len(set(game1_objects_names) & set(game2_objects_names)) == 0
Esempio n. 6
0
def test_playing_generated_games():
    NB_GAMES = 10
    rng = np.random.RandomState(1234)
    for i in range(NB_GAMES):

        # Sample game specs.
        world_size = rng.randint(1, 10)
        nb_objects = rng.randint(0, 20)
        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 = tw_textlabs.GameOptions()
            options.path = tmpdir
            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 = tw_textlabs.make(options)

            # Solve the game using WalkthroughAgent.
            agent = tw_textlabs.agents.WalkthroughAgent()
            tw_textlabs.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 = tw_textlabs.start(game_file)

            agent = tw_textlabs.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):
                command = agent.act(game_state, reward, done)
                game_state, reward, done = env.step(command)

                if done:
                    msg = "Finished before playing `max_steps` steps because of command '{}'.".format(
                        command)
                    if game_state.has_won:
                        msg += " (winning)"
                        assert game_state._game_progression.winning_policy is None

                    if game_state.has_lost:
                        msg += " (losing)"
                        assert game_state._game_progression.winning_policy is None

                    print(msg)
                    break

                # Make sure the game can still be solved.
                winning_policy = game_state._game_progression.winning_policy
                assert len(winning_policy) > 0
                assert game_state.state.is_sequence_applicable(winning_policy)