Monte Carlo Policy Evaluation
Monte Carlo Policy Evaluation
Estimate the value function using Monte Carlo first-visit returns from episode data.
Mathematical Definition
Return from time t:
Gt=rt+γrt+1+γ2rt+2+⋯First-Visit MC Value:
V(s)=average of all first-visit returns for state sFunction Arguments
episodes: list of episodes- each episode is list of (state, reward) tuplesgamma: float- discount factorn_states: int- number of states (labeled 0..n_states-1)
Examples
Input: episodes=[[(0,1),(1,2),(2,3)]], γ=1, n_states=3
Output: V = [6.0, 5.0, 3.0]
Input: episodes=[[(0,1),(0,-5),(0,2)]], γ=1, n_states=1
Output: V = [-2.0] (only first visit counts)
Hint 1
Process each episode backward to compute returns: Gt=rt+γGt+1.
Hint 2
Use a set to track visited states per episode to ensure first-visit only.
Requirements
- Use first-visit: only count first occurrence of each state per episode
- Maintain running sum and count of returns per state
- Return V as NumPy array of shape (n_states,)
- States never visited should have value 0.0
Constraints
- Number of episodes ≤ 1,000
- Episode length ≤ 500
- n_states ≤ 10,000
- NumPy only
- Time limit: 300ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Monte Carlo Policy Evaluation
Monte Carlo Policy Evaluation
Estimate the value function using Monte Carlo first-visit returns from episode data.
Mathematical Definition
Return from time t:
Gt=rt+γrt+1+γ2rt+2+⋯First-Visit MC Value:
V(s)=average of all first-visit returns for state sFunction Arguments
episodes: list of episodes- each episode is list of (state, reward) tuplesgamma: float- discount factorn_states: int- number of states (labeled 0..n_states-1)
Examples
Input: episodes=[[(0,1),(1,2),(2,3)]], γ=1, n_states=3
Output: V = [6.0, 5.0, 3.0]
Input: episodes=[[(0,1),(0,-5),(0,2)]], γ=1, n_states=1
Output: V = [-2.0] (only first visit counts)
Hint 1
Process each episode backward to compute returns: Gt=rt+γGt+1.
Hint 2
Use a set to track visited states per episode to ensure first-visit only.
Requirements
- Use first-visit: only count first occurrence of each state per episode
- Maintain running sum and count of returns per state
- Return V as NumPy array of shape (n_states,)
- States never visited should have value 0.0
Constraints
- Number of episodes ≤ 1,000
- Episode length ≤ 500
- n_states ≤ 10,000
- NumPy only
- Time limit: 300ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number