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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Complete Reinforcement Learning Implementation from Scratch
|
| 4 |
+
Author: Claude + Stevan
|
| 5 |
+
No external RL libraries - only numpy and standard library
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pickle
|
| 10 |
+
import os
|
| 11 |
+
import time
|
| 12 |
+
import argparse
|
| 13 |
+
from collections import deque
|
| 14 |
+
from typing import Tuple, List, Dict, Optional, Union, Callable
|
| 15 |
+
import struct
|
| 16 |
+
import json
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# =============================================================================
|
| 20 |
+
# SECTION 1: CUSTOM ENVIRONMENTS (Lines 1-300)
|
| 21 |
+
# =============================================================================
|
| 22 |
+
|
| 23 |
+
class GridWorld:
|
| 24 |
+
"""
|
| 25 |
+
Custom GridWorld environment implemented from scratch.
|
| 26 |
+
Agent navigates grid to reach goal while avoiding obstacles.
|
| 27 |
+
|
| 28 |
+
FIXED: Now uses deterministic grid layout that persists across resets.
|
| 29 |
+
State representation includes noise for training stability.
|
| 30 |
+
Proper reward shaping: -1 per move, -10 pit/wall, +10 goal.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
EMPTY = 0
|
| 34 |
+
WALL = 1
|
| 35 |
+
GOAL = 2
|
| 36 |
+
PIT = 3
|
| 37 |
+
AGENT = 4
|
| 38 |
+
|
| 39 |
+
UP = 0
|
| 40 |
+
DOWN = 1
|
| 41 |
+
LEFT = 2
|
| 42 |
+
RIGHT = 3
|
| 43 |
+
|
| 44 |
+
def __init__(
|
| 45 |
+
self,
|
| 46 |
+
width: int = 4,
|
| 47 |
+
height: int = 4,
|
| 48 |
+
mode: str = 'static',
|
| 49 |
+
max_steps: int = 50,
|
| 50 |
+
seed: Optional[int] = None
|
| 51 |
+
):
|
| 52 |
+
self.width = width
|
| 53 |
+
self.height = height
|
| 54 |
+
self.mode = mode
|
| 55 |
+
self.max_steps = max_steps
|
| 56 |
+
|
| 57 |
+
self.n_states = width * height * 4
|
| 58 |
+
self.n_actions = 4
|
| 59 |
+
self.state_shape = (height, width, 4)
|
| 60 |
+
self.state_dim = self.n_states
|
| 61 |
+
|
| 62 |
+
self.action_names = ['UP', 'DOWN', 'LEFT', 'RIGHT']
|
| 63 |
+
self.action_deltas = {
|
| 64 |
+
self.UP: (-1, 0),
|
| 65 |
+
self.DOWN: (1, 0),
|
| 66 |
+
self.LEFT: (0, -1),
|
| 67 |
+
self.RIGHT: (0, 1)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
self.rng = np.random.RandomState(seed)
|
| 71 |
+
self.initial_seed = seed
|
| 72 |
+
|
| 73 |
+
self.board = None
|
| 74 |
+
self.agent_pos = None
|
| 75 |
+
self.goal_pos = None
|
| 76 |
+
self.pit_pos = None
|
| 77 |
+
self.wall_pos = None
|
| 78 |
+
self.start_pos = None
|
| 79 |
+
self.step_count = 0
|
| 80 |
+
self.total_reward = 0.0
|
| 81 |
+
self.done = False
|
| 82 |
+
|
| 83 |
+
self._fixed_layout = None
|
| 84 |
+
self._generate_grid()
|
| 85 |
+
self._fixed_layout = self._save_layout()
|
| 86 |
+
|
| 87 |
+
def _save_layout(self) -> Dict:
|
| 88 |
+
return {
|
| 89 |
+
'board': self.board.copy(),
|
| 90 |
+
'goal_pos': self.goal_pos,
|
| 91 |
+
'pit_pos': self.pit_pos,
|
| 92 |
+
'wall_pos': self.wall_pos,
|
| 93 |
+
'start_pos': self.start_pos
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
def _restore_layout(self):
|
| 97 |
+
if self._fixed_layout is not None:
|
| 98 |
+
self.board = self._fixed_layout['board'].copy()
|
| 99 |
+
self.goal_pos = self._fixed_layout['goal_pos']
|
| 100 |
+
self.pit_pos = self._fixed_layout['pit_pos']
|
| 101 |
+
self.wall_pos = self._fixed_layout['wall_pos']
|
| 102 |
+
self.start_pos = self._fixed_layout['start_pos']
|
| 103 |
+
|
| 104 |
+
def _generate_grid(self) -> None:
|
| 105 |
+
self.board = np.zeros((4, self.height, self.width), dtype=np.float32)
|
| 106 |
+
|
| 107 |
+
self.start_pos = (0, 0)
|
| 108 |
+
self.agent_pos = list(self.start_pos)
|
| 109 |
+
|
| 110 |
+
if self.mode == 'static':
|
| 111 |
+
self.goal_pos = (self.height - 1, self.width - 1)
|
| 112 |
+
self.pit_pos = (self.height - 1, 1) if self.width > 2 else None
|
| 113 |
+
self.wall_pos = (1, 1) if self.width > 2 and self.height > 2 else None
|
| 114 |
+
else:
|
| 115 |
+
available = []
|
| 116 |
+
for i in range(self.height):
|
| 117 |
+
for j in range(self.width):
|
| 118 |
+
if (i, j) != self.start_pos:
|
| 119 |
+
available.append((i, j))
|
| 120 |
+
self.rng.shuffle(available)
|
| 121 |
+
self.goal_pos = available[0]
|
| 122 |
+
self.pit_pos = available[1] if len(available) > 1 else None
|
| 123 |
+
self.wall_pos = available[2] if len(available) > 2 else None
|
| 124 |
+
|
| 125 |
+
self.board[0, self.agent_pos[0], self.agent_pos[1]] = 1.0
|
| 126 |
+
self.board[1, self.goal_pos[0], self.goal_pos[1]] = 1.0
|
| 127 |
+
if self.pit_pos:
|
| 128 |
+
self.board[2, self.pit_pos[0], self.pit_pos[1]] = 1.0
|
| 129 |
+
if self.wall_pos:
|
| 130 |
+
self.board[3, self.wall_pos[0], self.wall_pos[1]] = 1.0
|
| 131 |
+
|
| 132 |
+
def reset(self, seed: Optional[int] = None) -> np.ndarray:
|
| 133 |
+
if self.mode == 'static' and self._fixed_layout is not None:
|
| 134 |
+
self._restore_layout()
|
| 135 |
+
elif seed is not None or self.mode == 'random':
|
| 136 |
+
if seed is not None:
|
| 137 |
+
self.rng = np.random.RandomState(seed)
|
| 138 |
+
self._generate_grid()
|
| 139 |
+
else:
|
| 140 |
+
self._restore_layout()
|
| 141 |
+
|
| 142 |
+
self.agent_pos = list(self.start_pos)
|
| 143 |
+
self.board[0] = 0.0
|
| 144 |
+
self.board[0, self.agent_pos[0], self.agent_pos[1]] = 1.0
|
| 145 |
+
|
| 146 |
+
self.step_count = 0
|
| 147 |
+
self.total_reward = 0.0
|
| 148 |
+
self.done = False
|
| 149 |
+
|
| 150 |
+
return self._get_state()
|
| 151 |
+
|
| 152 |
+
def _get_state(self) -> np.ndarray:
|
| 153 |
+
state = self.board.flatten().astype(np.float32)
|
| 154 |
+
noise = self.rng.rand(len(state)).astype(np.float32) / 100.0
|
| 155 |
+
return state + noise
|
| 156 |
+
|
| 157 |
+
def render_np(self) -> np.ndarray:
|
| 158 |
+
return self.board.copy()
|
| 159 |
+
|
| 160 |
+
def _is_valid_pos(self, pos: List[int]) -> bool:
|
| 161 |
+
row, col = pos
|
| 162 |
+
if row < 0 or row >= self.height:
|
| 163 |
+
return False
|
| 164 |
+
if col < 0 or col >= self.width:
|
| 165 |
+
return False
|
| 166 |
+
if self.wall_pos and (row, col) == self.wall_pos:
|
| 167 |
+
return False
|
| 168 |
+
return True
|
| 169 |
+
|
| 170 |
+
def step(self, action: int) -> Tuple[np.ndarray, float, bool, Dict]:
|
| 171 |
+
if self.done:
|
| 172 |
+
return self._get_state(), 0.0, True, {'episode_ended': True}
|
| 173 |
+
|
| 174 |
+
self.step_count += 1
|
| 175 |
+
|
| 176 |
+
delta = self.action_deltas[action]
|
| 177 |
+
new_pos = [self.agent_pos[0] + delta[0], self.agent_pos[1] + delta[1]]
|
| 178 |
+
|
| 179 |
+
reward = -1.0
|
| 180 |
+
done = False
|
| 181 |
+
info = {}
|
| 182 |
+
|
| 183 |
+
if not self._is_valid_pos(new_pos):
|
| 184 |
+
reward = -10.0
|
| 185 |
+
info['hit_wall'] = True
|
| 186 |
+
else:
|
| 187 |
+
self.board[0, self.agent_pos[0], self.agent_pos[1]] = 0.0
|
| 188 |
+
self.agent_pos = new_pos
|
| 189 |
+
self.board[0, self.agent_pos[0], self.agent_pos[1]] = 1.0
|
| 190 |
+
|
| 191 |
+
if tuple(self.agent_pos) == self.goal_pos:
|
| 192 |
+
reward = 10.0
|
| 193 |
+
done = True
|
| 194 |
+
info['reached_goal'] = True
|
| 195 |
+
elif self.pit_pos and tuple(self.agent_pos) == self.pit_pos:
|
| 196 |
+
reward = -10.0
|
| 197 |
+
done = True
|
| 198 |
+
info['fell_in_pit'] = True
|
| 199 |
+
|
| 200 |
+
if self.step_count >= self.max_steps:
|
| 201 |
+
done = True
|
| 202 |
+
info['max_steps_reached'] = True
|
| 203 |
+
|
| 204 |
+
self.total_reward += reward
|
| 205 |
+
self.done = done
|
| 206 |
+
info['step'] = self.step_count
|
| 207 |
+
info['total_reward'] = self.total_reward
|
| 208 |
+
|
| 209 |
+
return self._get_state(), reward, done, info
|
| 210 |
+
|
| 211 |
+
def render(self, mode: str = 'ascii') -> Optional[str]:
|
| 212 |
+
symbols = {
|
| 213 |
+
'empty': '.',
|
| 214 |
+
'agent': 'A',
|
| 215 |
+
'goal': 'G',
|
| 216 |
+
'pit': 'X',
|
| 217 |
+
'wall': '#'
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
lines = []
|
| 221 |
+
lines.append('=' * (self.width * 2 + 3))
|
| 222 |
+
for row in range(self.height):
|
| 223 |
+
line = '| '
|
| 224 |
+
for col in range(self.width):
|
| 225 |
+
if self.board[0, row, col] == 1.0:
|
| 226 |
+
line += symbols['agent'] + ' '
|
| 227 |
+
elif self.board[1, row, col] == 1.0:
|
| 228 |
+
line += symbols['goal'] + ' '
|
| 229 |
+
elif self.board[2, row, col] == 1.0:
|
| 230 |
+
line += symbols['pit'] + ' '
|
| 231 |
+
elif self.board[3, row, col] == 1.0:
|
| 232 |
+
line += symbols['wall'] + ' '
|
| 233 |
+
else:
|
| 234 |
+
line += symbols['empty'] + ' '
|
| 235 |
+
line += '|'
|
| 236 |
+
lines.append(line)
|
| 237 |
+
lines.append('=' * (self.width * 2 + 3))
|
| 238 |
+
lines.append(f'Step: {self.step_count} | Reward: {self.total_reward:.2f}')
|
| 239 |
+
|
| 240 |
+
output = '\n'.join(lines)
|
| 241 |
+
|
| 242 |
+
if mode == 'ascii':
|
| 243 |
+
print(output)
|
| 244 |
+
return None
|
| 245 |
+
elif mode == 'string':
|
| 246 |
+
return output
|
| 247 |
+
|
| 248 |
+
return output
|
| 249 |
+
|
| 250 |
+
def get_valid_actions(self) -> List[int]:
|
| 251 |
+
valid = []
|
| 252 |
+
for action in range(self.n_actions):
|
| 253 |
+
delta = self.action_deltas[action]
|
| 254 |
+
new_pos = [self.agent_pos[0] + delta[0], self.agent_pos[1] + delta[1]]
|
| 255 |
+
if self._is_valid_pos(new_pos):
|
| 256 |
+
valid.append(action)
|
| 257 |
+
return valid if valid else list(range(self.n_actions))
|
| 258 |
+
|
| 259 |
+
def clone(self) -> 'GridWorld':
|
| 260 |
+
env = GridWorld.__new__(GridWorld)
|
| 261 |
+
env.width = self.width
|
| 262 |
+
env.height = self.height
|
| 263 |
+
env.mode = self.mode
|
| 264 |
+
env.max_steps = self.max_steps
|
| 265 |
+
env.n_states = self.n_states
|
| 266 |
+
env.n_actions = self.n_actions
|
| 267 |
+
env.state_shape = self.state_shape
|
| 268 |
+
env.state_dim = self.state_dim
|
| 269 |
+
env.action_names = self.action_names
|
| 270 |
+
env.action_deltas = self.action_deltas
|
| 271 |
+
env.rng = np.random.RandomState()
|
| 272 |
+
env.rng.set_state(self.rng.get_state())
|
| 273 |
+
env.board = self.board.copy()
|
| 274 |
+
env.agent_pos = self.agent_pos.copy()
|
| 275 |
+
env.goal_pos = self.goal_pos
|
| 276 |
+
env.pit_pos = self.pit_pos
|
| 277 |
+
env.wall_pos = self.wall_pos
|
| 278 |
+
env.start_pos = self.start_pos
|
| 279 |
+
env.step_count = self.step_count
|
| 280 |
+
env.total_reward = self.total_reward
|
| 281 |
+
env.done = self.done
|
| 282 |
+
env._fixed_layout = self._fixed_layout.copy() if self._fixed_layout else None
|
| 283 |
+
return env
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
class ContinuousCartPole:
|
| 287 |
+
"""
|
| 288 |
+
CartPole environment with continuous state space.
|
| 289 |
+
Implemented from scratch using physics equations.
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
def __init__(
|
| 293 |
+
self,
|
| 294 |
+
gravity: float = 9.8,
|
| 295 |
+
cart_mass: float = 1.0,
|
| 296 |
+
pole_mass: float = 0.1,
|
| 297 |
+
pole_length: float = 0.5,
|
| 298 |
+
force_mag: float = 10.0,
|
| 299 |
+
dt: float = 0.02,
|
| 300 |
+
max_steps: int = 500,
|
| 301 |
+
seed: Optional[int] = None
|
| 302 |
+
):
|
| 303 |
+
self.gravity = gravity
|
| 304 |
+
self.cart_mass = cart_mass
|
| 305 |
+
self.pole_mass = pole_mass
|
| 306 |
+
self.pole_length = pole_length
|
| 307 |
+
self.force_mag = force_mag
|
| 308 |
+
self.dt = dt
|
| 309 |
+
self.max_steps = max_steps
|
| 310 |
+
|
| 311 |
+
self.total_mass = cart_mass + pole_mass
|
| 312 |
+
self.pole_mass_length = pole_mass * pole_length
|
| 313 |
+
|
| 314 |
+
self.x_threshold = 2.4
|
| 315 |
+
self.theta_threshold = 12 * np.pi / 180
|
| 316 |
+
|
| 317 |
+
self.n_actions = 2
|
| 318 |
+
self.state_dim = 4
|
| 319 |
+
|
| 320 |
+
self.rng = np.random.RandomState(seed)
|
| 321 |
+
self.state = None
|
| 322 |
+
self.step_count = 0
|
| 323 |
+
self.done = False
|
| 324 |
+
|
| 325 |
+
def reset(self, seed: Optional[int] = None) -> np.ndarray:
|
| 326 |
+
if seed is not None:
|
| 327 |
+
self.rng = np.random.RandomState(seed)
|
| 328 |
+
|
| 329 |
+
self.state = self.rng.uniform(-0.05, 0.05, size=(4,)).astype(np.float32)
|
| 330 |
+
self.step_count = 0
|
| 331 |
+
self.done = False
|
| 332 |
+
|
| 333 |
+
return self.state.copy()
|
| 334 |
+
|
| 335 |
+
def step(self, action: int) -> Tuple[np.ndarray, float, bool, Dict]:
|
| 336 |
+
if self.done:
|
| 337 |
+
return self.state.copy(), 0.0, True, {}
|
| 338 |
+
|
| 339 |
+
x, x_dot, theta, theta_dot = self.state
|
| 340 |
+
|
| 341 |
+
force = self.force_mag if action == 1 else -self.force_mag
|
| 342 |
+
|
| 343 |
+
cos_theta = np.cos(theta)
|
| 344 |
+
sin_theta = np.sin(theta)
|
| 345 |
+
|
| 346 |
+
temp = (force + self.pole_mass_length * theta_dot ** 2 * sin_theta) / self.total_mass
|
| 347 |
+
|
| 348 |
+
theta_acc = (self.gravity * sin_theta - cos_theta * temp) / (
|
| 349 |
+
self.pole_length * (4.0 / 3.0 - self.pole_mass * cos_theta ** 2 / self.total_mass)
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
x_acc = temp - self.pole_mass_length * theta_acc * cos_theta / self.total_mass
|
| 353 |
+
|
| 354 |
+
x = x + self.dt * x_dot
|
| 355 |
+
x_dot = x_dot + self.dt * x_acc
|
| 356 |
+
theta = theta + self.dt * theta_dot
|
| 357 |
+
theta_dot = theta_dot + self.dt * theta_acc
|
| 358 |
+
|
| 359 |
+
self.state = np.array([x, x_dot, theta, theta_dot], dtype=np.float32)
|
| 360 |
+
self.step_count += 1
|
| 361 |
+
|
| 362 |
+
done = bool(
|
| 363 |
+
x < -self.x_threshold
|
| 364 |
+
or x > self.x_threshold
|
| 365 |
+
or theta < -self.theta_threshold
|
| 366 |
+
or theta > self.theta_threshold
|
| 367 |
+
or self.step_count >= self.max_steps
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
reward = 1.0 if not done else 0.0
|
| 371 |
+
if self.step_count >= self.max_steps:
|
| 372 |
+
reward = 1.0
|
| 373 |
+
|
| 374 |
+
self.done = done
|
| 375 |
+
|
| 376 |
+
info = {
|
| 377 |
+
'step': self.step_count,
|
| 378 |
+
'x': x,
|
| 379 |
+
'theta': theta
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
return self.state.copy(), reward, done, info
|
| 383 |
+
|
| 384 |
+
def render(self, mode: str = 'ascii') -> Optional[str]:
|
| 385 |
+
if self.state is None:
|
| 386 |
+
return None
|
| 387 |
+
|
| 388 |
+
x, _, theta, _ = self.state
|
| 389 |
+
|
| 390 |
+
width = 60
|
| 391 |
+
cart_pos = int((x / self.x_threshold + 1) * width / 2)
|
| 392 |
+
cart_pos = max(2, min(width - 3, cart_pos))
|
| 393 |
+
|
| 394 |
+
pole_len = 4
|
| 395 |
+
pole_dx = int(pole_len * np.sin(theta))
|
| 396 |
+
pole_dy = int(pole_len * np.cos(theta))
|
| 397 |
+
|
| 398 |
+
lines = []
|
| 399 |
+
lines.append('=' * width)
|
| 400 |
+
|
| 401 |
+
for row in range(-pole_len, 2):
|
| 402 |
+
line = [' '] * width
|
| 403 |
+
if row == 1:
|
| 404 |
+
line[cart_pos-1:cart_pos+2] = ['[', 'C', ']']
|
| 405 |
+
elif row == 0:
|
| 406 |
+
line[cart_pos] = '|'
|
| 407 |
+
else:
|
| 408 |
+
expected_row = -row
|
| 409 |
+
if 0 <= expected_row <= pole_len:
|
| 410 |
+
expected_dx = int(expected_row * np.sin(theta))
|
| 411 |
+
pole_x = cart_pos + expected_dx
|
| 412 |
+
if 0 <= pole_x < width:
|
| 413 |
+
line[pole_x] = '*'
|
| 414 |
+
lines.append(''.join(line))
|
| 415 |
+
|
| 416 |
+
lines.append('-' * width)
|
| 417 |
+
lines.append(f'Step: {self.step_count} | x: {x:.2f} | theta: {np.degrees(theta):.1f}°')
|
| 418 |
+
lines.append('=' * width)
|
| 419 |
+
|
| 420 |
+
output = '\n'.join(lines)
|
| 421 |
+
|
| 422 |
+
if mode == 'ascii':
|
| 423 |
+
print(output)
|
| 424 |
+
return None
|
| 425 |
+
|
| 426 |
+
return output
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
# =============================================================================
|
| 430 |
+
# SECTION 2: NEURAL NETWORK COMPONENTS (Lines 300-600)
|
| 431 |
+
# =============================================================================
|
| 432 |
+
|
| 433 |
+
class Tensor:
|
| 434 |
+
"""Simple tensor wrapper for automatic gradient tracking."""
|
| 435 |
+
|
| 436 |
+
def __init__(self, data: np.ndarray, requires_grad: bool = False):
|
| 437 |
+
self.data = np.asarray(data, dtype=np.float32)
|
| 438 |
+
self.requires_grad = requires_grad
|
| 439 |
+
self.grad = None
|
| 440 |
+
self._backward = lambda: None
|
| 441 |
+
self._prev = set()
|
| 442 |
+
|
| 443 |
+
@property
|
| 444 |
+
def shape(self):
|
| 445 |
+
return self.data.shape
|
| 446 |
+
|
| 447 |
+
def zero_grad(self):
|
| 448 |
+
self.grad = None
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
class LinearLayer:
|
| 452 |
+
"""Fully connected layer with weights and biases."""
|
| 453 |
+
|
| 454 |
+
def __init__(
|
| 455 |
+
self,
|
| 456 |
+
in_features: int,
|
| 457 |
+
out_features: int,
|
| 458 |
+
bias: bool = True,
|
| 459 |
+
init_method: str = 'xavier'
|
| 460 |
+
):
|
| 461 |
+
self.in_features = in_features
|
| 462 |
+
self.out_features = out_features
|
| 463 |
+
self.use_bias = bias
|
| 464 |
+
|
| 465 |
+
if init_method == 'xavier':
|
| 466 |
+
limit = np.sqrt(6.0 / (in_features + out_features))
|
| 467 |
+
self.weights = np.random.uniform(-limit, limit, (in_features, out_features)).astype(np.float32)
|
| 468 |
+
elif init_method == 'he':
|
| 469 |
+
std = np.sqrt(2.0 / in_features)
|
| 470 |
+
self.weights = np.random.randn(in_features, out_features).astype(np.float32) * std
|
| 471 |
+
elif init_method == 'normal':
|
| 472 |
+
self.weights = np.random.randn(in_features, out_features).astype(np.float32) * 0.01
|
| 473 |
+
else:
|
| 474 |
+
self.weights = np.zeros((in_features, out_features), dtype=np.float32)
|
| 475 |
+
|
| 476 |
+
if bias:
|
| 477 |
+
self.bias = np.zeros(out_features, dtype=np.float32)
|
| 478 |
+
else:
|
| 479 |
+
self.bias = None
|
| 480 |
+
|
| 481 |
+
self.weight_grad = np.zeros_like(self.weights)
|
| 482 |
+
self.bias_grad = np.zeros(out_features, dtype=np.float32) if bias else None
|
| 483 |
+
|
| 484 |
+
self._input_cache = None
|
| 485 |
+
|
| 486 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 487 |
+
self._input_cache = x.copy()
|
| 488 |
+
output = np.dot(x, self.weights)
|
| 489 |
+
if self.use_bias:
|
| 490 |
+
output += self.bias
|
| 491 |
+
return output
|
| 492 |
+
|
| 493 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 494 |
+
batch_size = grad_output.shape[0] if grad_output.ndim > 1 else 1
|
| 495 |
+
|
| 496 |
+
if self._input_cache.ndim == 1:
|
| 497 |
+
self._input_cache = self._input_cache.reshape(1, -1)
|
| 498 |
+
if grad_output.ndim == 1:
|
| 499 |
+
grad_output = grad_output.reshape(1, -1)
|
| 500 |
+
|
| 501 |
+
# IN-PLACE update to preserve reference for optimizer
|
| 502 |
+
self.weight_grad[:] = np.dot(self._input_cache.T, grad_output) / batch_size
|
| 503 |
+
|
| 504 |
+
if self.use_bias:
|
| 505 |
+
self.bias_grad[:] = np.mean(grad_output, axis=0)
|
| 506 |
+
|
| 507 |
+
grad_input = np.dot(grad_output, self.weights.T)
|
| 508 |
+
|
| 509 |
+
return grad_input
|
| 510 |
+
|
| 511 |
+
def get_params(self) -> List[Tuple[np.ndarray, np.ndarray]]:
|
| 512 |
+
params = [(self.weights, self.weight_grad)]
|
| 513 |
+
if self.use_bias:
|
| 514 |
+
params.append((self.bias, self.bias_grad))
|
| 515 |
+
return params
|
| 516 |
+
|
| 517 |
+
def zero_grad(self):
|
| 518 |
+
self.weight_grad.fill(0)
|
| 519 |
+
if self.bias_grad is not None:
|
| 520 |
+
self.bias_grad.fill(0)
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
class ReLU:
|
| 524 |
+
"""Rectified Linear Unit activation."""
|
| 525 |
+
|
| 526 |
+
def __init__(self):
|
| 527 |
+
self._mask = None
|
| 528 |
+
|
| 529 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 530 |
+
self._mask = (x > 0).astype(np.float32)
|
| 531 |
+
return x * self._mask
|
| 532 |
+
|
| 533 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 534 |
+
return grad_output * self._mask
|
| 535 |
+
|
| 536 |
+
def get_params(self) -> List:
|
| 537 |
+
return []
|
| 538 |
+
|
| 539 |
+
def zero_grad(self):
|
| 540 |
+
pass
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
class LeakyReLU:
|
| 544 |
+
"""Leaky ReLU activation."""
|
| 545 |
+
|
| 546 |
+
def __init__(self, negative_slope: float = 0.01):
|
| 547 |
+
self.negative_slope = negative_slope
|
| 548 |
+
self._mask = None
|
| 549 |
+
|
| 550 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 551 |
+
self._mask = (x > 0).astype(np.float32)
|
| 552 |
+
return np.where(x > 0, x, x * self.negative_slope)
|
| 553 |
+
|
| 554 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 555 |
+
return grad_output * np.where(self._mask > 0, 1.0, self.negative_slope)
|
| 556 |
+
|
| 557 |
+
def get_params(self) -> List:
|
| 558 |
+
return []
|
| 559 |
+
|
| 560 |
+
def zero_grad(self):
|
| 561 |
+
pass
|
| 562 |
+
|
| 563 |
+
|
| 564 |
+
class Sigmoid:
|
| 565 |
+
"""Sigmoid activation function."""
|
| 566 |
+
|
| 567 |
+
def __init__(self):
|
| 568 |
+
self._output = None
|
| 569 |
+
|
| 570 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 571 |
+
x = np.clip(x, -500, 500)
|
| 572 |
+
self._output = 1.0 / (1.0 + np.exp(-x))
|
| 573 |
+
return self._output
|
| 574 |
+
|
| 575 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 576 |
+
return grad_output * self._output * (1.0 - self._output)
|
| 577 |
+
|
| 578 |
+
def get_params(self) -> List:
|
| 579 |
+
return []
|
| 580 |
+
|
| 581 |
+
def zero_grad(self):
|
| 582 |
+
pass
|
| 583 |
+
|
| 584 |
+
|
| 585 |
+
class Tanh:
|
| 586 |
+
"""Hyperbolic tangent activation."""
|
| 587 |
+
|
| 588 |
+
def __init__(self):
|
| 589 |
+
self._output = None
|
| 590 |
+
|
| 591 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 592 |
+
self._output = np.tanh(x)
|
| 593 |
+
return self._output
|
| 594 |
+
|
| 595 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 596 |
+
return grad_output * (1.0 - self._output ** 2)
|
| 597 |
+
|
| 598 |
+
def get_params(self) -> List:
|
| 599 |
+
return []
|
| 600 |
+
|
| 601 |
+
def zero_grad(self):
|
| 602 |
+
pass
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
class Softmax:
|
| 606 |
+
"""Softmax activation for probability outputs."""
|
| 607 |
+
|
| 608 |
+
def __init__(self, axis: int = -1):
|
| 609 |
+
self.axis = axis
|
| 610 |
+
self._output = None
|
| 611 |
+
|
| 612 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 613 |
+
x_max = np.max(x, axis=self.axis, keepdims=True)
|
| 614 |
+
exp_x = np.exp(x - x_max)
|
| 615 |
+
self._output = exp_x / np.sum(exp_x, axis=self.axis, keepdims=True)
|
| 616 |
+
return self._output
|
| 617 |
+
|
| 618 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 619 |
+
return grad_output * self._output * (1.0 - self._output)
|
| 620 |
+
|
| 621 |
+
def get_params(self) -> List:
|
| 622 |
+
return []
|
| 623 |
+
|
| 624 |
+
def zero_grad(self):
|
| 625 |
+
pass
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
class Dropout:
|
| 629 |
+
"""Dropout regularization layer."""
|
| 630 |
+
|
| 631 |
+
def __init__(self, p: float = 0.5):
|
| 632 |
+
self.p = p
|
| 633 |
+
self._mask = None
|
| 634 |
+
self.training = True
|
| 635 |
+
|
| 636 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 637 |
+
if not self.training:
|
| 638 |
+
return x
|
| 639 |
+
|
| 640 |
+
self._mask = (np.random.random(x.shape) > self.p).astype(np.float32)
|
| 641 |
+
return x * self._mask / (1.0 - self.p)
|
| 642 |
+
|
| 643 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 644 |
+
if not self.training:
|
| 645 |
+
return grad_output
|
| 646 |
+
return grad_output * self._mask / (1.0 - self.p)
|
| 647 |
+
|
| 648 |
+
def get_params(self) -> List:
|
| 649 |
+
return []
|
| 650 |
+
|
| 651 |
+
def zero_grad(self):
|
| 652 |
+
pass
|
| 653 |
+
|
| 654 |
+
|
| 655 |
+
class BatchNorm1d:
|
| 656 |
+
"""Batch normalization for 1D inputs."""
|
| 657 |
+
|
| 658 |
+
def __init__(self, num_features: int, eps: float = 1e-5, momentum: float = 0.1):
|
| 659 |
+
self.num_features = num_features
|
| 660 |
+
self.eps = eps
|
| 661 |
+
self.momentum = momentum
|
| 662 |
+
|
| 663 |
+
self.gamma = np.ones(num_features, dtype=np.float32)
|
| 664 |
+
self.beta = np.zeros(num_features, dtype=np.float32)
|
| 665 |
+
|
| 666 |
+
self.running_mean = np.zeros(num_features, dtype=np.float32)
|
| 667 |
+
self.running_var = np.ones(num_features, dtype=np.float32)
|
| 668 |
+
|
| 669 |
+
self.gamma_grad = np.zeros_like(self.gamma)
|
| 670 |
+
self.beta_grad = np.zeros_like(self.beta)
|
| 671 |
+
|
| 672 |
+
self._cache = None
|
| 673 |
+
self.training = True
|
| 674 |
+
|
| 675 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 676 |
+
if self.training:
|
| 677 |
+
mean = np.mean(x, axis=0)
|
| 678 |
+
var = np.var(x, axis=0)
|
| 679 |
+
|
| 680 |
+
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * mean
|
| 681 |
+
self.running_var = (1 - self.momentum) * self.running_var + self.momentum * var
|
| 682 |
+
|
| 683 |
+
x_norm = (x - mean) / np.sqrt(var + self.eps)
|
| 684 |
+
self._cache = (x, x_norm, mean, var)
|
| 685 |
+
else:
|
| 686 |
+
x_norm = (x - self.running_mean) / np.sqrt(self.running_var + self.eps)
|
| 687 |
+
|
| 688 |
+
return self.gamma * x_norm + self.beta
|
| 689 |
+
|
| 690 |
+
def backward(self, grad_output: np.ndarray) -> np.ndarray:
|
| 691 |
+
x, x_norm, mean, var = self._cache
|
| 692 |
+
batch_size = x.shape[0]
|
| 693 |
+
|
| 694 |
+
self.gamma_grad = np.sum(grad_output * x_norm, axis=0)
|
| 695 |
+
self.beta_grad = np.sum(grad_output, axis=0)
|
| 696 |
+
|
| 697 |
+
dx_norm = grad_output * self.gamma
|
| 698 |
+
dvar = np.sum(dx_norm * (x - mean) * -0.5 * (var + self.eps) ** -1.5, axis=0)
|
| 699 |
+
dmean = np.sum(dx_norm * -1 / np.sqrt(var + self.eps), axis=0)
|
| 700 |
+
dmean += dvar * np.mean(-2 * (x - mean), axis=0)
|
| 701 |
+
|
| 702 |
+
dx = dx_norm / np.sqrt(var + self.eps)
|
| 703 |
+
dx += dvar * 2 * (x - mean) / batch_size
|
| 704 |
+
dx += dmean / batch_size
|
| 705 |
+
|
| 706 |
+
return dx
|
| 707 |
+
|
| 708 |
+
def get_params(self) -> List[Tuple[np.ndarray, np.ndarray]]:
|
| 709 |
+
return [(self.gamma, self.gamma_grad), (self.beta, self.beta_grad)]
|
| 710 |
+
|
| 711 |
+
def zero_grad(self):
|
| 712 |
+
self.gamma_grad.fill(0)
|
| 713 |
+
self.beta_grad.fill(0)
|
| 714 |
+
|
| 715 |
+
|
| 716 |
+
class Sequential:
|
| 717 |
+
"""Sequential container for neural network layers."""
|
| 718 |
+
|
| 719 |
+
def __init__(self, layers: List = None):
|
| 720 |
+
self.layers = layers if layers is not None else []
|
| 721 |
+
self.training = True
|
| 722 |
+
|
| 723 |
+
def add(self, layer) -> 'Sequential':
|
| 724 |
+
self.layers.append(layer)
|
| 725 |
+
return self
|
| 726 |
+
|
| 727 |
+
def forward(self, x: np.ndarray) -> np.ndarray:
|
| 728 |
+
for layer in self.layers:
|
| 729 |
+
if hasattr(layer, 'training'):
|
| 730 |
+
layer.training = self.training
|
| 731 |
+
x = layer.forward(x)
|
| 732 |
+
return x
|
| 733 |
+
|
| 734 |
+
def backward(self, grad: np.ndarray) -> np.ndarray:
|
| 735 |
+
for layer in reversed(self.layers):
|
| 736 |
+
grad = layer.backward(grad)
|
| 737 |
+
return grad
|
| 738 |
+
|
| 739 |
+
def get_params(self) -> List[Tuple[np.ndarray, np.ndarray]]:
|
| 740 |
+
params = []
|
| 741 |
+
for layer in self.layers:
|
| 742 |
+
params.extend(layer.get_params())
|
| 743 |
+
return params
|
| 744 |
+
|
| 745 |
+
def zero_grad(self):
|
| 746 |
+
for layer in self.layers:
|
| 747 |
+
layer.zero_grad()
|
| 748 |
+
|
| 749 |
+
def train(self):
|
| 750 |
+
self.training = True
|
| 751 |
+
for layer in self.layers:
|
| 752 |
+
if hasattr(layer, 'training'):
|
| 753 |
+
layer.training = True
|
| 754 |
+
|
| 755 |
+
def eval(self):
|
| 756 |
+
self.training = False
|
| 757 |
+
for layer in self.layers:
|
| 758 |
+
if hasattr(layer, 'training'):
|
| 759 |
+
layer.training = False
|
| 760 |
+
|
| 761 |
+
def __call__(self, x: np.ndarray) -> np.ndarray:
|
| 762 |
+
return self.forward(x)
|
| 763 |
+
|
| 764 |
+
|
| 765 |
+
# =============================================================================
|
| 766 |
+
# SECTION 3: LOSS FUNCTIONS AND OPTIMIZERS (Lines 600-900)
|
| 767 |
+
# =============================================================================
|
| 768 |
+
|
| 769 |
+
class MSELoss:
|
| 770 |
+
"""Mean Squared Error loss."""
|
| 771 |
+
|
| 772 |
+
def __init__(self, reduction: str = 'mean'):
|
| 773 |
+
self.reduction = reduction
|
| 774 |
+
self._pred = None
|
| 775 |
+
self._target = None
|
| 776 |
+
|
| 777 |
+
def forward(self, pred: np.ndarray, target: np.ndarray) -> float:
|
| 778 |
+
self._pred = pred
|
| 779 |
+
self._target = target
|
| 780 |
+
|
| 781 |
+
diff = pred - target
|
| 782 |
+
loss = diff ** 2
|
| 783 |
+
|
| 784 |
+
if self.reduction == 'mean':
|
| 785 |
+
return float(np.mean(loss))
|
| 786 |
+
elif self.reduction == 'sum':
|
| 787 |
+
return float(np.sum(loss))
|
| 788 |
+
else:
|
| 789 |
+
return loss
|
| 790 |
+
|
| 791 |
+
def backward(self) -> np.ndarray:
|
| 792 |
+
grad = 2.0 * (self._pred - self._target)
|
| 793 |
+
|
| 794 |
+
if self.reduction == 'mean':
|
| 795 |
+
grad /= self._pred.size
|
| 796 |
+
|
| 797 |
+
return grad
|
| 798 |
+
|
| 799 |
+
def __call__(self, pred: np.ndarray, target: np.ndarray) -> float:
|
| 800 |
+
return self.forward(pred, target)
|
| 801 |
+
|
| 802 |
+
|
| 803 |
+
class HuberLoss:
|
| 804 |
+
"""Huber loss (smooth L1 loss)."""
|
| 805 |
+
|
| 806 |
+
def __init__(self, delta: float = 1.0, reduction: str = 'mean'):
|
| 807 |
+
self.delta = delta
|
| 808 |
+
self.reduction = reduction
|
| 809 |
+
self._pred = None
|
| 810 |
+
self._target = None
|
| 811 |
+
self._diff = None
|
| 812 |
+
|
| 813 |
+
def forward(self, pred: np.ndarray, target: np.ndarray) -> float:
|
| 814 |
+
self._pred = pred
|
| 815 |
+
self._target = target
|
| 816 |
+
self._diff = pred - target
|
| 817 |
+
|
| 818 |
+
abs_diff = np.abs(self._diff)
|
| 819 |
+
|
| 820 |
+
quadratic = np.minimum(abs_diff, self.delta)
|
| 821 |
+
linear = abs_diff - quadratic
|
| 822 |
+
|
| 823 |
+
loss = 0.5 * quadratic ** 2 + self.delta * linear
|
| 824 |
+
|
| 825 |
+
if self.reduction == 'mean':
|
| 826 |
+
return float(np.mean(loss))
|
| 827 |
+
elif self.reduction == 'sum':
|
| 828 |
+
return float(np.sum(loss))
|
| 829 |
+
else:
|
| 830 |
+
return loss
|
| 831 |
+
|
| 832 |
+
def backward(self) -> np.ndarray:
|
| 833 |
+
abs_diff = np.abs(self._diff)
|
| 834 |
+
|
| 835 |
+
grad = np.where(
|
| 836 |
+
abs_diff <= self.delta,
|
| 837 |
+
self._diff,
|
| 838 |
+
self.delta * np.sign(self._diff)
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
if self.reduction == 'mean':
|
| 842 |
+
grad /= self._pred.size
|
| 843 |
+
|
| 844 |
+
return grad
|
| 845 |
+
|
| 846 |
+
def __call__(self, pred: np.ndarray, target: np.ndarray) -> float:
|
| 847 |
+
return self.forward(pred, target)
|
| 848 |
+
|
| 849 |
+
|
| 850 |
+
class CrossEntropyLoss:
|
| 851 |
+
"""Cross entropy loss for classification."""
|
| 852 |
+
|
| 853 |
+
def __init__(self, reduction: str = 'mean'):
|
| 854 |
+
self.reduction = reduction
|
| 855 |
+
self._probs = None
|
| 856 |
+
self._target = None
|
| 857 |
+
|
| 858 |
+
def forward(self, logits: np.ndarray, target: np.ndarray) -> float:
|
| 859 |
+
max_logits = np.max(logits, axis=-1, keepdims=True)
|
| 860 |
+
exp_logits = np.exp(logits - max_logits)
|
| 861 |
+
self._probs = exp_logits / np.sum(exp_logits, axis=-1, keepdims=True)
|
| 862 |
+
|
| 863 |
+
self._target = target
|
| 864 |
+
|
| 865 |
+
if target.ndim == 1:
|
| 866 |
+
batch_size = logits.shape[0]
|
| 867 |
+
log_probs = np.log(self._probs[np.arange(batch_size), target] + 1e-10)
|
| 868 |
+
else:
|
| 869 |
+
log_probs = np.sum(target * np.log(self._probs + 1e-10), axis=-1)
|
| 870 |
+
|
| 871 |
+
loss = -log_probs
|
| 872 |
+
|
| 873 |
+
if self.reduction == 'mean':
|
| 874 |
+
return float(np.mean(loss))
|
| 875 |
+
elif self.reduction == 'sum':
|
| 876 |
+
return float(np.sum(loss))
|
| 877 |
+
else:
|
| 878 |
+
return loss
|
| 879 |
+
|
| 880 |
+
def backward(self) -> np.ndarray:
|
| 881 |
+
grad = self._probs.copy()
|
| 882 |
+
|
| 883 |
+
if self._target.ndim == 1:
|
| 884 |
+
batch_size = grad.shape[0]
|
| 885 |
+
grad[np.arange(batch_size), self._target] -= 1
|
| 886 |
+
else:
|
| 887 |
+
grad -= self._target
|
| 888 |
+
|
| 889 |
+
if self.reduction == 'mean':
|
| 890 |
+
grad /= grad.shape[0]
|
| 891 |
+
|
| 892 |
+
return grad
|
| 893 |
+
|
| 894 |
+
def __call__(self, logits: np.ndarray, target: np.ndarray) -> float:
|
| 895 |
+
return self.forward(logits, target)
|
| 896 |
+
|
| 897 |
+
|
| 898 |
+
class SGD:
|
| 899 |
+
"""Stochastic Gradient Descent optimizer."""
|
| 900 |
+
|
| 901 |
+
def __init__(
|
| 902 |
+
self,
|
| 903 |
+
params: List[Tuple[np.ndarray, np.ndarray]],
|
| 904 |
+
lr: float = 0.01,
|
| 905 |
+
momentum: float = 0.0,
|
| 906 |
+
weight_decay: float = 0.0
|
| 907 |
+
):
|
| 908 |
+
self.params = params
|
| 909 |
+
self.lr = lr
|
| 910 |
+
self.momentum = momentum
|
| 911 |
+
self.weight_decay = weight_decay
|
| 912 |
+
|
| 913 |
+
self.velocity = [np.zeros_like(p[0]) for p in params]
|
| 914 |
+
|
| 915 |
+
def step(self):
|
| 916 |
+
for i, (param, grad) in enumerate(self.params):
|
| 917 |
+
g = grad.copy()
|
| 918 |
+
if self.weight_decay > 0:
|
| 919 |
+
g = g + self.weight_decay * param
|
| 920 |
+
|
| 921 |
+
if self.momentum > 0:
|
| 922 |
+
self.velocity[i] = self.momentum * self.velocity[i] + g
|
| 923 |
+
param[:] = param - self.lr * self.velocity[i]
|
| 924 |
+
else:
|
| 925 |
+
param[:] = param - self.lr * g
|
| 926 |
+
|
| 927 |
+
def zero_grad(self):
|
| 928 |
+
for _, grad in self.params:
|
| 929 |
+
grad.fill(0)
|
| 930 |
+
|
| 931 |
+
|
| 932 |
+
class Adam:
|
| 933 |
+
"""Adam optimizer with momentum and adaptive learning rates."""
|
| 934 |
+
|
| 935 |
+
def __init__(
|
| 936 |
+
self,
|
| 937 |
+
params: List[Tuple[np.ndarray, np.ndarray]],
|
| 938 |
+
lr: float = 0.001,
|
| 939 |
+
beta1: float = 0.9,
|
| 940 |
+
beta2: float = 0.999,
|
| 941 |
+
eps: float = 1e-8,
|
| 942 |
+
weight_decay: float = 0.0
|
| 943 |
+
):
|
| 944 |
+
self.params = params
|
| 945 |
+
self.lr = lr
|
| 946 |
+
self.beta1 = beta1
|
| 947 |
+
self.beta2 = beta2
|
| 948 |
+
self.eps = eps
|
| 949 |
+
self.weight_decay = weight_decay
|
| 950 |
+
|
| 951 |
+
self.m = [np.zeros_like(p[0]) for p in params]
|
| 952 |
+
self.v = [np.zeros_like(p[0]) for p in params]
|
| 953 |
+
self.t = 0
|
| 954 |
+
|
| 955 |
+
def step(self):
|
| 956 |
+
self.t += 1
|
| 957 |
+
|
| 958 |
+
for i, (param, grad) in enumerate(self.params):
|
| 959 |
+
g = grad.copy()
|
| 960 |
+
if self.weight_decay > 0:
|
| 961 |
+
g = g + self.weight_decay * param
|
| 962 |
+
|
| 963 |
+
self.m[i] = self.beta1 * self.m[i] + (1 - self.beta1) * g
|
| 964 |
+
self.v[i] = self.beta2 * self.v[i] + (1 - self.beta2) * (g ** 2)
|
| 965 |
+
|
| 966 |
+
m_hat = self.m[i] / (1 - self.beta1 ** self.t)
|
| 967 |
+
v_hat = self.v[i] / (1 - self.beta2 ** self.t)
|
| 968 |
+
|
| 969 |
+
update = self.lr * m_hat / (np.sqrt(v_hat) + self.eps)
|
| 970 |
+
param[:] = param - update
|
| 971 |
+
|
| 972 |
+
def zero_grad(self):
|
| 973 |
+
for _, grad in self.params:
|
| 974 |
+
grad.fill(0)
|
| 975 |
+
|
| 976 |
+
|
| 977 |
+
class RMSprop:
|
| 978 |
+
"""RMSprop optimizer."""
|
| 979 |
+
|
| 980 |
+
def __init__(
|
| 981 |
+
self,
|
| 982 |
+
params: List[Tuple[np.ndarray, np.ndarray]],
|
| 983 |
+
lr: float = 0.01,
|
| 984 |
+
alpha: float = 0.99,
|
| 985 |
+
eps: float = 1e-8,
|
| 986 |
+
weight_decay: float = 0.0
|
| 987 |
+
):
|
| 988 |
+
self.params = params
|
| 989 |
+
self.lr = lr
|
| 990 |
+
self.alpha = alpha
|
| 991 |
+
self.eps = eps
|
| 992 |
+
self.weight_decay = weight_decay
|
| 993 |
+
|
| 994 |
+
self.v = [np.zeros_like(p[0]) for p in params]
|
| 995 |
+
|
| 996 |
+
def step(self):
|
| 997 |
+
for i, (param, grad) in enumerate(self.params):
|
| 998 |
+
g = grad.copy()
|
| 999 |
+
if self.weight_decay > 0:
|
| 1000 |
+
g = g + self.weight_decay * param
|
| 1001 |
+
|
| 1002 |
+
self.v[i] = self.alpha * self.v[i] + (1 - self.alpha) * (g ** 2)
|
| 1003 |
+
param[:] = param - self.lr * g / (np.sqrt(self.v[i]) + self.eps)
|
| 1004 |
+
|
| 1005 |
+
def zero_grad(self):
|
| 1006 |
+
for _, grad in self.params:
|
| 1007 |
+
grad.fill(0)
|
| 1008 |
+
|
| 1009 |
+
|
| 1010 |
+
# =============================================================================
|
| 1011 |
+
# SECTION 4: REPLAY BUFFERS (Lines 900-1200)
|
| 1012 |
+
# =============================================================================
|
| 1013 |
+
|
| 1014 |
+
class ReplayBuffer:
|
| 1015 |
+
"""Basic experience replay buffer."""
|
| 1016 |
+
|
| 1017 |
+
def __init__(self, capacity: int, state_dim: int, seed: Optional[int] = None):
|
| 1018 |
+
self.capacity = capacity
|
| 1019 |
+
self.state_dim = state_dim
|
| 1020 |
+
|
| 1021 |
+
self.states = np.zeros((capacity, state_dim), dtype=np.float32)
|
| 1022 |
+
self.actions = np.zeros(capacity, dtype=np.int64)
|
| 1023 |
+
self.rewards = np.zeros(capacity, dtype=np.float32)
|
| 1024 |
+
self.next_states = np.zeros((capacity, state_dim), dtype=np.float32)
|
| 1025 |
+
self.dones = np.zeros(capacity, dtype=np.float32)
|
| 1026 |
+
|
| 1027 |
+
self.position = 0
|
| 1028 |
+
self.size = 0
|
| 1029 |
+
|
| 1030 |
+
self.rng = np.random.RandomState(seed)
|
| 1031 |
+
|
| 1032 |
+
def push(
|
| 1033 |
+
self,
|
| 1034 |
+
state: np.ndarray,
|
| 1035 |
+
action: int,
|
| 1036 |
+
reward: float,
|
| 1037 |
+
next_state: np.ndarray,
|
| 1038 |
+
done: bool
|
| 1039 |
+
):
|
| 1040 |
+
self.states[self.position] = state
|
| 1041 |
+
self.actions[self.position] = action
|
| 1042 |
+
self.rewards[self.position] = reward
|
| 1043 |
+
self.next_states[self.position] = next_state
|
| 1044 |
+
self.dones[self.position] = float(done)
|
| 1045 |
+
|
| 1046 |
+
self.position = (self.position + 1) % self.capacity
|
| 1047 |
+
self.size = min(self.size + 1, self.capacity)
|
| 1048 |
+
|
| 1049 |
+
def sample(self, batch_size: int) -> Tuple[np.ndarray, ...]:
|
| 1050 |
+
indices = self.rng.randint(0, self.size, size=batch_size)
|
| 1051 |
+
|
| 1052 |
+
return (
|
| 1053 |
+
self.states[indices],
|
| 1054 |
+
self.actions[indices],
|
| 1055 |
+
self.rewards[indices],
|
| 1056 |
+
self.next_states[indices],
|
| 1057 |
+
self.dones[indices]
|
| 1058 |
+
)
|
| 1059 |
+
|
| 1060 |
+
def __len__(self) -> int:
|
| 1061 |
+
return self.size
|
| 1062 |
+
|
| 1063 |
+
def is_ready(self, batch_size: int) -> bool:
|
| 1064 |
+
return self.size >= batch_size
|
| 1065 |
+
|
| 1066 |
+
|
| 1067 |
+
class SumTree:
|
| 1068 |
+
"""Sum tree data structure for efficient priority sampling."""
|
| 1069 |
+
|
| 1070 |
+
def __init__(self, capacity: int):
|
| 1071 |
+
self.capacity = capacity
|
| 1072 |
+
self.tree = np.zeros(2 * capacity - 1, dtype=np.float64)
|
| 1073 |
+
self.data_pointer = 0
|
| 1074 |
+
|
| 1075 |
+
def _propagate(self, idx: int, change: float):
|
| 1076 |
+
parent = (idx - 1) // 2
|
| 1077 |
+
self.tree[parent] += change
|
| 1078 |
+
if parent != 0:
|
| 1079 |
+
self._propagate(parent, change)
|
| 1080 |
+
|
| 1081 |
+
def _retrieve(self, idx: int, s: float) -> int:
|
| 1082 |
+
left = 2 * idx + 1
|
| 1083 |
+
right = left + 1
|
| 1084 |
+
|
| 1085 |
+
if left >= len(self.tree):
|
| 1086 |
+
return idx
|
| 1087 |
+
|
| 1088 |
+
if s <= self.tree[left]:
|
| 1089 |
+
return self._retrieve(left, s)
|
| 1090 |
+
else:
|
| 1091 |
+
return self._retrieve(right, s - self.tree[left])
|
| 1092 |
+
|
| 1093 |
+
def total(self) -> float:
|
| 1094 |
+
return self.tree[0]
|
| 1095 |
+
|
| 1096 |
+
def update(self, idx: int, priority: float):
|
| 1097 |
+
change = priority - self.tree[idx]
|
| 1098 |
+
self.tree[idx] = priority
|
| 1099 |
+
self._propagate(idx, change)
|
| 1100 |
+
|
| 1101 |
+
def get_leaf(self, s: float) -> Tuple[int, float]:
|
| 1102 |
+
idx = self._retrieve(0, s)
|
| 1103 |
+
data_idx = idx - self.capacity + 1
|
| 1104 |
+
return data_idx, self.tree[idx]
|
| 1105 |
+
|
| 1106 |
+
|
| 1107 |
+
class PrioritizedReplayBuffer:
|
| 1108 |
+
"""Prioritized Experience Replay buffer using sum tree."""
|
| 1109 |
+
|
| 1110 |
+
def __init__(
|
| 1111 |
+
self,
|
| 1112 |
+
capacity: int,
|
| 1113 |
+
state_dim: int,
|
| 1114 |
+
alpha: float = 0.6,
|
| 1115 |
+
beta: float = 0.4,
|
| 1116 |
+
beta_increment: float = 0.001,
|
| 1117 |
+
epsilon: float = 1e-6,
|
| 1118 |
+
seed: Optional[int] = None
|
| 1119 |
+
):
|
| 1120 |
+
self.capacity = capacity
|
| 1121 |
+
self.state_dim = state_dim
|
| 1122 |
+
self.alpha = alpha
|
| 1123 |
+
self.beta = beta
|
| 1124 |
+
self.beta_increment = beta_increment
|
| 1125 |
+
self.epsilon = epsilon
|
| 1126 |
+
|
| 1127 |
+
self.tree = SumTree(capacity)
|
| 1128 |
+
|
| 1129 |
+
self.states = np.zeros((capacity, state_dim), dtype=np.float32)
|
| 1130 |
+
self.actions = np.zeros(capacity, dtype=np.int64)
|
| 1131 |
+
self.rewards = np.zeros(capacity, dtype=np.float32)
|
| 1132 |
+
self.next_states = np.zeros((capacity, state_dim), dtype=np.float32)
|
| 1133 |
+
self.dones = np.zeros(capacity, dtype=np.float32)
|
| 1134 |
+
|
| 1135 |
+
self.position = 0
|
| 1136 |
+
self.size = 0
|
| 1137 |
+
self.max_priority = 1.0
|
| 1138 |
+
|
| 1139 |
+
self.rng = np.random.RandomState(seed)
|
| 1140 |
+
|
| 1141 |
+
def push(
|
| 1142 |
+
self,
|
| 1143 |
+
state: np.ndarray,
|
| 1144 |
+
action: int,
|
| 1145 |
+
reward: float,
|
| 1146 |
+
next_state: np.ndarray,
|
| 1147 |
+
done: bool
|
| 1148 |
+
):
|
| 1149 |
+
self.states[self.position] = state
|
| 1150 |
+
self.actions[self.position] = action
|
| 1151 |
+
self.rewards[self.position] = reward
|
| 1152 |
+
self.next_states[self.position] = next_state
|
| 1153 |
+
self.dones[self.position] = float(done)
|
| 1154 |
+
|
| 1155 |
+
tree_idx = self.position + self.capacity - 1
|
| 1156 |
+
self.tree.update(tree_idx, self.max_priority ** self.alpha)
|
| 1157 |
+
|
| 1158 |
+
self.position = (self.position + 1) % self.capacity
|
| 1159 |
+
self.size = min(self.size + 1, self.capacity)
|
| 1160 |
+
|
| 1161 |
+
def sample(self, batch_size: int) -> Tuple[np.ndarray, ...]:
|
| 1162 |
+
indices = np.zeros(batch_size, dtype=np.int64)
|
| 1163 |
+
priorities = np.zeros(batch_size, dtype=np.float64)
|
| 1164 |
+
|
| 1165 |
+
segment = self.tree.total() / batch_size
|
| 1166 |
+
|
| 1167 |
+
self.beta = min(1.0, self.beta + self.beta_increment)
|
| 1168 |
+
|
| 1169 |
+
for i in range(batch_size):
|
| 1170 |
+
a = segment * i
|
| 1171 |
+
b = segment * (i + 1)
|
| 1172 |
+
s = self.rng.uniform(a, b)
|
| 1173 |
+
|
| 1174 |
+
data_idx, priority = self.tree.get_leaf(s)
|
| 1175 |
+
indices[i] = data_idx
|
| 1176 |
+
priorities[i] = priority
|
| 1177 |
+
|
| 1178 |
+
sampling_probs = priorities / self.tree.total()
|
| 1179 |
+
weights = (self.size * sampling_probs) ** (-self.beta)
|
| 1180 |
+
weights /= weights.max()
|
| 1181 |
+
weights = weights.astype(np.float32)
|
| 1182 |
+
|
| 1183 |
+
return (
|
| 1184 |
+
self.states[indices],
|
| 1185 |
+
self.actions[indices],
|
| 1186 |
+
self.rewards[indices],
|
| 1187 |
+
self.next_states[indices],
|
| 1188 |
+
self.dones[indices],
|
| 1189 |
+
indices,
|
| 1190 |
+
weights
|
| 1191 |
+
)
|
| 1192 |
+
|
| 1193 |
+
def update_priorities(self, indices: np.ndarray, td_errors: np.ndarray):
|
| 1194 |
+
for idx, td_error in zip(indices, td_errors):
|
| 1195 |
+
priority = (np.abs(td_error) + self.epsilon) ** self.alpha
|
| 1196 |
+
self.max_priority = max(self.max_priority, priority)
|
| 1197 |
+
|
| 1198 |
+
tree_idx = idx + self.capacity - 1
|
| 1199 |
+
self.tree.update(tree_idx, priority)
|
| 1200 |
+
|
| 1201 |
+
def __len__(self) -> int:
|
| 1202 |
+
return self.size
|
| 1203 |
+
|
| 1204 |
+
def is_ready(self, batch_size: int) -> bool:
|
| 1205 |
+
return self.size >= batch_size
|
| 1206 |
+
|
| 1207 |
+
|
| 1208 |
+
class NStepReplayBuffer:
|
| 1209 |
+
"""N-step returns replay buffer."""
|
| 1210 |
+
|
| 1211 |
+
def __init__(
|
| 1212 |
+
self,
|
| 1213 |
+
capacity: int,
|
| 1214 |
+
state_dim: int,
|
| 1215 |
+
n_steps: int = 3,
|
| 1216 |
+
gamma: float = 0.99,
|
| 1217 |
+
seed: Optional[int] = None
|
| 1218 |
+
):
|
| 1219 |
+
self.capacity = capacity
|
| 1220 |
+
self.state_dim = state_dim
|
| 1221 |
+
self.n_steps = n_steps
|
| 1222 |
+
self.gamma = gamma
|
| 1223 |
+
|
| 1224 |
+
self.main_buffer = ReplayBuffer(capacity, state_dim, seed)
|
| 1225 |
+
|
| 1226 |
+
self.n_step_buffer = deque(maxlen=n_steps)
|
| 1227 |
+
|
| 1228 |
+
self.rng = np.random.RandomState(seed)
|
| 1229 |
+
|
| 1230 |
+
def push(
|
| 1231 |
+
self,
|
| 1232 |
+
state: np.ndarray,
|
| 1233 |
+
action: int,
|
| 1234 |
+
reward: float,
|
| 1235 |
+
next_state: np.ndarray,
|
| 1236 |
+
done: bool
|
| 1237 |
+
):
|
| 1238 |
+
self.n_step_buffer.append((state, action, reward, next_state, done))
|
| 1239 |
+
|
| 1240 |
+
if len(self.n_step_buffer) == self.n_steps:
|
| 1241 |
+
n_step_return = 0.0
|
| 1242 |
+
for i in range(self.n_steps):
|
| 1243 |
+
n_step_return += (self.gamma ** i) * self.n_step_buffer[i][2]
|
| 1244 |
+
|
| 1245 |
+
first_state = self.n_step_buffer[0][0]
|
| 1246 |
+
first_action = self.n_step_buffer[0][1]
|
| 1247 |
+
last_next_state = self.n_step_buffer[-1][3]
|
| 1248 |
+
last_done = self.n_step_buffer[-1][4]
|
| 1249 |
+
|
| 1250 |
+
self.main_buffer.push(
|
| 1251 |
+
first_state,
|
| 1252 |
+
first_action,
|
| 1253 |
+
n_step_return,
|
| 1254 |
+
last_next_state,
|
| 1255 |
+
last_done
|
| 1256 |
+
)
|
| 1257 |
+
|
| 1258 |
+
if done:
|
| 1259 |
+
while len(self.n_step_buffer) > 0:
|
| 1260 |
+
n = len(self.n_step_buffer)
|
| 1261 |
+
n_step_return = 0.0
|
| 1262 |
+
for i in range(n):
|
| 1263 |
+
n_step_return += (self.gamma ** i) * self.n_step_buffer[i][2]
|
| 1264 |
+
|
| 1265 |
+
first_state = self.n_step_buffer[0][0]
|
| 1266 |
+
first_action = self.n_step_buffer[0][1]
|
| 1267 |
+
last_next_state = self.n_step_buffer[-1][3]
|
| 1268 |
+
|
| 1269 |
+
self.main_buffer.push(
|
| 1270 |
+
first_state,
|
| 1271 |
+
first_action,
|
| 1272 |
+
n_step_return,
|
| 1273 |
+
last_next_state,
|
| 1274 |
+
True
|
| 1275 |
+
)
|
| 1276 |
+
|
| 1277 |
+
self.n_step_buffer.popleft()
|
| 1278 |
+
|
| 1279 |
+
def sample(self, batch_size: int) -> Tuple[np.ndarray, ...]:
|
| 1280 |
+
return self.main_buffer.sample(batch_size)
|
| 1281 |
+
|
| 1282 |
+
def __len__(self) -> int:
|
| 1283 |
+
return len(self.main_buffer)
|
| 1284 |
+
|
| 1285 |
+
def is_ready(self, batch_size: int) -> bool:
|
| 1286 |
+
return self.main_buffer.is_ready(batch_size)
|
| 1287 |
+
|
| 1288 |
+
|
| 1289 |
+
# =============================================================================
|
| 1290 |
+
# SECTION 5: DQN AGENTS (Lines 1200-1600)
|
| 1291 |
+
# =============================================================================
|
| 1292 |
+
|
| 1293 |
+
class EpsilonGreedy:
|
| 1294 |
+
"""Epsilon-greedy exploration strategy with decay."""
|
| 1295 |
+
|
| 1296 |
+
def __init__(
|
| 1297 |
+
self,
|
| 1298 |
+
epsilon_start: float = 1.0,
|
| 1299 |
+
epsilon_end: float = 0.01,
|
| 1300 |
+
epsilon_decay: float = 0.995,
|
| 1301 |
+
decay_type: str = 'exponential',
|
| 1302 |
+
decay_steps: int = 10000,
|
| 1303 |
+
seed: Optional[int] = None
|
| 1304 |
+
):
|
| 1305 |
+
self.epsilon_start = epsilon_start
|
| 1306 |
+
self.epsilon_end = epsilon_end
|
| 1307 |
+
self.epsilon_decay = epsilon_decay
|
| 1308 |
+
self.decay_type = decay_type
|
| 1309 |
+
self.decay_steps = decay_steps
|
| 1310 |
+
|
| 1311 |
+
self.epsilon = epsilon_start
|
| 1312 |
+
self.step_count = 0
|
| 1313 |
+
|
| 1314 |
+
self.rng = np.random.RandomState(seed)
|
| 1315 |
+
|
| 1316 |
+
def get_action(self, q_values: np.ndarray, valid_actions: List[int] = None) -> int:
|
| 1317 |
+
if self.rng.random() < self.epsilon:
|
| 1318 |
+
if valid_actions is not None:
|
| 1319 |
+
return self.rng.choice(valid_actions)
|
| 1320 |
+
else:
|
| 1321 |
+
return self.rng.randint(0, len(q_values))
|
| 1322 |
+
else:
|
| 1323 |
+
if valid_actions is not None:
|
| 1324 |
+
mask = np.full(len(q_values), -np.inf)
|
| 1325 |
+
mask[valid_actions] = 0
|
| 1326 |
+
return int(np.argmax(q_values + mask))
|
| 1327 |
+
else:
|
| 1328 |
+
return int(np.argmax(q_values))
|
| 1329 |
+
|
| 1330 |
+
def decay(self):
|
| 1331 |
+
self.step_count += 1
|
| 1332 |
+
|
| 1333 |
+
if self.decay_type == 'exponential':
|
| 1334 |
+
self.epsilon = max(
|
| 1335 |
+
self.epsilon_end,
|
| 1336 |
+
self.epsilon * self.epsilon_decay
|
| 1337 |
+
)
|
| 1338 |
+
elif self.decay_type == 'linear':
|
| 1339 |
+
self.epsilon = max(
|
| 1340 |
+
self.epsilon_end,
|
| 1341 |
+
self.epsilon_start - (self.epsilon_start - self.epsilon_end) * (self.step_count / self.decay_steps)
|
| 1342 |
+
)
|
| 1343 |
+
|
| 1344 |
+
def reset(self):
|
| 1345 |
+
self.epsilon = self.epsilon_start
|
| 1346 |
+
self.step_count = 0
|
| 1347 |
+
|
| 1348 |
+
|
| 1349 |
+
class DQNNetwork:
|
| 1350 |
+
"""Neural network for DQN Q-value estimation."""
|
| 1351 |
+
|
| 1352 |
+
def __init__(
|
| 1353 |
+
self,
|
| 1354 |
+
state_dim: int,
|
| 1355 |
+
action_dim: int,
|
| 1356 |
+
hidden_dims: List[int] = None,
|
| 1357 |
+
activation: str = 'relu'
|
| 1358 |
+
):
|
| 1359 |
+
if hidden_dims is None:
|
| 1360 |
+
hidden_dims = [128, 128]
|
| 1361 |
+
|
| 1362 |
+
self.state_dim = state_dim
|
| 1363 |
+
self.action_dim = action_dim
|
| 1364 |
+
self.hidden_dims = hidden_dims
|
| 1365 |
+
|
| 1366 |
+
if activation == 'relu':
|
| 1367 |
+
activation_class = ReLU
|
| 1368 |
+
elif activation == 'leaky_relu':
|
| 1369 |
+
activation_class = LeakyReLU
|
| 1370 |
+
elif activation == 'tanh':
|
| 1371 |
+
activation_class = Tanh
|
| 1372 |
+
else:
|
| 1373 |
+
activation_class = ReLU
|
| 1374 |
+
|
| 1375 |
+
layers = []
|
| 1376 |
+
prev_dim = state_dim
|
| 1377 |
+
|
| 1378 |
+
for hidden_dim in hidden_dims:
|
| 1379 |
+
layers.append(LinearLayer(prev_dim, hidden_dim, init_method='he'))
|
| 1380 |
+
layers.append(activation_class())
|
| 1381 |
+
prev_dim = hidden_dim
|
| 1382 |
+
|
| 1383 |
+
layers.append(LinearLayer(prev_dim, action_dim, init_method='xavier'))
|
| 1384 |
+
|
| 1385 |
+
self.network = Sequential(layers)
|
| 1386 |
+
|
| 1387 |
+
def forward(self, state: np.ndarray) -> np.ndarray:
|
| 1388 |
+
if state.ndim == 1:
|
| 1389 |
+
state = state.reshape(1, -1)
|
| 1390 |
+
return self.network.forward(state)
|
| 1391 |
+
|
| 1392 |
+
def backward(self, grad: np.ndarray) -> np.ndarray:
|
| 1393 |
+
return self.network.backward(grad)
|
| 1394 |
+
|
| 1395 |
+
def get_params(self) -> List[Tuple[np.ndarray, np.ndarray]]:
|
| 1396 |
+
return self.network.get_params()
|
| 1397 |
+
|
| 1398 |
+
def zero_grad(self):
|
| 1399 |
+
self.network.zero_grad()
|
| 1400 |
+
|
| 1401 |
+
def copy_from(self, other: 'DQNNetwork'):
|
| 1402 |
+
for (p1, _), (p2, _) in zip(self.get_params(), other.get_params()):
|
| 1403 |
+
p1[:] = p2
|
| 1404 |
+
|
| 1405 |
+
def soft_update(self, other: 'DQNNetwork', tau: float):
|
| 1406 |
+
for (p1, _), (p2, _) in zip(self.get_params(), other.get_params()):
|
| 1407 |
+
p1[:] = tau * p2 + (1 - tau) * p1
|
| 1408 |
+
|
| 1409 |
+
def __call__(self, state: np.ndarray) -> np.ndarray:
|
| 1410 |
+
return self.forward(state)
|
| 1411 |
+
|
| 1412 |
+
|
| 1413 |
+
class DuelingDQNNetwork:
|
| 1414 |
+
"""Dueling DQN network architecture."""
|
| 1415 |
+
|
| 1416 |
+
def __init__(
|
| 1417 |
+
self,
|
| 1418 |
+
state_dim: int,
|
| 1419 |
+
action_dim: int,
|
| 1420 |
+
hidden_dims: List[int] = None
|
| 1421 |
+
):
|
| 1422 |
+
if hidden_dims is None:
|
| 1423 |
+
hidden_dims = [128, 128]
|
| 1424 |
+
|
| 1425 |
+
self.state_dim = state_dim
|
| 1426 |
+
self.action_dim = action_dim
|
| 1427 |
+
|
| 1428 |
+
layers = []
|
| 1429 |
+
prev_dim = state_dim
|
| 1430 |
+
|
| 1431 |
+
for hidden_dim in hidden_dims:
|
| 1432 |
+
layers.append(LinearLayer(prev_dim, hidden_dim, init_method='he'))
|
| 1433 |
+
layers.append(ReLU())
|
| 1434 |
+
prev_dim = hidden_dim
|
| 1435 |
+
|
| 1436 |
+
self.feature_network = Sequential(layers)
|
| 1437 |
+
|
| 1438 |
+
self.value_stream = Sequential([
|
| 1439 |
+
LinearLayer(prev_dim, 64, init_method='he'),
|
| 1440 |
+
ReLU(),
|
| 1441 |
+
LinearLayer(64, 1, init_method='xavier')
|
| 1442 |
+
])
|
| 1443 |
+
|
| 1444 |
+
self.advantage_stream = Sequential([
|
| 1445 |
+
LinearLayer(prev_dim, 64, init_method='he'),
|
| 1446 |
+
ReLU(),
|
| 1447 |
+
LinearLayer(64, action_dim, init_method='xavier')
|
| 1448 |
+
])
|
| 1449 |
+
|
| 1450 |
+
def forward(self, state: np.ndarray) -> np.ndarray:
|
| 1451 |
+
if state.ndim == 1:
|
| 1452 |
+
state = state.reshape(1, -1)
|
| 1453 |
+
|
| 1454 |
+
features = self.feature_network.forward(state)
|
| 1455 |
+
|
| 1456 |
+
value = self.value_stream.forward(features)
|
| 1457 |
+
advantage = self.advantage_stream.forward(features)
|
| 1458 |
+
|
| 1459 |
+
q_values = value + (advantage - np.mean(advantage, axis=1, keepdims=True))
|
| 1460 |
+
|
| 1461 |
+
return q_values
|
| 1462 |
+
|
| 1463 |
+
def backward(self, grad: np.ndarray) -> np.ndarray:
|
| 1464 |
+
batch_size = grad.shape[0]
|
| 1465 |
+
|
| 1466 |
+
grad_value = np.sum(grad, axis=1, keepdims=True)
|
| 1467 |
+
grad_advantage = grad - np.mean(grad, axis=1, keepdims=True)
|
| 1468 |
+
|
| 1469 |
+
grad_features_v = self.value_stream.backward(grad_value)
|
| 1470 |
+
grad_features_a = self.advantage_stream.backward(grad_advantage)
|
| 1471 |
+
|
| 1472 |
+
grad_features = grad_features_v + grad_features_a
|
| 1473 |
+
|
| 1474 |
+
return self.feature_network.backward(grad_features)
|
| 1475 |
+
|
| 1476 |
+
def get_params(self) -> List[Tuple[np.ndarray, np.ndarray]]:
|
| 1477 |
+
params = []
|
| 1478 |
+
params.extend(self.feature_network.get_params())
|
| 1479 |
+
params.extend(self.value_stream.get_params())
|
| 1480 |
+
params.extend(self.advantage_stream.get_params())
|
| 1481 |
+
return params
|
| 1482 |
+
|
| 1483 |
+
def zero_grad(self):
|
| 1484 |
+
self.feature_network.zero_grad()
|
| 1485 |
+
self.value_stream.zero_grad()
|
| 1486 |
+
self.advantage_stream.zero_grad()
|
| 1487 |
+
|
| 1488 |
+
def copy_from(self, other: 'DuelingDQNNetwork'):
|
| 1489 |
+
for (p1, _), (p2, _) in zip(self.get_params(), other.get_params()):
|
| 1490 |
+
p1[:] = p2
|
| 1491 |
+
|
| 1492 |
+
def soft_update(self, other: 'DuelingDQNNetwork', tau: float):
|
| 1493 |
+
for (p1, _), (p2, _) in zip(self.get_params(), other.get_params()):
|
| 1494 |
+
p1[:] = tau * p2 + (1 - tau) * p1
|
| 1495 |
+
|
| 1496 |
+
def __call__(self, state: np.ndarray) -> np.ndarray:
|
| 1497 |
+
return self.forward(state)
|
| 1498 |
+
|
| 1499 |
+
|
| 1500 |
+
class DQNAgent:
|
| 1501 |
+
"""Complete DQN Agent with vanilla, double, and dueling variants."""
|
| 1502 |
+
|
| 1503 |
+
def __init__(
|
| 1504 |
+
self,
|
| 1505 |
+
state_dim: int,
|
| 1506 |
+
action_dim: int,
|
| 1507 |
+
hidden_dims: List[int] = None,
|
| 1508 |
+
lr: float = 0.001,
|
| 1509 |
+
gamma: float = 0.99,
|
| 1510 |
+
buffer_size: int = 100000,
|
| 1511 |
+
batch_size: int = 64,
|
| 1512 |
+
target_update_freq: int = 100,
|
| 1513 |
+
tau: float = 0.005,
|
| 1514 |
+
use_double: bool = True,
|
| 1515 |
+
use_dueling: bool = False,
|
| 1516 |
+
use_per: bool = False,
|
| 1517 |
+
n_steps: int = 1,
|
| 1518 |
+
epsilon_start: float = 1.0,
|
| 1519 |
+
epsilon_end: float = 0.01,
|
| 1520 |
+
epsilon_decay: float = 0.995,
|
| 1521 |
+
seed: Optional[int] = None
|
| 1522 |
+
):
|
| 1523 |
+
self.state_dim = state_dim
|
| 1524 |
+
self.action_dim = action_dim
|
| 1525 |
+
self.gamma = gamma
|
| 1526 |
+
self.batch_size = batch_size
|
| 1527 |
+
self.target_update_freq = target_update_freq
|
| 1528 |
+
self.tau = tau
|
| 1529 |
+
self.use_double = use_double
|
| 1530 |
+
self.use_dueling = use_dueling
|
| 1531 |
+
self.use_per = use_per
|
| 1532 |
+
self.n_steps = n_steps
|
| 1533 |
+
self.gamma_n = gamma ** n_steps
|
| 1534 |
+
|
| 1535 |
+
if use_dueling:
|
| 1536 |
+
self.q_network = DuelingDQNNetwork(state_dim, action_dim, hidden_dims)
|
| 1537 |
+
self.target_network = DuelingDQNNetwork(state_dim, action_dim, hidden_dims)
|
| 1538 |
+
else:
|
| 1539 |
+
self.q_network = DQNNetwork(state_dim, action_dim, hidden_dims)
|
| 1540 |
+
self.target_network = DQNNetwork(state_dim, action_dim, hidden_dims)
|
| 1541 |
+
|
| 1542 |
+
self.target_network.copy_from(self.q_network)
|
| 1543 |
+
|
| 1544 |
+
self.optimizer = Adam(self.q_network.get_params(), lr=lr)
|
| 1545 |
+
self.loss_fn = HuberLoss()
|
| 1546 |
+
|
| 1547 |
+
if use_per:
|
| 1548 |
+
self.buffer = PrioritizedReplayBuffer(buffer_size, state_dim, seed=seed)
|
| 1549 |
+
elif n_steps > 1:
|
| 1550 |
+
self.buffer = NStepReplayBuffer(buffer_size, state_dim, n_steps, gamma, seed)
|
| 1551 |
+
else:
|
| 1552 |
+
self.buffer = ReplayBuffer(buffer_size, state_dim, seed)
|
| 1553 |
+
|
| 1554 |
+
self.exploration = EpsilonGreedy(
|
| 1555 |
+
epsilon_start, epsilon_end, epsilon_decay,
|
| 1556 |
+
decay_type='exponential', seed=seed
|
| 1557 |
+
)
|
| 1558 |
+
|
| 1559 |
+
self.train_steps = 0
|
| 1560 |
+
self.episodes = 0
|
| 1561 |
+
|
| 1562 |
+
self.metrics = {
|
| 1563 |
+
'losses': [],
|
| 1564 |
+
'q_values': [],
|
| 1565 |
+
'episode_rewards': [],
|
| 1566 |
+
'episode_lengths': [],
|
| 1567 |
+
'epsilon': []
|
| 1568 |
+
}
|
| 1569 |
+
|
| 1570 |
+
def select_action(self, state: np.ndarray, training: bool = True) -> int:
|
| 1571 |
+
q_values = self.q_network(state).flatten()
|
| 1572 |
+
|
| 1573 |
+
if training:
|
| 1574 |
+
action = self.exploration.get_action(q_values)
|
| 1575 |
+
else:
|
| 1576 |
+
action = int(np.argmax(q_values))
|
| 1577 |
+
|
| 1578 |
+
return action
|
| 1579 |
+
|
| 1580 |
+
def store_transition(
|
| 1581 |
+
self,
|
| 1582 |
+
state: np.ndarray,
|
| 1583 |
+
action: int,
|
| 1584 |
+
reward: float,
|
| 1585 |
+
next_state: np.ndarray,
|
| 1586 |
+
done: bool
|
| 1587 |
+
):
|
| 1588 |
+
self.buffer.push(state, action, reward, next_state, done)
|
| 1589 |
+
|
| 1590 |
+
def train_step(self) -> Optional[float]:
|
| 1591 |
+
if not self.buffer.is_ready(self.batch_size):
|
| 1592 |
+
return None
|
| 1593 |
+
|
| 1594 |
+
if self.use_per:
|
| 1595 |
+
states, actions, rewards, next_states, dones, indices, weights = self.buffer.sample(self.batch_size)
|
| 1596 |
+
else:
|
| 1597 |
+
states, actions, rewards, next_states, dones = self.buffer.sample(self.batch_size)
|
| 1598 |
+
weights = np.ones(self.batch_size, dtype=np.float32)
|
| 1599 |
+
|
| 1600 |
+
# Forward pass for current states
|
| 1601 |
+
current_q_all = self.q_network(states)
|
| 1602 |
+
current_q = current_q_all[np.arange(self.batch_size), actions]
|
| 1603 |
+
|
| 1604 |
+
# IMPORTANT: Save input caches before any other forward passes
|
| 1605 |
+
# because Double DQN will overwrite them
|
| 1606 |
+
saved_caches = []
|
| 1607 |
+
for layer in self.q_network.network.layers:
|
| 1608 |
+
if hasattr(layer, '_input_cache') and layer._input_cache is not None:
|
| 1609 |
+
saved_caches.append((layer, layer._input_cache.copy()))
|
| 1610 |
+
if hasattr(layer, '_mask') and layer._mask is not None:
|
| 1611 |
+
saved_caches.append((layer, '_mask', layer._mask.copy()))
|
| 1612 |
+
if hasattr(layer, '_output') and layer._output is not None:
|
| 1613 |
+
saved_caches.append((layer, '_output', layer._output.copy()))
|
| 1614 |
+
|
| 1615 |
+
with np.errstate(all='ignore'):
|
| 1616 |
+
next_q_target = self.target_network(next_states)
|
| 1617 |
+
|
| 1618 |
+
if self.use_double:
|
| 1619 |
+
next_q_online = self.q_network(next_states)
|
| 1620 |
+
best_actions = np.argmax(next_q_online, axis=1)
|
| 1621 |
+
next_q_max = next_q_target[np.arange(self.batch_size), best_actions]
|
| 1622 |
+
else:
|
| 1623 |
+
next_q_max = np.max(next_q_target, axis=1)
|
| 1624 |
+
|
| 1625 |
+
# Restore caches for backward pass
|
| 1626 |
+
for item in saved_caches:
|
| 1627 |
+
if len(item) == 2:
|
| 1628 |
+
layer, cache = item
|
| 1629 |
+
layer._input_cache = cache
|
| 1630 |
+
else:
|
| 1631 |
+
layer, attr, cache = item
|
| 1632 |
+
setattr(layer, attr, cache)
|
| 1633 |
+
|
| 1634 |
+
gamma = self.gamma_n if self.n_steps > 1 else self.gamma
|
| 1635 |
+
target_q = rewards + gamma * next_q_max * (1 - dones)
|
| 1636 |
+
|
| 1637 |
+
td_errors = current_q - target_q
|
| 1638 |
+
|
| 1639 |
+
if self.use_per:
|
| 1640 |
+
self.buffer.update_priorities(indices, td_errors)
|
| 1641 |
+
|
| 1642 |
+
weighted_td_errors = td_errors * weights
|
| 1643 |
+
loss = np.mean(weighted_td_errors ** 2)
|
| 1644 |
+
|
| 1645 |
+
self.q_network.zero_grad()
|
| 1646 |
+
|
| 1647 |
+
grad = np.zeros_like(current_q_all)
|
| 1648 |
+
grad[np.arange(self.batch_size), actions] = 2 * weighted_td_errors / self.batch_size
|
| 1649 |
+
|
| 1650 |
+
self.q_network.backward(grad)
|
| 1651 |
+
|
| 1652 |
+
self.optimizer.step()
|
| 1653 |
+
|
| 1654 |
+
self.train_steps += 1
|
| 1655 |
+
|
| 1656 |
+
if self.train_steps % self.target_update_freq == 0:
|
| 1657 |
+
if self.tau < 1.0:
|
| 1658 |
+
self.target_network.soft_update(self.q_network, self.tau)
|
| 1659 |
+
else:
|
| 1660 |
+
self.target_network.copy_from(self.q_network)
|
| 1661 |
+
|
| 1662 |
+
self.exploration.decay()
|
| 1663 |
+
|
| 1664 |
+
self.metrics['losses'].append(loss)
|
| 1665 |
+
self.metrics['q_values'].append(float(np.mean(current_q)))
|
| 1666 |
+
self.metrics['epsilon'].append(self.exploration.epsilon)
|
| 1667 |
+
|
| 1668 |
+
return loss
|
| 1669 |
+
|
| 1670 |
+
def end_episode(self, total_reward: float, episode_length: int):
|
| 1671 |
+
self.episodes += 1
|
| 1672 |
+
self.metrics['episode_rewards'].append(total_reward)
|
| 1673 |
+
self.metrics['episode_lengths'].append(episode_length)
|
| 1674 |
+
|
| 1675 |
+
def save(self, filepath: str):
|
| 1676 |
+
state = {
|
| 1677 |
+
'q_network_params': [(p.copy(), g.copy()) for p, g in self.q_network.get_params()],
|
| 1678 |
+
'target_network_params': [(p.copy(), g.copy()) for p, g in self.target_network.get_params()],
|
| 1679 |
+
'train_steps': self.train_steps,
|
| 1680 |
+
'episodes': self.episodes,
|
| 1681 |
+
'epsilon': self.exploration.epsilon,
|
| 1682 |
+
'metrics': self.metrics,
|
| 1683 |
+
'config': {
|
| 1684 |
+
'state_dim': self.state_dim,
|
| 1685 |
+
'action_dim': self.action_dim,
|
| 1686 |
+
'gamma': self.gamma,
|
| 1687 |
+
'batch_size': self.batch_size,
|
| 1688 |
+
'use_double': self.use_double,
|
| 1689 |
+
'use_dueling': self.use_dueling,
|
| 1690 |
+
'use_per': self.use_per,
|
| 1691 |
+
'n_steps': self.n_steps
|
| 1692 |
+
}
|
| 1693 |
+
}
|
| 1694 |
+
|
| 1695 |
+
with open(filepath, 'wb') as f:
|
| 1696 |
+
pickle.dump(state, f)
|
| 1697 |
+
|
| 1698 |
+
def load(self, filepath: str):
|
| 1699 |
+
with open(filepath, 'rb') as f:
|
| 1700 |
+
state = pickle.load(f)
|
| 1701 |
+
|
| 1702 |
+
for (p, g), (saved_p, saved_g) in zip(self.q_network.get_params(), state['q_network_params']):
|
| 1703 |
+
p[:] = saved_p
|
| 1704 |
+
g[:] = saved_g
|
| 1705 |
+
|
| 1706 |
+
for (p, g), (saved_p, saved_g) in zip(self.target_network.get_params(), state['target_network_params']):
|
| 1707 |
+
p[:] = saved_p
|
| 1708 |
+
g[:] = saved_g
|
| 1709 |
+
|
| 1710 |
+
self.train_steps = state['train_steps']
|
| 1711 |
+
self.episodes = state['episodes']
|
| 1712 |
+
self.exploration.epsilon = state['epsilon']
|
| 1713 |
+
self.metrics = state['metrics']
|
| 1714 |
+
|
| 1715 |
+
|
| 1716 |
+
# =============================================================================
|
| 1717 |
+
# SECTION 6: TRAINING LOOP (Lines 1600-1800)
|
| 1718 |
+
# =============================================================================
|
| 1719 |
+
|
| 1720 |
+
class Trainer:
|
| 1721 |
+
"""Complete training loop with logging and checkpointing."""
|
| 1722 |
+
|
| 1723 |
+
def __init__(
|
| 1724 |
+
self,
|
| 1725 |
+
agent: DQNAgent,
|
| 1726 |
+
env,
|
| 1727 |
+
eval_env=None,
|
| 1728 |
+
log_interval: int = 100,
|
| 1729 |
+
eval_interval: int = 1000,
|
| 1730 |
+
eval_episodes: int = 10,
|
| 1731 |
+
save_interval: int = 5000,
|
| 1732 |
+
checkpoint_dir: str = './checkpoints',
|
| 1733 |
+
early_stop_reward: float = None,
|
| 1734 |
+
early_stop_window: int = 100
|
| 1735 |
+
):
|
| 1736 |
+
self.agent = agent
|
| 1737 |
+
self.env = env
|
| 1738 |
+
self.eval_env = eval_env if eval_env is not None else env
|
| 1739 |
+
self.log_interval = log_interval
|
| 1740 |
+
self.eval_interval = eval_interval
|
| 1741 |
+
self.eval_episodes = eval_episodes
|
| 1742 |
+
self.save_interval = save_interval
|
| 1743 |
+
self.checkpoint_dir = checkpoint_dir
|
| 1744 |
+
self.early_stop_reward = early_stop_reward
|
| 1745 |
+
self.early_stop_window = early_stop_window
|
| 1746 |
+
|
| 1747 |
+
os.makedirs(checkpoint_dir, exist_ok=True)
|
| 1748 |
+
|
| 1749 |
+
self.training_history = {
|
| 1750 |
+
'episode': [],
|
| 1751 |
+
'reward': [],
|
| 1752 |
+
'length': [],
|
| 1753 |
+
'loss': [],
|
| 1754 |
+
'epsilon': [],
|
| 1755 |
+
'eval_reward': [],
|
| 1756 |
+
'eval_length': []
|
| 1757 |
+
}
|
| 1758 |
+
|
| 1759 |
+
def train(self, num_episodes: int) -> Dict:
|
| 1760 |
+
start_time = time.time()
|
| 1761 |
+
total_steps = 0
|
| 1762 |
+
best_eval_reward = float('-inf')
|
| 1763 |
+
|
| 1764 |
+
recent_rewards = deque(maxlen=self.early_stop_window)
|
| 1765 |
+
|
| 1766 |
+
for episode in range(num_episodes):
|
| 1767 |
+
state = self.env.reset()
|
| 1768 |
+
episode_reward = 0.0
|
| 1769 |
+
episode_length = 0
|
| 1770 |
+
episode_losses = []
|
| 1771 |
+
done = False
|
| 1772 |
+
|
| 1773 |
+
while not done:
|
| 1774 |
+
action = self.agent.select_action(state, training=True)
|
| 1775 |
+
next_state, reward, done, info = self.env.step(action)
|
| 1776 |
+
|
| 1777 |
+
self.agent.store_transition(state, action, reward, next_state, done)
|
| 1778 |
+
|
| 1779 |
+
loss = self.agent.train_step()
|
| 1780 |
+
if loss is not None:
|
| 1781 |
+
episode_losses.append(loss)
|
| 1782 |
+
|
| 1783 |
+
state = next_state
|
| 1784 |
+
episode_reward += reward
|
| 1785 |
+
episode_length += 1
|
| 1786 |
+
total_steps += 1
|
| 1787 |
+
|
| 1788 |
+
self.agent.end_episode(episode_reward, episode_length)
|
| 1789 |
+
recent_rewards.append(episode_reward)
|
| 1790 |
+
|
| 1791 |
+
self.training_history['episode'].append(episode)
|
| 1792 |
+
self.training_history['reward'].append(episode_reward)
|
| 1793 |
+
self.training_history['length'].append(episode_length)
|
| 1794 |
+
self.training_history['loss'].append(np.mean(episode_losses) if episode_losses else 0)
|
| 1795 |
+
self.training_history['epsilon'].append(self.agent.exploration.epsilon)
|
| 1796 |
+
|
| 1797 |
+
if episode % self.log_interval == 0:
|
| 1798 |
+
avg_reward = np.mean(list(recent_rewards))
|
| 1799 |
+
avg_loss = np.mean(episode_losses) if episode_losses else 0
|
| 1800 |
+
elapsed = time.time() - start_time
|
| 1801 |
+
|
| 1802 |
+
print(f"Episode {episode:5d} | "
|
| 1803 |
+
f"Reward: {episode_reward:7.2f} | "
|
| 1804 |
+
f"Avg100: {avg_reward:7.2f} | "
|
| 1805 |
+
f"Loss: {avg_loss:.4f} | "
|
| 1806 |
+
f"Eps: {self.agent.exploration.epsilon:.3f} | "
|
| 1807 |
+
f"Steps: {total_steps:7d} | "
|
| 1808 |
+
f"Time: {elapsed:.1f}s")
|
| 1809 |
+
|
| 1810 |
+
if episode % self.eval_interval == 0 and episode > 0:
|
| 1811 |
+
eval_reward, eval_length = self.evaluate()
|
| 1812 |
+
self.training_history['eval_reward'].append(eval_reward)
|
| 1813 |
+
self.training_history['eval_length'].append(eval_length)
|
| 1814 |
+
|
| 1815 |
+
print(f" [EVAL] Avg Reward: {eval_reward:.2f} | Avg Length: {eval_length:.1f}")
|
| 1816 |
+
|
| 1817 |
+
if eval_reward > best_eval_reward:
|
| 1818 |
+
best_eval_reward = eval_reward
|
| 1819 |
+
self.agent.save(os.path.join(self.checkpoint_dir, 'best_model.pkl'))
|
| 1820 |
+
|
| 1821 |
+
if episode % self.save_interval == 0 and episode > 0:
|
| 1822 |
+
self.agent.save(os.path.join(self.checkpoint_dir, f'checkpoint_{episode}.pkl'))
|
| 1823 |
+
|
| 1824 |
+
if self.early_stop_reward is not None:
|
| 1825 |
+
if len(recent_rewards) >= self.early_stop_window:
|
| 1826 |
+
if np.mean(recent_rewards) >= self.early_stop_reward:
|
| 1827 |
+
print(f"Early stopping: reached target reward {self.early_stop_reward}")
|
| 1828 |
+
break
|
| 1829 |
+
|
| 1830 |
+
self.agent.save(os.path.join(self.checkpoint_dir, 'final_model.pkl'))
|
| 1831 |
+
|
| 1832 |
+
return self.training_history
|
| 1833 |
+
|
| 1834 |
+
def evaluate(self) -> Tuple[float, float]:
|
| 1835 |
+
total_rewards = []
|
| 1836 |
+
total_lengths = []
|
| 1837 |
+
|
| 1838 |
+
for _ in range(self.eval_episodes):
|
| 1839 |
+
state = self.eval_env.reset()
|
| 1840 |
+
episode_reward = 0.0
|
| 1841 |
+
episode_length = 0
|
| 1842 |
+
done = False
|
| 1843 |
+
|
| 1844 |
+
while not done:
|
| 1845 |
+
action = self.agent.select_action(state, training=False)
|
| 1846 |
+
next_state, reward, done, info = self.eval_env.step(action)
|
| 1847 |
+
|
| 1848 |
+
state = next_state
|
| 1849 |
+
episode_reward += reward
|
| 1850 |
+
episode_length += 1
|
| 1851 |
+
|
| 1852 |
+
total_rewards.append(episode_reward)
|
| 1853 |
+
total_lengths.append(episode_length)
|
| 1854 |
+
|
| 1855 |
+
return np.mean(total_rewards), np.mean(total_lengths)
|
| 1856 |
+
|
| 1857 |
+
def save_history(self, filepath: str):
|
| 1858 |
+
with open(filepath, 'w') as f:
|
| 1859 |
+
json.dump(self.training_history, f, indent=2)
|
| 1860 |
+
|
| 1861 |
+
def load_history(self, filepath: str):
|
| 1862 |
+
with open(filepath, 'r') as f:
|
| 1863 |
+
self.training_history = json.load(f)
|
| 1864 |
+
|
| 1865 |
+
|
| 1866 |
+
# =============================================================================
|
| 1867 |
+
# SECTION 7: VISUALIZATION (Lines 1800-1950)
|
| 1868 |
+
# =============================================================================
|
| 1869 |
+
|
| 1870 |
+
class Visualizer:
|
| 1871 |
+
"""Visualization utilities for training metrics and agent behavior."""
|
| 1872 |
+
|
| 1873 |
+
def __init__(self, save_dir: str = './plots'):
|
| 1874 |
+
self.save_dir = save_dir
|
| 1875 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 1876 |
+
|
| 1877 |
+
def plot_training_curves(
|
| 1878 |
+
self,
|
| 1879 |
+
history: Dict,
|
| 1880 |
+
filename: str = 'training_curves.txt'
|
| 1881 |
+
) -> str:
|
| 1882 |
+
output_lines = []
|
| 1883 |
+
output_lines.append("=" * 80)
|
| 1884 |
+
output_lines.append("TRAINING CURVES (ASCII)")
|
| 1885 |
+
output_lines.append("=" * 80)
|
| 1886 |
+
|
| 1887 |
+
output_lines.append("\nREWARD OVER EPISODES:")
|
| 1888 |
+
output_lines.append("-" * 60)
|
| 1889 |
+
rewards = history.get('reward', [])
|
| 1890 |
+
if rewards:
|
| 1891 |
+
self._ascii_plot(rewards, output_lines, width=60, height=15)
|
| 1892 |
+
|
| 1893 |
+
output_lines.append("\nLOSS OVER EPISODES:")
|
| 1894 |
+
output_lines.append("-" * 60)
|
| 1895 |
+
losses = history.get('loss', [])
|
| 1896 |
+
if losses:
|
| 1897 |
+
self._ascii_plot(losses, output_lines, width=60, height=15)
|
| 1898 |
+
|
| 1899 |
+
output_lines.append("\nEPSILON DECAY:")
|
| 1900 |
+
output_lines.append("-" * 60)
|
| 1901 |
+
epsilon = history.get('epsilon', [])
|
| 1902 |
+
if epsilon:
|
| 1903 |
+
self._ascii_plot(epsilon, output_lines, width=60, height=10)
|
| 1904 |
+
|
| 1905 |
+
output_lines.append("\nSTATISTICS:")
|
| 1906 |
+
output_lines.append("-" * 60)
|
| 1907 |
+
if rewards:
|
| 1908 |
+
output_lines.append(f" Total Episodes: {len(rewards)}")
|
| 1909 |
+
output_lines.append(f" Max Reward: {max(rewards):.2f}")
|
| 1910 |
+
output_lines.append(f" Min Reward: {min(rewards):.2f}")
|
| 1911 |
+
output_lines.append(f" Mean Reward: {np.mean(rewards):.2f}")
|
| 1912 |
+
output_lines.append(f" Std Reward: {np.std(rewards):.2f}")
|
| 1913 |
+
output_lines.append(f" Final Avg (last 100): {np.mean(rewards[-100:]):.2f}")
|
| 1914 |
+
|
| 1915 |
+
output = '\n'.join(output_lines)
|
| 1916 |
+
|
| 1917 |
+
filepath = os.path.join(self.save_dir, filename)
|
| 1918 |
+
with open(filepath, 'w') as f:
|
| 1919 |
+
f.write(output)
|
| 1920 |
+
|
| 1921 |
+
return output
|
| 1922 |
+
|
| 1923 |
+
def _ascii_plot(
|
| 1924 |
+
self,
|
| 1925 |
+
data: List[float],
|
| 1926 |
+
output_lines: List[str],
|
| 1927 |
+
width: int = 60,
|
| 1928 |
+
height: int = 15
|
| 1929 |
+
):
|
| 1930 |
+
if not data:
|
| 1931 |
+
output_lines.append(" No data to plot")
|
| 1932 |
+
return
|
| 1933 |
+
|
| 1934 |
+
data = np.array(data)
|
| 1935 |
+
|
| 1936 |
+
if len(data) > width:
|
| 1937 |
+
step = len(data) // width
|
| 1938 |
+
data = [np.mean(data[i:i+step]) for i in range(0, len(data), step)][:width]
|
| 1939 |
+
data = np.array(data)
|
| 1940 |
+
|
| 1941 |
+
min_val = np.min(data)
|
| 1942 |
+
max_val = np.max(data)
|
| 1943 |
+
|
| 1944 |
+
if max_val == min_val:
|
| 1945 |
+
max_val = min_val + 1
|
| 1946 |
+
|
| 1947 |
+
normalized = ((data - min_val) / (max_val - min_val) * (height - 1)).astype(int)
|
| 1948 |
+
|
| 1949 |
+
grid = [[' ' for _ in range(len(data))] for _ in range(height)]
|
| 1950 |
+
|
| 1951 |
+
for x, y in enumerate(normalized):
|
| 1952 |
+
grid[height - 1 - y][x] = '*'
|
| 1953 |
+
|
| 1954 |
+
output_lines.append(f" {max_val:10.3f} |")
|
| 1955 |
+
for row in grid:
|
| 1956 |
+
output_lines.append(f" |{''.join(row)}")
|
| 1957 |
+
output_lines.append(f" {min_val:10.3f} |{'_' * len(data)}")
|
| 1958 |
+
output_lines.append(f" 0{' ' * (len(data) - 6)}{len(data)}")
|
| 1959 |
+
|
| 1960 |
+
def plot_q_values_heatmap(
|
| 1961 |
+
self,
|
| 1962 |
+
agent: DQNAgent,
|
| 1963 |
+
env,
|
| 1964 |
+
filename: str = 'q_values.txt'
|
| 1965 |
+
) -> str:
|
| 1966 |
+
output_lines = []
|
| 1967 |
+
output_lines.append("=" * 80)
|
| 1968 |
+
output_lines.append("Q-VALUES HEATMAP")
|
| 1969 |
+
output_lines.append("=" * 80)
|
| 1970 |
+
|
| 1971 |
+
if not hasattr(env, 'height') or not hasattr(env, 'width'):
|
| 1972 |
+
output_lines.append("Environment doesn't support grid visualization")
|
| 1973 |
+
return '\n'.join(output_lines)
|
| 1974 |
+
|
| 1975 |
+
action_names = ['UP', 'DOWN', 'LEFT', 'RIGHT']
|
| 1976 |
+
|
| 1977 |
+
for action_idx, action_name in enumerate(action_names):
|
| 1978 |
+
output_lines.append(f"\nQ-VALUES FOR ACTION: {action_name}")
|
| 1979 |
+
output_lines.append("-" * 40)
|
| 1980 |
+
|
| 1981 |
+
q_grid = np.zeros((env.height, env.width))
|
| 1982 |
+
|
| 1983 |
+
for row in range(env.height):
|
| 1984 |
+
for col in range(env.width):
|
| 1985 |
+
state = np.zeros((env.height, env.width), dtype=np.float32)
|
| 1986 |
+
state[row, col] = 4
|
| 1987 |
+
state_flat = state.flatten()
|
| 1988 |
+
|
| 1989 |
+
q_values = agent.q_network(state_flat).flatten()
|
| 1990 |
+
q_grid[row, col] = q_values[action_idx]
|
| 1991 |
+
|
| 1992 |
+
min_q = np.min(q_grid)
|
| 1993 |
+
max_q = np.max(q_grid)
|
| 1994 |
+
|
| 1995 |
+
symbols = ' ░▒▓█'
|
| 1996 |
+
|
| 1997 |
+
for row in range(env.height):
|
| 1998 |
+
line = " "
|
| 1999 |
+
for col in range(env.width):
|
| 2000 |
+
if max_q != min_q:
|
| 2001 |
+
normalized = (q_grid[row, col] - min_q) / (max_q - min_q)
|
| 2002 |
+
else:
|
| 2003 |
+
normalized = 0.5
|
| 2004 |
+
idx = min(int(normalized * (len(symbols) - 1)), len(symbols) - 1)
|
| 2005 |
+
line += symbols[idx] + ' '
|
| 2006 |
+
output_lines.append(line)
|
| 2007 |
+
|
| 2008 |
+
output_lines.append(f" Min: {min_q:.3f} | Max: {max_q:.3f}")
|
| 2009 |
+
|
| 2010 |
+
output = '\n'.join(output_lines)
|
| 2011 |
+
|
| 2012 |
+
filepath = os.path.join(self.save_dir, filename)
|
| 2013 |
+
with open(filepath, 'w') as f:
|
| 2014 |
+
f.write(output)
|
| 2015 |
+
|
| 2016 |
+
return output
|
| 2017 |
+
|
| 2018 |
+
def record_episode(
|
| 2019 |
+
self,
|
| 2020 |
+
agent: DQNAgent,
|
| 2021 |
+
env,
|
| 2022 |
+
filename: str = 'episode_recording.txt'
|
| 2023 |
+
) -> str:
|
| 2024 |
+
output_lines = []
|
| 2025 |
+
output_lines.append("=" * 80)
|
| 2026 |
+
output_lines.append("EPISODE RECORDING")
|
| 2027 |
+
output_lines.append("=" * 80)
|
| 2028 |
+
|
| 2029 |
+
state = env.reset()
|
| 2030 |
+
done = False
|
| 2031 |
+
step = 0
|
| 2032 |
+
total_reward = 0.0
|
| 2033 |
+
|
| 2034 |
+
while not done and step < 100:
|
| 2035 |
+
output_lines.append(f"\n--- Step {step} ---")
|
| 2036 |
+
|
| 2037 |
+
render = env.render(mode='string')
|
| 2038 |
+
if render:
|
| 2039 |
+
output_lines.append(render)
|
| 2040 |
+
|
| 2041 |
+
q_values = agent.q_network(state).flatten()
|
| 2042 |
+
action = int(np.argmax(q_values))
|
| 2043 |
+
|
| 2044 |
+
output_lines.append(f"Q-values: {q_values}")
|
| 2045 |
+
output_lines.append(f"Action: {env.action_names[action] if hasattr(env, 'action_names') else action}")
|
| 2046 |
+
|
| 2047 |
+
next_state, reward, done, info = env.step(action)
|
| 2048 |
+
total_reward += reward
|
| 2049 |
+
|
| 2050 |
+
output_lines.append(f"Reward: {reward:.2f} | Total: {total_reward:.2f}")
|
| 2051 |
+
|
| 2052 |
+
state = next_state
|
| 2053 |
+
step += 1
|
| 2054 |
+
|
| 2055 |
+
output_lines.append(f"\n{'=' * 40}")
|
| 2056 |
+
output_lines.append(f"EPISODE COMPLETE")
|
| 2057 |
+
output_lines.append(f"Total Steps: {step}")
|
| 2058 |
+
output_lines.append(f"Total Reward: {total_reward:.2f}")
|
| 2059 |
+
output_lines.append(f"Final Info: {info}")
|
| 2060 |
+
|
| 2061 |
+
output = '\n'.join(output_lines)
|
| 2062 |
+
|
| 2063 |
+
filepath = os.path.join(self.save_dir, filename)
|
| 2064 |
+
with open(filepath, 'w') as f:
|
| 2065 |
+
f.write(output)
|
| 2066 |
+
|
| 2067 |
+
return output
|
| 2068 |
+
|
| 2069 |
+
|
| 2070 |
+
# =============================================================================
|
| 2071 |
+
# SECTION 8: HYPERPARAMETER TUNING (Lines 1950-2050)
|
| 2072 |
+
# =============================================================================
|
| 2073 |
+
|
| 2074 |
+
class HyperparameterSearch:
|
| 2075 |
+
"""Grid and random search for hyperparameter tuning."""
|
| 2076 |
+
|
| 2077 |
+
def __init__(
|
| 2078 |
+
self,
|
| 2079 |
+
env_class,
|
| 2080 |
+
env_kwargs: Dict,
|
| 2081 |
+
param_grid: Dict,
|
| 2082 |
+
n_episodes: int = 100,
|
| 2083 |
+
eval_episodes: int = 10,
|
| 2084 |
+
n_trials: int = 10,
|
| 2085 |
+
seed: int = 42
|
| 2086 |
+
):
|
| 2087 |
+
self.env_class = env_class
|
| 2088 |
+
self.env_kwargs = env_kwargs
|
| 2089 |
+
self.param_grid = param_grid
|
| 2090 |
+
self.n_episodes = n_episodes
|
| 2091 |
+
self.eval_episodes = eval_episodes
|
| 2092 |
+
self.n_trials = n_trials
|
| 2093 |
+
self.seed = seed
|
| 2094 |
+
|
| 2095 |
+
self.results = []
|
| 2096 |
+
self.best_params = None
|
| 2097 |
+
self.best_score = float('-inf')
|
| 2098 |
+
|
| 2099 |
+
def _sample_params(self) -> Dict:
|
| 2100 |
+
params = {}
|
| 2101 |
+
for key, values in self.param_grid.items():
|
| 2102 |
+
if isinstance(values, list):
|
| 2103 |
+
params[key] = np.random.choice(values)
|
| 2104 |
+
elif isinstance(values, tuple) and len(values) == 2:
|
| 2105 |
+
low, high = values
|
| 2106 |
+
if isinstance(low, float):
|
| 2107 |
+
params[key] = np.random.uniform(low, high)
|
| 2108 |
+
else:
|
| 2109 |
+
params[key] = np.random.randint(low, high + 1)
|
| 2110 |
+
else:
|
| 2111 |
+
params[key] = values
|
| 2112 |
+
return params
|
| 2113 |
+
|
| 2114 |
+
def run_trial(self, params: Dict) -> float:
|
| 2115 |
+
np.random.seed(self.seed)
|
| 2116 |
+
|
| 2117 |
+
env = self.env_class(**self.env_kwargs)
|
| 2118 |
+
eval_env = self.env_class(**self.env_kwargs)
|
| 2119 |
+
|
| 2120 |
+
state_dim = env.n_states if hasattr(env, 'n_states') else env.state_dim
|
| 2121 |
+
action_dim = env.n_actions
|
| 2122 |
+
|
| 2123 |
+
agent = DQNAgent(
|
| 2124 |
+
state_dim=state_dim,
|
| 2125 |
+
action_dim=action_dim,
|
| 2126 |
+
hidden_dims=params.get('hidden_dims', [64, 64]),
|
| 2127 |
+
lr=params.get('lr', 0.001),
|
| 2128 |
+
gamma=params.get('gamma', 0.99),
|
| 2129 |
+
buffer_size=params.get('buffer_size', 10000),
|
| 2130 |
+
batch_size=params.get('batch_size', 32),
|
| 2131 |
+
target_update_freq=params.get('target_update_freq', 100),
|
| 2132 |
+
use_double=params.get('use_double', True),
|
| 2133 |
+
use_dueling=params.get('use_dueling', False),
|
| 2134 |
+
epsilon_start=params.get('epsilon_start', 1.0),
|
| 2135 |
+
epsilon_end=params.get('epsilon_end', 0.01),
|
| 2136 |
+
epsilon_decay=params.get('epsilon_decay', 0.995),
|
| 2137 |
+
seed=self.seed
|
| 2138 |
+
)
|
| 2139 |
+
|
| 2140 |
+
trainer = Trainer(
|
| 2141 |
+
agent, env, eval_env,
|
| 2142 |
+
log_interval=self.n_episodes + 1,
|
| 2143 |
+
eval_interval=self.n_episodes + 1,
|
| 2144 |
+
checkpoint_dir='/tmp/hp_search'
|
| 2145 |
+
)
|
| 2146 |
+
|
| 2147 |
+
trainer.train(self.n_episodes)
|
| 2148 |
+
|
| 2149 |
+
eval_reward, _ = trainer.evaluate()
|
| 2150 |
+
|
| 2151 |
+
return eval_reward
|
| 2152 |
+
|
| 2153 |
+
def search(self, method: str = 'random') -> Dict:
|
| 2154 |
+
print(f"Starting hyperparameter search ({method})")
|
| 2155 |
+
print("=" * 60)
|
| 2156 |
+
|
| 2157 |
+
for trial in range(self.n_trials):
|
| 2158 |
+
params = self._sample_params()
|
| 2159 |
+
|
| 2160 |
+
print(f"\nTrial {trial + 1}/{self.n_trials}")
|
| 2161 |
+
print(f"Params: {params}")
|
| 2162 |
+
|
| 2163 |
+
try:
|
| 2164 |
+
score = self.run_trial(params)
|
| 2165 |
+
|
| 2166 |
+
self.results.append({
|
| 2167 |
+
'params': params,
|
| 2168 |
+
'score': score
|
| 2169 |
+
})
|
| 2170 |
+
|
| 2171 |
+
print(f"Score: {score:.2f}")
|
| 2172 |
+
|
| 2173 |
+
if score > self.best_score:
|
| 2174 |
+
self.best_score = score
|
| 2175 |
+
self.best_params = params.copy()
|
| 2176 |
+
print(f" ** New best! **")
|
| 2177 |
+
|
| 2178 |
+
except Exception as e:
|
| 2179 |
+
print(f"Trial failed: {e}")
|
| 2180 |
+
self.results.append({
|
| 2181 |
+
'params': params,
|
| 2182 |
+
'score': float('-inf'),
|
| 2183 |
+
'error': str(e)
|
| 2184 |
+
})
|
| 2185 |
+
|
| 2186 |
+
print("\n" + "=" * 60)
|
| 2187 |
+
print("SEARCH COMPLETE")
|
| 2188 |
+
print(f"Best Score: {self.best_score:.2f}")
|
| 2189 |
+
print(f"Best Params: {self.best_params}")
|
| 2190 |
+
|
| 2191 |
+
return {
|
| 2192 |
+
'best_params': self.best_params,
|
| 2193 |
+
'best_score': self.best_score,
|
| 2194 |
+
'all_results': self.results
|
| 2195 |
+
}
|
| 2196 |
+
|
| 2197 |
+
|
| 2198 |
+
# =============================================================================
|
| 2199 |
+
# SECTION 9: MAIN ENTRY POINT (Lines 2050-2100)
|
| 2200 |
+
# =============================================================================
|
| 2201 |
+
|
| 2202 |
+
def create_default_config() -> Dict:
|
| 2203 |
+
return {
|
| 2204 |
+
'env': {
|
| 2205 |
+
'type': 'gridworld',
|
| 2206 |
+
'width': 4,
|
| 2207 |
+
'height': 4,
|
| 2208 |
+
'mode': 'static',
|
| 2209 |
+
'max_steps': 50
|
| 2210 |
+
},
|
| 2211 |
+
'agent': {
|
| 2212 |
+
'hidden_dims': [150, 100],
|
| 2213 |
+
'lr': 0.001,
|
| 2214 |
+
'gamma': 0.9,
|
| 2215 |
+
'buffer_size': 1000,
|
| 2216 |
+
'batch_size': 200,
|
| 2217 |
+
'target_update_freq': 500,
|
| 2218 |
+
'tau': 1.0,
|
| 2219 |
+
'use_double': True,
|
| 2220 |
+
'use_dueling': False,
|
| 2221 |
+
'use_per': False,
|
| 2222 |
+
'n_steps': 1,
|
| 2223 |
+
'epsilon_start': 1.0,
|
| 2224 |
+
'epsilon_end': 0.1,
|
| 2225 |
+
'epsilon_decay': 0.9999
|
| 2226 |
+
},
|
| 2227 |
+
'training': {
|
| 2228 |
+
'num_episodes': 5000,
|
| 2229 |
+
'log_interval': 500,
|
| 2230 |
+
'eval_interval': 1000,
|
| 2231 |
+
'eval_episodes': 100,
|
| 2232 |
+
'save_interval': 1000,
|
| 2233 |
+
'checkpoint_dir': './checkpoints',
|
| 2234 |
+
'early_stop_reward': None,
|
| 2235 |
+
'early_stop_window': 100
|
| 2236 |
+
},
|
| 2237 |
+
'seed': 42
|
| 2238 |
+
}
|
| 2239 |
+
|
| 2240 |
+
|
| 2241 |
+
def create_env(config: Dict):
|
| 2242 |
+
env_type = config['env']['type']
|
| 2243 |
+
|
| 2244 |
+
if env_type == 'gridworld':
|
| 2245 |
+
return GridWorld(
|
| 2246 |
+
width=config['env']['width'],
|
| 2247 |
+
height=config['env']['height'],
|
| 2248 |
+
mode=config['env'].get('mode', 'static'),
|
| 2249 |
+
max_steps=config['env']['max_steps'],
|
| 2250 |
+
seed=config.get('seed', None)
|
| 2251 |
+
)
|
| 2252 |
+
elif env_type == 'cartpole':
|
| 2253 |
+
return ContinuousCartPole(
|
| 2254 |
+
max_steps=config['env'].get('max_steps', 500),
|
| 2255 |
+
seed=config.get('seed', None)
|
| 2256 |
+
)
|
| 2257 |
+
else:
|
| 2258 |
+
raise ValueError(f"Unknown environment type: {env_type}")
|
| 2259 |
+
|
| 2260 |
+
|
| 2261 |
+
def create_agent(config: Dict, state_dim: int, action_dim: int) -> DQNAgent:
|
| 2262 |
+
agent_config = config['agent']
|
| 2263 |
+
|
| 2264 |
+
return DQNAgent(
|
| 2265 |
+
state_dim=state_dim,
|
| 2266 |
+
action_dim=action_dim,
|
| 2267 |
+
hidden_dims=agent_config['hidden_dims'],
|
| 2268 |
+
lr=agent_config['lr'],
|
| 2269 |
+
gamma=agent_config['gamma'],
|
| 2270 |
+
buffer_size=agent_config['buffer_size'],
|
| 2271 |
+
batch_size=agent_config['batch_size'],
|
| 2272 |
+
target_update_freq=agent_config['target_update_freq'],
|
| 2273 |
+
tau=agent_config['tau'],
|
| 2274 |
+
use_double=agent_config['use_double'],
|
| 2275 |
+
use_dueling=agent_config['use_dueling'],
|
| 2276 |
+
use_per=agent_config['use_per'],
|
| 2277 |
+
n_steps=agent_config['n_steps'],
|
| 2278 |
+
epsilon_start=agent_config['epsilon_start'],
|
| 2279 |
+
epsilon_end=agent_config['epsilon_end'],
|
| 2280 |
+
epsilon_decay=agent_config['epsilon_decay'],
|
| 2281 |
+
seed=config.get('seed', None)
|
| 2282 |
+
)
|
| 2283 |
+
|
| 2284 |
+
|
| 2285 |
+
def main():
|
| 2286 |
+
parser = argparse.ArgumentParser(description='Complete RL Training Script')
|
| 2287 |
+
|
| 2288 |
+
parser.add_argument('--env', type=str, default='gridworld',
|
| 2289 |
+
choices=['gridworld', 'cartpole'],
|
| 2290 |
+
help='Environment type')
|
| 2291 |
+
parser.add_argument('--episodes', type=int, default=5000,
|
| 2292 |
+
help='Number of training episodes')
|
| 2293 |
+
parser.add_argument('--lr', type=float, default=0.001,
|
| 2294 |
+
help='Learning rate')
|
| 2295 |
+
parser.add_argument('--gamma', type=float, default=0.9,
|
| 2296 |
+
help='Discount factor')
|
| 2297 |
+
parser.add_argument('--batch-size', type=int, default=200,
|
| 2298 |
+
help='Batch size')
|
| 2299 |
+
parser.add_argument('--buffer-size', type=int, default=1000,
|
| 2300 |
+
help='Replay buffer size')
|
| 2301 |
+
parser.add_argument('--hidden-dims', type=int, nargs='+', default=[150, 100],
|
| 2302 |
+
help='Hidden layer dimensions')
|
| 2303 |
+
parser.add_argument('--double', action='store_true', default=True,
|
| 2304 |
+
help='Use Double DQN')
|
| 2305 |
+
parser.add_argument('--dueling', action='store_true', default=False,
|
| 2306 |
+
help='Use Dueling DQN')
|
| 2307 |
+
parser.add_argument('--per', action='store_true', default=False,
|
| 2308 |
+
help='Use Prioritized Experience Replay')
|
| 2309 |
+
parser.add_argument('--n-steps', type=int, default=1,
|
| 2310 |
+
help='N-step returns')
|
| 2311 |
+
parser.add_argument('--seed', type=int, default=42,
|
| 2312 |
+
help='Random seed')
|
| 2313 |
+
parser.add_argument('--checkpoint-dir', type=str, default='./checkpoints',
|
| 2314 |
+
help='Checkpoint directory')
|
| 2315 |
+
parser.add_argument('--load', type=str, default=None,
|
| 2316 |
+
help='Load model from path')
|
| 2317 |
+
parser.add_argument('--eval-only', action='store_true',
|
| 2318 |
+
help='Only run evaluation')
|
| 2319 |
+
parser.add_argument('--visualize', action='store_true',
|
| 2320 |
+
help='Generate visualizations after training')
|
| 2321 |
+
|
| 2322 |
+
args = parser.parse_args()
|
| 2323 |
+
|
| 2324 |
+
np.random.seed(args.seed)
|
| 2325 |
+
|
| 2326 |
+
config = create_default_config()
|
| 2327 |
+
config['env']['type'] = args.env
|
| 2328 |
+
config['agent']['lr'] = args.lr
|
| 2329 |
+
config['agent']['gamma'] = args.gamma
|
| 2330 |
+
config['agent']['batch_size'] = args.batch_size
|
| 2331 |
+
config['agent']['buffer_size'] = args.buffer_size
|
| 2332 |
+
config['agent']['hidden_dims'] = args.hidden_dims
|
| 2333 |
+
config['agent']['use_double'] = args.double
|
| 2334 |
+
config['agent']['use_dueling'] = args.dueling
|
| 2335 |
+
config['agent']['use_per'] = args.per
|
| 2336 |
+
config['agent']['n_steps'] = args.n_steps
|
| 2337 |
+
config['training']['num_episodes'] = args.episodes
|
| 2338 |
+
config['training']['checkpoint_dir'] = args.checkpoint_dir
|
| 2339 |
+
config['seed'] = args.seed
|
| 2340 |
+
|
| 2341 |
+
print("=" * 60)
|
| 2342 |
+
print("REINFORCEMENT LEARNING TRAINING")
|
| 2343 |
+
print("=" * 60)
|
| 2344 |
+
print(f"Environment: {args.env}")
|
| 2345 |
+
print(f"Episodes: {args.episodes}")
|
| 2346 |
+
print(f"Learning Rate: {args.lr}")
|
| 2347 |
+
print(f"Gamma: {args.gamma}")
|
| 2348 |
+
print(f"Double DQN: {args.double}")
|
| 2349 |
+
print(f"Dueling DQN: {args.dueling}")
|
| 2350 |
+
print(f"PER: {args.per}")
|
| 2351 |
+
print(f"N-Steps: {args.n_steps}")
|
| 2352 |
+
print("=" * 60)
|
| 2353 |
+
|
| 2354 |
+
env = create_env(config)
|
| 2355 |
+
eval_env = create_env(config)
|
| 2356 |
+
|
| 2357 |
+
state_dim = env.state_dim
|
| 2358 |
+
action_dim = env.n_actions
|
| 2359 |
+
|
| 2360 |
+
print(f"State Dim: {state_dim}")
|
| 2361 |
+
print(f"Action Dim: {action_dim}")
|
| 2362 |
+
print("=" * 60)
|
| 2363 |
+
|
| 2364 |
+
agent = create_agent(config, state_dim, action_dim)
|
| 2365 |
+
|
| 2366 |
+
if args.load:
|
| 2367 |
+
print(f"Loading model from: {args.load}")
|
| 2368 |
+
agent.load(args.load)
|
| 2369 |
+
|
| 2370 |
+
if args.eval_only:
|
| 2371 |
+
print("Running evaluation only...")
|
| 2372 |
+
trainer = Trainer(agent, env, eval_env, checkpoint_dir=args.checkpoint_dir)
|
| 2373 |
+
eval_reward, eval_length = trainer.evaluate()
|
| 2374 |
+
print(f"Evaluation Results:")
|
| 2375 |
+
print(f" Avg Reward: {eval_reward:.2f}")
|
| 2376 |
+
print(f" Avg Length: {eval_length:.1f}")
|
| 2377 |
+
return
|
| 2378 |
+
|
| 2379 |
+
trainer = Trainer(
|
| 2380 |
+
agent, env, eval_env,
|
| 2381 |
+
log_interval=config['training']['log_interval'],
|
| 2382 |
+
eval_interval=config['training']['eval_interval'],
|
| 2383 |
+
eval_episodes=config['training']['eval_episodes'],
|
| 2384 |
+
save_interval=config['training']['save_interval'],
|
| 2385 |
+
checkpoint_dir=config['training']['checkpoint_dir'],
|
| 2386 |
+
early_stop_reward=config['training']['early_stop_reward'],
|
| 2387 |
+
early_stop_window=config['training']['early_stop_window']
|
| 2388 |
+
)
|
| 2389 |
+
|
| 2390 |
+
print("\nStarting training...")
|
| 2391 |
+
history = trainer.train(config['training']['num_episodes'])
|
| 2392 |
+
|
| 2393 |
+
trainer.save_history(os.path.join(args.checkpoint_dir, 'training_history.json'))
|
| 2394 |
+
|
| 2395 |
+
if args.visualize:
|
| 2396 |
+
print("\nGenerating visualizations...")
|
| 2397 |
+
viz = Visualizer(save_dir=args.checkpoint_dir)
|
| 2398 |
+
|
| 2399 |
+
training_curves = viz.plot_training_curves(history)
|
| 2400 |
+
print(training_curves)
|
| 2401 |
+
|
| 2402 |
+
if args.env == 'gridworld':
|
| 2403 |
+
q_heatmap = viz.plot_q_values_heatmap(agent, env)
|
| 2404 |
+
print(q_heatmap)
|
| 2405 |
+
|
| 2406 |
+
episode_recording = viz.record_episode(agent, eval_env)
|
| 2407 |
+
print(episode_recording)
|
| 2408 |
+
|
| 2409 |
+
print("\n" + "=" * 60)
|
| 2410 |
+
print("TRAINING COMPLETE")
|
| 2411 |
+
print("=" * 60)
|
| 2412 |
+
|
| 2413 |
+
final_eval_reward, final_eval_length = trainer.evaluate()
|
| 2414 |
+
print(f"Final Evaluation:")
|
| 2415 |
+
print(f" Avg Reward: {final_eval_reward:.2f}")
|
| 2416 |
+
print(f" Avg Length: {final_eval_length:.1f}")
|
| 2417 |
+
|
| 2418 |
+
if history['reward']:
|
| 2419 |
+
print(f"\nTraining Statistics:")
|
| 2420 |
+
print(f" Total Episodes: {len(history['reward'])}")
|
| 2421 |
+
print(f" Best Reward: {max(history['reward']):.2f}")
|
| 2422 |
+
print(f" Final Avg (last 100): {np.mean(history['reward'][-100:]):.2f}")
|
| 2423 |
+
|
| 2424 |
+
print(f"\nCheckpoints saved to: {args.checkpoint_dir}")
|
| 2425 |
+
|
| 2426 |
+
|
| 2427 |
+
if __name__ == '__main__':
|
| 2428 |
+
main()
|
| 2429 |
+
|
| 2430 |
+
|
| 2431 |
+
# =============================================================================
|
| 2432 |
+
# SECTION 8: PPO - PROXIMAL POLICY OPTIMIZATION (Lines 2430+)
|
| 2433 |
+
# =============================================================================
|
| 2434 |
+
|
| 2435 |
+
class PPOBuffer:
|
| 2436 |
+
"""GAE buffer za PPO"""
|
| 2437 |
+
|
| 2438 |
+
def __init__(self, state_dim: int, size: int, gamma: float = 0.99, lam: float = 0.95):
|
| 2439 |
+
self.states = np.zeros((size, state_dim), dtype=np.float32)
|
| 2440 |
+
self.actions = np.zeros(size, dtype=np.int32)
|
| 2441 |
+
self.rewards = np.zeros(size, dtype=np.float32)
|
| 2442 |
+
self.values = np.zeros(size, dtype=np.float32)
|
| 2443 |
+
self.log_probs = np.zeros(size, dtype=np.float32)
|
| 2444 |
+
self.advantages = np.zeros(size, dtype=np.float32)
|
| 2445 |
+
self.returns = np.zeros(size, dtype=np.float32)
|
| 2446 |
+
|
| 2447 |
+
self.gamma = gamma
|
| 2448 |
+
self.lam = lam
|
| 2449 |
+
self.ptr = 0
|
| 2450 |
+
self.path_start = 0
|
| 2451 |
+
self.max_size = size
|
| 2452 |
+
|
| 2453 |
+
def store(self, state, action, reward, value, log_prob):
|
| 2454 |
+
assert self.ptr < self.max_size
|
| 2455 |
+
self.states[self.ptr] = state
|
| 2456 |
+
self.actions[self.ptr] = action
|
| 2457 |
+
self.rewards[self.ptr] = reward
|
| 2458 |
+
self.values[self.ptr] = value
|
| 2459 |
+
self.log_probs[self.ptr] = log_prob
|
| 2460 |
+
self.ptr += 1
|
| 2461 |
+
|
| 2462 |
+
def finish_path(self, last_value: float = 0):
|
| 2463 |
+
"""Compute GAE advantages"""
|
| 2464 |
+
path_slice = slice(self.path_start, self.ptr)
|
| 2465 |
+
rewards = np.append(self.rewards[path_slice], last_value)
|
| 2466 |
+
values = np.append(self.values[path_slice], last_value)
|
| 2467 |
+
|
| 2468 |
+
# GAE-Lambda
|
| 2469 |
+
deltas = rewards[:-1] + self.gamma * values[1:] - values[:-1]
|
| 2470 |
+
self.advantages[path_slice] = self._discount_cumsum(deltas, self.gamma * self.lam)
|
| 2471 |
+
self.returns[path_slice] = self._discount_cumsum(rewards[:-1], self.gamma)
|
| 2472 |
+
|
| 2473 |
+
self.path_start = self.ptr
|
| 2474 |
+
|
| 2475 |
+
def _discount_cumsum(self, x, discount):
|
| 2476 |
+
n = len(x)
|
| 2477 |
+
out = np.zeros(n, dtype=np.float32)
|
| 2478 |
+
out[-1] = x[-1]
|
| 2479 |
+
for i in range(n - 2, -1, -1):
|
| 2480 |
+
out[i] = x[i] + discount * out[i + 1]
|
| 2481 |
+
return out
|
| 2482 |
+
|
| 2483 |
+
def get(self):
|
| 2484 |
+
assert self.ptr == self.max_size
|
| 2485 |
+
self.ptr = 0
|
| 2486 |
+
self.path_start = 0
|
| 2487 |
+
|
| 2488 |
+
# Normalize advantages
|
| 2489 |
+
adv_mean = np.mean(self.advantages)
|
| 2490 |
+
adv_std = np.std(self.advantages) + 1e-8
|
| 2491 |
+
self.advantages = (self.advantages - adv_mean) / adv_std
|
| 2492 |
+
|
| 2493 |
+
return {
|
| 2494 |
+
'states': self.states,
|
| 2495 |
+
'actions': self.actions,
|
| 2496 |
+
'returns': self.returns,
|
| 2497 |
+
'advantages': self.advantages,
|
| 2498 |
+
'log_probs': self.log_probs
|
| 2499 |
+
}
|
| 2500 |
+
|
| 2501 |
+
|
| 2502 |
+
class ActorCritic:
|
| 2503 |
+
"""Actor-Critic za PPO - čist numpy"""
|
| 2504 |
+
|
| 2505 |
+
def __init__(self, state_dim: int, action_dim: int, hidden_dims: List[int] = [64, 64], lr: float = 3e-4):
|
| 2506 |
+
self.state_dim = state_dim
|
| 2507 |
+
self.action_dim = action_dim
|
| 2508 |
+
self.lr = lr
|
| 2509 |
+
|
| 2510 |
+
# Shared layers
|
| 2511 |
+
dims = [state_dim] + hidden_dims
|
| 2512 |
+
self.shared_weights = []
|
| 2513 |
+
self.shared_biases = []
|
| 2514 |
+
|
| 2515 |
+
for i in range(len(dims) - 1):
|
| 2516 |
+
w = np.random.randn(dims[i], dims[i + 1]).astype(np.float32) * np.sqrt(2.0 / dims[i])
|
| 2517 |
+
b = np.zeros(dims[i + 1], dtype=np.float32)
|
| 2518 |
+
self.shared_weights.append(w)
|
| 2519 |
+
self.shared_biases.append(b)
|
| 2520 |
+
|
| 2521 |
+
# Actor head (policy)
|
| 2522 |
+
self.actor_w = np.random.randn(hidden_dims[-1], action_dim).astype(np.float32) * 0.01
|
| 2523 |
+
self.actor_b = np.zeros(action_dim, dtype=np.float32)
|
| 2524 |
+
|
| 2525 |
+
# Critic head (value)
|
| 2526 |
+
self.critic_w = np.random.randn(hidden_dims[-1], 1).astype(np.float32) * 1.0
|
| 2527 |
+
self.critic_b = np.zeros(1, dtype=np.float32)
|
| 2528 |
+
|
| 2529 |
+
# Adam state
|
| 2530 |
+
self._init_adam()
|
| 2531 |
+
|
| 2532 |
+
def _init_adam(self):
|
| 2533 |
+
self.t = 0
|
| 2534 |
+
self.m = {}
|
| 2535 |
+
self.v = {}
|
| 2536 |
+
|
| 2537 |
+
all_params = self.shared_weights + self.shared_biases + [self.actor_w, self.actor_b, self.critic_w, self.critic_b]
|
| 2538 |
+
for i, p in enumerate(all_params):
|
| 2539 |
+
self.m[i] = np.zeros_like(p)
|
| 2540 |
+
self.v[i] = np.zeros_like(p)
|
| 2541 |
+
|
| 2542 |
+
def forward(self, state: np.ndarray):
|
| 2543 |
+
"""Forward pass"""
|
| 2544 |
+
x = state
|
| 2545 |
+
self.activations = [x]
|
| 2546 |
+
|
| 2547 |
+
for w, b in zip(self.shared_weights, self.shared_biases):
|
| 2548 |
+
x = np.tanh(x @ w + b)
|
| 2549 |
+
self.activations.append(x)
|
| 2550 |
+
|
| 2551 |
+
# Actor output (logits)
|
| 2552 |
+
logits = x @ self.actor_w + self.actor_b
|
| 2553 |
+
|
| 2554 |
+
# Critic output (value)
|
| 2555 |
+
value = (x @ self.critic_w + self.critic_b).squeeze()
|
| 2556 |
+
|
| 2557 |
+
return logits, value
|
| 2558 |
+
|
| 2559 |
+
def get_action(self, state: np.ndarray, deterministic: bool = False):
|
| 2560 |
+
"""Sample action from policy"""
|
| 2561 |
+
logits, value = self.forward(state)
|
| 2562 |
+
|
| 2563 |
+
# Softmax
|
| 2564 |
+
logits_max = np.max(logits, axis=-1, keepdims=True)
|
| 2565 |
+
exp_logits = np.exp(logits - logits_max)
|
| 2566 |
+
probs = exp_logits / np.sum(exp_logits, axis=-1, keepdims=True)
|
| 2567 |
+
|
| 2568 |
+
if deterministic:
|
| 2569 |
+
action = np.argmax(probs, axis=-1)
|
| 2570 |
+
else:
|
| 2571 |
+
if probs.ndim == 1:
|
| 2572 |
+
action = np.random.choice(self.action_dim, p=probs)
|
| 2573 |
+
else:
|
| 2574 |
+
action = np.array([np.random.choice(self.action_dim, p=p) for p in probs])
|
| 2575 |
+
|
| 2576 |
+
# Log probability
|
| 2577 |
+
log_prob = np.log(probs[action] + 1e-8) if probs.ndim == 1 else np.log(probs[np.arange(len(action)), action] + 1e-8)
|
| 2578 |
+
|
| 2579 |
+
return action, value, log_prob
|
| 2580 |
+
|
| 2581 |
+
def evaluate_actions(self, states: np.ndarray, actions: np.ndarray):
|
| 2582 |
+
"""Evaluate log probs and values for given states/actions"""
|
| 2583 |
+
logits, values = self.forward(states)
|
| 2584 |
+
|
| 2585 |
+
# Softmax
|
| 2586 |
+
logits_max = np.max(logits, axis=-1, keepdims=True)
|
| 2587 |
+
exp_logits = np.exp(logits - logits_max)
|
| 2588 |
+
probs = exp_logits / np.sum(exp_logits, axis=-1, keepdims=True)
|
| 2589 |
+
|
| 2590 |
+
# Log probs for taken actions
|
| 2591 |
+
log_probs = np.log(probs[np.arange(len(actions)), actions] + 1e-8)
|
| 2592 |
+
|
| 2593 |
+
# Entropy
|
| 2594 |
+
entropy = -np.sum(probs * np.log(probs + 1e-8), axis=-1).mean()
|
| 2595 |
+
|
| 2596 |
+
return log_probs, values, entropy
|
| 2597 |
+
|
| 2598 |
+
|
| 2599 |
+
class PPOAgent:
|
| 2600 |
+
"""Proximal Policy Optimization Agent"""
|
| 2601 |
+
|
| 2602 |
+
def __init__(
|
| 2603 |
+
self,
|
| 2604 |
+
state_dim: int,
|
| 2605 |
+
action_dim: int,
|
| 2606 |
+
hidden_dims: List[int] = [64, 64],
|
| 2607 |
+
lr: float = 3e-4,
|
| 2608 |
+
gamma: float = 0.99,
|
| 2609 |
+
lam: float = 0.95,
|
| 2610 |
+
clip_ratio: float = 0.2,
|
| 2611 |
+
target_kl: float = 0.01,
|
| 2612 |
+
train_iters: int = 80,
|
| 2613 |
+
value_coef: float = 0.5,
|
| 2614 |
+
entropy_coef: float = 0.01,
|
| 2615 |
+
max_grad_norm: float = 0.5,
|
| 2616 |
+
seed: int = None
|
| 2617 |
+
):
|
| 2618 |
+
if seed is not None:
|
| 2619 |
+
np.random.seed(seed)
|
| 2620 |
+
|
| 2621 |
+
self.state_dim = state_dim
|
| 2622 |
+
self.action_dim = action_dim
|
| 2623 |
+
self.gamma = gamma
|
| 2624 |
+
self.lam = lam
|
| 2625 |
+
self.clip_ratio = clip_ratio
|
| 2626 |
+
self.target_kl = target_kl
|
| 2627 |
+
self.train_iters = train_iters
|
| 2628 |
+
self.value_coef = value_coef
|
| 2629 |
+
self.entropy_coef = entropy_coef
|
| 2630 |
+
self.max_grad_norm = max_grad_norm
|
| 2631 |
+
|
| 2632 |
+
self.actor_critic = ActorCritic(state_dim, action_dim, hidden_dims, lr)
|
| 2633 |
+
|
| 2634 |
+
def get_action(self, state: np.ndarray, deterministic: bool = False):
|
| 2635 |
+
return self.actor_critic.get_action(state, deterministic)
|
| 2636 |
+
|
| 2637 |
+
def update(self, buffer_data: Dict) -> Dict:
|
| 2638 |
+
"""PPO update"""
|
| 2639 |
+
states = buffer_data['states']
|
| 2640 |
+
actions = buffer_data['actions']
|
| 2641 |
+
old_log_probs = buffer_data['log_probs']
|
| 2642 |
+
advantages = buffer_data['advantages']
|
| 2643 |
+
returns = buffer_data['returns']
|
| 2644 |
+
|
| 2645 |
+
total_loss = 0
|
| 2646 |
+
policy_loss = 0
|
| 2647 |
+
value_loss = 0
|
| 2648 |
+
|
| 2649 |
+
for i in range(self.train_iters):
|
| 2650 |
+
log_probs, values, entropy = self.actor_critic.evaluate_actions(states, actions)
|
| 2651 |
+
|
| 2652 |
+
# Policy loss (PPO clip)
|
| 2653 |
+
ratio = np.exp(log_probs - old_log_probs)
|
| 2654 |
+
clip_adv = np.clip(ratio, 1 - self.clip_ratio, 1 + self.clip_ratio) * advantages
|
| 2655 |
+
policy_loss = -np.mean(np.minimum(ratio * advantages, clip_adv))
|
| 2656 |
+
|
| 2657 |
+
# Value loss
|
| 2658 |
+
value_loss = np.mean((values - returns) ** 2)
|
| 2659 |
+
|
| 2660 |
+
# Total loss
|
| 2661 |
+
loss = policy_loss + self.value_coef * value_loss - self.entropy_coef * entropy
|
| 2662 |
+
|
| 2663 |
+
# Approximate KL divergence for early stopping
|
| 2664 |
+
approx_kl = np.mean(old_log_probs - log_probs)
|
| 2665 |
+
if approx_kl > 1.5 * self.target_kl:
|
| 2666 |
+
break
|
| 2667 |
+
|
| 2668 |
+
total_loss = loss
|
| 2669 |
+
|
| 2670 |
+
# Gradient update (simplified - full backprop would need more code)
|
| 2671 |
+
# For now using finite differences approximation
|
| 2672 |
+
self._update_params(states, actions, advantages, returns, old_log_probs)
|
| 2673 |
+
|
| 2674 |
+
return {
|
| 2675 |
+
'loss': total_loss,
|
| 2676 |
+
'policy_loss': policy_loss,
|
| 2677 |
+
'value_loss': value_loss,
|
| 2678 |
+
'entropy': entropy,
|
| 2679 |
+
'kl': approx_kl
|
| 2680 |
+
}
|
| 2681 |
+
|
| 2682 |
+
def _update_params(self, states, actions, advantages, returns, old_log_probs, eps=1e-4):
|
| 2683 |
+
"""Simplified parameter update using numerical gradients"""
|
| 2684 |
+
lr = self.actor_critic.lr
|
| 2685 |
+
|
| 2686 |
+
# Update actor weights
|
| 2687 |
+
for idx, w in enumerate(self.actor_critic.shared_weights):
|
| 2688 |
+
grad = np.zeros_like(w)
|
| 2689 |
+
# Sample gradient estimation (faster than full finite diff)
|
| 2690 |
+
for _ in range(min(10, w.size)):
|
| 2691 |
+
i, j = np.random.randint(0, w.shape[0]), np.random.randint(0, w.shape[1])
|
| 2692 |
+
w[i, j] += eps
|
| 2693 |
+
loss_plus = self._compute_loss(states, actions, advantages, returns, old_log_probs)
|
| 2694 |
+
w[i, j] -= 2 * eps
|
| 2695 |
+
loss_minus = self._compute_loss(states, actions, advantages, returns, old_log_probs)
|
| 2696 |
+
w[i, j] += eps
|
| 2697 |
+
grad[i, j] = (loss_plus - loss_minus) / (2 * eps)
|
| 2698 |
+
|
| 2699 |
+
# Gradient clipping
|
| 2700 |
+
grad_norm = np.linalg.norm(grad)
|
| 2701 |
+
if grad_norm > self.max_grad_norm:
|
| 2702 |
+
grad = grad * self.max_grad_norm / grad_norm
|
| 2703 |
+
|
| 2704 |
+
w -= lr * grad
|
| 2705 |
+
|
| 2706 |
+
def _compute_loss(self, states, actions, advantages, returns, old_log_probs):
|
| 2707 |
+
log_probs, values, entropy = self.actor_critic.evaluate_actions(states, actions)
|
| 2708 |
+
ratio = np.exp(log_probs - old_log_probs)
|
| 2709 |
+
clip_adv = np.clip(ratio, 1 - self.clip_ratio, 1 + self.clip_ratio) * advantages
|
| 2710 |
+
policy_loss = -np.mean(np.minimum(ratio * advantages, clip_adv))
|
| 2711 |
+
value_loss = np.mean((values - returns) ** 2)
|
| 2712 |
+
return policy_loss + self.value_coef * value_loss - self.entropy_coef * entropy
|
| 2713 |
+
|
| 2714 |
+
def save(self, path: str):
|
| 2715 |
+
data = {
|
| 2716 |
+
'shared_weights': self.actor_critic.shared_weights,
|
| 2717 |
+
'shared_biases': self.actor_critic.shared_biases,
|
| 2718 |
+
'actor_w': self.actor_critic.actor_w,
|
| 2719 |
+
'actor_b': self.actor_critic.actor_b,
|
| 2720 |
+
'critic_w': self.actor_critic.critic_w,
|
| 2721 |
+
'critic_b': self.actor_critic.critic_b
|
| 2722 |
+
}
|
| 2723 |
+
with open(path, 'wb') as f:
|
| 2724 |
+
pickle.dump(data, f)
|
| 2725 |
+
|
| 2726 |
+
def load(self, path: str):
|
| 2727 |
+
with open(path, 'rb') as f:
|
| 2728 |
+
data = pickle.load(f)
|
| 2729 |
+
self.actor_critic.shared_weights = data['shared_weights']
|
| 2730 |
+
self.actor_critic.shared_biases = data['shared_biases']
|
| 2731 |
+
self.actor_critic.actor_w = data['actor_w']
|
| 2732 |
+
self.actor_critic.actor_b = data['actor_b']
|
| 2733 |
+
self.actor_critic.critic_w = data['critic_w']
|
| 2734 |
+
self.actor_critic.critic_b = data['critic_b']
|
| 2735 |
+
|
| 2736 |
+
|
| 2737 |
+
def train_ppo(env, agent: PPOAgent, num_episodes: int = 1000, steps_per_epoch: int = 4000):
|
| 2738 |
+
"""PPO Training Loop"""
|
| 2739 |
+
buffer = PPOBuffer(agent.state_dim, steps_per_epoch, agent.gamma, agent.lam)
|
| 2740 |
+
|
| 2741 |
+
state = env.reset()
|
| 2742 |
+
episode_reward = 0
|
| 2743 |
+
episode_length = 0
|
| 2744 |
+
episode_rewards = []
|
| 2745 |
+
|
| 2746 |
+
print("\n" + "=" * 60)
|
| 2747 |
+
print("PPO TRAINING")
|
| 2748 |
+
print("=" * 60)
|
| 2749 |
+
|
| 2750 |
+
for epoch in range(num_episodes // 10):
|
| 2751 |
+
for t in range(steps_per_epoch):
|
| 2752 |
+
action, value, log_prob = agent.get_action(state)
|
| 2753 |
+
next_state, reward, done, info = env.step(action)
|
| 2754 |
+
|
| 2755 |
+
episode_reward += reward
|
| 2756 |
+
episode_length += 1
|
| 2757 |
+
|
| 2758 |
+
buffer.store(state, action, reward, value, log_prob)
|
| 2759 |
+
state = next_state
|
| 2760 |
+
|
| 2761 |
+
epoch_ended = t == steps_per_epoch - 1
|
| 2762 |
+
|
| 2763 |
+
if done or epoch_ended:
|
| 2764 |
+
if epoch_ended and not done:
|
| 2765 |
+
_, last_value, _ = agent.get_action(state)
|
| 2766 |
+
else:
|
| 2767 |
+
last_value = 0
|
| 2768 |
+
|
| 2769 |
+
buffer.finish_path(last_value)
|
| 2770 |
+
|
| 2771 |
+
if done:
|
| 2772 |
+
episode_rewards.append(episode_reward)
|
| 2773 |
+
episode_reward = 0
|
| 2774 |
+
episode_length = 0
|
| 2775 |
+
state = env.reset()
|
| 2776 |
+
|
| 2777 |
+
# Update
|
| 2778 |
+
data = buffer.get()
|
| 2779 |
+
update_info = agent.update(data)
|
| 2780 |
+
|
| 2781 |
+
avg_reward = np.mean(episode_rewards[-10:]) if episode_rewards else 0
|
| 2782 |
+
print(f"Epoch {epoch:4d} | Avg Reward: {avg_reward:8.2f} | Loss: {update_info['loss']:.4f} | KL: {update_info['kl']:.4f}")
|
| 2783 |
+
|
| 2784 |
+
return episode_rewards
|
| 2785 |
+
|
| 2786 |
+
|
| 2787 |
+
print("\n✅ PPO Implementation Added!")
|
| 2788 |
+
print("Run with: python rl_complete.py --env gridworld --ppo")
|