Search code examples
pythonreinforcement-learningopenai-gym

Calling Env State Tuple


I'm new to Open Ai Gym and currently running Reinforcement Learning (RL) in the Taxi Environment and my research requires me to be able to call the State tuple (or called "State Space" in the Taxi.py file ) for some data mining / state-action pair operation.

Is there a function to call this?

Eg: State(123) = (taxi_row, taxi_col, passenger_location, destination)

In RL the state and actions are represented in matrices form, column = state, row = action.

In the source code (taxi.py) its called "state space is represented by (taxi_row, taxi_col, passenger_location, destination)"


Solution

  • You can do that like so:

    >>> import gym
    >>> env = gym.make('Taxi-v2')
    >>> from gym.envs.toy_text.taxi import *
    >>> 
    >>> 
    >>> x = TaxiEnv()
    >>> random_state = 123
    >>> taxi_row, taxi_col, passenger_index, destination_index = x.decode(random_state)
    >>> taxi_row
    1
    >>> taxi_col
    1
    >>> passenger_index
    0
    >>> destination_index
    3
    

    In your question, you want the passenger_location and destination. But the code I used was returning the passenger_index and destination_index. So, you can get the location easily if you understood the environment map.

    The following is the simple map used in the environment:

    MAP = [
        "+---------+",
        "|R: | : :G|",
        "| : | : : |",
        "| : : : : |",
        "| | : | : |",
        "|Y| : |B: |",
        "+---------+",
    ]
    

    In this map, we have four different locations (R, G, Y, B). Now, you can get the passenger location and destination easily knowing the index like so:

    • Passenger locations:

      • 0: R(ed)
      • 1: G(reen)
      • 2: Y(ellow)
      • 3: B(lue)
      • 4: in taxi
    • Destinations:

      • 0: R(ed)
      • 1: G(reen)
      • 2: Y(ellow)
      • 3: B(lue)

    Hope this answers your question!!