Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
msg = "Finished before playing `max_steps` steps because of command '{}'.".format(command)
if game_state.has_won:
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)
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)
if done:
assert game_state._winning_policy is None
game_state, reward, done = env.reset(), 0, False
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
done = False
nb_resets = 1
start_time = time.time()
for _ in range(args.max_steps):
command = agent.act(game_state, reward, done)
game_state, reward, done = env.step(command)
def make_agent(args):
if args.mode == "random":
agent = textworld.agents.NaiveAgent()
elif args.mode == "random-cmd":
agent = textworld.agents.RandomCommandAgent()
elif args.mode == "human":
agent = textworld.agents.HumanAgent(autocompletion=args.hints, walkthrough=args.hints)
elif args.mode == 'walkthrough':
agent = textworld.agents.WalkthroughAgent()
else:
raise ValueError("Unknown agent: {}".format(args.mode))
return agent