File size: 4,270 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0075", "name": "Implement aspiration windows with iterative deepening for more efficient alpha-beta search"}
{"type": "message", "message": {"role": "system", "content": "You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance."}}
{"type": "message", "message": {"role": "user", "content": "Implement aspiration windows with iterative deepening for more efficient alpha-beta search"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n\nstruct AspirationSearch {\n    static const int INF = 999999;\n\n    struct SearchInfo {\n        int score;\n        int depth;\n        int nodes;\n        int selDepth;\n        uint64_t hash;\n        int timeMs;\n    };\n\n    SearchInfo aspirationSearch(BitboardBoard& board, int maxDepth) {\n        SearchInfo info = {};\n        int prevScore = 0;\n        int alpha, beta;\n\n        for (int depth = 1; depth <= maxDepth; depth++) {\n            if (depth <= 4) {\n                // Full window search for first few depths\n                alpha = -INF;\n                beta = INF;\n            } else {\n                // Aspiration window: ±50 centipawns around previous score\n                int delta = 50;\n                alpha = prevScore - delta;\n                beta = prevScore + delta;\n\n                while (true) {\n                    int score = alphaBeta(board, depth, alpha, beta, true);\n\n                    if (score <= alpha) {\n                        // Fail low: widen window downward\n                        alpha = std::max(alpha - delta * 4, -INF);\n                    } else if (score >= beta) {\n                        // Fail high: widen window upward\n                        beta = std::min(beta + delta * 4, INF);\n                    } else {\n                        // Exact score within window\n                        prevScore = score;\n                        break;\n                    }\n                    delta *= 4;\n                }\n            }\n\n            int score = alphaBeta(board, depth, alpha, beta, true);\n            prevScore = score;\n            info.score = score;\n            info.depth = depth;\n\n            // Soft time management\n            if (depth >= 6) {\n                double elapsed = getElapsedMs();\n                double projected = elapsed * (std::pow((double)35, depth) /\n                                             std::pow((double)35, depth - 1));\n                if (projected > timeLimit * 0.6) break;\n            }\n        }\n\n        return info;\n    }\n\n    int alphaBeta(BitboardBoard& board, int depth, int alpha, int beta, bool isRoot) {\n        if (depth <= 0) return quiesce(board, alpha, beta);\n\n        TTEntry* ttEntry = tt.probe(board.getHash());\n        if (ttEntry && ttEntry->depth >= depth && !isRoot) {\n            if (ttEntry->flag == TT_EXACT) return ttEntry->score;\n            if (ttEntry->flag == TT_ALPHA && ttEntry->score <= alpha) return alpha;\n            if (ttEntry->flag == TT_BETA && ttEntry->score >= beta) return beta;\n        }\n\n        Move moves[256];\n        int numMoves = generateLegalMoves(board, moves);\n        sortMoves(moves, numMoves);\n\n        int bestScore = -INF;\n        Move bestMove = moves[0];\n\n        for (int i = 0; i < numMoves; i++) {\n            board.makeMove(moves[i]);\n            int score = -alphaBeta(board, depth - 1, -beta, -alpha, false);\n            board.unmakeMove(moves[i]);\n\n            if (score > bestScore) {\n                bestScore = score;\n                bestMove = moves[i];\n            }\n            if (score > alpha) alpha = score;\n            if (score >= beta) {\n                if (!isCapture(moves[i])) {\n                    historyHeuristic[board.sideToMove][moves[i].from][moves[i].to] += depth * depth;\n                }\n                break;\n            }\n        }\n\n        // Store in TT\n        int flag = bestScore <= alpha ? TT_ALPHA : bestScore >= beta ? TT_BETA : TT_EXACT;\n        tt.store(board.getHash(), depth, bestScore, flag, bestMove.encode());\n\n        return bestScore;\n    }\n};"}}