Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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)
if done:
def new_event_using_commands(self, commands: List[str]) -> Event:
""" Creates a new event using predefined text commands.
This launches a `textworld.play` session to execute provided commands.
Args:
commands: Text commands.
Returns:
The resulting event.
"""
with make_temp_directory() as tmpdir:
try:
game_file = self.compile(pjoin(tmpdir, "record_event.ulx"))
recorder = Recorder()
agent = textworld.agents.WalkthroughAgent(commands)
textworld.play(game_file, agent=agent, wrappers=[recorder], silent=True)
except textworld.agents.WalkthroughDone:
pass # Quest is done.
# Skip "None" actions.
actions, commands = zip(*[(a, c) for a, c in zip(recorder.actions, commands) if a is not None])
event = Event(actions=actions, commands=commands)
return event
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
def main():
args = parse_args()
agent = textworld.agents.WalkthroughAgent()
for i, game in enumerate(args.games, start=1):
print("{}. Testing {} ...".format(i, game))
env = textworld.start(game)
env.infos.admissible_commands = True
agent.reset(env)
game_state = env.reset()
if args.verbose:
env.render()
reward = 0
done = False
while not done:
command = agent.act(game_state, reward, done)
assert command in game_state.admissible_commands
This launches a `textworld.play` session to execute provided commands.
Args:
commands: Text commands.
Returns:
The resulting event.
"""
with make_temp_directory() as tmpdir:
try:
game_file = self.compile(pjoin(tmpdir, "record_event.ulx"))
recorder = Recorder()
agent = textworld.agents.WalkthroughAgent(commands)
textworld.play(game_file, agent=agent, wrappers=[recorder], silent=True)
except textworld.agents.WalkthroughDone:
pass # Quest is done.
# Skip "None" actions.
actions, commands = zip(*[(a, c) for a, c in zip(recorder.actions, commands) if a is not None])
event = Event(actions=actions, commands=commands)
return event
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
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