Spaces:
Sleeping
Sleeping
File size: 4,999 Bytes
b00d5d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | """
Configuration file for RL Traffic Signal Control project.
Contains all hyperparameters and settings for the environment,
agents, and training process.
"""
import os
from pathlib import Path
# ============================================================================
# PROJECT PATHS
# ============================================================================
ROOT_DIR = Path(__file__).parent
MODELS_DIR = ROOT_DIR / "models"
MODELS_DIR.mkdir(exist_ok=True)
RESULTS_DIR = ROOT_DIR / "results"
RESULTS_DIR.mkdir(exist_ok=True)
# ============================================================================
# ENVIRONMENT CONFIGURATION
# ============================================================================
NUM_LANES = 2 # Lanes per direction at each intersection
EPISODE_LENGTH = 3600 # Steps per episode (simulated 1 hour)
TIME_STEP = 1 # Simulation time step in seconds
# Traffic generation
TRAFFIC_DENSITY = 0.02 # Drastically reduced to hit ~1000-5000 throughput per episode
PEAK_HOURS = [(7, 9), (17, 19)] # Morning and evening rush hours
PEAK_MULTIPLIER = 1.5 # Traffic density multiplier during peak hours
# Signal timing constraints
MIN_GREEN_TIME = 10 # Minimum green light duration (seconds)
MAX_GREEN_TIME = 60 # Maximum green light duration (seconds)
YELLOW_TIME = 3 # Yellow light duration (seconds)
ALL_RED_TIME = 2 # All-red clearance time (seconds)
# ============================================================================
# AGENT CONFIGURATION
# ============================================================================
AGENT_TYPE = "dqn" # Options: "dqn", "q_learning"
STATE_SIZE = 9 # [N_SR, N_L, E_SR, E_L, S_SR, S_L, W_SR, W_L, phase]
ACTION_SIZE = 2 # 0=keep current phase, 1=switch phase
# Deep Q-Network (DQN) hyperparameters
DQN_CONFIG = {
"learning_rate": 0.0001, # Low LR for stability
"gamma": 0.99, # Discount factor
"epsilon_start": 1.0, # Initial exploration rate
"epsilon_end": 0.01, # Final exploration rate
"epsilon_decay": 0.998, # Slow decay for thorough exploration
"memory_size": 50000, # Replay buffer size
"batch_size": 256, # Larger batch = better GPU utilisation
"target_update": 10, # Target network update frequency (episodes)
"hidden_layers": [256, 256], # Slightly larger network for 9D state space
"train_frequency": 4, # Train every N env steps (reduces CPU-GPU overhead)
}
# Q-Learning (tabular) hyperparameters
Q_LEARNING_CONFIG = {
"learning_rate": 0.1, # Alpha
"gamma": 0.99, # Discount factor
"epsilon_start": 1.0, # Initial exploration rate
"epsilon_end": 0.01, # Final exploration rate
"epsilon_decay": 0.995, # Exploration decay rate
"num_bins": 10, # Bins per state dimension for discretization
}
# ============================================================================
# TRAINING CONFIGURATION
# ============================================================================
NUM_EPISODES = 1000 # Total training episodes
EVAL_FREQUENCY = 50 # Evaluate every N episodes
SAVE_FREQUENCY = 100 # Save checkpoint every N episodes
EARLY_STOPPING_PATIENCE = 100 # Stop if no improvement for N episodes
MIN_REWARD_THRESHOLD = -1000 # Minimum average reward threshold
# Logging
LOG_FREQUENCY = 10 # Log metrics every N episodes
USE_TENSORBOARD = False # Disabled by default (no extra deps)
# ============================================================================
# EVALUATION CONFIGURATION
# ============================================================================
NUM_EVAL_EPISODES = 10 # Episodes for evaluation
RENDER_EVAL = False # Render environment during evaluation
# ============================================================================
# VISUALIZATION
# ============================================================================
FIGURE_SIZE = (12, 6)
DPI = 100
METRICS = [
"episode_reward",
"average_waiting_time",
"average_queue_length",
"throughput",
]
# ============================================================================
# RANDOM SEED
# ============================================================================
RANDOM_SEED = 42
# ============================================================================
# DEVICE
# ============================================================================
try:
import torch
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"[Config] Device: {DEVICE}")
except ImportError:
DEVICE = "cpu"
print("[Config] PyTorch not found, using CPU")
|