File size: 2,239 Bytes
0210a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Standalone Verification Script for Neural CBF Checkpoint
Applies Branch-and-Bound verification to certify regional forward invariance.
"""

import sys
import os
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import numpy as np
import torch

from src.dynamics.toy_nav import DubinsCarSystem
from src.cbf.network import NeuralCBF
from src.verifier.symbolic_adapter import RegionalCBFVerifier


def main():
    print("=" * 60)
    print("PHASE 2: FORMAL VERIFICATION OF NEURAL CBF")
    print("=" * 60)

    # 1. Initialize Dubins Car System
    sys = DubinsCarSystem(tau=0.5, K=1.0, dt=0.02)

    # 2. Load Checkpoint
    cbf_net = NeuralCBF(state_dim=4, hidden_dims=[64, 64], activation="tanh")
    ckpt_path = "checkpoints/cbf_dubins.pt"
    if os.path.exists(ckpt_path):
        cbf_net.load_state_dict(torch.load(ckpt_path, map_location="cpu", weights_only=True))
        print(f"[+] Loaded model checkpoint from: {ckpt_path}")
    else:
        print("[!] Warning: Checkpoint not found, verifying baseline network.")

    # 3. Define State Domain around Obstacle [2.5, 0.0]
    # px in [0.5, 4.5], py in [-2.0, 2.0], theta in [-pi/2, pi/2], v in [0.5, 2.0]
    domain_min = np.array([0.5, -2.0, -np.pi / 2, 0.5], dtype=np.float32)
    domain_max = np.array([4.5,  2.0,  np.pi / 2, 2.0], dtype=np.float32)

    print(f"[+] Verifying Domain: px in [{domain_min[0]}, {domain_max[0]}] | py in [{domain_min[1]}, {domain_max[1]}]")

    verifier = RegionalCBFVerifier(system=sys, cbf_net=cbf_net, alpha_cbf=1.5)

    res = verifier.verify_domain(
        domain_min=domain_min,
        domain_max=domain_max,
        max_depth=10,
        max_boxes=400,
        time_limit_sec=20.0,
    )

    print("\n" + "=" * 60)
    print("VERIFICATION RESULTS SUMMARY")
    print("=" * 60)
    print(f"Status:                   {res['status']}")
    print(f"Verified Volume Ratio:    {res['verified_volume_ratio'] * 100:.2f}%")
    print(f"Total Boxes Explored:     {res['total_boxes_explored']}")
    print(f"Counterexamples Found:    {res['counterexamples_found']}")
    print(f"Wall-Clock Time:          {res['elapsed_seconds']:.2f} seconds")
    print("=" * 60)


if __name__ == "__main__":
    main()