Real-time dynamic programming (RTDP) is value iteration that only updates the states it actually visits while acting greedily. On undiscounted stochastic shortest-path tasks with negative rewards and zero initial values it converges to an optimal policy on the relevant states, often without touching large parts of the state space.
Write rtdp(P, R, start_states, goal_states, n_episodes, seed=0, max_steps=1000):
P has shape (n_states, n_actions, n_states) with transition probabilities and R has shape (n_states, n_actions) with expected rewards. There is no discounting ().rng = np.random.default_rng(seed) once. Each episode starts in start_states[int(rng.integers(len(start_states)))].u = rng.random() and take the first index whose cumulative probability np.cumsum(P[s, a]) is greater than u (use the last index if none is).max_steps steps.Return (V, updates, lengths): the value array, an integer array with the number of updates per state, and a list with the number of steps of each episode.