Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

SearchProblem

Environment kuimaze2.SearchProblem shall be used for problems of finding the shortest path in a maze. It is used in optional task 01-easy_search and the first mandatory semestral task 03-search.

Available methods

After creation of the SearchProblem instance (see Usage), you can use the following methods of the environment:

  • env.get_start() returns the start State of the problem.
  • env.get_goals() returns the set of goal States of the problem.
  • env.is_goal(state) returns True, if the state is among the goal states.
  • env.get_actions(state) returns a list of all actions available in the given state.
  • env.get_transition_result(state, action) returns a pair of (new_state, transition_cost) when choosing applying action in state.
  • env.render() updates the graphical display of the environment. For the explanation of various parameters of this method, see help(env.render).

Usage

To use the environment, you need to:

  • Import it:
    >>> from kuimaze2 import SearchProblem, Map
  • Create an instance of the environment with the chosen map:
    >>> MAP = "S...G"
    >>> env = SearchProblem(Map.from_string(MAP))
  • Get the start (and goals, if needed):
    >>> start = env.get_start() # start State
    >>> start
    State(r=0, c=0)
    >>> goals = env.get_goals() # set of goal States
    >>> goals
    {State(r=0, c=4)}
  • Communicate with the environment:
    >>> actions = env.get_actions(start)
    >>> actions
    [<Action.UP: 0>, <Action.RIGHT: 1>, <Action.DOWN: 2>, <Action.LEFT: 3>]
    >>> next_state, cost = env.get_transition_result(start, actions[0])
    >>> next_state
    State(r=0, c=0)   # It is not possible to go UP from the start state, so stay in place
    >>> cost
    1                 # Although you stayed in place, the action costs you 1 unit
    >>> env.get_transition_result(start, actions[1])  # ... but it should be possible to go RIGHT
    (State(r=0, c=1), 1)                              # And yes, it is, we moved.

To update the graphical display of the environment (if you created it with env = SearchProblem(…, graphics=True)), use:

env.render()
Method render() has many arguments that can be used to display various parts of your algorithm. See help(SearchProblem.render).

courses/be5b33kui/semtasks/kuimaze/10_searchproblem.txt · Last modified: 2024/02/20 16:09 by xposik