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

Module gridworld

Routines for working with gridworlds.

Note (24 June 2012): Several pieces of source code are taken or derived from btsynth; see http://scottman.net/2012/btsynth

Classes
  GridWorld
4-connected grids with primitives like obstacles and goals.
Functions
GridWorld, or None if timeout occurs.
random_world(size, wall_density=0.2, num_init=1, num_goals=2, prefix='Y', ensure_feasible=False, timeout=None, num_trolls=0)
Generate random gridworld of given size.
GridWorld
narrow_passage(size, passage_width=1, num_init=1, num_goals=2, passage_length=0.4, ptop=None, prefix='Y')
Generate a narrow-passage world: this is a world containing two zones (initial, final) with a tube connecting them.
GridWorld
unoccupied(size, prefix='Y')
Generate entirely unoccupied gridworld of given size.
(GRSpec, list)
add_trolls(Y, troll_list, prefix='X', start_anywhere=False, nonbool=True, get_moves_lists=True)
Create GR(1) specification with troll-like obstacles.
 
extract_coord(var_name)
Assuming prefix_R_C format, return (prefix,row,column) tuple.
 
animate_paths(Z, paths, jitter=0.0, save_prefix=None)
Animate a list of paths simultaneously in world Z using matplotlib.
Variables
  __package__ = 'tulip'
Function Details

random_world(size, wall_density=0.2, num_init=1, num_goals=2, prefix='Y', ensure_feasible=False, timeout=None, num_trolls=0)

 

Generate random gridworld of given size.

While an instance of GridWorld is returned, other views of the result are possible; e.g., to obtain a description string, use GridWorld.dumps.

Parameters:
  • size - a pair, indicating number of rows and columns.
  • wall_density - the ratio of walls to total number of cells.
  • num_init - number of possible initial positions.
  • num_goals - number of positions to be visited infinitely often.
  • prefix - string to be used as prefix for naming gridworld cell variables.
  • num_trolls - number of random trolls to generate, each occupies an area of radius 1. If nonzero, then a list specifying the trolls will also be returned.
  • ensure_feasible - guarantee that all goals and initial positions are mutually reachable, assuming a 4-connected grid. This method may not be complete, i.e., may fail to return a feasible random gridworld with the given parameters. Note that "feasibility" does not account for nondeterminism (in particular, nonzero num_trolls argument has no effect.)
  • timeout - if ensure_feasible, then quit if no correct random world is found before timeout seconds. If timeout is None (default), then do not impose time constraints.
Returns: GridWorld, or None if timeout occurs.

narrow_passage(size, passage_width=1, num_init=1, num_goals=2, passage_length=0.4, ptop=None, prefix='Y')

 

Generate a narrow-passage world: this is a world containing two zones (initial, final) with a tube connecting them.

Parameters:
  • size - a pair, indicating number of rows and columns.
  • passage_width - the width of the connecting passage in cells.
  • passage_length - the length of the passage as a proportion of the width of the world.
  • num_init - number of possible initial positions.
  • num_goals - number of positions to be visited infinitely often.
  • ptop - row number of top of passage, default (None) is random
  • prefix - string to be used as prefix for naming gridworld cell variables.
Returns: GridWorld

unoccupied(size, prefix='Y')

 

Generate entirely unoccupied gridworld of given size.

Parameters:
  • size - a pair, indicating number of rows and columns.
  • prefix - String to be used as prefix for naming gridworld cell variables.
Returns: GridWorld

add_trolls(Y, troll_list, prefix='X', start_anywhere=False, nonbool=True, get_moves_lists=True)

 

Create GR(1) specification with troll-like obstacles.

Trolls are introduced into the specification with names derived from the given prefix and a number (matching the order in troll_list). Note that mutual exclusion is only between the controlled "Y gridworld" position and each troll, but not between trolls.

Parameters:
  • Y (GridWorld) - The controlled gridworld, describing in particular static obstacles that must be respected by the trolls.
  • troll_list - List of pairs of center position, to which the troll must always eventually return, and radius defining the extent of the trollspace. The radius is measured using infinity-norm.
  • start_anywhere - If True, then initial troll position can be anywhere in its trollspace. Else (default), the troll is assumed to begin each game at its center position.
  • nonbool - If True, then use variables with integer domains.
  • get_moves_lists - Consult returned value description below.
Returns: (GRSpec, list)
If get_moves_lists is True, then returns (spec, moves_N) where spec is the specification incorporating all of the trolls, and moves_N is a list of lists of states (where "state" is given as a dictionary with keys of variable names), where the length of moves_N is equal to the number of trolls, and each element of moves_N is a list of possible states of that the corresponding troll (dynamic obstacle). If get_moves_lists is False, then moves_N is not returned and not computed.

extract_coord(var_name)

 

Assuming prefix_R_C format, return (prefix,row,column) tuple.

prefix is of type string, row and column are integers.

The "nowhere" coordinate has form prefix_n_n. To indicate this, (-1, -1) is returned as the row, column position.

If error, return None or throw exception.

animate_paths(Z, paths, jitter=0.0, save_prefix=None)

 

Animate a list of paths simultaneously in world Z using matplotlib.

Parameters:
  • Z - Gridworld for which paths were generated.
  • paths - List of paths to animate (one per robot). Each path is a list of pairs of gridworld cells, i.e., of the form [(r0, c0), (r1, c1), ...], where the first position is row r0 and column c0, etc.
  • jitter - Random jitter added to each coordinate value in animation. Makes the robot's path more visible by avoiding overlap.
  • save_prefix - If not None, do not show an animation but produce a series of images "<save_prefix>nnn.png" which can be compiled into an animated GIF.