File size: 9,168 Bytes
1561c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33b3ec2
008dcf6
1561c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Nexus-Nano Search Engine
Fast alpha-beta with minimal overhead

Focus: Speed > Depth
Target: Sub-second responses
"""

import chess
import logging
from typing import Optional, Tuple, List, Dict

from .evaluate import NexusNanoEvaluator
from .transposition import TranspositionTable, NodeType
from .move_ordering import MoveOrderer
from .time_manager import TimeManager
from .endgame import EndgameDetector

logger = logging.getLogger(__name__)


class NexusNanoEngine:
    """Ultra-fast 2.8M parameter chess engine"""
    
    MATE_SCORE = 100000
    MAX_PLY = 100
    
    def __init__(self, model_path: str, num_threads: int = 1):
        """Initialize with single-threaded config"""
        
        self.evaluator = NexusNanoEvaluator(model_path, num_threads)
        self.tt = TranspositionTable(size_mb=64)  # 64MB TT
        self.move_orderer = MoveOrderer()
        self.time_manager = TimeManager()
        self.endgame_detector = EndgameDetector()
        
        self.nodes_evaluated = 0
        self.depth_reached = 0
        self.sel_depth = 0
        self.principal_variation = []
        
        logger.info("⚡ Nexus-Nano Engine initialized")
        logger.info(f"   Model: {self.evaluator.get_model_size_mb():.2f} MB")
        logger.info(f"   TT: 64 MB")
    
    def get_best_move(
        self,
        fen: str,
        depth: int = 4,
        time_limit: int = 2000
    ) -> Dict:
        """
        Fast move search
        
        Args:
            fen: Position
            depth: Max depth (1-6 recommended)
            time_limit: Time in ms
        """
        
        board = chess.Board(fen)
        
        # Reset
        self.nodes_evaluated = 0
        self.depth_reached = 0
        self.sel_depth = 0
        self.principal_variation = []
        
        # Time setup
        time_limit_sec = time_limit / 1000.0
        self.time_manager.start_search(time_limit_sec, time_limit_sec)
        
        # Clear old data
        self.move_orderer.clear()
        self.tt.increment_age()
        
        # Special cases
        legal_moves = list(board.legal_moves)
        
        if len(legal_moves) == 0:
            return self._no_legal_moves()
        
        if len(legal_moves) == 1:
            return self._single_move(board, legal_moves[0])
        
        # Iterative deepening (fast)
        best_move = legal_moves[0]
        best_score = float('-inf')
        
        for current_depth in range(1, depth + 1):
            if self.time_manager.should_stop(current_depth):
                break
            
            score, move, pv = self._search_root(
                board, current_depth, float('-inf'), float('inf')
            )
            
            if move:
                best_move = move
                best_score = score
                self.depth_reached = current_depth
                self.principal_variation = pv
        
        return {
            'best_move': best_move.uci(),
            'evaluation': round(best_score / 100.0, 2),
            'depth_searched': self.depth_reached,
            'seldepth': self.sel_depth,
            'nodes_evaluated': self.nodes_evaluated,
            'time_taken': int(self.time_manager.elapsed() * 1000),
            'pv': [m.uci() for m in self.principal_variation],
            'nps': int(self.nodes_evaluated / max(self.time_manager.elapsed(), 0.001)),
            'tt_stats': self.tt.get_stats(),
            'move_ordering_stats': self.move_orderer.get_stats()
        }
    
    def _search_root(
        self,
        board: chess.Board,
        depth: int,
        alpha: float,
        beta: float
    ) -> Tuple[float, Optional[chess.Move], List[chess.Move]]:
        """Root search"""
        
        legal_moves = list(board.legal_moves)
        
        # TT probe
        zobrist_key = self.tt.compute_zobrist_key(board)
        tt_result = self.tt.probe(zobrist_key, depth, alpha, beta)
        tt_move = tt_result[1] if tt_result else None
        
        # Order moves
        ordered_moves = self.move_orderer.order_moves(
            board, legal_moves, depth, tt_move
        )
        
        best_move = ordered_moves[0]
        best_score = float('-inf')
        best_pv = []
        
        for move in ordered_moves:
            board.push(move)
            score, pv = self._alpha_beta(board, depth - 1, -beta, -alpha)
            score = -score
            board.pop()
            
            if score > best_score:
                best_score = score
                best_move = move
                best_pv = [move] + pv
            
            if score > alpha:
                alpha = score
            
            if self.time_manager.should_stop(depth):
                break
        
        self.tt.store(zobrist_key, depth, best_score, NodeType.EXACT, best_move)
        
        return best_score, best_move, best_pv
    
    def _alpha_beta(
        self,
        board: chess.Board,
        depth: int,
        alpha: float,
        beta: float
    ) -> Tuple[float, List[chess.Move]]:
        """Fast alpha-beta search"""
        
        self.sel_depth = max(self.sel_depth, self.MAX_PLY - depth)
        
        # Draw check
        if board.is_repetition(2) or board.is_fifty_moves():
            return 0, []
        
        # TT probe
        zobrist_key = self.tt.compute_zobrist_key(board)
        tt_result = self.tt.probe(zobrist_key, depth, alpha, beta)
        
        if tt_result and tt_result[0] is not None:
            return tt_result[0], []
        
        tt_move = tt_result[1] if tt_result else None
        
        # Quiescence
        if depth <= 0:
            return self._quiescence(board, alpha, beta, 0), []
        
        # Legal moves
        legal_moves = list(board.legal_moves)
        if not legal_moves:
            if board.is_check():
                return -self.MATE_SCORE + (self.MAX_PLY - depth), []
            return 0, []
        
        ordered_moves = self.move_orderer.order_moves(
            board, legal_moves, depth, tt_move
        )
        
        # Search
        best_score = float('-inf')
        best_pv = []
        node_type = NodeType.UPPER_BOUND
        
        for move in ordered_moves:
            board.push(move)
            score, pv = self._alpha_beta(board, depth - 1, -beta, -alpha)
            score = -score
            board.pop()
            
            if score > best_score:
                best_score = score
                best_pv = [move] + pv
                
                if score > alpha:
                    alpha = score
                    node_type = NodeType.EXACT
                    
                    if not board.is_capture(move):
                        self.move_orderer.update_killer_move(move, depth)
                    
                    if score >= beta:
                        node_type = NodeType.LOWER_BOUND
                        break
        
        self.tt.store(zobrist_key, depth, best_score, node_type, best_pv[0] if best_pv else None)
        
        return best_score, best_pv
    
    def _quiescence(
        self,
        board: chess.Board,
        alpha: float,
        beta: float,
        qs_depth: int
    ) -> float:
        """Fast quiescence (captures only)"""
        
        self.nodes_evaluated += 1
        
        # Stand-pat
        stand_pat = self.evaluator.evaluate_hybrid(board)
        stand_pat = self.endgame_detector.adjust_evaluation(board, stand_pat)
        
        if stand_pat >= beta:
            return beta
        if alpha < stand_pat:
            alpha = stand_pat
        
        # Depth limit
        if qs_depth >= 6:
            return stand_pat
        
        # Captures only (no checks for speed)
        captures = [m for m in board.legal_moves if board.is_capture(m)]
        
        if not captures:
            return stand_pat
        
        captures = self.move_orderer.order_moves(board, captures, 0)
        
        for move in captures:
            board.push(move)
            score = -self._quiescence(board, -beta, -alpha, qs_depth + 1)
            board.pop()
            
            if score >= beta:
                return beta
            if score > alpha:
                alpha = score
        
        return alpha
    
    def _no_legal_moves(self) -> Dict:
        return {
            'best_move': '0000',
            'evaluation': 0.0,
            'depth_searched': 0,
            'nodes_evaluated': 0,
            'time_taken': 0
        }
    
    def _single_move(self, board: chess.Board, move: chess.Move) -> Dict:
        eval_score = self.evaluator.evaluate_hybrid(board)
        
        return {
            'best_move': move.uci(),
            'evaluation': round(eval_score / 100.0, 2),
            'depth_searched': 0,
            'nodes_evaluated': 1,
            'time_taken': 0,
            'pv': [move.uci()]
        }
    
    def validate_fen(self, fen: str) -> bool:
        try:
            chess.Board(fen)
            return True
        except:
            return False
    
    def get_model_size(self) -> float:
        return self.evaluator.get_model_size_mb()