def new_event_using_commands(self, commands: List[str]) -> Event: """ Creates a new event using predefined text commands. This launches a `tw_textlabs.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 = tw_textlabs.agents.WalkthroughAgent(commands) tw_textlabs.play(game_file, agent=agent, wrapper=recorder, silent=True) except tw_textlabs.agents.WalkthroughDone: pass # Quest is done. # Skip "None" actions. actions = [action for action in recorder.actions if action is not None] event = Event(actions=actions) return event
def test(self) -> None: """ Test the game being built. This launches a `tw_textlabs.play` session. """ with make_temp_directory() as tmpdir: game_file = self.compile(pjoin(tmpdir, "test_game.ulx")) tw_textlabs.play(game_file)
def set_quest_from_commands(self, commands: List[str], ask_for_state: bool = False) -> Quest: """ Defines the game's quest using predefined text commands. This launches a `tw_textlabs.play` session. 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 = tw_textlabs.agents.WalkthroughAgent(commands) tw_textlabs.play(game_file, agent=agent, wrapper=recorder, silent=True) except tw_textlabs.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) ] event = Event(actions=actions, conditions=winning_facts) self.quests = [Quest(win_events=[event])] # Calling build will generate the description for the quest. self.build() return self.quests[-1]
def test_making_a_custom_game(): 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", "--seed", "1234", "--output", game_file ] assert check_call(command) == 0 assert os.path.isdir(output_folder) assert os.path.isfile(game_file) # Solve the game using WalkthroughAgent. agent = tw_textlabs.agents.WalkthroughAgent() tw_textlabs.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 = tw_textlabs.agents.WalkthroughAgent() tw_textlabs.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 = tw_textlabs.agents.WalkthroughAgent() tw_textlabs.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 = tw_textlabs.agents.WalkthroughAgent() tw_textlabs.play(output_folder + ".ulx", agent=agent, silent=True)
def test_make_lab_game_from_level(): seed = "3311063" level = 22 with make_temp_directory(prefix="test_tw-make-lab") as tmpdir: output_folder = pjoin(tmpdir, "gen_games") game_file = pjoin(output_folder, "challenge_game%d_%s.ulx" % (level, seed)) command = [ "tw-make-lab", "challenge", "--seed", seed, "--max_search_steps", "2000", "--output", game_file, "--challenge", "tw-lab_game-level%d" % (level) ] print(' '.join(command)) try: assert check_call(command) == 0 except CalledProcessError as e: print(e.output) assert os.path.isdir(output_folder) assert os.path.isfile(game_file) # Solve the game using WalkthroughAgent. print("Solving game") agent = tw_textlabs.agents.WalkthroughAgent() stats = tw_textlabs.play(game_file, agent=agent, silent=True) assert (stats['score'] == 1)
def test_make_custom_lab_game(): seed = "32423" with make_temp_directory(prefix="test_tw-make-lab") as tmpdir: output_folder = pjoin(tmpdir, "gen_games") game_file = pjoin(output_folder, "game_%s.ulx" % (seed)) command = [ "tw-make-lab", "custom", "--seed", seed, "--output", game_file, "--surface_mode", "medium", "--merge_serial_actions", "--merge_parallel_actions", "--max_quest_length", "13", "--max_search_steps", "2000", "--lab_config_path", str(fpath) ] print(' '.join(command)) try: assert check_call(command) == 0 except CalledProcessError as e: print(e.output) assert os.path.isdir(output_folder) assert os.path.isfile(game_file) # Solve the game using WalkthroughAgent. agent = tw_textlabs.agents.WalkthroughAgent() stats = tw_textlabs.play(game_file, agent=agent, silent=True) assert (stats['score'] == 1)
def test_game_walkthrough_agent(game_file): agent = tw_textlabs.agents.WalkthroughAgent() env = tw_textlabs.start(game_file) env.enable_extra_info("score") agent.reset(env) stats = tw_textlabs.play(game_file, agent=agent, silent=True) print(stats) assert (stats['score'] == 1)
def test_making_challenge_game(): with make_temp_directory(prefix="test_tw-challenge") as tmpdir: for challenge in tw_textlabs.challenges.CHALLENGES: env_id = "tw-{}-level1".format(challenge) output_folder = pjoin(tmpdir, "gen_games") game_file = pjoin(output_folder, env_id + ".ulx") command = [ "tw-make", "challenge", env_id, "--seed", "1234", "--output", game_file ] assert check_call(command) == 0 assert os.path.isdir(output_folder) assert os.path.isfile(game_file) # Solve the game using WalkthroughAgent. agent = tw_textlabs.agents.WalkthroughAgent() tw_textlabs.play(game_file, agent=agent, silent=True)
def test_making_a_game(play_the_game=False): rng_map = np.random.RandomState(1234) map_ = tw_textlabs.generator.make_small_map(1, rng_map) world = World.from_map(map_) world.set_player_room() # First room generated (i.e. the only one). rng_objects = np.random.RandomState(123) nb_objects = 10 world.populate(nb_objects, rng=rng_objects) rng_quest = np.random.RandomState(124) quest = make_quest(world, quest_length=5, rng=rng_quest) # Define the grammar we'll use. rng_grammar = np.random.RandomState(1234) grammar_flags = { "theme": "house", "include_adj": False, "only_last_action": True, "blend_instructions": True, "blend_descriptions": True, "refer_by_name_only": True, "instruction_extension": [], } grammar = tw_textlabs.generator.make_grammar(grammar_flags, rng=rng_grammar) # Generate the world representation. game = tw_textlabs.generator.make_game_with(world, [quest], grammar) with make_temp_directory(prefix="test_render_wrapper") as tmpdir: options = tw_textlabs.GameOptions() options.path = tmpdir game_file = compile_game(game, options) if play_the_game: tw_textlabs.play(game_file)
def make_and_play_game(level, lab_game_options): game = make_game_from_level(level, lab_game_options) if game: with make_temp_directory(prefix="test_tw-make") as tmpdir: output_folder = Path(tmpdir) / "gen_games" game_file = output_folder / ("%s.ulx" % (game.metadata["uuid"])) lab_game_options.path = game_file game_file = tw_textlabs.generator.compile_game( game, lab_game_options) # Solve the game using WalkthroughAgent. agent = tw_textlabs.agents.WalkthroughAgent() stats = tw_textlabs.play(game_file, agent=agent, silent=True) game.metadata.update({'quest_desc': game.main_quest.desc}) game.metadata.update(stats) return game
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)