Reinforcement Learning Basic Concepts

The main characters of RL are the agent and the environment. The environment is the world that the agent lives in and interacts with. At every step of interaction, the agent sees a (possibly partial) observation of the state of the world, and then decides on an action to take. The environment changes when the agent acts on it, but may also change on its own.

The agent also perceives a reward signal from the environment, a number that tells it how good or bad the current world state is. The goal of the agent is to maximize its cumulative reward, called return. Reinforcement learning methods are ways that the agent can learn behaviors to achieve its goal.

additional terminology:

  • states and observations,
  • action spaces,
  • policies,
  • trajectories,
  • different formulations of return,
  • the RL optimization problem,
  • and value functions.

States and Observations

A state is a complete description of the state of the world. There is no information about the world which is hidden from the state. An observation is a partial description of a state, which may omit information.

Action Spaces

The set of all valid actions in a given environment is often called the action space. Some environments, like Atari and Go, have discrete action spaces, where only a finite number of moves are available to the agent. Other environments, like where the agent controls a robot in a physical world, have continuous action spaces. In continuous spaces, actions are real-valued vectors.

Policies

A policy is the agent’s behavior. It is a map from state to action.

A policy is a rule used by an agent to decide what actions to take.

It can be deterministic, in which case it is usually denoted by $\mu$: $a_t = \mu(s_t)$ or it may be stochastic, in which case it is usually denoted by $\pi$: $a_t = \pi(\cdot|s_t)$ or $\pi(a|s) = \Bbb P[A_t=a|S_t=s]$

Deterministic Policies

pi_net = nn.Sequential(
              nn.Linear(obs_dim, 64),
              nn.Tanh(),
              nn.Linear(64, 64),
              nn.Tanh(),
              nn.Linear(64, act_dim)
            )

obs_tensor = torch.as_tensor(obs, dtype=torch.float32)
actions = pi_net(obs_tensor)

Stochastic Policies

The two most common kinds of stochastic policies in deep RL are categorical policies and diagonal Gaussian policies.

Categorical policies can be used in discrete action spaces, while diagonal Gaussian policies are used in continuous action spaces.

A categorical policy is like a classifier over discrete actions. one final linear layer that gives you logits for each action, followed by a softmax to convert the logits into probabilities.

Two key computations are centrally important for using and training stochastic policies:

  • sampling actions from the policy,
  • and computing log likelihoods of particular actions, $\log\pi_\theta(a|s)$.

Trajectories

A trajectory $\tau$ is a sequence of states and actions in the world,

Trajectories are also frequently called episodes or rollouts.

Rewards

  • A reward $R_t$ is a scalar feedback signal
  • Indicates how well agent is doing at step $t$
  • The agent’s job is to maximize cumulative reward

Reinforcement learning is based on the reward hypothesis

Definition (Reward Hypothesis): All goals can be described by the maximization of expected cumulative reward.

One kind of return is the finite-horizon undiscounted return.

Another kind of return is the infinite-horizon discounted return, which is the sum of all rewards ever obtained by the agent, but discounted by how far off in the future they’re obtained.

Why would we ever want a discount factor, though? Don’t we just want to get all rewards? We do, but the discount factor is both intuitively appealing and mathematically convenient. On an intuitive level: cash now is better than cash later. Mathematically: an infinite-horizon sum of rewards may not converge to a finite value, and is hard to deal with in equations. But with a discount factor and under reasonable conditions, the infinite sum converges.

we frequently set up algorithms to optimize the undiscounted return, but use discount factors in estimating value functions:

  1. We typically measure the “performance” of an RL algorithm by looking at the average episodic undiscounted sum of rewards, but we implement algorithms that use discount factors in value function learning, and increase probabilities of actions in proportion to advantage functions that use discount factors.
  2. From the above, you might get the impression that we’re measuring undiscounted return, but optimizing discounted return. The truth is, we aren’t exactly optimizing for either! If we were truly optimizing for discounted return, there would be a discount factor included in the objective function for each state, corresponding to what timestep in a trajectory that state occurred. But, in typical implementations, we ignore that discount factor and weight each state’s contribution equally.

The RL Problem

Whatever the choice of return measure (whether infinite-horizon discounted, or finite-horizon undiscounted), and whatever the choice of policy, the goal in RL is to select a policy which maximizes expected return when the agent acts according to it.

Let’s suppose that both the environment transitions and the policy are stochastic. In this case, the probability of a T-step trajectory is: \(P(\tau|\pi) = \rho_0(s_0)\prod_{t=0}^{T-1}P(s_{t+1}|s_t, a_t)\pi(a_t|s_t).\) The expected return (for whichever measure), denoted by $J(\pi)$, is then: \(J(\pi) = \int_\tau P(\tau|\pi)R(\tau) = E_{\tau \sim \pi}[R(\tau)]\) The central optimization problem in RL can then be expressed by \(\pi^* = \arg \max_\pi J(\pi),\) with $\pi^*$ being the optimal policy


