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
def compile(self, path: str) -> str:
"""
Compile this game.
Parameters
----------
path :
Path where to save the generated game.
Returns
-------
game_file
Path to the game file.
"""
self._working_game = self.build()
options = textworld.GameOptions()
options.path = path
options.force_recompile = True
game_file = textworld.generator.compile_game(self._working_game, options)
return game_file
def _make_game(self, seeds):
options = GameOptions()
options.seeds = seeds
options.grammar = GrammarOptions(self.grammar_flags)
game = make_game_from_level(self.level, options)
hashid = encode_seeds([self.game_generator_seed, self.level] + [seeds[k] for k in sorted(seeds)])
game_name = "{}_{}".format(self.spec.id, hashid)
game_file = textworld.generator.compile_game(game, path=pjoin("gen_games", str(self.spec.id), game_name + ".ulx"))
return game_file
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 = textworld.generator.make_game(options)
if args.no_quest:
game.quests = []
game_name = "neverending"
path = pjoin(args.output, game_name + ".ulx")
options = textworld.GameOptions()
options.path = path
options.force_recompile = True
game_file = textworld.generator.compile_game(game, options)
return game_file
def main():
args = parse_args()
if args.very_verbose:
args.verbose = args.very_verbose
warnings.simplefilter("default", textworld.TextworldGenerationWarning)
if args.seed is None:
args.seed = np.random.randint(65635)
print("Random seed: {}".format(args.seed))
rng = np.random.RandomState(args.seed)
options = textworld.GameOptions()
options.grammar.theme = args.theme
options.grammar.include_adj = args.include_adj
options.grammar.only_last_action = args.only_last_action
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():