def test_load_textworld_games(): request_infos = EnvInfos() request_infos.admissible_commands = True request_infos.description = False request_infos.location = False request_infos.facts = False request_infos.last_action = False request_infos.game = True max_episode_steps = 100 batch_size = 5 name = "test" base_dir = "test-data/rl_games/" env = load_textworld_games( [ f"{base_dir}tw-cooking-recipe1+take1+cook+open-BNVaijeLTn3jcvneFBY2.z8", f"{base_dir}tw-cooking-recipe1+take1+open-BNVaijeLTn3jcvneFBY2.z8", f"{base_dir}tw-cooking-recipe1+take1+cook+open-BNVaijeLTn3jcvneFBY2.z8", f"{base_dir}tw-cooking-recipe1+take1+open-BNVaijeLTn3jcvneFBY2.z8", ], name, request_infos, max_episode_steps, batch_size, ) assert len(env.gamefiles) == 4 assert env.request_infos == request_infos assert env.batch_size == batch_size # for some reason env.spec.max_episode_steps is None # assert env.spec.max_episode_steps == max_episode_steps assert env.spec.id.split("-")[1] == name
def __init__(self, cfg): super(TextWorld, self).__init__() self.cfg = cfg # Load vocab self.nlp = spacy.load('en') with open('./vocab.txt') as f: self.vocab = f.read().split('\n') # Create tokenized dict self.word2id = {} for i, w in enumerate(self.vocab): self.word2id[w] = i # Set important tokens self.start = self.word2id['<S>'] self.pad = self.word2id['<PAD>'] self.end = self.word2id['</S>'] self.unk = self.word2id['<UNK>'] self.preposition_map = { 'chop': 'with', 'cook': 'with', 'dice': 'with', 'insert': 'into', 'lock': 'with', 'put': 'on', 'slice': 'with', 'take': 'from', 'unlock': 'with' } self.preposition_map_encoded = { self.word2id[key]: self.word2id[val] for (key, val) in self.preposition_map.items() } self.possible_cmds = [ 'chop', 'close', 'cook', 'dice', 'drink', 'drop', 'eat', 'examine', 'go', 'insert', 'inventory', 'lock', 'look', 'open', 'prepare', 'put', 'slice', 'take', 'unlock' ] self.possible_cmds_encoded = [ self.word2id[cmd] for cmd in self.possible_cmds ] # Get list of games self.games = glob.glob(cfg['data_dir'] + '*.ulx') # Start session requested_infos = EnvInfos(extras=['walkthrough']) requested_infos.entities = True requested_infos.admissible_commands = True env_id = textworld.gym.register_games( self.games, requested_infos, max_episode_steps=cfg['max_steps']) env_id = textworld.gym.make_batch(env_id, batch_size=cfg['num_agents'], parallel=False) self.env = gym.make(env_id)
def request_infos(self) -> Optional[EnvInfos]: """Request the infos the agent expects from the environment Returns: request_infos: EnvInfos""" request_infos = EnvInfos() request_infos.admissible_commands = self.use_admissible return request_infos
def select_additional_infos(): request_infos = EnvInfos() request_infos.description = True request_infos.inventory = True request_infos.entities, request_infos.verbs = True, True request_infos.max_score = True request_infos.extras = ["recipe"] request_infos.admissible_commands = True return request_infos
def select_additional_infos(self) -> EnvInfos: """ Returns what additional information should be made available at each game step. Requested information will be included within the `infos` dictionary passed to `CustomAgent.act()`. To request specific information, create a :py:class:`textworld.EnvInfos <textworld.envs.wrappers.filter.EnvInfos>` and set the appropriate attributes to `True`. The possible choices are: * `description`: text description of the current room, i.e. output of the `look` command; * `inventory`: text listing of the player's inventory, i.e. output of the `inventory` command; * `max_score`: maximum reachable score of the game; * `objective`: objective of the game described in text; * `entities`: names of all entities in the game; * `verbs`: verbs understood by the the game; * `command_templates`: templates for commands understood by the the game; * `admissible_commands`: all commands relevant to the current state; In addition to the standard information, game specific information can be requested by appending corresponding strings to the `extras` attribute. For this competition, the possible extras are: * `'recipe'`: description of the cookbook; * `'walkthrough'`: one possible solution to the game (not guaranteed to be optimal); Example: Here is an example of how to request information and retrieve it. >>> from textworld import EnvInfos >>> request_infos = EnvInfos(description=True, inventory=True, extras=["recipe"]) ... >>> env = gym.make(env_id) >>> ob, infos = env.reset() >>> print(infos["description"]) >>> print(infos["inventory"]) >>> print(infos["extra.recipe"]) Notes: The following information *won't* be available at test time: * 'walkthrough' Requesting additional infos comes with some penalty (called handicap). The exact penalty values will be defined in function of the average scores achieved by agents using the same handicap. Handicap is defined as follows max_score, has_won, has_lost, # Handicap 0 description, inventory, verbs, objective, # Handicap 1 command_templates, # Handicap 2 entities, # Handicap 3 extras=["recipe"], # Handicap 4 admissible_commands, # Handicap 5 """ request_infos = EnvInfos() request_infos.admissible_commands = True return request_infos
def request_infos_for_eval() -> EnvInfos: request_infos = EnvInfos() request_infos.admissible_commands = True request_infos.description = True request_infos.location = True request_infos.facts = True request_infos.last_action = True request_infos.game = True return request_infos
def select_additional_infos(self) -> EnvInfos: request_infos = EnvInfos() request_infos.has_won = True request_infos.has_lost = True request_infos.description = True request_infos.inventory = True request_infos.command_templates = True request_infos.entities = False request_infos.admissible_commands = False return request_infos
def select_additional_infos(self) -> EnvInfos: request_infos = EnvInfos() request_infos.description = True request_infos.inventory = True request_infos.entities = True request_infos.verbs = True request_infos.extras = ["recipe", "walkthrough"] request_infos.admissible_commands = True return request_infos
def select_additional_infos(self) -> EnvInfos: request_infos = EnvInfos() request_infos.description = True request_infos.inventory = True if self.config['general']['hcp'] >= 2: request_infos.entities = True request_infos.verbs = True if self.config['general']['hcp'] >= 4: request_infos.extras = ["recipe"] if self.config['general']['hcp'] >= 5: request_infos.admissible_commands = True # TEST request_infos.entities = True request_infos.verbs = True request_infos.extras = ["recipe", "walkthrough"] request_infos.admissible_commands = True return request_infos
def request_infos() -> Optional[EnvInfos]: """Request the infos the agent expects from the environment Returns: request_infos: EnvInfos""" request_infos = EnvInfos() request_infos.description = True request_infos.inventory = True request_infos.entities = True request_infos.verbs = True request_infos.admissible_commands = True request_infos.command_templates = True request_infos.max_score = True return request_infos
def get_infos(eval=True, recipe=True, walkthrough=True): request_infos = \ EnvInfos(verbs=True, moves=True, inventory=True, description=True, objective=True, intermediate_reward=True, policy_commands=True, max_score=True, admissible_commands=True, last_action=True, game=True, facts=True, entities=True, won=True, lost=True, location=True) request_infos.verbs = True request_infos.extras = [] if recipe: request_infos.extras += ['recipe'] if walkthrough: request_infos.extras += ['walkthrough'] if eval: request_infos.max_score = True request_infos.admissible_commands = True request_infos.command_templates = True return request_infos
def setup(self) -> EnvInfos: requested_infos = EnvInfos() requested_infos.description = True requested_infos.inventory = True requested_infos.entities = True requested_infos.verbs = True requested_infos.extras = ["recipe", "walkthrough"] requested_infos.admissible_commands = True env_id = textworld.gym.register_games( self.games, requested_infos, max_episode_steps=self.max_nb_steps, name="training") env_id = textworld.gym.make_batch(env_id, batch_size=self.batch_size, parallel=True) return gym.make(env_id)