An RL agent may include one or more of these components:

  • Policy: agent’s behavior function
  • Value function: how good is each state and/or action
  • Model: agent’s representation of the environment

Value Functions

  • Value function is a prediction of future reward

  • Used to evaluate the goodness/badness of states

  • And therefore to select between actions, e.g. \(V_\pi(s) = \Bbb E_\pi[R_{t+1} + \gamma R_{t+2} + \gamma^2R_{t+3} + ...|S_t=s]\)

There are four main functions of note here.

  1. The On-Policy Value Function, $V^\pi(s) = E_{\tau\sim\pi}[R(\tau)|s_0=s]$
  2. The On-Policy Action-Value Function, $Q^\pi(s, a) = E_{\tau\sim\pi}[R(\tau)|s_0=s, a_0=a]$
  3. The Optimal Value Function, $V^*(s) = \max_\pi E_{\tau\sim\pi}[R(\tau)|s_0=s]$
  4. The Optimal Action-Value Function, $Q^*(s, a) = \max_\pi E_{\tau\sim\pi}[R(\tau)|s_0=s, a_0=a]$

two key connections between the value function and the action-value function: \(V^\pi(s) = E_{a\sim\pi}[Q^\pi(s, a)], \\ V^*(s) = \max_a Q^*(s, a).\) The optimal action: \(a^*(s) = \arg \max_aQ^*(s, a).\)

Model

  • A model predicts what the environment will do next

  • $P$ predicts the next state

  • $R$ predicts the next (immediate) reward, e.g. \(P_{ss'}^a = \Bbb P[S_{t+1}=s'|S_t=s, A_t=a] \\ R_s^a = \Bbb E[R_{t+1}|S_t=s, A_t = a]\)


Bellman Equations

The basic idea behind the Bellman equations is this:

The value of your starting point is the reward you expect to get from being there, plus the value of wherever you land next.

The Bellman equations for the on-policy value functions are \(V^\pi(s) = \underset {s'\sim P} {\underset {a\sim\pi} E}[r(s, a)+\gamma V^\pi(s')], \\ Q^\pi(s, a) = \underset {s'\sim P} E[r(s, a) + \gamma \underset {a'\sim\pi}E[Q^\pi(s', a')]]\)

Advantage Functions

Sometimes in RL, we don’t need to describe how good an action is in an absolute sense, but only how much better it is than others on average. That is to say, we want to know the relative advantage of that action. We make this concept precise with the advantage function. \(A^\pi(s, a) = Q^\pi(s, a)-V^\pi(s).\)



Formalism

So far, we’ve discussed the agent’s environment in an informal way, but if you try to go digging through the literature, you’re likely to run into the standard mathematical formalism for this setting: Markov Decision Processes (MDPs). An MDP is a 5-tuple, $\langle S, A, R, P, \rho_0 \rangle$.

The name Markov Decision Process refers to the fact that the system obeys the Markov property: transitions only depend on the most recent state and action, and no prior history.

History and state

The history is the sequence of observations, actions, rewards \(H_t = O_1, R_1, A_1, ..., A_{t-1}, O_t, R_t\) State is the information used to determine what happens next.

state is a function of the history: \(S_t = f(H_t)\)

Information State

An information state (a.k.a Markov state) contains all useful information from the history.

A state $S_t$ is Markov if and only if \(\Bbb P[S_{t+1}|S_t] = \Bbb P[S_{t+1}|S_1, ..., S_t]\)

  • The future is independent of the past given the present \(H_{1:t} \rightarrow S_t \rightarrow H_{t+1:\infin}\)

  • Once the state is known, the history may be thrown away

  • i.e. The state is a sufficient statistic of the future

  • The environment state $S_t^e$ is Markov

  • The history $H_t$ is Markov


Markov decision processes formally describe an environment for reinforcement learning

Where the environment is fully observable

Full observability: agent directly observes environment state \(O_t = S_t^a = S_t^e\)

  • Agent state = environment state = information state
  • Formally, this is a Markov decision process (MDP)

Partial observability: agent indirectly observes environment.

  • Now agent state $\not=$ environment state
  • Formally this is a partially observable Markov decision process (POMDP)

Definition $G_t$​

The return $G_t$ is the total discounted reward from time-step $t$. \(G_t = R_{t+1} + \gamma R_{t+2} +\ ...=\underset {k=0} {\overset \infty \sum} \gamma^kR_{t+k+1}\)

  • The discount $\gamma \in [0, 1]$ is the present value of future rewards
  • The value of receiving reward R after k+1 time-steps is $\gamma^kR$
  • This values immediate reward above delayed reward.
  • $\gamma$ close to 0 leads to “myopic” evaluation
  • $\gamma$ close to 1 leads to “far-sighted” evaluation

The value function $v(s)$ gives the long-term value of state $s$

Definition state value function

The state value function v(s) of an MRP is the expected return starting from state $s$ \(v(s) = \Bbb E[G_t|S_t=s]\) Bellman Equation for MRPs

The value function can be decomposed into two parts:

  • immediate reward $R_{t+1}$
  • discounted value of successor state $\gamma v(S_{t+1})$
