Spaces:
Sleeping
Sleeping
| import sys | |
| import os | |
| from pathlib import Path | |
| # Add project root to path | |
| ROOT_DIR = Path(__file__).parent | |
| sys.path.append(str(ROOT_DIR)) | |
| import config as cfg | |
| from environment import TrafficEnvironment | |
| from agent import DQNAgent | |
| def test_model(): | |
| print("Initializing environment...") | |
| env = TrafficEnvironment(cfg) | |
| print("Initializing Agent...") | |
| agent = DQNAgent(cfg.STATE_SIZE, cfg.ACTION_SIZE, cfg.DQN_CONFIG) | |
| model_path = ROOT_DIR / "models" / "dqn_best.pth" | |
| if model_path.exists(): | |
| agent.load(str(model_path)) | |
| print("Model loaded successfully.") | |
| else: | |
| print("ERROR: Model not found at", model_path) | |
| return | |
| state, _ = env.reset() | |
| print("\n--- Simulation Test ---") | |
| action_1_count = 0 | |
| phase_changes = 0 | |
| last_phase = env.current_phase | |
| for i in range(200): | |
| action = agent.select_action(state, training=False) | |
| if action == 1: | |
| action_1_count += 1 | |
| next_state, reward, _, _, info = env.step(action) | |
| if env.current_phase != last_phase: | |
| phase_changes += 1 | |
| print(f"Step {i}: Phase changed from {last_phase} to {env.current_phase}") | |
| last_phase = env.current_phase | |
| state = next_state | |
| print("\n--- Test Results ---") | |
| print(f"Total Steps: 200") | |
| print(f"Agent chose Switch (1): {action_1_count} times") | |
| print(f"Actual Phase Changes: {phase_changes}") | |
| print(f"Final Queues: {env.queue_lengths}") | |
| if __name__ == '__main__': | |
| test_model() | |