| """General utilities for S23DR 2026.""" | |
| import numpy as np | |
| import os | |
| from typing import Optional | |
| def set_seed(seed=42): | |
| import random | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| try: | |
| import torch | |
| torch.manual_seed(seed) | |
| if torch.cuda.is_available(): | |
| torch.cuda.manual_seed_all(seed) | |
| except ImportError: | |
| pass | |
| def get_hf_token(): | |
| return os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") | |
| def setup_hf_auth(): | |
| token = get_hf_token() | |
| if token: | |
| from huggingface_hub import login | |
| login(token=token, add_to_git_credential=False) | |
| return True | |
| return False | |
| def empty_solution(): | |
| return np.zeros((2, 3)), [(0, 1)], [6] | |
| def validate_solution(vertices, edges): | |
| if vertices is None or edges is None: | |
| return False | |
| if len(vertices) < 2 or len(edges) < 1: | |
| return False | |
| max_idx = len(vertices) - 1 | |
| for a, b in edges: | |
| if a < 0 or b < 0 or a > max_idx or b > max_idx or a == b: | |
| return False | |
| return True | |
| def format_time(seconds): | |
| if seconds < 60: | |
| return f"{seconds:.1f}s" | |
| elif seconds < 3600: | |
| return f"{seconds/60:.1f}m" | |
| else: | |
| return f"{seconds/3600:.1f}h" | |