Package kuimaze :: Module maze :: Class Maze
[hide private]
[frames] | no frames]

Class Maze

source code

Maze class takes care of GUI and interaction functions.

Instance Methods [hide private]
 
__init__(self, image, grad, node_rewards=None, path_costs=None, trans_probs=None, show_level=SHOW.FULL_MAZE, start_node=None, goal_nodes=None)
Parameters node_rewards, path_costs and trans_probs are meant for defining more complicated mazes.
source code
 
get_state_reward(self, state) source code
namedtuple state
get_start_state(self)
Returns a start state
source code
 
close_gui(self) source code
 
set_node_utils(self, utils)
a visualisation method - sets an interal variable for displaying utilities
source code
boolean
is_goal_state(self, current_state)
Check whether a current_node is goal state or not
source code
 
is_danger_state(self, current_state) source code
list
get_goal_nodes(self)
Returns a list of goal nodes
source code
list of namedtuple weighted_state
get_all_states(self)
Returns a list of all the problem states
source code
tuple
get_dimensions(self)
Returns dimensions of problem
source code
action from ACTION
get_actions(self, current_state)
Generate (yield) actions possible for the current_state It does not check the outcome this is left to the result method
source code
namedtuple state
result(self, current_state, action)
Apply the action and get the state; deterministic version
source code
list of tuples
get_next_states_and_probs(self, curr, action)
For the commanded action it generates all posiible outcomes with associated probabilities
source code
 
set_explored(self, states)
sets explored states list, preparation for visualisation
source code
 
set_probs(self, obey, confusionL, confusionR, confusion180) source code
 
set_probs_table(self, obey, confusionL, confusionR, confusion180) source code
 
set_visited(self, states)
sets seen states list, preparation for visualisation
source code
 
non_det_result(self, action) source code
boolean
__is_inside(self, current_state)
Check whether a state is inside a problem
source code
boolean
__is_inside_valid(self, current_state)
Check whether a state is inside a problem and is not a wall
source code
 
clear_player_data(self)
Clear player data for using with different player or running another find_path
source code
 
__clear_player_data(self)
Clear player data for using with different player or running another find_path
source code
 
set_player(self, player)
Set player associated with this problem.
source code
 
show_and_break(self, drawed_nodes=None)
Main GUI function - call this from BaseAgent.find_path() to update GUI and break at this point to be able to step your actions.
source code
 
show_path(self, full_path)
Show resulting path_section given as a list of consecutive namedtuples path_section to show in GUI.
source code
 
set_show_level(self, show_level)
Set new show level.
source code
 
set_eps_folder(self)
Set folder where the EPS files will be saved.
source code
 
__setup_gui(self)
Setup and draw basic GUI.
source code
 
__destroy_gui(self, unblock=True)
Safely destroy GUI.
source code
 
__renew_gui(self)
Renew GUI if a new player connects to a problem object.
source code
 
__set_show_level_cb(self)
Just a simple callback for tkinter radiobuttons for selecting show level
source code
 
__clear_lines(self)
Clear path_section lines if running same player twice.
source code
 
__set_cell_color(self, current_node, color)
Set collor at position given by current position.
source code
 
save_as_eps(self, disabled)
Save canvas as color EPS - response for third button.
source code
 
__get_cell_center_coords(self, x, y)
Mapping from problem coordinates to GUI coordinates.
source code
 
__get_cell_center(self, x)
Mapping from problem coordinate to GUI coordinate, only one coord.
source code
 
__gui_update_map(self, explored_only=True)
Updating cell colors depending on what has been already explored.
source code
 
visualise(self, dictionary)
Update state rewards in GUI.
source code
 
__draw_text(self, current_node, string)
Draw text in the center of cells in the same manner as draw colors is done.
source code
 
__text_to_top(self)
Move text fields to the top layer of the canvas - to cover arrow :return:
source code
 
__draw_text_four(self, current_node, my_list)
Draw four text cells into one square
source code
 
__color_string_depth(self, color, x, y)
Method adjust color due to depth of square in maze :param color: color string in hexadecimal ...
source code
 
