|
| 1 | +# Problem 1: Tabular Q-Iteration for MountainCar |
| 2 | + |
| 3 | +In this problem, you will implement Q-iteration (dynamic programming) for the MountainCar environment. The continuous observation space has been discretized into a 200x200 grid. |
| 4 | + |
| 5 | +## Environment |
| 6 | + |
| 7 | +**MountainCar** has a car stuck in a valley that must build momentum to reach the goal on the right. |
| 8 | + |
| 9 | +**Observation space (2 dimensions):** |
| 10 | +- Position: range [-1.2, 0.6], goal at position >= 0.5 |
| 11 | +- Velocity: range [-0.07, 0.07] |
| 12 | + |
| 13 | +**Actions:** |
| 14 | +- 0: Push left |
| 15 | +- 1: No push |
| 16 | +- 2: Push right |
| 17 | + |
| 18 | +**Reward:** -1 for each timestep (encourages reaching the goal quickly) |
| 19 | + |
| 20 | +## Transition Tables |
| 21 | + |
| 22 | +We provide precomputed transition tables that describe the discretized environment dynamics. |
| 23 | + |
| 24 | +> **WARNING:** Do NOT modify or regenerate the transition tables (`.npy` files). The grading server uses the same tables - changing them will cause your submission to fail. |
| 25 | +
|
| 26 | +### State Space Discretization |
| 27 | + |
| 28 | +The state space is discretized into a 200x200 grid: |
| 29 | + |
| 30 | +- **s0 (first index):** Position index, ranging from 0 to 199 |
| 31 | + - Index 0 = position -1.2 (leftmost) |
| 32 | + - Index 199 = position 0.6 (rightmost) |
| 33 | + - Goal region (position >= 0.5) is roughly indices 189-199 |
| 34 | + |
| 35 | +- **s1 (second index):** Velocity index, ranging from 0 to 199 |
| 36 | + - Index 0 = velocity -0.07 (moving left fastest) |
| 37 | + - Index 199 = velocity 0.07 (moving right fastest) |
| 38 | + - Index ~100 = velocity ~0 (stationary) |
| 39 | + |
| 40 | +### Table Formats |
| 41 | + |
| 42 | +**P (transition_next_states.npy):** shape `(200, 200, 3, 2)` |
| 43 | + |
| 44 | +```python |
| 45 | +P[s0, s1, a] = [s0', s1'] # numpy array of 2 integers |
| 46 | +``` |
| 47 | + |
| 48 | +Given current state indices (s0, s1) and action a, returns the next state indices. |
| 49 | + |
| 50 | +Example: |
| 51 | +```python |
| 52 | +next_state = P[100, 100, 2] # State (100,100), action 2 (push right) |
| 53 | +s0_next, s1_next = next_state[0], next_state[1] |
| 54 | +# Now you can look up Q[s0_next, s1_next, :] to get Q-values at next state |
| 55 | +``` |
| 56 | + |
| 57 | +**R (transition_rewards.npy):** shape `(200, 200, 3)` |
| 58 | + |
| 59 | +```python |
| 60 | +R[s0, s1, a] = reward # single float, always -1.0 |
| 61 | +``` |
| 62 | + |
| 63 | +The immediate reward for taking action a in state (s0, s1). |
| 64 | + |
| 65 | +**D (transition_dones.npy):** shape `(200, 200, 3)` |
| 66 | + |
| 67 | +```python |
| 68 | +D[s0, s1, a] = done # boolean: True or False |
| 69 | +``` |
| 70 | + |
| 71 | +Whether the episode terminates after taking action a. True only when the car reaches the goal. |
| 72 | + |
| 73 | +## Your Task |
| 74 | + |
| 75 | +### 1. Implement `q_iteration()` in `problem_1.py` |
| 76 | + |
| 77 | +Implement Q-iteration using the Bellman optimality equation: |
| 78 | + |
| 79 | +``` |
| 80 | +Q(s, a) = R(s, a) + gamma * (1 - D(s, a)) * max_a' Q(s', a') |
| 81 | +``` |
| 82 | + |
| 83 | +where s' = P(s, a) is the next state. |
| 84 | + |
| 85 | +**Algorithm:** |
| 86 | +1. Initialize Q-table to zeros: shape (200, 200, 3) |
| 87 | +2. Repeat until convergence or max_iterations: |
| 88 | + - For each state-action pair (s0, s1, a): |
| 89 | + - Look up next state: `s0', s1' = P[s0, s1, a]` |
| 90 | + - Look up reward: `r = R[s0, s1, a]` |
| 91 | + - Look up done: `d = D[s0, s1, a]` |
| 92 | + - Update: `Q_new[s0, s1, a] = r + gamma * (1 - d) * max_a' Q[s0', s1', a']` |
| 93 | + - If max|Q_new - Q| < theta: converged, stop |
| 94 | + - Q = Q_new |
| 95 | +3. Return the converged Q-table |
| 96 | + |
| 97 | +### 2. Implement `forward()` in `policy.py` |
| 98 | + |
| 99 | +Implement the action selection method: |
| 100 | +1. Discretize the observation using `self.discretize_state(obs)` |
| 101 | +2. Look up the Q-values for that state in `self.q_table` |
| 102 | +3. Return the action with the highest Q-value |
| 103 | + |
| 104 | +## Running Your Solution |
| 105 | + |
| 106 | +```bash |
| 107 | +python problem_1.py |
| 108 | +``` |
| 109 | + |
| 110 | +This will: |
| 111 | +1. Load the transition tables |
| 112 | +2. Run your Q-iteration implementation |
| 113 | +3. Save the Q-table to `checkpoint.pt` |
| 114 | +4. Evaluate your policy |
| 115 | + |
| 116 | +## Submission |
| 117 | + |
| 118 | +Submit two files to the leaderboard: |
| 119 | +- `checkpoint.pt` - Your Q-table saved with torch.save (shape: 200x200x3) |
| 120 | +- `policy.py` - With your implementation of the `forward()` method |
| 121 | + |
| 122 | +## Grading |
| 123 | + |
| 124 | +Your policy must achieve a **mean reward better than -150** to pass. |
| 125 | + |
| 126 | +A well-implemented solution should: |
| 127 | +- Consistently reach the goal (position >= 0.5) |
| 128 | +- Complete episodes in fewer than 150 steps on average |
| 129 | +- Achieve 100% success rate |
0 commit comments