# Policy and Value Iteration Algorithms

This section on value-based methods is split into two parts. I will first lay out three classic algorithms: policy iteration, value iteration, fitted-Q iteration; and then shift to state-of-the-art deep Q learning. I think it's a main goal to not only understand each algorithm but also how these value-based methods relate to each other. It's always important to know exactly what you are doing before throwing a deep neural net at it :)

Before, in policy gradient algorithms, we directly learn and update a policy by evaluating it using a value or advantage function. Here in the world of value-based methods, we instead only learn a good value function for each (state, action) pair, so that eventually for each state, we can induce a good policy by just taking the $$\text{argmax}$$ action for the value function at each state we encounter.

### Tabular Case: policy iteration v.s. fitted value iteration

If we put together policy iteration and value iteration algorithms, the key difference is whether to update the policy immediately after we finish evaluating all the experienced states under this current policy.

**Policy Iteration**

For policy iteration algorithms, there are two alternating steps:

1. **Policy Evaluation**:  for the current policy $$\pi$$, each state $$s$$ under this policy is mapped to a value $$V(s)$$ defined recursively as ( $$r + \text{discount} * V(s')$$ ). Note that, these state values are specific to each policy $$\pi$$ and values of next states are taken as expectation because we might encounter some _stochastic_ state transition dynamics. \
   \
   &#xNAN;_&#x73;ide note_: The above equation is called _bootstrapped update_ because when evaluating the values for next states, we are still using the current estimate of V-function.\
   \
   &#x20;<img src="https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTEwIx-UITfMMS9w7I8%2Flec7_1.png?alt=media&#x26;token=7b2579ca-bc1f-4c4a-ac23-a529653d149a" alt="" data-size="original"> \

2. **Policy Update**:  Once all states under the current policy are given values, we simply switch to a new policy which takes the $$\text{argmax}$$ action for advantage function at every state. Note that fitting value function $$V$$ is equivalent to fitting advantage function $$A$$ by definition: <img src="https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTEwj9lC-8WMUwig5wv%2Flec7_2.png?alt=media&#x26;token=3164b763-da8f-4c15-8503-fec5b297d415" alt="" data-size="original"> \


#### Value Iteration

However, since the step two above uses a simple $$\text{argmax}$$ update rule, we can omit this action-reselecting step and fit a Q-function instead of V-function, hence the name **value iteration**:\
&#x20;&#x20;

![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTExD3K_aJy8auk5NMn%2Flec7_3.png?alt=media\&token=5b573d57-a0c5-4f3e-8aaa-2ae508f112ef)

Notice we can do this because the state value _V_ doesn't depend on action choices, thus the $$\text{argmax}$$of $$A$$ and $$Q$$ is the same. So if we have $$Q(s,a)$$ values, we can use it to get a policy that chooses the best action at every state.

#### Aside: should I use stochastic or deterministic policy?

I think this is an interesting question that came up during lecture, and the short answer is, for fully-observed MDPs, there always exists an optimal, deterministic policy (but at the same time there could also be a stochastic one that's equally optimal). However, for partially-observed MDPs such as games, stochastic can outperform _any_ deterministic.

### When the table is too big:  _fitted_  value iterations

A big assumption for the above policy and value iteration algorithms to work is that all the possible state-action pairs can be put in one reasonably-sized table like this:&#x20;

<div align="left"><img src="https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTExp1beAimmlUXlWQW%2Flec7_4.png?alt=media&#x26;token=af8353e7-c209-44cc-8736-28dfc1d02cc8" alt=""></div>

and we can use Dynamic Programming to solve for an optimal policy. _However_, most problems we are trying to solve in RL (e.g. driving a car or playing a challenging game) contain some much larger state and action spaces. Simple DP won't be efficient enough in this case, hence the use of _fitted_ value iterations, whose key distinction from tabular algorithms is the option to use a neural network to parametrize the value function _V_, and using gradient update to improve its evaluation.

Notice how, if we use this fitted value update above, we still need to iterate over all possible next states induced by all possible next actions, before giving an estimate of the current state value. This means that we need to visit states outside of the sample trajectory we have, and this process can be expensive; plus, sometimes we might not understand the transition dynamics at all. In order to avoid this, we notice Q-functions narrow down our estimate to be state-action pair specific, hence **fitted Q-value iteration**:\
\


![](https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTEy8wt3RR41PAm7wVN%2Flec7_5.png?alt=media\&token=73ce41e3-1a0d-49fd-af86-ffc025e389d5)

Unfortunately, it's nearly _impossible_ for fitted-Q to converge under large state/action spaces, but it's still important to understand how we get here so we can discuss why the below deep-Q algorithm works.

### Details on Fitted-Q

A few details to better grasp fitted Q algorithm:

* It's off-policy: the sample state-action transitions don't need to be generated under the same policy for the Q function to give it an estimate (thus the step-2 update)
* We can make it online/on-policy by always updating the policy before taking a new action and gather samples:\
  &#x20;<img src="https://1896916525-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSZWjXi87wJwJVBghav%2F-MTErveZBJEKaFmlIvq8%2F-MTEywn97Ms3N0300GmR%2Flec7_7.png?alt=media&#x26;token=ffdd7f8a-4600-4d55-94bb-cf5844f2ed16" alt="" data-size="original"> \

* In the online version above, we can use different strategies in step-1 to give the algorithm sufficient exploration in the often-huge state-action space. For now, two such strategies to know about are “**epsilon-greedy**” and “**Boltzmann exploration**".

### Theories on why fitted-Q doesn't work

Skipped for now, but generally:

#### works: Value function learning theory

#### not working: Non-tabular value function learning

#### moving target and correlated samples

###

All included screenshots credit to Lecture 7 [(slides)](http://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-7.pdf)



