-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrozenlake_env.py
More file actions
478 lines (415 loc) · 18.1 KB
/
Copy pathfrozenlake_env.py
File metadata and controls
478 lines (415 loc) · 18.1 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from typing import Tuple, Dict, Any
class FrozenLakeEnv:
"""
A flexible FrozenLake environment following the Gymnasium philosophy.
S: Start
F: Frozen lake (safe)
H: Hole (terminal)
G: Goal (terminal)
"""
def __init__(self, nrow=5, ncol=5, holes=None, goal=None, start_state=(0, 0)):
"""
Initialize FrozenLake environment with flexible dimensions and layout.
Args:
nrow (int): Number of rows
ncol (int): Number of columns
holes (list): List of (row, col) tuples for hole positions
goal (tuple): (row, col) tuple for goal position
start_state (tuple): (row, col) tuple for start position
"""
self.nrow = nrow
self.ncol = ncol
self.num_states = self.nrow * self.ncol
self.num_actions = 4 # LEFT, DOWN, RIGHT, UP
# Initialize grid with all frozen cells
self.desc = np.array([['F' for _ in range(ncol)] for _ in range(nrow)])
# Set start position
self.start_state = start_state
start_row, start_col = start_state
if 0 <= start_row < nrow and 0 <= start_col < ncol:
self.desc[start_row, start_col] = 'S'
else:
raise ValueError(f"Start state {start_state} is out of bounds for grid {nrow}x{ncol}")
# Set holes
if holes is None:
holes = []
self.holes = holes
for hole_row, hole_col in holes:
if 0 <= hole_row < nrow and 0 <= hole_col < ncol:
if (hole_row, hole_col) != start_state:
self.desc[hole_row, hole_col] = 'H'
else:
raise ValueError(f"Hole position {(hole_row, hole_col)} conflicts with start position")
else:
raise ValueError(f"Hole position {(hole_row, hole_col)} is out of bounds for grid {nrow}x{ncol}")
# Set goal
if goal is None:
goal = (nrow - 1, ncol - 1) # Default to bottom-right
self.goal = goal
goal_row, goal_col = goal
if 0 <= goal_row < nrow and 0 <= goal_col < ncol:
if (goal_row, goal_col) != start_state and (goal_row, goal_col) not in holes:
self.desc[goal_row, goal_col] = 'G'
else:
raise ValueError(f"Goal position {goal} conflicts with start or hole positions")
else:
raise ValueError(f"Goal position {goal} is out of bounds for grid {nrow}x{ncol}")
# Set terminal states (holes and goal)
self.terminal_states = holes + [goal]
self.state = self.start_state
self.last_action = None
self.action_space = [0, 1, 2, 3] # LEFT, DOWN, RIGHT, UP
self.observation_space = (self.nrow, self.ncol)
def reset(self, seed=None, options=None) -> Tuple[Tuple[int, int], Dict[str, Any]]:
"""Reset the environment to initial state following Gymnasium API"""
if seed is not None:
self.seed(seed)
self.state = self.start_state
self.last_action = None
info = {}
return self.state, info
def step(self, action: int) -> Tuple[Tuple[int, int], float, bool, bool, Dict[str, Any]]:
assert action in self.action_space, "Invalid action."
row, col = self.state
self.last_action = action
# Define action effects
if action == 0: # LEFT
col = max(col - 1, 0)
elif action == 1: # DOWN
row = min(row + 1, self.nrow - 1)
elif action == 2: # RIGHT
col = min(col + 1, self.ncol - 1)
elif action == 3: # UP
row = max(row - 1, 0)
next_state = (row, col)
self.state = next_state
terminated = next_state in self.terminal_states
truncated = False # No time limit in this environment
reward = 0.0
if next_state == self.goal: # Goal
reward = 1.0
info = {}
return next_state, reward, terminated, truncated, info
def render(self, mode='matplotlib'):
if mode == 'text':
# Text-based rendering (original)
desc = self.desc.copy()
row, col = self.state
desc[row, col] = 'A' # Agent
print("\n".join(["".join(row) for row in desc]))
elif mode == 'matplotlib':
# Matplotlib-based rendering
self._render_matplotlib()
def _render_matplotlib(self, fig=None, ax=None):
"""Render the environment using matplotlib"""
if fig is None or ax is None:
fig, ax = plt.subplots(figsize=(8, 8))
new_figure = True
else:
new_figure = False
ax.clear()
# Create grid
for i in range(self.nrow + 1):
ax.axhline(y=i, color='black', linewidth=2)
for j in range(self.ncol + 1):
ax.axvline(x=j, color='black', linewidth=2)
# Color the cells based on their type
for i in range(self.nrow):
for j in range(self.ncol):
cell_type = self.desc[i, j]
if cell_type == 'S': # Start
color = 'lightgreen'
text = 'START'
elif cell_type == 'F': # Frozen (safe)
color = 'lightblue'
text = ''
elif cell_type == 'H': # Hole
color = 'red'
text = 'HOLE'
elif cell_type == 'G': # Goal
color = 'gold'
text = 'GOAL'
else:
color = 'white'
text = ''
# Draw cell background
rect = patches.Rectangle((j, self.nrow - i - 1), 1, 1,
linewidth=1, edgecolor='black',
facecolor=color, alpha=0.7)
ax.add_patch(rect)
# Add text label
if text:
ax.text(j + 0.5, self.nrow - i - 0.5, text,
ha='center', va='center', fontsize=10, fontweight='bold')
# Draw agent
agent_row, agent_col = self.state
agent_circle = patches.Circle((agent_col + 0.5, self.nrow - agent_row - 0.5),
0.3, color='green', alpha=0.8)
ax.add_patch(agent_circle)
ax.text(agent_col + 0.5, self.nrow - agent_row - 0.5, 'AGENT',
ha='center', va='center', fontsize=8, fontweight='bold', color='white')
# Set up the plot
ax.set_xlim(0, self.ncol)
ax.set_ylim(0, self.nrow)
ax.set_aspect('equal')
ax.set_title(f'FrozenLake {self.nrow}x{self.ncol} - Agent at {self.state}', fontsize=14, fontweight='bold')
ax.set_xticks(range(self.ncol + 1))
ax.set_yticks(range(self.nrow + 1))
ax.set_xticklabels([])
ax.set_yticklabels([])
if new_figure:
plt.tight_layout()
plt.show(block=False)
plt.pause(0.1)
return fig, ax
def render_game_state(self, ax, state, step_count, current_action, action_mapping):
"""Render the current game state with agent and action arrow"""
# Create grid
for i in range(self.nrow + 1):
ax.axhline(y=i, color='black', linewidth=2)
for j in range(self.ncol + 1):
ax.axvline(x=j, color='black', linewidth=2)
# Color the cells
for i in range(self.nrow):
for j in range(self.ncol):
cell_type = self.desc[i, j]
if cell_type == 'S':
color = 'lightgreen'
text = 'START'
text_color = 'darkgreen'
elif cell_type == 'F':
color = 'lightblue'
text = ''
text_color = 'black'
elif cell_type == 'H':
color = 'red'
text = 'HOLE'
text_color = 'white'
elif cell_type == 'G':
color = 'gold'
text = 'GOAL'
text_color = 'darkred'
else:
color = 'white'
text = ''
text_color = 'black'
# Draw cell
rect = patches.Rectangle((j, self.nrow - i - 1), 1, 1,
facecolor=color, edgecolor='black', linewidth=1, alpha=0.8)
ax.add_patch(rect)
# Add text
if text:
ax.text(j + 0.5, self.nrow - i - 0.5, text,
ha='center', va='center', fontsize=10, fontweight='bold', color=text_color)
# Draw agent with action arrow
agent_row, agent_col = state
# Agent circle
agent_circle = patches.Circle((agent_col + 0.5, self.nrow - agent_row - 0.5),
0.25, color='green', alpha=0.9, zorder=10)
ax.add_patch(agent_circle)
# Action arrow
if step_count > 0: # Don't show arrow on first step
arrow_props = dict(arrowstyle='->', color='red', lw=3, alpha=0.8)
if current_action == 0: # LEFT
ax.annotate('', xy=(agent_col + 0.2, self.nrow - agent_row - 0.5),
xytext=(agent_col + 0.8, self.nrow - agent_row - 0.5), arrowprops=arrow_props)
elif current_action == 1: # DOWN
ax.annotate('', xy=(agent_col + 0.5, self.nrow - agent_row - 0.8),
xytext=(agent_col + 0.5, self.nrow - agent_row - 0.2), arrowprops=arrow_props)
elif current_action == 2: # RIGHT
ax.annotate('', xy=(agent_col + 0.8, self.nrow - agent_row - 0.5),
xytext=(agent_col + 0.2, self.nrow - agent_row - 0.5), arrowprops=arrow_props)
elif current_action == 3: # UP
ax.annotate('', xy=(agent_col + 0.5, self.nrow - agent_row - 0.2),
xytext=(agent_col + 0.5, self.nrow - agent_row - 0.8), arrowprops=arrow_props)
# Setup
ax.set_xlim(0, self.ncol)
ax.set_ylim(0, self.nrow)
ax.set_aspect('equal')
ax.set_title(f'Current State: {state}\nNext Action: {action_mapping[current_action]}',
fontsize=12, fontweight='bold')
ax.set_xticks([])
ax.set_yticks([])
def seed(self, seed=None):
np.random.seed(seed)
def close(self):
"""Close the environment (for Gymnasium compatibility)"""
plt.close('all') # Close all matplotlib figures
def get_user_input_for_environment():
"""
Get user input for creating a custom FrozenLake environment.
Returns:
tuple: (nrow, ncol, holes, goal, start_state)
"""
print("\n🏒 Welcome to FrozenLake Environment Creator! 🏒")
print("=" * 50)
# Get grid dimensions
while True:
try:
nrow = int(input("Enter number of rows (minimum 2): "))
if nrow >= 2:
break
else:
print("❌ Number of rows must be at least 2.")
except ValueError:
print("❌ Please enter a valid integer.")
while True:
try:
ncol = int(input("Enter number of columns (minimum 2): "))
if ncol >= 2:
break
else:
print("❌ Number of columns must be at least 2.")
except ValueError:
print("❌ Please enter a valid integer.")
print(f"\n📏 Grid size: {nrow} x {ncol}")
print(f"📍 Valid positions: row (0-{nrow-1}), column (0-{ncol-1})")
# Get start position
print("\n🏁 Start Position:")
while True:
try:
start_input = input(f"Enter start position as 'row,col' (default: 0,0): ").strip()
if not start_input:
start_state = (0, 0)
else:
start_row, start_col = map(int, start_input.split(','))
start_state = (start_row, start_col)
if 0 <= start_state[0] < nrow and 0 <= start_state[1] < ncol:
break
else:
print(f"❌ Start position must be within grid bounds (0-{nrow-1}, 0-{ncol-1}).")
except ValueError:
print("❌ Please enter position as 'row,col' (e.g., '0,0').")
# Get goal position
print("\n🎯 Goal Position:")
while True:
try:
goal_input = input(f"Enter goal position as 'row,col' (default: {nrow-1},{ncol-1}): ").strip()
if not goal_input:
goal = (nrow-1, ncol-1)
else:
goal_row, goal_col = map(int, goal_input.split(','))
goal = (goal_row, goal_col)
if 0 <= goal[0] < nrow and 0 <= goal[1] < ncol:
if goal != start_state:
break
else:
print("❌ Goal position cannot be the same as start position.")
else:
print(f"❌ Goal position must be within grid bounds (0-{nrow-1}, 0-{ncol-1}).")
except ValueError:
print("❌ Please enter position as 'row,col' (e.g., '4,4').")
# Get holes
print("\n🕳️ Hole Positions:")
print("Enter hole positions one by one. Press Enter without input to finish.")
holes = []
hole_count = 1
while True:
try:
hole_input = input(f"Enter hole #{hole_count} position as 'row,col' (or press Enter to finish): ").strip()
if not hole_input:
break
hole_row, hole_col = map(int, hole_input.split(','))
hole_pos = (hole_row, hole_col)
# Validate hole position
if not (0 <= hole_pos[0] < nrow and 0 <= hole_pos[1] < ncol):
print(f"❌ Hole position must be within grid bounds (0-{nrow-1}, 0-{ncol-1}).")
continue
if hole_pos == start_state:
print("❌ Hole position cannot be the same as start position.")
continue
if hole_pos == goal:
print("❌ Hole position cannot be the same as goal position.")
continue
if hole_pos in holes:
print("❌ This hole position already exists.")
continue
holes.append(hole_pos)
print(f"✅ Added hole at {hole_pos}")
hole_count += 1
except ValueError:
print("❌ Please enter position as 'row,col' (e.g., '2,3').")
# Summary
print("\n📋 Environment Summary:")
print(f" Grid Size: {nrow} x {ncol}")
print(f" Start: {start_state}")
print(f" Goal: {goal}")
print(f" Holes: {holes if holes else 'None'}")
return nrow, ncol, holes, goal, start_state
def make_frozen_lake(nrow=None, ncol=None, holes=None, goal=None, start_state=None, interactive=False):
"""
Factory function following Gymnasium philosophy with flexible parameters.
Args:
nrow (int): Number of rows (default: 5)
ncol (int): Number of columns (default: 5)
holes (list): List of (row, col) tuples for hole positions
goal (tuple): (row, col) tuple for goal position
start_state (tuple): (row, col) tuple for start position (default: (0, 0))
interactive (bool): If True, get parameters from user input
Usage:
env = make_frozen_lake() # Default 5x5 with goal at (4,4)
env = make_frozen_lake(nrow=3, ncol=4, holes=[(1,1), (2,2)], goal=(2,3))
env = make_frozen_lake(interactive=True) # User input
"""
if interactive:
nrow, ncol, holes, goal, start_state = get_user_input_for_environment()
else:
# Use defaults if not specified
if nrow is None:
nrow = 5
if ncol is None:
ncol = 5
if start_state is None:
start_state = (0, 0)
if goal is None:
goal = (nrow - 1, ncol - 1)
if holes is None:
holes = []
return FrozenLakeEnv(nrow=nrow, ncol=ncol, holes=holes, goal=goal, start_state=start_state)
# Example usage following Gymnasium philosophy
if __name__ == "__main__":
print("🎮 FrozenLake Environment Demo")
print("Choose an option:")
print("1. Default 5x5 environment")
print("2. Custom 3x4 environment with holes")
print("3. Interactive environment creator")
choice = input("Enter your choice (1-3): ").strip()
if choice == "1":
# Default environment
env = make_frozen_lake()
print("\n🏒 Created default 5x5 FrozenLake")
elif choice == "2":
# Custom environment example
env = make_frozen_lake(nrow=3, ncol=4, holes=[(1, 1), (1, 2)], goal=(2, 3), start_state=(0, 0))
print("\n🏒 Created custom 3x4 FrozenLake with holes at (1,1) and (1,2)")
elif choice == "3":
# Interactive environment
env = make_frozen_lake(interactive=True)
print("\n🏒 Created interactive FrozenLake")
else:
print("Invalid choice, using default environment")
env = make_frozen_lake()
# Demo the environment
print("\n🎲 Running random episode...")
state, info = env.reset()
env.render()
terminated = False
truncated = False
step_count = 0
while not (terminated or truncated) and step_count < 50:
action = np.random.choice(env.action_space)
state, reward, terminated, truncated, info = env.step(action)
step_count += 1
print(f"Step {step_count}: Action {action}, State {state}, Reward {reward}")
if terminated:
if reward > 0:
print("🎉 Reached the goal!")
else:
print("💀 Fell into a hole!")
elif step_count >= 50:
print("⏰ Maximum steps reached")
env.render()
env.close()