### Install PyTorch Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Installs PyTorch, torchvision, and torchaudio. ```python !pip install torch torchvision torchaudio ``` -------------------------------- ### Install TextWorld from source Source: https://github.com/microsoft/textworld/blob/main/README.md Alternatively, install TextWorld after cloning the repo by navigating to the root folder and running pip install . ```bash pip install . ``` -------------------------------- ### Install TextWorld and Gym Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Installs TextWorld and a specific version of Gym. ```python !pip install textworld !pip install gym==0.21 ``` -------------------------------- ### Install visualization dependencies Source: https://github.com/microsoft/textworld/blob/main/README.md To install tools for visualizing game states, run this command. ```bash pip install textworld[vis] ``` -------------------------------- ### Example of requesting and retrieving information Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.gym.core.md This example demonstrates how to request specific information from the environment and then access it in the `infos` dictionary. ```python from textworld import EnvInfos request_infos = EnvInfos(description=True, inventory=True) env = gym.make(env_id) ob, infos = env.reset() print(infos["description"]) print(infos["inventory"]) ``` -------------------------------- ### Installing TextWorld with visualization extras Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing TextWorld generated games with OpenAI Gym.ipynb This command installs TextWorld with the necessary extras for visualization. ```bash !pip install textworld[vis] ``` -------------------------------- ### Install TextWorld Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing TextWorld generated games with OpenAI Gym.ipynb Installs the TextWorld library using pip. ```python !pip install textworld ``` -------------------------------- ### RegexDict Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.utils.md Demonstrates how to use RegexDict to query a dictionary with regular expressions. ```python from collections import OrderedDict class RegexDict(OrderedDict): def get_matching(self, *regexes: List[str], exclude: List[str] = []) -> List[Any]: ... # Example usage (conceptual, as actual instantiation and population are not shown): # my_dict = RegexDict({'apple': 1, 'banana': 2, 'apricot': 3}) # matching_values = my_dict.get_matching('ap.*') # print(matching_values) # Expected: [1, 3] ``` -------------------------------- ### Install chromedriver_installer Source: https://github.com/microsoft/textworld/blob/main/README.md If you have Chrome installed, you can use this command to install chromedriver. ```bash pip install chromedriver_installer ``` -------------------------------- ### Object Descriptor Examples Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Examples of object descriptors for containers (c) and supporters (s), including special functions for openable and on descriptions. ```inform7 (c)_desc:The (name) looks strong, and impossible to force open. (s)_desc:The (name) is #supp_stable#. supp_stable:stable;wobbly;unstable;balanced;durable;reliable;solid;undependable ``` ```inform7 openable_desc:[if open]It is open.[else if locked]It is closed.[otherwise]It is locked. on_desc:On the (name), is [a list of things on the (name)]. ``` -------------------------------- ### Install TextWorld via pip Source: https://github.com/microsoft/textworld/blob/main/README.md The easiest way to install TextWorld is via pip. ```bash pip install textworld ``` -------------------------------- ### Start a game Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing text-based games with TextWorld.ipynb Starts a TextWorld game environment with the specified game file and requested information. ```python # We are now ready to start the game. env = textworld.start('./zork1.z5', request_infos=infos) ``` -------------------------------- ### Install TextWorld development branch Source: https://github.com/microsoft/textworld/blob/main/textworld/challenges/tw_cooking/README.md Installs the development branch of TextWorld from GitHub. ```bash pip install https://github.com/Microsoft/TextWorld/archive/main.zip ``` -------------------------------- ### Start the game Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing TextWorld generated games with OpenAI Gym.ipynb Resets the environment to start a new game, returning the initial observation and additional information. ```python obs, infos = env.reset() print(obs) ``` -------------------------------- ### Troubleshooting Port Conflicts Source: https://github.com/microsoft/textworld/blob/main/docker/README.md Example of how to change the host port if the default port is already in use. ```bash docker run -p 9999:8888 -it --rm marccote19/textworld ``` -------------------------------- ### Install Jupyter Notebook Source: https://github.com/microsoft/textworld/blob/main/README.md Command to install Jupyter Notebook using pip. ```bash pip install jupyter ``` -------------------------------- ### Minimal Environment Interaction Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.md Demonstrates how to create, load, reset, and interact with a TextWorld environment using basic commands. ```python >>> import textworld >>> options = textworld.GameOptions() >>> options.seeds = 1234 >>> options.nb_objects = 5 >>> options.quest_length = 2 >>> game_file, _ = textworld.make(options, path='./') # Generate a random game. >>> env = textworld.start(game_file) # Load the game. >>> game_state = env.reset() # Start a new game. >>> env.render() I hope you're ready to go into rooms and interact with objects, because you've just entered TextWorld! Here is how to play! First thing I need you to do is to ensure that the type G chest is open. And then, pick up the keycard from the type G chest inside the attic. Got that? Good! -= Attic =- You arrive in an attic. A normal kind of place. You begin to take stock of what's in the room. You make out a type G chest. You can see a TextWorld style locker. The TextWorld style locker contains a frisbee and a sock. There is a TextWorld style key on the floor. >>> command = "take key" # Command to send to the game. >>> game_state, reward, done = env.step(command) >>> env.render() (the TextWorld style key) You pick up the TextWorld style key from the ground. ``` -------------------------------- ### TextWorld Game Generation and Gym Integration Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.gym.md This example shows how to generate a TextWorld game, compile it, register it with Gym, and interact with it, requesting specific information like descriptions, inventory, and custom extras. ```python >>> from textworld.generator import make_game, compile_game >>> options = textworld.GameOptions() >>> options.seeds = 1234 >>> game = make_game(options) >>> game.extras["more"] = "This is extra information." >>> gamefile = compile_game(game) >>> import gym >>> import textworld.gym >>> from textworld import EnvInfos >>> request_infos = EnvInfos(description=True, inventory=True, extras=["more"]) >>> env_id = textworld.gym.register_games([gamefile], request_infos) >>> env = gym.make(env_id) >>> ob, infos = env.reset() >>> print(infos["extra.more"]) This is extra information. ``` -------------------------------- ### Example Room Types in house_obj.twf Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md An example of defining room types within the 'house_obj.twf' grammar file. ```textworld room_type:clean;cook;rest;work;storage ``` -------------------------------- ### VariableTypeTree Loading Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Illustrates how to load a `VariableTypeTree` from a file path. ```python from textworld.generator.vtypes import VariableTypeTree # Assuming 'variables.txt' contains variable type definitions # Example content of variables.txt: # container: object -> room # key: object -> container # Load the VariableTypeTree from a file tree = VariableTypeTree.load('path/to/your/variables.txt') print(f"Loaded VariableTypeTree with {len(tree.vtypes)} types.") ``` -------------------------------- ### tw-play command-line usage Source: https://github.com/microsoft/textworld/blob/main/docs/source/tw-play.md Usage example for the tw-play command-line tool, which allows playing TextWorld games. ```bash usage: tw-play [-h] [--mode MODE] [--seed SEED] [--max-steps STEPS] [--viewer [PORT]] [--hint] [-v] [-vv] game ``` -------------------------------- ### Recursive Expansion Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Illustrates a more complex recursive expansion where 'greeting' itself contains another non-terminal '#greet_how#'. ```textworld introduction: Hello, #greeting# greeting:#greet_how# to meet you!;how are you?;have we met? greet_how:nice;great;wonderful ``` -------------------------------- ### Filter Wrapper Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.envs.wrappers.md Example demonstrating how to use the Filter wrapper to request and retrieve specific information from the environment. ```python >>> from textworld import EnvInfos >>> from textworld.envs.wrappers import Filter >>> request_infos = EnvInfos(description=True, inventory=True, extras=["more"]) >>> env = textworld.start(gamefile, request_infos) >>> env = Filter(env) >>> ob, infos = env.reset() >>> print(infos["description"]) >>> print(infos["inventory"]) >>> print(infos["extra.more"]) ``` -------------------------------- ### Registering a single game and creating an environment Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.gym.md This example demonstrates how to register a single TextWorld game, create a gym environment, and retrieve custom information. ```python >>> from textworld.generator import make_game, compile_game >>> options = textworld.GameOptions() >>> options.seeds = 1234 >>> game = make_game(options) >>> game.extras["more"] = "This is extra information." >>> gamefile = compile_game(game) >>> import gym >>> import textworld.gym >>> from textworld import EnvInfos >>> request_infos = EnvInfos(description=True, inventory=True, extras=["more"]) >>> env_id = textworld.gym.register_game(gamefile, request_infos) >>> env = gym.make(env_id) >>> ob, infos = env.reset() >>> print(infos["extra.more"]) This is extra information. ``` -------------------------------- ### Place the player Source: https://github.com/microsoft/textworld/blob/main/notebooks/Handcrafting a game.ipynb Set the player's starting location to Room A. ```python M.set_player(roomA) M.render() ``` -------------------------------- ### Inform 7 Functions for Listing Contents Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Examples of Inform 7 functions for listing the contents of containers and supporters. ```inform7 # Containers In the chest [is-are a list of things in the chest]. -> In the chest are a pen and an apple. The chest has [a list of things in the chest] in it. -> The chest has a pen and an apple in it. # Supporters On the shelf [is-are a list of things on the shelf]. -> On the shelf are a pen and an apple. The shelf has [a list of things on the shelf]. -> On the shelf are a pen and an apple. ``` -------------------------------- ### Record a quest Source: https://github.com/microsoft/textworld/blob/main/notebooks/Handcrafting a game.ipynb This code snippet shows how to start recording a quest in TextWorld. ```python quest = M.record_quest() ``` -------------------------------- ### Register and Play a Text-Based Game Source: https://github.com/microsoft/textworld/blob/main/README.md Example of how to register a custom game and play it using the TextWorld Gym API. ```python # Register a text-based game as a new environment. env_id = textworld.gym.register_game("tw_games/custom_game.z8", max_episode_steps=50) env = textworld.gym.make(env_id) # Start the environment. obs, infos = env.reset() # Start new episode. env.render() score, moves, done = 0, 0, False while not done: command = input("> ") obs, score, done, infos = env.step(command) env.render() moves += 1 env.close() print("moves: {}; score: {}".format(moves, score)) ``` -------------------------------- ### Example of adding and checking properties Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.maker.md Demonstrates how to add a property to an entity and check if it exists using the add_property and has_property methods. ```python >>> from textworld import GameMaker >>> M = GameMaker() >>> chest = M.new(type="c", name="chest") >>> chest.has_property('closed') False >>> chest.add_property('closed') >>> chest.has_property('closed') True ``` -------------------------------- ### Evaluate the random agent Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Example calls to the play function to evaluate the RandomAgent on different game reward settings. ```python # We report the score and steps averaged over 10 playthroughs. play(RandomAgent(), "./games/tw-rewardsDense_goalDetailed.z8") # Dense rewards play(RandomAgent(), "./games/tw-rewardsBalanced_goalDetailed.z8") # Balanced rewards play(RandomAgent(), "./games/tw-rewardsSparse_goalDetailed.z8") # Sparse rewards ``` -------------------------------- ### Flavour Grammar File Examples Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Lists the four required grammar files for a space-themed flavour named 'spaceship'. ```textworld spaceship_obj.twf spaceship_room.twf spaceship_instruction.twf spaceship_question.twf ``` -------------------------------- ### Get New ID for a Type Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Demonstrates how to get the next available ID for a given type using `get_new`. ```python from textworld.generator.vtypes import get_new # Example usage types_counts = {'object': 5, 'container': 2} next_object_id = get_new('object', types_counts) next_container_id = get_new('container', types_counts) print(f"Next object ID: {next_object_id}") print(f"Next container ID: {next_container_id}") ``` -------------------------------- ### Install TextWorld with visualization extras Source: https://github.com/microsoft/textworld/blob/main/notebooks/Handcrafting a game.ipynb For Google Colab, first install chromium-chromedriver and selenium, then install textworld with the [vis] extras to enable visual rendering of the world. ```python # For Google Colab, first install #!apt install chromium-chromedriver #!pip install selenium==3.12.0 # then !pip install textworld[vis] ``` -------------------------------- ### Testing the agent trained on 100 games. Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Loads a pre-trained agent and tests its performance on two different games. ```python agent = torch.load('checkpoints/agent_trained_on_multiple_games.pt') agent.test() play(agent, "./games/tw-rewardsDense_goalDetailed.z8") # Averaged over 10 playthroughs. play(agent, "./games/tw-another_game.z8") # Averaged over 10 playthroughs. ``` -------------------------------- ### Compare it to the agent trained on a single game. Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Loads an agent trained on a single game and tests its performance on the same two games. ```python agent = torch.load('checkpoints/agent_trained_on_single_game.pt') agent.test() play(agent, "./games/tw-rewardsDense_goalDetailed.z8") # Averaged over 10 playthroughs. play(agent, "./games/tw-another_game.z8") # Averaged over 10 playthroughs. ``` -------------------------------- ### Initial Agent Performance Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Evaluate the agent's initial performance before training. ```python agent = NeuralAgent() play(agent, "./games/tw-rewardsDense_goalDetailed.z8") ``` -------------------------------- ### Create and place a key Source: https://github.com/microsoft/textworld/blob/main/notebooks/Handcrafting a game.ipynb Create a key object, make it match the door's lock, and place it on the supporter. ```python key = M.new(type="k", name="old key") # Create a 'k' (i.e. key) object. M.add_fact("match", key, door) # Tell the game 'old key' is matching the 'door''s lock supporter.add(key) # Add the 'old key' on the supporter. M.render() ``` -------------------------------- ### Generating a new game for testing generalization Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Uses the tw-make command to generate a new game with specific parameters for testing agent generalization. ```bash !tw-make tw-simple --rewards dense --goal detailed --seed 1 --output games/tw-another_game.z8 -v -f ``` -------------------------------- ### Exit description symbols example Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Example definitions for symbols used to describe exits, including handling of doors and plurality. ```textworld room_exit_desc:There [is an exit|are exits] to the (dir). room_(d)_exit_desc_alt:There [is a door|are doors], (name-adj), leading (dir). room_(d)_exit_desc:The [exit|exits] to the (dir) [is|are] blocked by (name). ``` -------------------------------- ### room_desc_group symbol definition example Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md An example of how the 'room_desc_group' symbol can be defined to handle multiple objects grouped by noun or adjective. ```textworld room_desc_group:There are (^) (val) here, (name). ``` -------------------------------- ### Testing the agent on a single game Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Loads a pre-trained agent and tests it on a specific game, reporting average steps and score. ```python # We report the score and steps averaged over 10 playthroughs. agent = torch.load('checkpoints/agent_trained_on_single_game.pt') agent.test() play(agent, "./games/tw-rewardsDense_goalDetailed.z8") # Dense rewards game. ``` -------------------------------- ### Generating 100 training games Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Generates a large number of training games using a shell command for improved agent generalization. ```bash # You can skip this if you already downloaded the data in the prequisite section. ! seq 1 100 | xargs -n1 -P4 tw-make tw-simple --rewards dense --goal detailed --format z8 --output training_games/ --seed ``` -------------------------------- ### Evaluating the agent on a test distribution Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Generates 20 test games and evaluates the agent's performance against a random agent. ```bash # You can skip this if you already downloaded the games in the prequisite section. ! seq 1 20 | xargs -n1 -P4 tw-make tw-simple --rewards dense --goal detailed --test --format z8 --output testing_games/ --seed ``` ```python agent = torch.load('checkpoints/agent_trained_on_multiple_games.pt') agent.test() play(RandomAgent(), "./testing_games/", nb_episodes=20 * 10) play(agent, "./testing_games/", nb_episodes=20 * 10) # Averaged over 10 playthroughs for each test game. ``` -------------------------------- ### VariableTypeTree Deserialization Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Demonstrates deserializing a `VariableTypeTree` from a list of dictionaries. ```python from textworld.generator.vtypes import VariableTypeTree # Example data representing VariableTypes serialized_data = [ {'name': 'container', 'type': 'object', 'parent': 'room'}, {'name': 'key', 'type': 'object', 'parent': 'container'} ] # Deserialize the data into a VariableTypeTree tree = VariableTypeTree.deserialize(serialized_data) print(f"Deserialized VariableTypeTree with {len(tree.vtypes)} types.") ``` -------------------------------- ### Playing games with RandomAgent and the trained agent Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Compares the performance of a RandomAgent and the trained agent on the newly generated game. ```python # We report the score and steps averaged over 10 playthroughs. play(RandomAgent(), "./games/tw-another_game.z8") play(agent, "./games/tw-another_game.z8") ``` -------------------------------- ### VariableType Serialization Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Shows how to serialize a `VariableType` object into a string representation. ```python from textworld.generator.vtypes import VariableType # Create a VariableType object var_type = VariableType(type="object", name="container", parent="room") # Serialize the VariableType object serialized_type = var_type.serialize() print(f"Serialized VariableType: {serialized_type}") ``` -------------------------------- ### Compound Actions Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Examples of compound action descriptions used in TextWorld's instruction grammars. ```text ig_unlock_open: Unlocking and opening either a door or container. ig_unlock_open_take: Unlock, open a container and take something out of it. ig_open_take: Open a closed container and take something out of it. ig_take_unlock:Take a key and use it to unlock a door or container. ig_open_insert:Open a container and insert something into it. ig_insert_close:Insert something into a container and then close the container. ig_close_lock:Close a container or door and then lock it. ``` -------------------------------- ### tw-make usage Source: https://github.com/microsoft/textworld/blob/main/docs/source/tw-make.md The main usage command for tw-make. ```bash usage: tw-make [-h] [--third-party PATH] {custom,tw-coin_collector,tw-treasure_hunter,tw-simple,tw-cooking} ... ``` -------------------------------- ### Docker Run Source: https://github.com/microsoft/textworld/blob/main/README.md Run the TextWorld Docker container. ```bash docker run -p 8888:8888 -it --rm marccote19/textworld ``` -------------------------------- ### get_chains function Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Generates chains of actions (quests) starting from or ending at the given state, with configurable options. ```python def get_chains(state: [State](textworld.logic.md#textworld.logic.State), options: [ChainingOptions](#textworld.generator.chaining.ChainingOptions)) -> Iterable[[Chain](#textworld.generator.chaining.Chain)]: # Generates chains of actions (quests) starting from or ending at the # given state. # Parameters: # state – The initial state for chaining. # options – Options to configure chaining behaviour. # Returns: # All possible quests according to the constraints. ``` -------------------------------- ### Parse Variable Types Content Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Shows how to parse a string containing multiple variable type expressions. ```python from textworld.generator.vtypes import parse_variable_types content = """ container: object -> room key: object -> container """ parsed_types = parse_variable_types(content) print(f"Parsed {len(parsed_types)} variable types.") for vt in parsed_types: print(f"- {vt.name}: {vt.type} -> {vt.parent}") ``` -------------------------------- ### Initialize Environment Information Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing text-based games with TextWorld.ipynb Configures the information to be requested from the game environment. ```python # Let the environment know what information we want as part of the game state. infos = textworld.EnvInfos( feedback=True, # Response from the game after typing a text command. description=True, # Text describing the room the player is currently in. inventory=True # Text describing the player's inventory. ) ``` -------------------------------- ### Variable Type Tree Sampling Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Shows how to sample an object type from a `VariableTypeTree` given a parent type. ```python import random from textworld.generator.vtypes import VariableTypeTree # Assume 'tree' is a loaded VariableTypeTree object # For demonstration, let's create a dummy tree serialized_data = [ {'name': 'room', 'type': 'location'}, {'name': 'container', 'type': 'object', 'parent': 'room'}, {'name': 'key', 'type': 'object', 'parent': 'container'} ] tree = VariableTypeTree.deserialize(serialized_data) # Sample an object type given the parent type 'room' rng = random.Random(42) # for reproducibility sampled_type = tree.sample(parent_type='room', rng=rng) print(f"Sampled type from 'room': {sampled_type}") ``` -------------------------------- ### VariableType Parsing Example Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.generator.md Demonstrates how to parse a variable type expression using the `parse` class method of `VariableType`. ```python from textworld.generator.vtypes import VariableType # Example of parsing a variable type expression expr = "container: object & supporter -> room" var_type = VariableType.parse(expr) print(f"Parsed VariableType: {var_type}") print(f"Name: {var_type.name}") print(f"Type: {var_type.type}") print(f"Parents: {var_type.parent}") ``` -------------------------------- ### Unique Product Alternative (for comparison) Source: https://github.com/microsoft/textworld/blob/main/docs/source/textworld.utils.md Shows the equivalent logic using itertools.product and a set for filtering, highlighting the efficiency of unique_product. ```python import itertools def unique_product_alternative(*args): for result in itertools.product(*args): if len(set(result)) == len(result): yield result # Example usage: results = list(unique_product_alternative('ABC', 'Ax', 'xy')) print(results) # Expected output: ['Axy', 'BAx', 'BAy', 'Bxy', 'CAx', 'CAy', 'Cxy'] ``` -------------------------------- ### Training the agent on multiple games Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Trains the agent on a set of 100 games and measures the training time. ```python # You can skip this if you already downloaded the data in the prequisite section. from time import time agent = NeuralAgent() print("Training on 100 games") agent.train() # Tell the agent it should update its parameters. starttime = time() play(agent, "./training_games/", nb_episodes=100 * 5, verbose=False) # Each game will be seen 5 times. print("Trained in {:.2f} secs".format(time() - starttime)) ``` -------------------------------- ### Frotz Interpreter with Random Seed Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/zork1.md Example of how to use the frotz interpreter to provide a random seed for Zork1. ```bash /frotz/dfrotz -s 1 zork1.z5 ``` -------------------------------- ### Training the Neural Agent Source: https://github.com/microsoft/textworld/blob/main/notebooks/Building a simple agent.ipynb Train the neural agent for a few episodes and evaluate its performance. ```python # You can skip this if you already downloaded the data in the prequisite section. from time import time agent = NeuralAgent() print("Training") agent.train() # Tell the agent it should update its parameters. starttime = time() play(agent, "./games/tw-rewardsDense_goalDetailed.z8", nb_episodes=500, verbose=False) # Dense rewards game. print("Trained in {:.2f} secs".format(time() - starttime)) ``` -------------------------------- ### Example Game Interaction Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing text-based games with TextWorld.ipynb This snippet shows a sequence of actions and game responses during a TextWorld session. ```text > examine note The dinner is almost ready! It's only missing a grilled carrot. > -= Kitchen =-0/8 Available actions: ['close wooden door', 'drop old key', 'examine counter', 'examine kitchen island', 'examine note', 'examine old key', 'examine refrigerator', 'examine screen door', 'examine stove', 'examine wooden door', 'go north', 'go south', 'go west', 'inventory', 'look', 'open refrigerator', 'open screen door', 'put old key on counter', 'put old key on kitchen island', 'put old key on stove', 'take note from kitchen island'] > open screen door You open screen door. > -= Kitchen =-0/9 Available actions: ['close screen door', 'close wooden door', 'drop old key', 'examine counter', 'examine kitchen island', 'examine note', 'examine old key', 'examine refrigerator', 'examine screen door', 'examine stove', 'examine wooden door', 'go east', 'go north', 'go south', 'go west', 'inventory', 'look', 'open refrigerator', 'put old key on counter', 'put old key on kitchen island', 'put old key on stove', 'take note from kitchen island'] > go east -= Backyard =- You've entered a backyard. You can barely contain your excitement. You scan the room for a set of chairs, and you find a set of chairs! The set of chairs is typical. But the thing is empty. What, you think everything in TextWorld should have stuff on it? What's that over there? It looks like it's a bbq. I guess it's true what they say, if you're looking for a bbq, go to TextWorld. However, the bbq, like an empty bbq, has nothing on it. You can see a patio table. The patio table is normal. But oh no! there's nothing on this piece of junk. There is an open screen door leading west. You need an unguarded exit? You should try going south. > -= Backyard =-0/10 Available actions: ['close screen door', 'drop old key', 'examine bbq', 'examine old key', 'examine patio table', 'examine screen door', 'examine set of chairs', 'go south', 'go west', 'inventory', 'look', 'put old key on bbq', 'put old key on patio table', 'put old key on set of chairs'] > go south -= Garden =- I am so happy to announce that you are now in the garden. You begin to take stock of what's here. You don't like doors? Why not try going north, that entranceway is unblocked. There is a half of a bag of chips, a carrot, an apple and a shovel on the floor. > -= Garden =-0/11 Available actions: ['drop old key', 'examine apple', 'examine carrot', 'examine half of a bag of chips', 'examine old key', 'examine shovel', 'go north', 'inventory', 'look', 'take apple', 'take carrot', 'take half of a bag of chips', 'take shovel'] > take carrot You pick up the carrot from the ground. > -= Garden =-0/12 Available actions: ['drop carrot', 'drop old key', 'eat carrot', 'examine apple', 'examine carrot', 'examine half of a bag of chips', 'examine old key', 'examine shovel', 'go north', 'inventory', 'look', 'take apple', 'take half of a bag of chips', 'take shovel'] > go north -= Backyard =- You've entered a backyard. You can barely contain your excitement. You scan the room for a set of chairs, and you find a set of chairs! The set of chairs is typical. But the thing is empty. What, you think everything in TextWorld should have stuff on it? What's that over there? It looks like it's a bbq. I guess it's true what they say, if you're looking for a bbq, go to TextWorld. However, the bbq, like an empty bbq, has nothing on it. You can see a patio table. The patio table is normal. But oh no! there's nothing on this piece of junk. There is an open screen door leading west. You need an unguarded exit? You should try going south. > -= Backyard =-0/13 Available actions: ['close screen door', 'drop carrot', 'drop old key', 'eat carrot', 'examine bbq', 'examine carrot', 'examine old key', 'examine patio table', 'examine screen door', 'examine set of chairs', 'go south', 'go west', 'inventory', 'look', 'put carrot on bbq', 'put carrot on patio table', 'put carrot on set of chairs', 'put old key on bbq', 'put old key on patio table', 'put old key on set of chairs'] ``` ``` Output: > go west -= Kitchen =- You arrive in a kitchen. A standard one. You see a refrigerator. You check the price tag that's still affixed to the refrigerator. 100 bucks? What a deal! You'll have to ask where they got this! You see a counter. The counter is typical. But the thing hasn't got anything on it. Look out! It's a- oh, never mind, it's just a stove. But there isn't a thing ``` -------------------------------- ### Castle Flavor Custom Adjectives Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Example of custom adjectives for objects in the 'castle' flavor to provide a distinct feel. ```text (o)_noun:quill;parchment (o)_adj:dusty;dirty;filthy;plain;small;hefty;large;imposing;impressive dark_(o)_noun:skull;manacles;bone;diary;relic;amulet dark_(o)_adj:stained;warped;twisted;foul;unholy;cursed magic_(o)_noun:quill;parchment;crystal;wand;book;orb;relic;amulet magic_(o)_adj:sparkling;glowing;hovering;magical;mysterious fight_(o)_noun:sword;bow;quiver;chestplate;helmet;knife;shield fight_(o)_adj:hefty;sturdy;large;imposing;impressive;durable royal_(o)_noun:coin;crown;cape;scepter;necklace;chalice;amulet royal_(o)_adj:frilly;decorated;austere;elegant;immaculate;ornate;intricate servant_(o)_noun:tunic;hat;pitchfork;scythe;parchment;diary servant_(o)_adj:dusty;dirty;filthy;plain;small ``` -------------------------------- ### Pull and Run TextWorld Docker Image Source: https://github.com/microsoft/textworld/blob/main/docker/README.md Instructions to pull the TextWorld Docker image and run it, exposing the Jupyter notebook port. ```bash docker pull marccote19/textworld docker run -p 8888:8888 -it --rm marccote19/textworld ``` -------------------------------- ### House Flavor Object Definitions Source: https://github.com/microsoft/textworld/blob/main/docs/source/notes/cfg.md Example of custom nouns with a shared set of adjectives for objects in the 'house' flavor. ```text (o)_noun:pencil;pen (o)_adj:new;old;used;dusty;clean;large;small;fancy;plain;ornate;antique;contemporary;modern;dirty;elegant;immaculate;simple;hefty;modest;gaudy;frilly;decorated;austere clean_(o)_noun:iron;paper;towel;mat;soap;mop;broom;shirt;sock;vacuum;sponge storage_(o)_noun:lightbulb;broom;shirt;sock;shoe;glove;hat;cane;scarf cook_(o)_noun:fork;knife;cup;mug;spoon;bowl;napkin;whisk;ladle;blender;teacup;kettle;teapot;glass rest_(o)_noun:tv;controller;pillow;blanket;plant;book;dvd;cd;toy;lamp;laptop work_(o)_noun:pen;pencil;stapler;staple;printer;mouse;keyboard;mug;disk;cd;book;folder;binder ``` -------------------------------- ### Make the gym-like environment Source: https://github.com/microsoft/textworld/blob/main/notebooks/Playing TextWorld generated games with OpenAI Gym.ipynb Creates a new environment using the registered env_id. ```python env = textworld.gym.make(env_id) ``` -------------------------------- ### Play a game with visualization Source: https://github.com/microsoft/textworld/blob/main/README.md Plays a game and opens a browser tab to visualize the game state. ```bash tw-play tw_games/custom_game.z8 --viewer ``` -------------------------------- ### tw-make custom command Source: https://github.com/microsoft/textworld/blob/main/docs/source/tw-make.md Command to create a custom TextWorld game. ```bash tw-make custom [-h] [--world-size SIZE] [--nb-objects NB] [--theme THEME] [--include-adj] [--blend-descriptions] [--ambiguous-instructions] [--only-last-action] [--blend-instructions] [--entity-numbering] [--nb-parallel-quests NB_PARALLEL_QUESTS] [--quest-length LENGTH] [--quest-breadth BREADTH] [--quest-min-length LENGTH] [--quest-max-length LENGTH] [--quest-min-breadth BREADTH] [--quest-max-breadth BREADTH] [--quest-min-depth DEPTH] [--quest-max-depth DEPTH] [--output PATH] [--seed SEED] [--format {z8}] [--overview] [--save-overview] [-f] [--silent | -v] ```