Tabular Q-Learning (Single Update)
Tabular Q-Learning (Single Update)
Implement a single tabular Q-learning update given a transition sample.
Mathematical Definition
Q-Learning Update:
Q(s,a)←Q(s,a)+α[r+γa′maxQ(snext,a′)−Q(s,a)]where α is the learning rate and γ is the discount factor.
Function Arguments
Q: 2D array, shape (n_states, n_actions)- Q-tables: int- current state indexa: int- action takenr: float- reward receiveds_next: int- next state indexalpha: float- learning rategamma: float- discount factor
Examples
Input: Q=[[0,0],[0.5,1]], s=0, a=1, r=1, s_next=1, α=0.5, γ=0.9
Output: Q_new = [[0, 0.95], [0.5, 1]]
Input: Q=[[0,0],[0,0]], s=1, a=0, r=2, s_next=1, α=1.0, γ=0
Output: Q_new = [[0, 0], [2, 0]]
Hint 1
Compute the TD target: target=r+γmaxa′Q(snext,a′)
Hint 2
Use Q.copy() to create Q_new, then update Q_new[s, a] using the TD target.
Requirements
- Do not modify Q in-place, work on a copy
- Return updated Q_new
Constraints
- Q.shape[0] ≤ 10,000, Q.shape[1] ≤ 1,000
- NumPy only
- Time limit: 200ms
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Tabular Q-Learning (Single Update)
Tabular Q-Learning (Single Update)
Implement a single tabular Q-learning update given a transition sample.
Mathematical Definition
Q-Learning Update:
Q(s,a)←Q(s,a)+α[r+γa′maxQ(snext,a′)−Q(s,a)]where α is the learning rate and γ is the discount factor.
Function Arguments
Q: 2D array, shape (n_states, n_actions)- Q-tables: int- current state indexa: int- action takenr: float- reward receiveds_next: int- next state indexalpha: float- learning rategamma: float- discount factor
Examples
Input: Q=[[0,0],[0.5,1]], s=0, a=1, r=1, s_next=1, α=0.5, γ=0.9
Output: Q_new = [[0, 0.95], [0.5, 1]]
Input: Q=[[0,0],[0,0]], s=1, a=0, r=2, s_next=1, α=1.0, γ=0
Output: Q_new = [[0, 0], [2, 0]]
Hint 1
Compute the TD target: target=r+γmaxa′Q(snext,a′)
Hint 2
Use Q.copy() to create Q_new, then update Q_new[s, a] using the TD target.
Requirements
- Do not modify Q in-place, work on a copy
- Return updated Q_new
Constraints
- Q.shape[0] ≤ 10,000, Q.shape[1] ≤ 1,000
- NumPy only
- Time limit: 200ms
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number