Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_playing_a_game():
with make_temp_directory(prefix="test_tw-play") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 5
options.nb_objects = 10
options.quest_length = 5
options.quest_breadth = 2
options.seeds = 1234
game_file, _ = textworld.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
# Sample game specs.
world_size = rng.randint(1, 10)
nb_objects = rng.randint(0, 20)
quest_depth = 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.path = tmpdir
options.nb_rooms = world_size
options.nb_objects = nb_objects
options.chaining.max_depth = quest_depth
options.chaining.max_breadth = quest_breadth
options.seeds = game_seed
game_file, game = textworld.make(options)
# 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)
env.infos.policy_commands = True
env.infos.game = True
agent = textworld.agents.RandomCommandAgent()
agent.reset(env)
env.seed(4321)
game_state = env.reset()
# 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 = 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()
def test_extract_vocab():
with make_temp_directory(prefix="test_extract_vocab") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 5
options.nb_objects = 10
options.quest_length = 5
options.quest_breadth = 2
options.seeds = 1234
game_file1, _ = textworld.make(options)
options.seeds = 12345
game_file2, _ = textworld.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 "Found {}".format(nb_words) in stdout
def test_making_game_is_reproducible_with_seed():
with make_temp_directory(prefix="test_render_wrapper") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 2
options.nb_objects = 20
options.chaining.max_depth = 3
options.chaining.max_breadth = 2
options.seeds = 123
game_file1, game1 = textworld.make(options)
options2 = options.copy()
game_file2, game2 = textworld.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_making_game_with_names_to_exclude():
g_rng.set_seed(42)
with make_temp_directory(prefix="test_render_wrapper") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 2
options.nb_objects = 20
options.chaining.max_depth = 3
options.chaining.max_breadth = 2
options.seeds = 123
game_file1, game1 = textworld.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 = textworld.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_extract_entities():
with make_temp_directory(prefix="test_extract_entities") as tmpdir:
options = textworld.GameOptions()
options.path = tmpdir
options.nb_rooms = 5
options.nb_objects = 10
options.quest_length = 5
options.quest_breadth = 2
options.seeds = 1234
game_file, _ = textworld.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 "Found {}".format(nb_entities) in stdout
options.grammar.blend_instructions = args.blend_instructions
options.grammar.blend_descriptions = args.blend_descriptions
options.grammar.ambiguous_instructions = args.ambiguous_instructions
options.nb_rooms = args.world_size
options.nb_objects = args.nb_objects
options.quest_length = args.quest_length
options.quest_breadth = args.quest_breadth
agent = make_agent(args)
reward_history = []
for i in range(args.nb_games) if args.nb_games > 0 else itertools.count():
options = options.copy()
options.seeds = rng.randint(65635)
game_file, game = textworld.make(options, args.output)
print("Starting game {}".format(game_file))
env = textworld.start(game_file)
agent.reset(env)
if args.vizu >= 0:
from textworld.envs.wrappers import HtmlViewer
env = HtmlViewer(env, port=args.vizu)
game_state = env.reset()
if args.mode == "human" or args.verbose:
env.render()
reward = 0
done = False