Skip to content
ELECTRONICS ENGINEERINGelectronics-engineering.com
Reinforcement and Imitation Learning

Markov decision processes: agent, environment, return, policy and value functions

The language of reinforcement learning: the agent-environment loop, the Markov property, return and discount, policy, state and action value, advantage, the gridworld with random and optimal policy.

https://www.electronics-engineering.com/en/markov-decision-processes-return-policy-value-functions/

No ratings yet: be the first to rate this page.

In supervised learning someone provides the right answer for every example. In reinforcement learning (RL) nobody knows it: an agent tries actions in an environment, receives a numerical reward and must learn by itself which behaviour maximises it in the long run. This is how programs that play better than champions (AlphaGo), robots that walk, traffic controllers and the final stage of large language models (RLHF) are trained. This first lesson defines the mathematical language of the whole course: the Markov decision process, the return, the policy and the value functions.

The agent and the environment

At each instant t = 0, 1, 2, \dots the agent observes the state s_t, chooses an action a_t, and the environment responds with a reward r_{t+1} and a new state s_{t+1}. The loop repeats. For a robot the state is the joint positions and velocities, the action the motor torques, the reward a number that rewards moving forward without falling. For CartPole, the example we will use often, the state is the cart position and velocity, the pole angle and angular velocity, the action is pushing left or right, and the reward is +1 for every step in which the pole stays up.

The agent-environment loop
The agent, blue, receives the state s t and the reward r t and chooses the action a t; the environment, green, responds with the new state and the new reward. The one-step delay is shown by the z to the minus 1 block.

The Markov decision process

An MDP (Markov decision process) is the five-tuple (\mathcal S, \mathcal A, P, R, \gamma):

  • \mathcal S the set of states, \mathcal A that of actions;
  • P(s' \mid s, a) the probability of moving to s' by taking a in s;
  • R(s, a) the average reward obtained by taking a in s;
  • \gamma \in [0, 1] the discount factor.

The word Markov indicates the fundamental assumption: the future depends on the past only through the present state, P(s_{t+1} \mid s_t, a_t, s_{t-1}, \dots) = P(s_{t+1} \mid s_t, a_t). If the observation does not contain everything needed (for example a single image of a ball, which does not tell its velocity), it is recovered by putting previous observations in the state too or by using networks with memory.

The return and discounting

The goal is not the immediate reward but the return, the sum of discounted future rewards:

    \[ G_t = r_{t+1} + \gamma r_{t+2} + \gamma^2 r_{t+3} + \dots = \sum_{k=0}^{\infty}\gamma^k r_{t+k+1} \]

Discounting has three reasons. Mathematical: if rewards are bounded by r_{max} and \gamma \lt  1, the geometric series converges, |G_t| \le \frac{r_{max}}{1 - \gamma}. Practical: a distant reward is more uncertain. Interpretive: \frac{1}{1 - \gamma} is the horizon over which the agent “looks”: with \gamma = 0.99 about 100 steps. The recursive relation, the basis of the whole course, holds:

    \[ G_t = r_{t+1} + \gamma\,G_{t+1} \]

Policy and value functions

A policy \pi(a \mid s) is the rule by which the agent chooses: the probability of taking a in state s (deterministic if it always gives the same action). To evaluate it one defines:

    \[ V^\pi(s) = \mathbb E_\pi[G_t \mid s_t = s],\qquad Q^\pi(s, a) = \mathbb E_\pi[G_t \mid s_t = s,\ a_t = a] \]

V^\pi(s), the value of the state, is the average return starting from s and following \pi; Q^\pi(s, a), the action value, is the average return taking a first and then following \pi. They are linked by V^\pi(s) = \sum_a\pi(a \mid s)\,Q^\pi(s, a). The difference A^\pi(s, a) = Q^\pi(s, a) - V^\pi(s) is called the advantage: how much better than average action a is. All modern algorithms use it.

A policy \pi^* is optimal if V^{\pi^*}(s) \ge V^\pi(s) in every state, for every other \pi. In a finite MDP a deterministic optimal policy always exists (this is proved in the lesson on Bellman equations), and knowing Q^* it is immediate: \pi^*(s) = \arg\max_a Q^*(s, a).

A complete example: the gridworld

A 4×4 grid; the top-left and bottom-right cells are the exits. The agent moves one cell up, down, left or right (if it hits the border it stays put), and each step costs -1: maximising the return means leaving as soon as possible. With \gamma = 1:

  • for the optimal policy the value of each cell is minus the number of steps to the nearest exit: 0, −1, −2, −3;
  • for the random policy (each direction with probability 1/4) the values are much more negative: −14 near the exits, −22 in the far corners, because the random walk wanders for a long time before leaving.
State values of the 4 by 4 gridworld
On the left the random policy, with values from minus 14 to minus 22; on the right the optimal policy, with values from 0 to minus 3 and the arrows of the best direction. The dark cells at top left and bottom right are the exits.

The fact that the random policy is worth −22 in a corner, and the optimal one −3, shows the central point of RL: from the same states and the same actions, the chosen behaviour changes the result radically. The next lesson shows how to compute these values, and from them the optimal policy, with the Bellman equations; the following lessons how to learn them when P and R are unknown and the state is too large for a table.

Where RL sits among control techniques

Those who have studied Control Systems will recognise the structure: the state, the input (action), the dynamics (P), a cost index to optimise (-G). Optimal control (LQR, MPC) solves the same problem knowing the model; RL solves it learning from experience, even when there is no model or it is too complicated. The price is the number of trials: this is why RL is almost always done in simulation, and the speed of simulators (lesson on libraries) is decisive.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
1
0
Would love your thoughts, please comment.x
()
x