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_play_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 = 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()
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
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_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
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()
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)
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()
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:
def test_game_random_agent(self):
env = textworld.start(self.game_file)
agent = textworld.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 test_simultaneous_runs(self):
envs = []
for i in range(1, 100):
env = textworld.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")
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()
max_steps = 100
reward = 0
done = False
def test_making_a_game_using_basic_theme():
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", "--theme", "basic", "--seed", "1234", "--output", game_file]
assert check_call(command) == 0
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.play(game_file, agent=agent, silent=True)
assert os.path.isdir(output_folder)
game_file = glob.glob(pjoin(output_folder, "*.ulx"))[0]
# Solve the game using WalkthroughAgent.
agent = textworld.agents.WalkthroughAgent()
textworld.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 = textworld.agents.WalkthroughAgent()
textworld.play(output_folder + ".ulx", agent=agent, silent=True)