File size: 1,598 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
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()