Package tulip :: Module gridworld :: Class GridWorld
[frames] | no frames]

Class GridWorld


4-connected grids with primitives like obstacles and goals.

Instance Methods
 
__init__(self, gw_desc=None, prefix='Y')
Load gridworld described in given string, or make empty instance.
 
__eq__(self, other)
Test for equality.
 
__ne__(self, other)
 
__str__(self)
str(x)
 
__getitem__(self, key, use_next=False, nonbool=True)
Return variable name corresponding to this cell.
 
__copy__(self)
 
copy(self)
Deep-copy GridWorld instance.
 
state(self, key, offset=(0, 0), nonbool=True)
Return dictionary form of state with keys of variable names.
 
is_empty(self, coord, extend=False)
Is cell at coord empty?
 
mark_occupied(self, coord)
Mark cell at coord as statically (permanently) occupied.
 
mark_empty(self, coord)
Mark cell at coord as empty.
 
is_reachable(self, start, stop)
Decide whether there is a path from start cell to stop.
 
plot(self, font_pt=18, show_grid=False, grid_width=2, troll_list=None, axes=None)
Draw figure depicting this gridworld.
 
pretty(self, show_grid=False, line_prefix='', path=None, goal_order=False)
Return pretty-for-printing string.
 
size(self)
Return size of gridworld as a tuple in row-major order.
 
loads(self, gw_desc)
Reincarnate using given gridworld description string.
 
load(self, gw_file)
Read description from given file.
 
dumps(self, line_prefix='')
Dump gridworld description string.
GridWorld
dump_subworld(self, size, offset=(0, 0), prefix='Y', extend=False)
Generate new GridWorld instance from part of current one.
PropPreservingPartition
dump_ppartition(self, side_lengths=(1.0, 1.0), offset=(0.0, 0.0), nonbool=True)
Return proposition-preserving partition from this gridworld.
GRSpec
spec(self, offset=(0, 0), controlled_dyn=True, nonbool=True)
Return GRSpec instance describing this gridworld.
GridWorld
scale(self, xf=1, yf=1)
Return a new gridworld equivalent to this but scaled by integer factor (xf, yf).

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, gw_desc=None, prefix='Y')
(Constructor)

 

Load gridworld described in given string, or make empty instance.

Parameters:
  • gw_desc - String containing a gridworld description, or None to create an empty instance.
  • prefix - String to be used as prefix for naming gridworld cell variables.
Overrides: object.__init__

__eq__(self, other)
(Equality operator)

 

Test for equality.

Does not compare prefixes of cell variable names.

__str__(self)
(Informal representation operator)

 

str(x)

Overrides: object.__str__
(inherited documentation)

__getitem__(self, key, use_next=False, nonbool=True)
(Indexing operator)

 

Return variable name corresponding to this cell.

Supports negative wrapping, e.g., if Y is an instance of GridWorld, then Y[-1,-1] will return the variable name of the cell in the bottom-right corner, Y[0,-1] the name of the top-right corner cell, etc. As usual in Python, you can only wrap around once.

Parameters:
  • next - Use the primed (i.e., state at next time step) form of the variable.
  • nonbool - If True, then use variables with integer domains.

copy(self)

 

Deep-copy GridWorld instance.

The copy has no dependence on the original.

state(self, key, offset=(0, 0), nonbool=True)

 

Return dictionary form of state with keys of variable names.

Supports negative indices for key, e.g., as in __getitem__.

The offset argument is motivated by the use-case of multiple agents whose moves are governed by separate "gridworlds" but who interact in a shared space; with an offset, we can make "sub-gridworlds" and enforce rules like mutual exclusion.

Parameters:
  • nonbool - If True, then use variables with integer domains.

is_empty(self, coord, extend=False)

 

Is cell at coord empty?

Parameters:
  • coord - (row, column) pair; supports negative indices.
  • extend - If True, then do not wrap indices and treat any cell outside the grid as being occupied.

is_reachable(self, start, stop)

 

Decide whether there is a path from start cell to stop.

Assume the gridworld is 4-connected.

Parameters:
  • start - (row, column) pair; supports negative indices.
  • stop - same as start argument.
Returns:
True if there is a path, False otherwise.

plot(self, font_pt=18, show_grid=False, grid_width=2, troll_list=None, axes=None)

 

Draw figure depicting this gridworld.