\[v(s) = \Bbb E[G_t|S_t=s] = \Bbb E[R_{t+1}+\gamma v(S_{t+1})|S_t=s]\]

Markov Decision Process

A Markov decision process (MDP) is a Markov reward process with decisions. It is an environment in which all states are Markov.

Definition

A Markov Decision Process is a tuple $<S, A, P, R, \gamma>$

  • $S$ is a finite set of states

  • A is a finite set of actions

  • P is a state transition probability matrix, $P_{ss’}^a=\Bbb P[S_{t+1}=s’|S_t=s, A_t=a]$

  • R is a reward function, $R_s^a = \Bbb E[R_{t+1}|S_t=s, A_t=a]$

  • $\gamma$ is a discount factor $\gamma \in [0, 1]$

Policies for MDP

Definition

A policy $\pi$ is a distribution over actions given states, \(\pi(a|s) = \Bbb P[A_t=a|S_t=s]\)

  • A policy fully defines the behavior of an agent
  • MDP policies depend on the current state (not the history)

Value Function for MDP

Definition

The state-value function $v_\pi(s)$ of an MDP is the expected return starting from state s, and then following policy $\pi$ \(v_\pi(s) = \Bbb E_\pi[G_t|S_t=s]\) The action-value function $q_\pi(s, a)$ is the expected return starting from state s, taking action a, and then following policy $\pi$ \(q_\pi(s, a)=\Bbb E_\pi[G_t|S_t=s, A_t=a]\)


Bellman Expectation Equation

The state-value function can again be decomposed into immediate reward plus discounted value of successor state, \(v_\pi(s) = \Bbb E_\pi[R_{t+1}+\gamma v_\pi(S_{t+1})|S_t=s]\) The action-value function can similarly be decomposed, \(q_\pi(s, a)=\Bbb E_\pi[R_{t+1}+\gamma q_\pi(S_{t+1}, A_{t+1})|S_t=s, A_t=a]\)


Formula derivation:

Expected discounted return: \(\begin{align} G_t & \dot= R_{t+1} + \gamma R_{t+2} + \gamma^2R_{t+3} + \gamma^3R_{t+4}+ \dots \\ & = R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + \gamma^2R_{t+4}+ \dots) \\ & = R_{t+1} + \gamma G_{t+1} \end{align}\) where $\gamma$ is a parameter, $0 \le \gamma \le 1$, called the discount rate.

Bellman equation for $v_\pi$ \(\begin{align} v_\pi(s) &\dot=\Bbb E_\pi[G_t|S_t=s]\\ &= \Bbb E_\pi[R_{t+1}+\gamma G_{t+1}|S_t=s]\\ &= \underset a \sum\pi(a|s)\underset {s'} \sum\underset r \sum p(s', r|s,a)[r+\gamma \Bbb E_\pi[G_{t+1}|S_{t+1}=s']]\\ &=\underset a \sum \pi(a|s) \underset {s',r} \sum p(s', r|s,a)[r+\gamma v_\pi(s')],\ for\ all\ s \in \cal S \end{align}\)


We denote all the optimal policies by $\pi_*$.

$\pi \ge \pi’$ if and only if $v_\pi(s) \ge v_{\pi’}(s)$ for all $s \in \cal S$

optimal state-value function

$v_*(s) \dot= \underset \pi \max v_\pi(s)$, for all $s \in \cal S$

optimal action-value function, denoted $q_*$:

$q_*(s, a) \dot=\underset \pi \max q_\pi(s, a)$, for all $s \in \cal S$ and $a \in \cal A(s)$.

$q_$ in terms of $v_$:

\[q_*(s, a) = \Bbb E[R_{t+1} + \gamma v_*(S_{t+1}) | S_t=s, A_t=a]\]

Bellman optimality equation for $v_*$

\[\begin{align} v_\pi(s) &= \underset {a \in \cal A(s)} \max q_{\pi_*}(s, a)\\ &= \underset a \max \Bbb E_{\pi_*}[G_t|S_t=s, A_t=a]\\ &= \underset a \max \Bbb E_\pi[R_{t+1}+\gamma G_{t+1}|S_t=s, A_t=a]\\ &= \underset a \max \Bbb E[R_{t+1} + \gamma v_*(S_{t+1}) | S_t=s, A_t=a] \\ &=\underset a \max\underset {s',r} \sum p(s', r|s,a)[r+\gamma v_\pi(s')] \end{align}\]

Bellman optimality equation for $q_*$

\[q_*(s, a) = \Bbb E[R_{t+1} + \gamma \underset {a'} \max q_*(S_{t+1}, a') | S_t=s, A_t=a]\\ = \underset {s', r} \sum p(s', r|s, a)[r+ \gamma \underset {a'} \max q_*(s', a')].\]

Reference:

Reinforcement Learning An Introduction




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • DQN
  • Policy Gradient Algorithms
  • Policy Gradient Method
  • Function Approximation
  • Model-Free Prediction