def test_step(self): env = tw_textlabs.start(self.game_file) npt.assert_raises(GameNotRunningError, env.step, "look") # Test sending command when the game is done. env = tw_textlabs.start(self.game_file) env.reset() env.step("quit") env.step("yes") npt.assert_raises(GameNotRunningError, env.step, "look") # Test sending empty command. env = tw_textlabs.start(self.game_file) env.reset() env.step("")
def test_step(self): env = tw_textlabs.start(self.game_file) npt.assert_raises(GameNotRunningError, env.step, "look") # TODO: not supported by Jericho # # Test sending command when the game has quit. # env = tw_textlabs.start(self.game_file) # env.reset() # env.step("quit") # env.step("yes") # npt.assert_raises(GameNotRunningError, env.step, "look") # Test sending empty command. env = tw_textlabs.start(self.game_file) env.reset() env.step("")
def test_winning_game(): MAX_NB_STEPS = 1000 # Just in case. env = tw_textlabs.start("./games/zork1.z5") walkthrough_file = os.path.abspath( pjoin(env.game_filename, "..", "solutions", "zork1.txt")) with open(walkthrough_file) as f: commands = f.readlines() agent = tw_textlabs.agents.WalkthroughAgent(commands) env.seed(1) # In order for the walkthrough to work. game_state = env.reset() # env.render() done = False for t in range(MAX_NB_STEPS): command = agent.act(game_state, 0, done) game_state, reward, done = env.step(command) # env.render() if done: break print("Done after {} steps. Score {}/{}.".format(game_state.nb_moves, game_state.score, game_state.max_score)) assert game_state.has_won assert not game_state.has_lost
def setUp(self): self.env = tw_textlabs.start(self.game_file) self.env.activate_state_tracking() self.env.compute_intermediate_reward() self.env.enable_extra_info("description") self.env.enable_extra_info("inventory") self.game_state = self.env.reset()
def test_quest_winning_condition_go(): M = tw_textlabs.GameMaker() # R1 -- R2 -- R3 R1 = M.new_room("West room") R2 = M.new_room("Center room") R3 = M.new_room("East room") M.set_player(R1) M.connect(R1.east, R2.west) M.connect(R2.east, R3.west) M.set_quest_from_commands(["go east", "go east"]) game = M.build() game_name = "test_quest_winning_condition_go" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.reset() game_state, _, done = env.step("go east") assert not done assert not game_state.has_won game_state, _, done = env.step("go east") assert done assert game_state.has_won
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 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_quest_winning_condition(): g_rng.set_seed(2018) map_ = make_small_map(n_rooms=5, possible_door_states=["open"]) world = World.from_map(map_) def _rule_to_skip(rule): # Examine, look and inventory shouldn't be used for chaining. if rule.name.startswith("look"): return True if rule.name.startswith("inventory"): return True if rule.name.startswith("examine"): return True return False for rule in KnowledgeBase.default().rules.values(): if _rule_to_skip(rule): continue options = ChainingOptions() options.backward = True options.max_depth = 1 options.create_variables = True options.rules_per_depth = [[rule]] options.restricted_types = {"r"} chain = sample_quest(world.state, options) assert len(chain.actions) > 0, rule.name event = Event(chain.actions) quest = Quest(win_events=[event]) # Set the initial state required for the quest. tmp_world = World.from_facts(chain.initial_state.facts) game = make_game_with(tmp_world, [quest], make_grammar({})) if tmp_world.player_room is None: # Randomly place the player in the world since # the action doesn't care about where the player is. tmp_world.set_player_room() game_name = "test_quest_winning_condition_" + rule.name.replace( "/", "_") with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.reset() game_state, _, done = env.step("look") assert not done assert not game_state.has_won game_state, _, done = env.step(event.commands[0]) assert done assert game_state.has_won
def test_100_sequential_runs(self): for i in range(1, 100): env = tw_textlabs.start(self.game_file) env.reset() game_state, reward, done = env.step('take inventory') self.assertIsNotNone(game_state, "Checking gamestate is not None") self.assertIsNotNone(reward, "Checking reward is not None") self.assertFalse( done, "Checking we don't finish the game by looking at our stuff")
def test_game_random_agent(self): env = tw_textlabs.start(self.game_file) agent = tw_textlabs.agents.RandomCommandAgent() agent.reset(env) game_state = env.reset() reward = 0 done = False for _ in range(5): command = agent.act(game_state, reward, done) game_state, reward, done = env.step(command)
def _init_game_by_seed(self, slot_seed: int) -> Tuple[str, Dict[str, Any]]: if self.textworld_env is not None: self.textworld_env.close() current_gamefile = self.games_collection[slot_seed] env = tw_textlabs.start(current_gamefile) self.textworld_env = Filter(self.request_infos)(env) self.ob, infos = self.textworld_env.reset() infos['game_file'] = current_gamefile return self.ob, infos
def test_simultaneous_runs(self): envs = [] for i in range(1, 100): env = tw_textlabs.start(self.game_file) env.reset() envs.append(env) game_state, reward, done = envs[-1].step('take inventory') self.assertIsNotNone(game_state, "Checking gamestate is not None") self.assertIsNotNone(reward, "Checking reward is not None") self.assertFalse( done, "Checking we don't finish the game by looking at our stuff")
def test_take_all_and_variants(): M = tw_textlabs.GameMaker() # Empty room. room = M.new_room("room") M.set_player(room) game = M.build() game_name = "test_take_all_and_variants" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.reset() variants_to_test = itertools.product(["take", "get", "pick up"], ["all", "everything", "each"]) for command in variants_to_test: game_state, _, done = env.step(" ".join(command)) assert game_state.feedback.strip( ) == "You have to be more specific!" # Multiple objects to take. red_ball = M.new(type="o", name="red ball") blue_ball = M.new(type="o", name="blue ball") room.add(red_ball, blue_ball) game = M.build() game_name = "test_take_all_and_variants2" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("inventory") env.reset() game_state, _, done = env.step("take all ball") assert "red ball:" in game_state.feedback assert "blue ball:" in game_state.feedback assert "red ball" in game_state.inventory assert "blue ball" in game_state.inventory
def test_game_walkthrough_agent(self): agent = tw_textlabs.agents.WalkthroughAgent() env = tw_textlabs.start(self.game_file) env.activate_state_tracking() commands = self.game.main_quest.commands agent.reset(env) game_state = env.reset() reward = 0 done = False for walkthrough_command in commands: self.assertFalse(done, 'walkthrough finished game too early') command = agent.act(game_state, reward, done) self.assertEqual(walkthrough_command, command, "Walkthrough agent issued unexpected command") game_state, reward, done = env.step(command) self.assertTrue(done, 'Walkthrough did not finish the game')
def evaluate(agent, game, args): env = tw_textlabs.start(game) log.debug("Using {}".format(env.__class__.__name__)) agent.reset(env) start_time = time.time() game_state = env.reset() log.debug("Environment reset.\n{}\n".format(env.render(mode="text"))) max_score = game_state.max_score nb_losts = 0 highscore = 0 score = 0 done = False for step in range(1, args.nb_steps + 1): action = agent.act(game_state, score, done) game_state, score, done = env.step(action) msg = "{:5d}. Time: {:9.2f}\tScore: {:3d}\tMove: {:5d}\tAction: {:20s}" msg = msg.format(step, time.time() - start_time, game_state.score, game_state.nb_moves, action) log.info(msg) log.debug(env.render(mode="text")) if done: highscore = max(score, highscore) if game_state.has_won: if highscore == max_score: break # No reason to play that game more. elif game_state.has_lost: nb_losts += 1 else: assert True, "Games should either end with a win or a fail." # Replay the game in the hope of achieving a better score. game_state = env.reset() log.debug("Environment reset.\n{}\n".format(env.render(mode="text"))) env.close() # Keep highest score. highscore = max(score, highscore) return step, nb_losts, highscore, max_score, time.time() - start_time
def test_description(self): env = tw_textlabs.start(self.game_file) game_state = env.reset() npt.assert_raises(ExtraInfosIsMissingError, getattr, game_state, "description") game_state, _, _ = self.env.step("look") assert game_state.feedback.strip( ) == self.game_state.description.strip() assert game_state.feedback.strip() == game_state.description.strip() game_state, _, _ = self.env.step("go east") game_state, _, _ = self.env.step("look") assert game_state.feedback.strip() == game_state.description.strip() # End the game. game_state, _, _ = self.env.step("insert carrot into chest") game_state, _, _ = self.env.step("close chest") assert game_state.description != ""
def benchmark(game_file, args): env = tw_textlabs.start(game_file) print("Using {}".format(env.__class__.__name__)) if args.mode == "random": agent = tw_textlabs.agents.NaiveAgent() elif args.mode == "random-cmd": agent = tw_textlabs.agents.RandomCommandAgent(seed=args.agent_seed) elif args.mode == "walkthrough": agent = tw_textlabs.agents.WalkthroughAgent() agent.reset(env) if args.activate_state_tracking: env.activate_state_tracking if args.compute_intermediate_reward: env.compute_intermediate_reward() game_state = env.reset() if args.verbose: env.render() reward = 0 done = False start_time = time.time() for _ in range(args.max_steps): command = agent.act(game_state, reward, done) game_state, reward, done = env.step(command) if done: #print("Win! Reset.") env.reset() done = False if args.verbose: env.render() duration = time.time() - start_time speed = args.max_steps / duration print("Done {:,} steps in {:.2f} secs ({:,.1f} steps/sec)".format( args.max_steps, duration, speed)) return speed
def test_loading_unsupported_game(self): game_file = pjoin(self.tmpdir, "dummy.z8") shutil.copyfile(self.game_file, game_file) env = tw_textlabs.start(game_file) game_state = env.reset() assert game_state.max_score == 0 for command in self.game.main_quest.commands: game_state, score, done = env.step(command) # Score is always 0 and done is always False for unsupported games. assert score == 0 assert game_state.score == 0 assert not done assert "The End" in game_state.feedback assert not game_state.game_ended assert not game_state.has_won assert not game_state.has_lost
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 reset(self) -> Tuple[str, Dict[str, Any]]: """ Resets the text-based environment. Resetting this environment means starting the next game in the pool. Returns: A tuple (observation, info) where * observation: text observed in the initial state; * infos: additional information as requested. """ if self.tw_textlabs_env is not None: self.tw_textlabs_env.close() self.current_gamefile = next(self._gamefiles_iterator) env = tw_textlabs.start(self.current_gamefile) self.debug_env = env self.tw_textlabs_env = Filter(self.request_infos)(env) self.ob, infos = self.tw_textlabs_env.reset() # For debug purposes infos['game_file'] = self.current_gamefile return self.ob, infos
def main(): args = parse_args() agent = tw_textlabs.agents.WalkthroughAgent() for i, game in enumerate(args.games, start=1): print("{}. Testing {} ...".format(i, game)) env = tw_textlabs.start(game) env.activate_state_tracking() agent.reset(env) game_state = env.reset() if args.verbose: env.render() reward = 0 done = False while not done: command = agent.act(game_state, reward, done) assert command in game_state.admissible_commands game_state, reward, done = env.step(command) if args.verbose: env.render()
def test_disambiguation_questions(): M = tw_textlabs.GameMaker() room = M.new_room("room") M.set_player(room) tasty_apple = M.new(type="o", name="tasty apple") tasty_orange = M.new(type="o", name="tasty orange") room.add(tasty_apple, tasty_orange) game = M.build() game_name = "test_names_disambiguation" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("description") env.enable_extra_info("inventory") game_state = env.reset() previous_inventory = game_state.inventory previous_description = game_state.description game_state, _, _ = env.step("take tasty") assert "?" in game_state.feedback # Disambiguation question. # When there is a question in Inform7, the next string sent to the game # will be considered as the answer. We now make sure that asking for # extra information like `description` or `inventory` before answering # the question works. assert game_state.description == previous_description assert game_state.inventory == previous_inventory # Now answering the question. game_state, _, _ = env.step("apple") assert "That's not a verb I recognise." not in game_state.feedback assert "tasty orange" not in game_state.inventory assert "tasty apple" in game_state.inventory assert "tasty apple" not in game_state.description
def test_activate_state_tracking(self): env = tw_textlabs.start(self.game_file) game_state = env.reset() npt.assert_raises(StateTrackingIsRequiredError, getattr, game_state, 'admissible_commands')
def test_names_disambiguation(): M = tw_textlabs.GameMaker() room = M.new_room("room") M.set_player(room) apple = M.new(type="o", name="apple") orange = M.new(type="o", name="orange") tasty_apple = M.new(type="o", name="tasty apple") tasty_orange = M.new(type="o", name="tasty orange") room.add(apple, orange, tasty_apple, tasty_orange) game = M.build() game_name = "test_names_disambiguation" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("description") env.enable_extra_info("inventory") env.reset() game_state, _, done = env.step("take tasty apple") assert "tasty apple" in game_state.inventory game_state, _, done = env.step("take tasty orange") assert "tasty orange" in game_state.inventory env.reset() game_state, _, done = env.step("take orange") assert "tasty orange" not in game_state.inventory assert "orange" in game_state.inventory game_state, _, done = env.step("take tasty") assert "?" in game_state.feedback # Disambiguation question. game_state, _, done = env.step("apple") assert "tasty orange" not in game_state.inventory assert "tasty apple" in game_state.inventory assert "tasty apple" not in game_state.description # Actions with two arguments. M = tw_textlabs.GameMaker() roomA = M.new_room("roomA") roomB = M.new_room("roomB") roomC = M.new_room("roomC") M.set_player(roomA) path = M.connect(roomA.east, roomB.west) gateway = M.new_door(path, name="gateway") path = M.connect(roomA.west, roomC.east) rectangular_gateway = M.new_door(path, name="rectangular gateway") keycard = M.new(type="k", name="keycard") rectangular_keycard = M.new(type="k", name="rectangular keycard") roomA.add(keycard, rectangular_keycard) M.add_fact("match", keycard, gateway) M.add_fact("match", rectangular_keycard, rectangular_gateway) M.add_fact("locked", gateway) M.add_fact("locked", rectangular_gateway) game = M.build() game_name = "test_names_disambiguation" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("description") env.enable_extra_info("inventory") env.reset() game_state, _, done = env.step("take keycard") assert "keycard" in game_state.inventory game_state, _, done = env.step( "take keycard") # Already in your inventory. assert "rectangular keycard" not in game_state.inventory game_state, _, done = env.step("take rectangular keycard") assert "rectangular keycard" in game_state.inventory game_state, _, done = env.step( "unlock gateway with rectangular keycard") assert "That doesn't seem to fit the lock." in game_state.command_feedback game_state, _, done = env.step("unlock gateway with keycard") game_state, _, done = env.step("open gateway") game_state, _, done = env.step("go east") assert "-= Roomb =-" in game_state.description game_state, _, done = env.step("go west") game_state, _, done = env.step( "unlock rectangular gateway with keycard") assert "That doesn't seem to fit the lock." in game_state.command_feedback game_state, _, done = env.step( "unlock rectangular gateway with rectangular keycard") game_state, _, done = env.step("open rectangular gateway") game_state, _, done = env.step("go west") assert "-= Roomc =-" in game_state.description # Test invariance of the order in which ambiguous object names are defined. # First define "type G safe" then a "safe". M = tw_textlabs.GameMaker() garage = M.new_room("garage") M.set_player(garage) key = M.new(type="k", name="key") typeG_safe = M.new(type="c", name="type G safe") safe = M.new(type="c", name="safe") safe.add(key) garage.add(safe, typeG_safe) M.add_fact("open", safe) game = M.build() game_name = "test_names_disambiguation" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("inventory") game_state = env.reset() game_state, _, done = env.step("take key from safe") assert "key" in game_state.inventory # First define "safe" then "type G safe". M = tw_textlabs.GameMaker() garage = M.new_room("garage") M.set_player(garage) key = M.new(type="k", name="key") safe = M.new(type="c", name="safe") typeG_safe = M.new(type="c", name="type G safe") safe.add(key) garage.add(safe, typeG_safe) M.add_fact("open", safe) game = M.build() game_name = "test_names_disambiguation" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) env.enable_extra_info("inventory") game_state = env.reset() game_state, _, done = env.step("take key from safe") assert "key" in game_state.inventory
def test_cannot_win_or_lose_a_quest_twice(): g_rng.set_seed(2018) M = tw_textlabs.GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") carrot = M.new(type='f', name='carrot') lettuce = M.new(type='f', name='lettuce') M.inventory.add(carrot) M.inventory.add(lettuce) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) # The goals event_carrot_in_closed_chest = Event(conditions={ M.new_fact("in", carrot, chest), M.new_fact("closed", chest) }) event_drop_carrot_R1 = Event(conditions={M.new_fact("at", carrot, R1)}) event_drop_carrot_R2 = Event(conditions={M.new_fact("at", carrot, R2)}) quest1 = Quest(win_events=[event_carrot_in_closed_chest], fail_events=[event_drop_carrot_R1, event_drop_carrot_R2]) event_lettuce_in_closed_chest = Event(conditions={ M.new_fact("in", lettuce, chest), M.new_fact("closed", chest) }) event_drop_lettuce_R1 = Event(conditions={M.new_fact("at", lettuce, R1)}) event_drop_lettuce_R2 = Event(conditions={M.new_fact("at", lettuce, R2)}) quest2 = Quest(win_events=[event_lettuce_in_closed_chest], fail_events=[event_drop_lettuce_R1, event_drop_lettuce_R2]) M.quests = [quest1, quest2] game = M.build() game_name = "test_cannot_win_or_lose_a_quest_twice" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) # Make sure we do not rely on the quest progression to # determine if the game was lost. assert not env._compute_intermediate_reward # Complete quest1 then fail it. env.reset() game_state, score, done = env.step("go east") assert score == 0 game_state, score, done = env.step("insert carrot into chest") assert score == 0 game_state, score, done = env.step("close chest") assert score == 1 assert not done game_state, score, done = env.step("open chest") # Re-completing quest1 doesn't award more points. game_state, score, done = env.step("close chest") assert score == 1 assert not done game_state, score, done = env.step("open chest") game_state, score, done = env.step("take carrot from chest") game_state, score, done = env.step("drop carrot") assert score == 1 assert not done # Then fail quest2. game_state, score, done = env.step("drop lettuce") assert done assert game_state.has_lost assert not game_state.has_won env.reset() game_state, score, done = env.step("go east") game_state, score, done = env.step("insert carrot into chest") game_state, score, done = env.step("insert lettuce into chest") game_state, score, done = env.step("close chest") assert score == 2 assert done assert not game_state.has_lost assert game_state.has_won
def test_quest_with_multiple_winning_and_losing_conditions(): g_rng.set_seed(2018) M = tw_textlabs.GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") carrot = M.new(type='f', name='carrot') lettuce = M.new(type='f', name='lettuce') M.inventory.add(carrot) M.inventory.add(lettuce) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) # The goal quest = Quest( win_events=[ Event(conditions={ M.new_fact("in", carrot, chest), M.new_fact("closed", chest) }), Event(conditions={M.new_fact("eaten", lettuce)}) ], fail_events=[ Event(conditions={ M.new_fact("in", lettuce, chest), M.new_fact("closed", chest) }), Event(conditions={M.new_fact("eaten", carrot)}) ]) M.quests = [quest] game = M.build() game_name = "test_quest_with_multiple_winning_and_losing_conditions" with make_temp_directory(prefix=game_name) as tmpdir: game_file = _compile_game(game, path=tmpdir) env = tw_textlabs.start(game_file) # Make sure we do not rely on the quest progression to # determine if the game was lost. assert not env._compute_intermediate_reward # Failing - 1 env.reset() game_state, _, done = env.step("eat carrot") assert done assert game_state.has_lost assert not game_state.has_won # Failing - 2 env.reset() game_state, _, done = env.step("go east") assert not done game_state, _, done = env.step("insert lettuce into chest") assert not done game_state, _, done = env.step("close chest") assert done assert game_state.has_lost assert not game_state.has_won # Failing - 1 env.reset() game_state, _, done = env.step("eat lettuce") assert done assert not game_state.has_lost assert game_state.has_won # Winning - 2 env.reset() game_state, _, done = env.step("go east") assert not done game_state, _, done = env.step("insert carrot into chest") assert not done game_state, _, done = env.step("close chest") assert done assert not game_state.has_lost assert game_state.has_won
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)
def test_compute_intermediate_reward(self): env = tw_textlabs.start(self.game_file) env.activate_state_tracking() game_state = env.reset() npt.assert_raises(OraclePolicyIsRequiredError, getattr, game_state, 'intermediate_reward')
def setUp(self): self.env = tw_textlabs.start(self.game_file) self.game_state = self.env.reset()
def setUp(self): self.env = tw_textlabs.start(self.game_file) self.env.activate_state_tracking() self.env.compute_intermediate_reward() self.game_state = self.env.reset()