-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonte_carlo.py
More file actions
244 lines (190 loc) · 7.9 KB
/
monte_carlo.py
File metadata and controls
244 lines (190 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from blackjack import ModifiedBlackjackEnv
from collections import defaultdict
class MonteCarloControl:
"""
Monte Carlo Control with epsilon-greedy exploration.
Uses time-varying step-size and exploration rate.
"""
def __init__(self, N0=100):
"""
Initialize Monte Carlo Control.
Args:
N0: Constant for epsilon-greedy exploration schedule
"""
self.N0 = N0
# Q-values: Q[state][action]
self.Q = defaultdict(lambda: np.zeros(2))
# Visit counts
self.N_s = defaultdict(int) # N(s) - state visits
self.N_sa = defaultdict(lambda: np.zeros(2)) # N(s,a) - state-action visits
self.env = ModifiedBlackjackEnv()
def get_epsilon(self, state):
"""Calculate epsilon for epsilon-greedy policy."""
return self.N0 / (self.N0 + self.N_s[state])
def get_action(self, state, epsilon):
"""Select action using epsilon-greedy policy."""
if np.random.random() < epsilon:
# Explore: random action
return np.random.randint(2)
else:
# Exploit: greedy action
return np.argmax(self.Q[state])
def generate_episode(self):
"""
Generate an episode using current policy.
Returns:
episode: List of (state, action, reward) tuples
"""
episode = []
state = self.env.reset()
done = False
while not done:
# Get epsilon for current state
epsilon = self.get_epsilon(state)
# Select action
action = self.get_action(state, epsilon)
# Take action
next_state, reward, done = self.env.step(action)
# Store transition
episode.append((state, action, reward))
if not done:
state = next_state
return episode
def update_Q_values(self, episode):
"""
Update Q-values using Monte Carlo update with first-visit.
Args:
episode: List of (state, action, reward) tuples
"""
# Calculate return (no discounting, gamma=1)
G = sum([reward for _, _, reward in episode])
# Track which state-action pairs we've already updated (first-visit)
visited = set()
for state, action, _ in episode:
sa_pair = (state, action)
# First-visit Monte Carlo
if sa_pair not in visited:
visited.add(sa_pair)
# Update visit counts
self.N_s[state] += 1
self.N_sa[state][action] += 1
# Calculate step-size
alpha = 1.0 / self.N_sa[state][action]
# Update Q-value
self.Q[state][action] += alpha * (G - self.Q[state][action])
def train(self, num_episodes=500000, print_every=50000):
"""
Train using Monte Carlo Control.
Args:
num_episodes: Number of episodes to train
print_every: Print progress every N episodes
"""
print(f"Training Monte Carlo Control for {num_episodes} episodes...")
print(f"N0 = {self.N0}")
print()
for episode_num in range(1, num_episodes + 1):
# Generate episode
episode = self.generate_episode()
# Update Q-values
self.update_Q_values(episode)
# Print progress
if episode_num % print_every == 0:
avg_return = np.mean([sum([r for _, _, r in self.generate_episode()])
for _ in range(1000)])
print(f"Episode {episode_num}: Avg return over 1000 episodes = {avg_return:.3f}")
print("\nTraining complete!")
def get_optimal_value_function(self):
"""
Extract optimal value function V*(s) = max_a Q*(s,a).
Returns:
V: Dictionary mapping states to optimal values
"""
V = {}
for state in self.Q:
V[state] = np.max(self.Q[state])
return V
def plot_value_function(self):
"""
Plot the optimal value function as a 3D surface.
Similar to Sutton & Barto Figure 5.2.
"""
# Get optimal value function
V = self.get_optimal_value_function()
# Create grid for plotting
dealer_cards = range(1, 14) # 1-13
player_sums = range(1, 22) # 1-21
# Create meshgrid
X, Y = np.meshgrid(dealer_cards, player_sums)
Z = np.zeros_like(X, dtype=float)
# Fill in values
for i, player_sum in enumerate(player_sums):
for j, dealer_card in enumerate(dealer_cards):
state = (dealer_card, player_sum)
if state in V:
Z[i, j] = V[state]
else:
Z[i, j] = 0 # Unvisited states
# Create 3D plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis',
edgecolor='none', alpha=0.8)
# Labels and title
ax.set_xlabel('Dealer Showing', fontsize=12)
ax.set_ylabel('Player Sum', fontsize=12)
ax.set_zlabel('Value', fontsize=12)
ax.set_title('Optimal Value Function V*(s)', fontsize=14, fontweight='bold')
# Set axis limits
ax.set_xlim(1, 13)
ax.set_ylim(1, 21)
# Add colorbar
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
# Adjust viewing angle
ax.view_init(elev=30, azim=45)
plt.tight_layout()
plt.savefig('optimal_value_function.png', dpi=300, bbox_inches='tight')
plt.show()
print("Plot saved as 'optimal_value_function.png'")
def get_optimal_policy(self):
"""
Extract optimal policy from Q-values.
Returns:
policy: Dictionary mapping states to optimal actions
"""
policy = {}
for state in self.Q:
policy[state] = np.argmax(self.Q[state])
return policy
def print_policy_sample(self):
"""Print a sample of the optimal policy."""
policy = self.get_optimal_policy()
print("\nSample of Optimal Policy (0=stick, 1=hit):")
print("=" * 50)
# Show policy for dealer card = 7 (middle value)
dealer_card = 7
print(f"\nWhen dealer shows {dealer_card}:")
for player_sum in range(12, 22):
state = (dealer_card, player_sum)
if state in policy:
action = policy[state]
action_name = "stick" if action == 0 else "hit"
print(f" Player sum {player_sum:2d}: {action_name}")
# Run Monte Carlo Control
if __name__ == "__main__":
# Create and train agent
mc = MonteCarloControl(N0=100)
mc.train(num_episodes=500000, print_every=50000)
# Print sample policy
mc.print_policy_sample()
# Plot value function
print("\nGenerating plot...")
mc.plot_value_function()
# Save Q-values for later use in Sarsa
import pickle
with open('optimal_Q_values.pkl', 'wb') as f:
pickle.dump(dict(mc.Q), f)
print("\nOptimal Q-values saved to 'optimal_Q_values.pkl'")