voxelgym2D.envs.base_env module

Base class for all environments

class voxelgym2D.envs.base_env.CustomFormatter(fmt=None, datefmt=None, style='%', validate=True)[source]

Bases: Formatter

Custom formatter with colors for different log levels.

_format = '%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d'
white = '\x1b[37;1m'
green = '\x1b[32;1m'
yellow = '\x1b[33;1m'
purple = '\x1b[35;1m'
red = '\x1b[31;1m'
reset = '\x1b[0m'
COLORED_FORMATS = {'CRITICAL': '\x1b[31;1m%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d\x1b[0m', 'DEBUG': '\x1b[37;1m%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d\x1b[0m', 'ERROR': '\x1b[35;1m%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d\x1b[0m', 'INFO': '\x1b[32;1m%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d\x1b[0m', 'WARNING': '\x1b[33;1m%(asctime)s - %(levelname)s - %(message)s - %(filename)s:%(lineno)d\x1b[0m'}
format(record)[source]

Format the log record.

Parameters:

record (LogRecord) – Log record

Returns:

Formatted log record

Return type:

str

__init__(fmt=None, datefmt=None, style='%', validate=True)

Initialize the formatter with specified format strings.

Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.

Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting, str.format() ({}) formatting or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

converter()
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,

tm_sec,tm_wday,tm_yday,tm_isdst)

Convert seconds since the Epoch to a time tuple expressing local time. When ‘seconds’ is not passed in, convert the current time instead.

default_msec_format = '%s,%03d'
default_time_format = '%Y-%m-%d %H:%M:%S'
formatException(ei)

Format and return the specified exception information as a string.

This default implementation just uses traceback.print_exception()

formatMessage(record)
formatStack(stack_info)

This method is provided as an extension point for specialized formatting of stack information.

The input data is a string as returned from a call to traceback.print_stack(), but with the last trailing newline removed.

The base implementation just returns the value passed in.

formatTime(record, datefmt=None)

Return the creation time of the specified LogRecord as formatted text.

This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the ‘converter’ attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the ‘converter’ attribute in the Formatter class.

usesTime()

Check if the format uses the creation time of the record.

class voxelgym2D.envs.base_env.CustomLogger(name, log_level='ERROR')[source]

Bases: object

Logger class.

Parameters:
  • name (str) –

  • log_level (str) –

__init__(name, log_level='ERROR')[source]

Initialize the logger.

Parameters:
  • name (str) – Name of the logger.

  • log_level (str, optional) – Log level, by default “INFO”

_setup_logger()[source]

Setup the logger.

get_logger()[source]

Get the logger

Returns:

Logger

Return type:

logging.Logger

class voxelgym2D.envs.base_env.BaseEnv(render_mode=None, mapfile='600x600.npy', view_size=21, image_size=42, max_collisions=0, max_steps=60, show_path=True, multi_output=False, partial_reward=True, inference_mode=False, log_level='ERROR')[source]

Bases: Env

Base class for all environments

metadata: Dict[str, Any] = {'render_fps': 1, 'render_modes': ['None']}
__init__(render_mode=None, mapfile='600x600.npy', view_size=21, image_size=42, max_collisions=0, max_steps=60, show_path=True, multi_output=False, partial_reward=True, inference_mode=False, log_level='ERROR')[source]
Parameters:
  • render_mode (Optional[str], optional) – render mode, by default None

  • mapfile (str) – name of the map file in the maps folder

  • view_size (int) – size of the view window for observation

  • image_size (int) – size of the image to be returned as observation

  • max_collisions (int) – maximum number of collisions allowed before episode ends

  • max_steps (int) – maximum number of steps allowed before episode ends

  • show_path (bool) – whether to show the last travesed action path in the observation

  • multi_output (bool) – whether to add additional outputs in the observation

  • partial_reward (bool) – whether to give rewards for each step

  • inference_mode (bool) – whether to run in inference mode

  • log_level (str, optional) – log level, by default “ERROR”. One of “DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”

render_mode: str | None = None
observation_space: spaces.Space[ObsType]
get_logger()[source]

Returns the logger

Returns:

logger – logger object

Return type:

Logger

static find_obstacle_neighbor_count(grid_map)[source]

Finds the number of neighboring obstacles for each cell in the grid map

Parameters:

grid_map (np.ndarray) – grid map with obstacles marked as 1s and free cells marked as 0s

Returns:

