Download scripts/verify_cbf.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/scripts/verify_cbf.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/scripts/verify_cbf.py
-
curl -L -o verify_cbf.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/scripts/verify_cbf.py
2.24 kB
| """ | |
| 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() | |