Figure legend (symbolic form in parenthesis):

  • "I" ('m+') : possible initial position;
  • "G" ('r*') : goal;
  • "E" ('gx') : goal of a troll; its extent is indicated by gray cells
Parameters:
  • font_pt - size (in points) for rendering text in the figure. If 0, then use symbols instead (see legend above).
  • troll_list - ...same as the argument with the same name given to add_trolls.
  • axes - Instance of matplotlib.axes._subplots.AxesSubplot on which to draw, or None, in which case a new figure is created.

pretty(self, show_grid=False, line_prefix='', path=None, goal_order=False)

 

Return pretty-for-printing string.

Parameters:
  • show_grid - If True, then grid the pretty world and show row and column labels along the outer edges.
  • line_prefix - prefix each line with this string.

loads(self, gw_desc)

 

Reincarnate using given gridworld description string.

Parameters:
  • gw_desc - String containing a gridworld description.

    In a gridworld description, any line beginning with # is ignored (regarded as a comment). The first non-blank and non-comment line must give the grid size as two positive integers separated by whitespace, with the first being the number of rows and the second the number of columns.

    Each line after the size line is used to construct a row of the gridworld. These are read in order with maximum number of lines being the number of rows in the gridworld. A row definition is whitespace-sensitive up to the number of columns (any characters beyond the column count are ignored, so in particular trailing whitespace is allowed) and can include the following symbols:

    • ' ' : an empty cell,
    • '*' : a statically occupied cell,
    • 'I' : possible initial cell,
    • 'G' : goal cell (must be visited infinitely often).

    If the end of file is reached before all rows have been constructed, then the remaining rows are assumed to be empty. After all rows have been constructed, the remainder of the file is ignored.

load(self, gw_file)

 

Read description from given file.

Merely a convenience wrapper for the loads method.

dumps(self, line_prefix='')

 

Dump gridworld description string.

Parameters:
  • line_prefix - prefix each line with this string.

dump_subworld(self, size, offset=(0, 0), prefix='Y', extend=False)

 

Generate new GridWorld instance from part of current one.

Does not perform automatic truncation (to make desired subworld fit); instead a ValueError exception is raised. However, the "extend" argument can be used to achieve something similar.

Possible initial positions and goals are not included in the returned GridWorld instance.

Parameters:
  • size - (height, width)
  • prefix - String to be used as prefix for naming subgridworld cell variables.
  • extend - If True, then any size and offset is permitted, where any positions outside the actual gridworld are assumed to be occupied.
Returns: GridWorld

dump_ppartition(self, side_lengths=(1.0, 1.0), offset=(0.0, 0.0), nonbool=True)

 

Return proposition-preserving partition from this gridworld.

Adjacency of cells is as returned by prop2partition.prop2part().

Parameters:
  • side_lengths - pair (W, H) giving width and height of each cell, assumed to be the same across the grid.
  • offset - 2-dimensional coordinate declaring where the bottom-left corner of the gridworld should be placed in the continuous space; default places it at the origin.
Returns: PropPreservingPartition

spec(self, offset=(0, 0), controlled_dyn=True, nonbool=True)

 

Return GRSpec instance describing this gridworld.

The offset argument is motivated by the use-case of multiple agents whose moves are governed by separate "gridworlds" but who interact in a shared space; with an offset, we can make "sub-gridworlds" and enforce rules like mutual exclusion.

If nonbool is False, then variables are named according to prefix_R_C, where prefix is given (attribute of this GridWorld object), R is the row, and C is the column of the cell (0-indexed). If nonbool is True (default), cells are identified with subformulae of the form:

 ((prefix_r = R) & (prefix_c = C))

GridWorld.__getitem__ and extract_coord provide reference implementations.

Parameters:
  • offset - index offset to apply when generating the specification; e.g., given prefix of "Y", offset=(2,1) would cause the variable for the cell at (0,3) to be named Y_2_4.
  • controlled_dyn - whether to treat this gridworld as describing controlled ("system") or uncontrolled ("environment") variables.
  • nonbool - If True, then use variables with integer domains.
Returns: GRSpec

scale(self, xf=1, yf=1)

 

Return a new gridworld equivalent to this but scaled by integer factor (xf, yf). In the new world, obstacles are increased in size but initials and goals change their position only. If this world is of size (h, w) then the returned world will be of size (h*yf, w*xf).

Parameters:
  • xf - integer scaling factor for columns
  • yf - integer scaling factor for rows
Returns: GridWorld