Create engine/time_manager.py
Browse files- engine/time_manager.py +84 -0
engine/time_manager.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Time Manager for Nexus-Core
|
| 3 |
+
Simplified adaptive time control
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import time
|
| 7 |
+
from typing import Optional
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TimeManager:
|
| 11 |
+
"""Adaptive time allocation for searches"""
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.start_time = 0.0
|
| 15 |
+
self.allocated_time = 0.0
|
| 16 |
+
self.hard_limit = 0.0
|
| 17 |
+
self.move_overhead = 0.050 # 50ms overhead
|
| 18 |
+
|
| 19 |
+
def allocate_time(
|
| 20 |
+
self,
|
| 21 |
+
time_left: float,
|
| 22 |
+
increment: float = 0.0,
|
| 23 |
+
moves_to_go: Optional[int] = None,
|
| 24 |
+
move_number: int = 1
|
| 25 |
+
) -> tuple[float, float]:
|
| 26 |
+
"""Calculate time allocation"""
|
| 27 |
+
|
| 28 |
+
# Emergency reserve
|
| 29 |
+
emergency_reserve = min(2.0, time_left * 0.1)
|
| 30 |
+
available_time = max(0, time_left - emergency_reserve - self.move_overhead)
|
| 31 |
+
|
| 32 |
+
# Estimate moves remaining
|
| 33 |
+
if moves_to_go:
|
| 34 |
+
expected_moves = moves_to_go
|
| 35 |
+
else:
|
| 36 |
+
expected_moves = max(20, 40 - move_number // 2)
|
| 37 |
+
|
| 38 |
+
# Base allocation
|
| 39 |
+
base_time = available_time / expected_moves
|
| 40 |
+
increment_bonus = increment * 0.8
|
| 41 |
+
|
| 42 |
+
# Soft limit
|
| 43 |
+
soft_limit = base_time + increment_bonus
|
| 44 |
+
|
| 45 |
+
# Hard limit (3x soft or 50% remaining)
|
| 46 |
+
hard_limit = min(
|
| 47 |
+
soft_limit * 3.0,
|
| 48 |
+
available_time * 0.5
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Minimums
|
| 52 |
+
soft_limit = max(soft_limit, 0.1)
|
| 53 |
+
hard_limit = max(hard_limit, soft_limit)
|
| 54 |
+
|
| 55 |
+
return soft_limit, hard_limit
|
| 56 |
+
|
| 57 |
+
def start_search(self, allocated_time: float, hard_limit: float):
|
| 58 |
+
"""Initialize timer"""
|
| 59 |
+
self.start_time = time.time()
|
| 60 |
+
self.allocated_time = allocated_time
|
| 61 |
+
self.hard_limit = hard_limit
|
| 62 |
+
|
| 63 |
+
def should_stop(self, depth: int, best_move_stable: bool = False) -> bool:
|
| 64 |
+
"""Check if should stop"""
|
| 65 |
+
elapsed = time.time() - self.start_time
|
| 66 |
+
|
| 67 |
+
# Hard limit
|
| 68 |
+
if elapsed >= self.hard_limit:
|
| 69 |
+
return True
|
| 70 |
+
|
| 71 |
+
# Soft limit with stable move
|
| 72 |
+
if elapsed >= self.allocated_time:
|
| 73 |
+
if best_move_stable or depth >= 4:
|
| 74 |
+
return True
|
| 75 |
+
|
| 76 |
+
return False
|
| 77 |
+
|
| 78 |
+
def elapsed(self) -> float:
|
| 79 |
+
"""Get elapsed time"""
|
| 80 |
+
return time.time() - self.start_time
|
| 81 |
+
|
| 82 |
+
def remaining(self) -> float:
|
| 83 |
+
"""Get remaining time"""
|
| 84 |
+
return max(0, self.allocated_time - self.elapsed())
|