__set_grad_data(self)
Sets data needed for rendering 3D ilusion :return: None
source code
Class Variables [hide private]
  __deltas = [[0,-1], [1, 0], [0, 1], [-1, 0], [1,-1], [1, 1], [...
Method Details [hide private]

__init__(self, image, grad, node_rewards=None, path_costs=None, trans_probs=None, show_level=SHOW.FULL_MAZE, start_node=None, goal_nodes=None)
(Constructor)

source code 

Parameters node_rewards, path_costs and trans_probs are meant for defining more complicated mazes. Parameter start_node redefines start state completely, parameter goal_nodes will add nodes to a list of goal nodes.

Parameters:
  • image (string) - path_section to an image file describing problem. Expects to find RGB image in given path_section

    white color - empty space

    black color - wall space

    red color - goal state

    blue color - start state

  • node_rewards (either string pointing to stored numpy.ndarray or numpy.ndarray itself or None for default value. Shape of numpy.ndarray must be (x, y) where (x, y) is shape of problem.) - optional setting of state rewards. If not set, or incorrect input, it will be set to default value - all nodes have reward of zero.
  • path_costs (either string pointing to stored numpy.ndarray or numpy.ndarray itself or None for default value. Shape of numpy.ndarray must be (x, y, 2) where (x, y) is shape of problem.) - optional setting of path_section costs. If not set, or incorrect input, it will be set to default value - all paths have cost of one.
  • trans_probs (either string pointing to stored numpy.ndarray or numpy.ndarray itself or None for default value. Shape of numpy.ndarray must be (x, y, 4, 4) where (x, y) is shape of problem.) - optional setting of transition probabilities for modelling MDP. If not set, or incorrect input, it will be set to default value - actions have probability of 1 for itself and 0 for any other.
  • show_level (kuimaze.SHOW) - Controlling level of displaying in GUI.
  • start_node (namedtuple state or None for default start state loaded from image.) - Redefining start state. Must be a valid state inside a problem without a wall.
  • goal_nodes (iterable of namedtuples state or None for default set of goal nodes loaded from image.) - Appending to a list of goal nodes. Must be valid nodes inside a problem without a wall.
Raises:
  • AssertionError - When image is not RGB image or if show is not of type kuimaze.SHOW or if initialization didn't finish correctly.

get_start_state(self)

source code 

Returns a start state

Returns: namedtuple state
start state

set_node_utils(self, utils)

source code 

a visualisation method - sets an interal variable for displaying utilities

Parameters:
  • utils - dictionary of utilities, indexed by tuple - state coordinates
Returns:
None

is_goal_state(self, current_state)

source code 

Check whether a current_node is goal state or not

Parameters:
Returns: boolean
True if state is a goal state, False otherwise

get_goal_nodes(self)

source code 

Returns a list of goal nodes

Returns: list
list of goal nodes

get_all_states(self)

source code 

Returns a list of all the problem states

Returns: list of namedtuple weighted_state
list of all states

get_dimensions(self)

source code 

Returns dimensions of problem

Returns: tuple
x and y dimensions of problem. Note that state indices are zero-based so if returned dimensions are (5, 5), state (5, 5) is not inside problem.

get_actions(self, current_state)

source code 

Generate (yield) actions possible for the current_state It does not check the outcome this is left to the result method

Parameters:
  • current_state
Returns: action from ACTION
action (relevant for the problem - problem in this case)

result(self, current_state, action)

source code 

Apply the action and get the state; deterministic version

Parameters:
Returns: namedtuple state
state (result of the action applied at the current_state)

get_next_states_and_probs(self, curr, action)

source code 

For the commanded action it generates all posiible outcomes with associated probabilities

Parameters:
Returns: list of tuples
list of tuples (next_state, probability_of_ending_in_the_next_state)

set_explored(self, states)

source code 

sets explored states list, preparation for visualisation

Parameters:
  • states - iterable of state

set_visited(self, states)

source code 

sets seen states list, preparation for visualisation

Parameters:
  • states - iterable of state

__is_inside(self, current_state)

source code 

Check whether a state is inside a problem

Parameters:
Returns: boolean
True if state is inside problem, False otherwise

__is_inside_valid(self, current_state)

source code 

Check whether a state is inside a problem and is not a wall

Parameters:
Returns: boolean
True if state is inside problem and is not a wall, False otherwise

set_player(self, player)

source code 

Set player associated with this problem.

Parameters:
  • player (BaseAgent or its descendant) - player to be used for association
Raises:
  • AssertionError - if player is not instance of BaseAgent or its descendant

show_and_break(self, drawed_nodes=None)

source code 

Main GUI function - call this from BaseAgent.find_path() to update GUI and break at this point to be able to step your actions. Example of its usage can be found at BaseAgent.find_path()

Don't use it too often as it is quite expensive and rendering after single exploration might be slowing your code down a lot.

You can optionally set parameter drawed_nodes to a list of lists of dimensions corresponding to dimensions of problem and if show_level is higher or equal to SHOW.NODE_REWARDS, it will plot those in state centers instead of state rewards. If this parameter is left unset, no redrawing of texts in center of nodes is issued, however, it can be set to True which will draw node_rewards saved in the problem.

If show_level is SHOW.NONE, thisets function has no effect

Parameters:
  • drawed_nodes (list of lists of the same dimensions as problem or boolean or None) - custom objects convertible to string to draw to center of nodes or True or None

show_path(self, full_path)

source code 

Show resulting path_section given as a list of consecutive namedtuples path_section to show in GUI. Example of such usage can be found in BaseAgent.find_path()

Parameters:

set_show_level(self, show_level)

source code 

Set new show level. It will redraw whole GUI, so it takes a while.

Parameters:
  • show_level (SHOW) - new show_level to set
Raises:
  • AssertionError - if show_level is not an instance of SHOW

set_eps_folder(self)

source code 

Set folder where the EPS files will be saved.

Parameters:
  • folder (string with a valid path_section) - folder to save EPS files

__setup_gui(self)

source code 

Setup and draw basic GUI. Imports tkinter.

__destroy_gui(self, unblock=True)

source code 

Safely destroy GUI. It is possible to pass an argument whether to unblock find_path() method, by default it is unblocking.

Parameters:
  • unblock (boolean) - Whether to unblock find_path() method by calling this method

__set_cell_color(self, current_node, color)

source code 

Set collor at position given by current position. Code inspired by old implementation of RPH Maze (predecessor of kuimaze)

Parameters:

__get_cell_center_coords(self, x, y)

source code 

Mapping from problem coordinates to GUI coordinates.

Parameters:
  • x - x coord in problem
  • y - y coord in problem
Returns:
(x, y) coordinates in GUI (centers of cells)

__get_cell_center(self, x)

source code 

Mapping from problem coordinate to GUI coordinate, only one coord.

Parameters:
  • x - coord in problem (could be either x or y)
Returns:
center of cell corresponding to such coordinate in GUI

__gui_update_map(self, explored_only=True)

source code 

Updating cell colors depending on what has been already explored.

Parameters:
  • explored_only (boolean) - if True, update only explored position and leave unexplored black. if False, draw everything

visualise(self, dictionary)

source code 

Update state rewards in GUI. If drawed_nodes is passed and is not None, it is expected to be list of lists of objects with string representation of same dimensions as the problem. Might fail on IndexError if passed list is smaller. if one of these objects in list is None, then no text is printed.

If drawed_nodes is None, then node_rewards saved in Maze objects are printed instead

Parameters:
  • drawed_nodes (list of lists of appropriate dimensions or None) - list of lists of objects to be printed in GUI instead of state rewards
Raises:
  • IndexError - if drawed_nodes parameter doesn't match dimensions of problem

__draw_text(self, current_node, string)

source code 

Draw text in the center of cells in the same manner as draw colors is done.

Parameters:
  • current_node (namedtuple state) - position on which the text is to be printed in Maze coordinates
  • string (string) - string to be drawn

__draw_text_four(self, current_node, my_list)

source code 

Draw four text cells into one square

Parameters:
  • current_node - position on which the text is to be printed in Maze coordinates
  • my_list (list of floats or ints) - list to be drawn

__color_string_depth(self, color, x, y)

source code 

Method adjust color due to depth of square in maze :param color: color string in hexadecimal ... for example "#FFF000000" for red :param x: index of square :param y: index of square :return: new color string


Class Variable Details [hide private]

__deltas

Value:
[[0,-1], [1, 0], [0, 1], [-1, 0], [1,-1], [1, 1], [-1,-1], [-1, 1]]