def test_html_viewer(): # Integration test for visualization service num_nodes = 3 num_items = 10 options = tw_textlabs.GameOptions() options.seeds = 1234 options.nb_rooms = num_nodes options.nb_objects = num_items options.quest_length = 3 options.grammar.theme = "house" options.grammar.include_adj = True game = tw_textlabs.generator.make_game(options) game_name = "test_html_viewer_wrapper" with make_temp_directory(prefix=game_name) as tmpdir: options.path = tmpdir game_file = compile_game(game, options) env = tw_textlabs.start(game_file) env = HtmlViewer(env, open_automatically=False, port=8080) env.reset() # Cause rendering to occur. # options.binary_location = "/bin/chromium" driver = get_webdriver() driver.get("http://127.0.0.1:8080") nodes = driver.find_elements_by_class_name("node") assert len(nodes) == num_nodes items = driver.find_elements_by_class_name("item") objects = [obj for obj in game.world.objects if obj.type != "I"] assert len(items) == len(objects) env.close() driver.close()
def generate_never_ending_game(args): g_rng.set_seed(args.seed) msg = "--max-steps {} --nb-objects {} --nb-rooms {} --quest-length {} --quest-breadth {} --seed {}" print( msg.format(args.max_steps, args.nb_objects, args.nb_rooms, args.quest_length, args.quest_breadth, g_rng.seed)) print("Generating game...") options = GameOptions() options.seeds = g_rng.seed options.nb_rooms = args.nb_rooms options.nb_objects = args.nb_objects options.quest_length = args.quest_length options.quest_breadth = args.quest_breadth game = tw_textlabs.generator.make_game(options) if args.no_quest: game.quests = [] game_name = "neverending" path = pjoin(args.output, game_name + ".ulx") options = tw_textlabs.GameOptions() options.path = path options.force_recompile = True game_file = tw_textlabs.generator.compile_game(game, options) return game_file
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
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)
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)
def test_game_comparison(): options = tw_textlabs.GameOptions() options.nb_rooms = 5 options.nb_objects = 5 options.quest_length = 2 options.quest_breadth = 2 options.seeds = {"map": 1, "objects": 2, "quest": 3, "grammar": 4} game1 = tw_textlabs.generator.make_game(options) game2 = tw_textlabs.generator.make_game(options) assert game1 == game2 # Test __eq__ assert game1 in {game2} # Test __hash__ options = options.copy() options.seeds = {"map": 4, "objects": 3, "quest": 2, "grammar": 1} game3 = tw_textlabs.generator.make_game(options) assert game1 != game3
def test_extract_entities(): 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_file, _ = tw_textlabs.make(options) outfile = pjoin(tmpdir, "entities.txt") command = ["tw-extract", "entities", game_file, "--output", outfile] stdout = check_output(command).decode() assert os.path.isfile(outfile) nb_entities = len(open(outfile).readlines()) assert "Extracted {}".format(nb_entities) in stdout
def _compile_game(game, folder): grammar_flags = { "theme": "house", "include_adj": False, "only_last_action": True, "blend_instructions": True, "blend_descriptions": True, "refer_by_name_only": True, "instruction_extension": [] } rng_grammar = np.random.RandomState(1234) grammar = tw_textlabs.generator.make_grammar(grammar_flags, rng=rng_grammar) game.change_grammar(grammar) game_name = "test_game" options = tw_textlabs.GameOptions() options.path = pjoin(folder, game_name + ".ulx") game_file = tw_textlabs.generator.compile_game(game, options) return game_file
def compile(self, path: str) -> str: """ Compile this game. Parameters ---------- name : Name of the generated game file (without extension). Returns ------- game_file Path to the game file. """ self._working_game = self.build() options = tw_textlabs.GameOptions() options.path = path options.force_recompile = True game_file = tw_textlabs.generator.compile_game(self._working_game, options) return game_file
def test_logger(): rng = np.random.RandomState(1234) game_logger = GameLogger() for _ in range(10): options = tw_textlabs.GameOptions() options.nb_rooms = 5 options.nb_objects = 10 options.quest_length = 3 options.quest_breadth = 3 options.seeds = rng.randint(65635) game = tw_textlabs.generator.make_game(options) game_logger.collect(game) with make_temp_directory(prefix="tw_textlabs_tests") as tests_folder: filename = pjoin(tests_folder, "game_logger.pkl") game_logger.save(filename) game_logger2 = GameLogger.load(filename) assert game_logger is not game_logger2 assert game_logger.stats() == game_logger2.stats()
def _compile_game(game, folder): grammar_flags = { "theme": "house", "include_adj": False, "only_last_action": True, "blend_instructions": True, "blend_descriptions": True, "refer_by_name_only": True, "instruction_extension": [] } rng_grammar = np.random.RandomState(1234) grammar = tw_textlabs.generator.make_grammar(grammar_flags, rng=rng_grammar) game.change_grammar(grammar) game_name = "tw-test_game" options = tw_textlabs.GameOptions() options.path = pjoin(folder, game_name + ".z8") game_file = tw_textlabs.generator.compile_game(game, options) # Enable the following to regenerate the test game for Jericho. # shutil.copyfile(game_file, "/tmp/tw-game.z8") return game_file
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
def test_variable_infos(verbose=False): options = tw_textlabs.GameOptions() options.nb_rooms = 5 options.nb_objects = 10 options.quest_length = 3 options.quest_breadth = 2 options.seeds = 1234 options.grammar.theme = "house" options.grammar.include_adj = True game = tw_textlabs.generator.make_game(options) for var_id, var_infos in game.infos.items(): if var_id not in ["P", "I"]: if verbose: print(var_infos.serialize()) assert var_infos.id is not None assert var_infos.type is not None assert var_infos.name is not None assert var_infos.noun is not None assert var_infos.adj is not None assert var_infos.desc is not None
def test_game_ended_when_no_quest(self): M = GameMaker() room = M.new_room() M.set_player(room) item = M.new(type="o") room.add(item) game = M.build() game_name = "test_game_ended_when_no_quest" with make_temp_directory(prefix=game_name) as tmpdir: options = tw_textlabs.GameOptions() options.path = tmpdir game_file = tw_textlabs.generator.compile_game(game, options) env = tw_textlabs.start(game_file) env.activate_state_tracking() game_state = env.reset() assert not game_state.game_ended game_state, _, done = env.step("look") assert not done assert not game_state.game_ended
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 _compile_game(game, path): options = tw_textlabs.GameOptions() options.path = path return compile_game(game, options)
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)