Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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")
def benchmark(game_file, args):
infos = textworld.EnvInfos()
if args.activate_state_tracking or args.mode == "random-cmd":
infos.admissible_commands = True
if args.compute_intermediate_reward:
infos.intermediate_reward = True
env = textworld.start(game_file, infos)
print("Using {}".format(env))
if args.mode == "random":
agent = textworld.agents.NaiveAgent()
elif args.mode == "random-cmd":
agent = textworld.agents.RandomCommandAgent(seed=args.agent_seed)
elif args.mode == "walkthrough":
agent = textworld.agents.WalkthroughAgent()
agent.reset(env)
game_state = env.reset()
if args.verbose:
env.render()
reward = 0
def evaluate(agent, game, args):
env = textworld.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)
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
for t in range(args.max_steps) if args.max_steps > 0 else itertools.count():
command = agent.act(game_state, reward, done)
game_state, reward, done = env.step(command)