Skip to content

Commit 6cdfcfc

Browse files
Add detailed documentation for transition tables
Explains array shapes, indexing, and how to use the tables in the Q-iteration algorithm with concrete examples. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4726b45 commit 6cdfcfc

1 file changed

Lines changed: 53 additions & 7 deletions

File tree

homeworks/homework_1/problem_1/problem_1.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,63 @@
1717
WARNING: Do NOT modify or regenerate the transition tables (.npy files).
1818
The grading server uses the same tables - changing them will cause your submission to fail.
1919
20+
TRANSITION TABLES:
21+
=================
22+
23+
The state space is discretized into a 200x200 grid:
24+
- s0 (first index): Position index, ranging from 0 to 199
25+
- Index 0 corresponds to position -1.2 (leftmost)
26+
- Index 199 corresponds to position 0.6 (rightmost)
27+
- The goal region (position >= 0.5) is roughly indices 189-199
28+
- s1 (second index): Velocity index, ranging from 0 to 199
29+
- Index 0 corresponds to velocity -0.07 (moving left fastest)
30+
- Index 199 corresponds to velocity 0.07 (moving right fastest)
31+
- Index ~100 corresponds to velocity ~0 (stationary)
32+
33+
The transition tables are numpy arrays:
34+
2035
P (transition_next_states.npy): shape (200, 200, 3, 2)
21-
P[s0, s1, a] = [s0', s1']
22-
The next state indices when taking action a from state (s0, s1)
36+
P[s0, s1, a] = [s0', s1'] (a numpy array of 2 integers)
37+
38+
Given current state indices (s0, s1) and action a, returns the
39+
next state indices [s0', s1'].
40+
41+
Example:
42+
next_state = P[100, 100, 2] # State (100,100), action 2 (push right)
43+
s0_next, s1_next = next_state[0], next_state[1]
44+
# Now you can look up Q[s0_next, s1_next, :] to get Q-values at next state
2345
2446
R (transition_rewards.npy): shape (200, 200, 3)
25-
R[s0, s1, a] = reward
26-
The reward is -1 for each step until the goal is reached.
47+
R[s0, s1, a] = reward (a single float)
48+
49+
The immediate reward for taking action a in state (s0, s1).
50+
In MountainCar, this is always -1 for every step (encourages reaching
51+
the goal quickly).
52+
53+
Example:
54+
reward = R[100, 100, 2] # Always -1.0
2755
2856
D (transition_dones.npy): shape (200, 200, 3)
29-
D[s0, s1, a] = done (True/False)
30-
True when the car reaches the goal (position >= 0.5)
57+
D[s0, s1, a] = done (a boolean: True or False)
58+
59+
Whether the episode terminates after taking action a in state (s0, s1).
60+
True only when the car reaches the goal (position >= 0.5).
61+
62+
Example:
63+
done = D[195, 150, 2] # True if this transition reaches the goal
64+
# If done is True, there is no future reward (episode ends)
65+
66+
USING THE TABLES IN Q-ITERATION:
67+
===============================
68+
69+
For each state-action pair (s0, s1, a):
70+
1. Look up the next state: s0', s1' = P[s0, s1, a]
71+
2. Look up the reward: r = R[s0, s1, a]
72+
3. Look up if terminal: d = D[s0, s1, a]
73+
4. Apply Bellman update:
74+
Q_new[s0, s1, a] = r + gamma * (1 - d) * max_a' Q[s0', s1', a']
75+
76+
Note: (1 - d) ensures we don't add future value for terminal states
3177
3278
Your task:
3379
Implement Q-iteration using the Bellman optimality equation:
@@ -38,7 +84,7 @@
3884
3985
Submission:
4086
- checkpoint.pt: Your Q-table saved with torch.save (shape: 200x200x3)
41-
- policy.py is provided - do not modify it
87+
- policy.py: Implement the forward() method to select actions using your Q-table
4288
4389
Expected performance:
4490
A well-implemented solution should consistently reach the goal.

0 commit comments

Comments
 (0)