Gridworld

class sorrel.worlds.Gridworld(height: int, width: int, layers: int, default_entity: Entity)

Basic gridworld environment class with functions for getting and manipulating the locations of entities.

height

The height of the gridworld.

Type:

int

width

The width of the gridworld.

Type:

int

layers

The number of layers that the gridworld has.

Type:

int

default_entity

An entity that the gridworld is filled with at creation by default.

Type:

sorrel.entities.entity.Entity

world

A representation of the gridworld as a Numpy array of Entities, with dimensions height x width x layers.

turn

The number of turns taken by the environment.

Type:

int

max_turns

The total number of turns possible in an epoch.

Type:

int

total_reward

The total reward accumulated by all agents in the environment.

Type:

float

Methods

Class Methods

Gridworld.create_world() None

Assigns self.world a new gridworld of size self.height x self.width x self.layers filled with deep copies of self.default_entity.

Also sets self.turn and self.total_reward to 0.

This function is used in self.__init__(), and may be useful for resetting environments.

Gridworld.add(target_location: tuple[int, ...], entity: Entity) None

Adds an entity to the world at a location, replacing any existing entity at that location.

Parameters:
  • target_location (tuple[int, ...]) – the location of the entity.

  • entity (Entity) – the entity to be added.

Gridworld.remove(target_location: tuple[int, ...]) Entity

Remove the entity at a location.

The target location will then be filled with a deep copy of self.default_entity.

Parameters:

target_location (tuple[int, ...]) – the location of the entity.

Returns:

the entity previously at the given location.

Return type:

Entity

Gridworld.move(entity: Entity, new_location: tuple[int, ...]) bool

Move an entity to a new location.

The entity at the new location will be removed. The old location of the entity will be filled with a deep copy of self.default_entity.

Parameters:
  • entity (Entity) – entity to be moved.

  • new_location (tuple[int, ...]) – location to move the entity to.

Returns:

True if move was successful (i.e. the entity currently at new_location is passable), False otherwise.

Return type:

bool

Gridworld.observe(target_location: tuple[int, ...]) Entity

Observes the entity at a location.

Parameters:

target_location (tuple[int, ...]) – the location to observe.

Returns:

the entity at the observed location.

Return type:

Entity

Gridworld.valid_location(index: tuple[int, ...]) bool

Check if the given index is in the world.

Parameters:

index (tuple[int, ...]) – A tuple of coordinates or a Location object.

Returns:

Whether the index is in env.world.

Return type:

bool

Static Methods

Tip

You can still call a static method using an instance.

Gridworld.get_entities_of_kind(kind: str) list[Entity]

Given the kind of an entity, return a list of entities in a world that are the same kind.

Parameters:
  • world (np.array) – the world of a particular Gridworld.

  • kind (str) – the class string (or string representation) of the query entity.

Returns:

a list of all entities in the world that have the same kind.

Return type:

list[Entity]