How to use the textworld.g_rng.next function in textworld

To help you get started, we’ve selected a few textworld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / TextWorld / textworld / generator / __init__.py View on Github external
def make_world(world_size, nb_objects=0, rngs=None):
    """ Make a world (map + objects).

    Parameters
    ----------
    world_size : int
        Number of rooms in the world.
    nb_objects : int
        Number of objects in the world.
    """
    if rngs is None:
        rngs = {}
        rng = g_rng.next()
        rngs['map'] = RandomState(rng.randint(65635))
        rngs['objects'] = RandomState(rng.randint(65635))

    map_ = make_map(n_rooms=world_size, rng=rngs['map'])
    world = World.from_map(map_)
    world.set_player_room()
    world.populate(nb_objects=nb_objects, rng=rngs['objects'])
    return world
github microsoft / TextWorld / textworld / generator / __init__.py View on Github external
def make_map(n_rooms, size=None, rng=None, possible_door_states=["open", "closed", "locked"]):
    """ Make a map.

    Parameters
    ----------
    n_rooms : int
        Number of rooms in the map.
    size : tuple of int
        Size (height, width) of the grid delimiting the map.
    """
    rng = g_rng.next() if rng is None else rng

    if size is None:
        edge_size = int(np.ceil(np.sqrt(n_rooms + 1)))
        size = (edge_size, edge_size)

    map = create_map(rng, n_rooms, size[0], size[1], possible_door_states)
    return map
github microsoft / TextWorld / textworld / generator / world.py View on Github external
def populate_room(self, nb_objects: int, room: Variable,
                      rng: Optional[RandomState] = None,
                      object_types_probs: Optional[Dict[str, float]] = None) -> List[Proposition]:
        rng = g_rng.next() if rng is None else rng
        state = []
        types_counts = self.kb.types.count(self.state)

        inventory = Variable("I", "I")
        objects_holder = [inventory, room]

        locked_or_closed_objects = []
        lockable_objects = []
        for s in self.facts:
            # Look for containers and supporters to put stuff in/on them.
            if s.name == "at" and s.arguments[0].type in ["c", "s"] and s.arguments[1].name == room.name:
                objects_holder.append(s.arguments[0])

            # Look for containers and doors without a matching key.
            if s.name == "at" and s.arguments[0].type in ["c", "d"] and s.arguments[1].name == room.name:
                obj_propositions = [p.name for p in self.facts if s.arguments[0].name in p.names]
github microsoft / TextWorld / textworld / generator / text_grammar.py View on Github external
def __init__(self, options: Union[GrammarOptions, Mapping[str, Any]] = {}, rng: Optional[RandomState] = None):
        """
        Arguments:
            options:
                For customizing text generation process (see
                :py:class:`textworld.generator.GrammarOptions `
                for the list of available options).
            rng:
                Random generator used for sampling tag expansions.
        """
        self.options = GrammarOptions(options)
        self.grammar = OrderedDict()
        self.rng = g_rng.next() if rng is None else rng
        self.allowed_variables_numbering = self.options.allowed_variables_numbering
        self.unique_expansion = self.options.unique_expansion
        self.all_expansions = defaultdict(list)

        # The current used symbols
        self.overflow_dict = OrderedDict()
        self.used_names = set(self.options.names_to_exclude)

        # Load the grammar associated to the provided theme.
        self.theme = self.options.theme

        # Load the object names file
        path = pjoin(KnowledgeBase.default().text_grammars_path, glob.escape(self.theme) + "*.twg")
        files = glob.glob(path)
        if len(files) == 0:
            raise MissingTextGrammar(path)
github microsoft / TextWorld / textworld / generator / __init__.py View on Github external
def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None) -> Grammar:
    rng = g_rng.next() if rng is None else rng
    grammar = Grammar(options, rng)
    grammar.check()
    return grammar
github microsoft / TextWorld / textworld / generator / world.py View on Github external
def populate_with(self, objects: List[WorldObject],
                      rng: Optional[RandomState] = None) -> List[Proposition]:
        rng = g_rng.next() if rng is None else rng
        room_names = [room.id for room in self.rooms]
        nb_objects_per_room = {room_name: 0 for room_name in room_names}
        indices = np.arange(len(room_names))
        for _ in range(len(objects)):
            idx = rng.choice(indices)
            nb_objects_per_room[room_names[idx]] += 1

        state = []
        for room in self.rooms:
            state += self.populate_room_with(objects[:nb_objects_per_room[room.id]], room, rng)
            objects = objects[nb_objects_per_room[room.id]:]

        self.add_facts(state)
        return state
github microsoft / TextWorld / textworld / generator / world.py View on Github external
def populate_room_with(self, objects: WorldObject, room: WorldRoom,
                           rng: Optional[RandomState] = None) -> List[Proposition]:
        rng = g_rng.next() if rng is None else rng
        state = []

        objects_holder = [room]

        locked_or_closed_objects = []
        lockable_objects = []
        for s in self.facts:
            # Look for containers and supporters to put stuff in/on them.
            if s.name == "at" and s.arguments[0].type in ["c", "s"] and s.arguments[1].name == room.name:
                objects_holder.append(s.arguments[0])

            # Look for containers and doors without a matching key.
            if s.name == "at" and s.arguments[0].type in ["c", "d"] and s.arguments[1].name == room.name:
                obj_propositions = [p.name for p in self.facts if s.arguments[0].name in p.names]
                if "match" not in obj_propositions and s.arguments[0] not in lockable_objects:
                    lockable_objects.append(s.arguments[0])