Policy is the strategy used by a reinforcement agent to decide which action to make in the current state. A policy takes state as input and returns the action to be performed. We know that the goal of reinforcement agent is to maximise the cumulative reward it receives and to achieve this goal optimal policy that maximises the cumulative reward has to be found. Policy iteration is a two-part algorithm that helps identify the optimal policy, which yields the highest cumulative reward. It comprises policy evaluation and policy improvement steps, using which we'll evaluate each policy and modify them until optimal policy is found.

**Policy Evaluation**

Policy evaluation is the initial step in policy iteration. Here, we calculate the state-value function, also known as the V-function, using Bellman's equation for all states in the state space. The state-value function estimates the expected return of being in a particular state, helping us evaluate how effective it is to follow the given policy at each state.

The algorithm starts with a random policy, *Ï€*, and initializes the state-value function, *V*(*s*), to zero for all states. It then assigns the current value in *V*(*s*) to *v*oldâ€‹ and updates *V*(*s*) as per Bellman's equation. The difference between the updated *V*(*s*) and vold*v*oldâ€‹ is computed. If the difference is significantly small, it indicates the values have converged; otherwise, the steps are repeated until they converge. As a result of this step, we'll know the expected return for the policy *Ï€* in each state.

**1. Initialize:**

Â Â - Set V(s) = 0 for all statesÂ s.

Â Â - Set a small threshold Îµ (for determining convergence).

Â Â - Initialize Î´ to a large number (to enter the loop).

**2. Loop Until Convergence:**

- Set Î´ to 0 at the start of each iteration (to capture the maximum change).

Â Â - For each state s in the state space S:

Â Â Â Â Â Â Â Â Â Â - Save the current value of V(s) to vold.

Â Â Â Â Â Â Â Â Â Â - Update V(s) using the Bellman equation:

Â Â Â Â - Calculate the absolute change |vold - V(s)|.

Â Â Â Â - Update Î´:

Â Â - Check if Î´ Â < Â Îµ. If Î´ Â < Â Îµ, break from the loop; otherwise, continue.

**3. Conclusion:**

Â Â - Once Î´ Â < Â Îµ, the value functionÂ V(s) for the given policy Ï€ has converged to a stable set of values.

This algorithm ensures that all state values have sufficiently stabilized according to the policy Ï€ before moving on, making it a robust method for policy evaluation in reinforcement learning.

**Policy Improvement**

Policy improvement follows policy evaluation. In this step, we calculate the action-value function, also known as the Q-function, for each action in a state under the policy using the state value function from policy evaluation. The action-value function estimates the expected return of performing an action in a state under the policy. We repeat the calculation of the action function for all actions in all states to identify the best action-state pair that produces the maximum return and modify the current policy, *Ï€k*â€‹, to choose this action-state pair. The improved policy, *Ï€k*+1â€‹, will then be tested again using the policy evaluation step. We will repeat this process until the policy converges, i.e., further improvements do not yield significant changes in the policy.

**1. Initialize:**

Â - Assume that you start with some policy Ï€ that is an arbitrary mapping from states to actions.

Â - Initialize policy_is_stable to True to check for convergence.

**2. Loop Over All States:**

Â - For each state s in the state space S:

- Save the current action prescribed by the policy for state s, aold= Ï€(s) (where Â Ï€(s) is the prescribed action by policy when state s is passed).

Â Â Â Â Â Â Â Â Â Â - Find the best action by using the action value function with argmax:

Â - Check if the action has changed:

- If aold != Ï€(s) set policy_is_stable to False.

**3. Convergence Check:**

Â - If policy_is_stable is True, then the policy has converged, and you can end the improvement loop.

Â - If policy_is_stable is False, repeat the loop with the improved policy, updating the value function V(s) as necessary before the next iteration of policy improvement.

Â

In policy iteration, we repeat policy evaluation and policy improvement until we find the optimal value function and policy. Each iteration will produce a new and improved policy that is better than the previous iteration.

## Comentarios