neighbors – number of neighboring obstacles for each cell in the grid map

Return type:

np.ndarray

_start_end_counts()[source]

Create arrays to keep track of the start and end cell counts

Return type:

Tuple[ndarray, ndarray]

Returns:

  • start_counts (np.ndarray) – shape like self.grid_map with the count of start cells

  • end_counts (np.ndarray) – shape like self.grid_map with the count of end cells

_make_astar_matrix()[source]

Creates the astar matrix for the current world map and sets the astar grid

Return type:

None

_run_astar(target)[source]

Runs the A* algorithm on the current world map and returns the path, path cost and number of nodes visited

Parameters:

target (np.ndarray) – target location

Return type:

Tuple[List[Tuple[int, int]], float, int]

Returns:

  • path (List[Tuple[int, int]]) – path from agent to target

  • path_cost (float) – cost of the path

  • runs (int) – number of nodes visited

_slice_grid_map()[source]

Slices the grid map into a 2D numpy array of size (2*view_size, 2*view_size) Generate a mapping from the sliced grid map to the original grid map

Return type:

Tuple[Callable, Optional[ndarray]]

Returns:

  • mapping (Callable(int, int)) – mapping from the sliced grid map to the original grid map

  • potential_start_location (Union[np.ndarray, None]) – potential start location for the agent

_find_target()[source]

Finds a target location for the agent to move to

Returns:

target_location – target location

Return type:

np.ndarray

Raises:

RuntimeError – If a target location cannot be found

_get_info()[source]

Returns the info dictionary for the current step of the episode

Returns:

info – info dictionary

Return type:

Dict

_create_base_obs()[source]

Creates the base observation for the episode which can be reused throughout the episode

Return type:

None

_get_obs()[source]

Returns the observation for the current step of the episode

Returns:

obs – observation for the current step of the episode

Return type:

Union[np.ndarray, OrderedDict]

_get_new_index_from_counts(counts_mat, alpha_p=1.0)[source]

Returns a new index sampled from the counts matrix

Parameters:
  • counts_mat (np.ndarray) – counts matrix from which is used to sample the new index

  • alpha_p (float) – parameter to control the sampling probability

Returns:

sampled_index – sampled index from the counts matrix in the form (y, x)

Return type:

Tuple[int, int]

_soft_reset()[source]

Moves the agent to the center of the map and resets the target

Return type:

None

reset(*, seed=None, options=None)[source]

Resets the environment to the initial state and returns the initial observation and info

Parameters:
  • seed (Union[int, None]) – seed to use for the environment

  • options (Union[Dict, None]) – options to use for the environment

Return type:

Tuple[Union[ndarray, OrderedDict], Dict]

Returns:

  • obs (np.ndarray or OrderedDict) – observation from manystep environment

  • info (Dict) – info dictionary of the last step in the stack

_compute_reward(completion_reward=False)[source]

Computes the reward for the current step of the episode

Parameters:

completion_reward (bool) –

_take_action(action)[source]

Takes the action and returns the new agent location

Return type:

Tuple[List, bool]

Parameters:

action (ndarray) –

_is_protocol = False
_np_random: np.random.Generator | None = None
close()[source]

Closes all open matplotlib figures

Return type:

None

get_wrapper_attr(name)

Gets the attribute name from the environment.

Return type:

Any

Parameters:

name (str) –

property np_random: Generator

Returns the environment’s internal _np_random that if not set will initialise with a random seed.

Returns:

Instances of np.random.Generator

reward_range = (-inf, inf)
spec: EnvSpec | None = None
property unwrapped: Env[ObsType, ActType]

Returns the base non-wrapped environment.

Returns:

The base non-wrapped gymnasium.Env instance

Return type:

Env

action_space: spaces.Space[ActType]
step(action)[source]

Takes a step in the environment and returns the observation, reward, terminated, truncated and info

Parameters:

action (np.ndarray) – the action to take

Return type:

Tuple[Union[ndarray, OrderedDict], float, bool, bool, Dict]

Returns:

  • observation (np.ndarray or OrderedDict) – observation

  • reward (float) – reward

  • terminated (bool) – whether the episode terminated

  • truncated (bool) – whether the episode was truncated

  • info (Dict) – info dictionary

render()[source]

Renders the environment

Return type:

None

static ordinal(num)[source]

Returns the ordinal of a number

Parameters:

num (int) – the number to get the ordinal of

Returns:

the ordinal of the number

Return type:

str