主要内容

state2idx

Returns the index associated with a given state name for a GridWorld or GenericMDP object

Since R2026a

    Description

    idx = state2idx(mdp,stateName) returns the positive integer idx associated with the mdp state name stateName. Use idx to index the state stateName within the mdp transition or reward matrix. You can also use idx to refer to stateName within an environment object created from mdp.

    example

    Examples

    collapse all

    Create an 8-by-13 gridworld object with "Kings" moves. For more information, see createGridWorld and Create Custom Grid World Environments.

    gw = createGridWorld(8,13,"Kings");

    Display the allowed actions.

    gw.Actions
    ans = 8×1 string
        "N"
        "S"
        "E"
        "W"
        "NE"
        "NW"
        "SE"
        "SW"
    
    

    Use the action2idx function to obtain the index associated with the "SE" action.

    action2idx(gw,"SE")
    ans = 
    7
    

    Use the idx2action function to obtain the name of the fifth action.

    idx2action(gw,5)
    ans = 
    "NE"
    

    Use the state2idx function to obtain the index associated with the state "[3,5]".

    state2idx(gw,"[3,5]")
    ans = 
    35
    

    Use the idx2state function to obtain the name of the twenty-fifth state.

    idx2state(gw,25)
    ans = 
    "[1,4]"
    

    Set two terminal states.

    gw.TerminalStates = ["[6,12]";"[4,8]"];

    Set the rewards for reaching the terminal states, using state2idx to index the terminal states.

    gw.R(:,state2idx(gw,gw.TerminalStates),:) = 10;

    Set to zero the probability of transitioning out from state "[2,4]". Use state2idx to obtain the index associated with the state "[2,4]".

    gw.T(state2idx(gw,"[2,4]"),:,:) = 0;

    For any action, set to one the probability from transitioning from state "[2,4]" to state "[4,4]".

    gw.T(state2idx(gw,"[2,4]"),state2idx(gw,"[4,4]"),:) = 1;

    Use rlMDPEnv to create the grid world environment env from the GridWorld object gw.

    env = rlMDPEnv(gw);

    To specify a reset function that sets the state to [3,3], first, obtain the index of this state.

    x0 = state2idx(env.Model,"[3,3]")
    x0 = 
    19
    

    Then, create an anonymous function handle that sets the initial state to x0. The value of x0 is saved in the anonymous function workspace at definition time.

    env.ResetFcn = @() x0;

    Call the environment reset function, (which in turn calls the function specified in env.ResetFcn), and return the initial state.

    x = reset(env)
    x = 
    19
    

    Display the name of the current state.

    idx2state(env.Model,x)
    ans = 
    "[3,3]"
    

    Move the agent position in the southeast direction.

    [xn,rn,id]=step(env,action2idx(env.Model,"SE"))
    xn = 
    28
    
    rn = 
    0
    
    id = logical
       0
    
    

    Use idx2state to display the name of the next state.

    idx2state(env.Model,xn)
    ans = 
    "[4,4]"
    

    Input Arguments

    collapse all

    Markov Decision Process object, specified as a GenericMDP or GridWorld object. For more information, see createMDP and createGridWorld, respectively.

    Example: mdp = createMDP(8,["up";"down"]); creates the GenericMDP object mdp.

    State name, specified as a string or character vector.

    Example: "s2"

    Output Arguments

    collapse all

    State index, specified as a positive integer. Use idx to index the state stateName within the mdp transition or reward matrix. You can also use idx to refer to stateName within an environment object created from mdp.

    Version History

    Introduced in R2026a