Implement the complete Policy Iteration algorithm for solving finite Markov Decision Processes (MDPs).
Policy iteration alternates between two phases until convergence:
The algorithm terminates when the policy no longer changes (policy is stable).
Your function receives:
num_states: number of states (states are numbered 0 to num_states-1)num_actions: number of actions (actions are numbered 0 to num_actions-1)transitions: a nested structure where transitions[s][a] is a list of tuples (probability, next_state, reward) describing the dynamics when taking action a in state sgamma: the discount factor (0 <= gamma < 1)theta: a small threshold for determining convergence during policy evaluationThe initial policy should assign action 0 to every state. The initial value function should be all zeros.
During policy improvement, when multiple actions have the same maximum Q-value, choose the action with the smallest index.
Return a tuple (policy, values) where policy is a list of integers (the optimal action per state) and values is a list of floats (the value of each state under the optimal policy), each rounded to 4 decimal places.