Greedy Policy from Action Values
Problem Statement
Policy improvement makes the policy greedy with respect to the current action values. Given a matrix Q where Q[s] is the list of action values at state s, return the greedy deterministic policy: a list where entry s is the best action index at that state (lowest index on ties).
Implement greedy_policy(Q).
Example:
greedy_policy([[1.0, 2.0], [3.0, 0.0]])
[1, 0]
- Process the first state with action values [1.0,2.0]: compare the values to find the maximum, where 2.0>1.0, so the greedy policy selects action index 1.
- Process the second state with action values [3.0,0.0]: compare the values to find the maximum, where 3.0>0.0, so the greedy policy selects action index 0.
- The final output is [1, 0]
Constraints:
1 <= num states <= 1000, eachQ[s]non-empty.- Break ties toward the lowest action index.
1. Background Knowledge
In Reinforcement Learning, an agent interacts with an environment by selecting actions based on a policy π. A policy maps states to actions. When we have estimates of the expected return for taking each action in a given state, we can improve the policy by making it greedy: at every state, choose the action with the highest estimated value. This process is known as policy improvement and is a core component of algorithms like Policy Iteration and Q-Learning.
The input to this problem is a matrix Q, often called the action-value function or Q-function. Each row Q[s] corresponds to a state s and contains the estimated value of taking each possible action in that state. The goal is to derive a deterministic policy from these values. A deterministic policy selects exactly one action per state, as opposed to a stochastic policy which might distribute probability mass across multiple actions.
A critical detail in this problem is the tie-breaking rule: if multiple actions share the same maximum value in a given state, the policy must select the action with the lowest index. This ensures the resulting policy is well-defined and reproducible, which is essential for consistent evaluation and further learning steps.
2. Algorithm Approach
The problem reduces to a straightforward argmax operation applied independently to each row of the matrix Q. For each state s, we need to find the index a that maximizes Q[s][a]. If there are ties, we select the smallest such index a.
This is a classic pattern in optimization and machine learning: given a vector of scores, return the index of the best score. The key insight is that this operation is state-local; the decision at state s depends only on the values in Q[s] and not on values in other rows. Therefore, we can process each row independently in a single pass.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.