diff --git a/beans.bin b/beans.bin deleted file mode 100644 index 219a711..0000000 Binary files a/beans.bin and /dev/null differ diff --git a/nnue.bin b/nnue.bin new file mode 100644 index 0000000..74d336c Binary files /dev/null and b/nnue.bin differ diff --git a/source/accumulator.cpp b/source/accumulator.cpp index 6301ee8..fa3696d 100644 --- a/source/accumulator.cpp +++ b/source/accumulator.cpp @@ -4,7 +4,7 @@ namespace ACC { -int CalculateIndex(bool perspective, int square, int pieceType, bool side) { +int CalculateIndex(bool perspective, bool side, int pieceType, int square) { if (perspective == Black) { side = 1 - side; // Flip side @@ -14,15 +14,72 @@ int CalculateIndex(bool perspective, int square, int pieceType, bool side) { return side * 64 * 6 + pieceType * 64 + square; } -void Accumulator::Add(const NNUE::Network* net, int index) { - for (int i = 0; i < NNUE::HL_SIZE; i ++) { - values[i] += net->accumulator_weights[index * NNUE::INPUT_SIZE + i]; +// All friendly, for quiets +void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) { + int addW = CalculateIndex(White, stm, addPT, add); + int addB = CalculateIndex(Black, stm, addPT, add); + + int subW = CalculateIndex(White, stm, subPT, sub); + int subB = CalculateIndex(Black, stm, subPT, sub); + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; + black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; + + white.values[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i]; + black.values[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i]; } } -void Accumulator::Sub(const NNUE::Network* net, int index) { - for (int i = 0; i < NNUE::HL_SIZE; i ++) { - values[i] -= net->accumulator_weights[index * NNUE::INPUT_SIZE + i]; +// Captures +void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) { + int addW = CalculateIndex(White, stm, addPT, add); + int addB = CalculateIndex(Black, stm, addPT, add); + + int subW1 = CalculateIndex(White, stm, subPT1, sub1); + int subB1 = CalculateIndex(Black, stm, subPT1, sub1); + + int subW2 = CalculateIndex(White, ~stm, subPT2, sub2); + int subB2 = CalculateIndex(Black, ~stm, subPT2, sub2); + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; + black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; + + white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; + black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; + + white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; + black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; + } +} + +// Castling +void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) { + int addW1 = CalculateIndex(White, stm, addPT1, add1); + int addB1 = CalculateIndex(Black, stm, addPT1, add1); + + int addW2 = CalculateIndex(White, stm, addPT2, add2); + int addB2 = CalculateIndex(Black, stm, addPT2, add2); + + int subW1 = CalculateIndex(White, stm, subPT1, sub1); + int subB1 = CalculateIndex(Black, stm, subPT1, sub1); + + int subW2 = CalculateIndex(White, stm, subPT2, sub2); + int subB2 = CalculateIndex(Black, stm, subPT2, sub2); + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + white.values[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]; + black.values[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]; + + white.values[i] += NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i]; + black.values[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]; + + white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; + black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; + + white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; + black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; } } diff --git a/source/accumulator.h b/source/accumulator.h index bbf650c..7a6c1ca 100644 --- a/source/accumulator.h +++ b/source/accumulator.h @@ -5,16 +5,18 @@ namespace ACC { struct Accumulator { std::array values; - - void Add(const NNUE::Network* net, int index); - void Sub(const NNUE::Network* net, int index); }; struct AccumulatorPair { Accumulator white; Accumulator black; + + // yoinked from Quinniboi - Prelude + void addSub(bool stm, int add, int addPT, int sub, int subPT); + void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2); + void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2); }; -int CalculateIndex(bool perspective, int square, int pieceType, bool side); +int CalculateIndex(bool perspective, bool side, int pieceType, int square); } diff --git a/source/board.cpp b/source/board.cpp index 3d5429c..58a3698 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -278,9 +278,33 @@ bool Board::MakeMove(Move move) { int targetPiece = GetPieceType(move.MoveTo()); int direction = attackerColor ? south : north; + int endPiece = attackerPiece; + // Removing attacker piece from old position RemovePiece(attackerPiece, move.MoveFrom(), attackerColor); + if (move.IsPromo()) { + if (move.IsCapture()) { + endPiece = move.GetFlags() - 9; + Promote(move.MoveTo(), endPiece, sideToMove, true); + } else { + endPiece = move.GetFlags() - 5; + Promote(move.MoveTo(), endPiece, sideToMove, false); + } + } + + if (move.IsCapture()) { + if (move.GetFlags() != epCapture) { + accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece); + } else { + accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, enPassantTarget, Pawn); + } + } else { + if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) { + accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece); + } + } + switch (move.GetFlags()) { case quiet: @@ -312,6 +336,8 @@ bool Board::MakeMove(Move move) { SetPiece(Rook, rookSquare - 2, attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor); + + accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook); break; } case queenCastle: @@ -325,32 +351,9 @@ bool Board::MakeMove(Move move) { SetPiece(Rook, rookSquare + 3, attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor); + accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook); break; } - case knightPromotion: - Promote(move.MoveTo(), Knight, attackerColor, false); - break; - case bishopPromotion: - Promote(move.MoveTo(), Bishop, attackerColor, false); - break; - case rookPromotion: - Promote(move.MoveTo(), Rook, attackerColor, false); - break; - case queenPromotion: - Promote(move.MoveTo(), Queen, attackerColor, false); - break; - case knightPromoCapture: - Promote(move.MoveTo(), Knight, attackerColor, true); - break; - case bishopPromoCapture: - Promote(move.MoveTo(), Bishop, attackerColor, true); - break; - case rookPromoCapture: - Promote(move.MoveTo(), Rook, attackerColor, true); - break; - case queenPromoCapture: - Promote(move.MoveTo(), Queen, attackerColor, true); - break; default: break; } @@ -370,7 +373,6 @@ bool Board::MakeMove(Move move) { } enPassantTarget = newEpTarget; - MOVEGEN::GenThreatMaps(*this); @@ -387,7 +389,7 @@ bool Board::MakeMove(Move move) { } else { halfMoves++; } - + positionIndex++; positionHistory[positionIndex] = hashKey; @@ -416,8 +418,8 @@ void Board::ResetAccPair() { while (whitePieces) { int square = whitePieces.getLS1BIndex(); - int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), White); - int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), White); + int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square); + int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square); for (int i = 0; i < NNUE::HL_SIZE; i++) { accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; @@ -430,8 +432,8 @@ void Board::ResetAccPair() { while (blackPieces) { int square = blackPieces.getLS1BIndex(); - int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), Black); - int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), Black); + int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square); + int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square); for (int i = 0; i < NNUE::HL_SIZE; i++) { accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; diff --git a/source/main.cpp b/source/main.cpp index 069cfac..c13160b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char* argv[]) { UTILS::InitZobrist(); SEARCH::InitLMRTable(); - NNUE::net.Load("beans.bin"); + NNUE::net.Load("nnue.bin"); Board board; diff --git a/source/move.h b/source/move.h index ac7b9b7..d9ee537 100644 --- a/source/move.h +++ b/source/move.h @@ -60,6 +60,10 @@ public: || GetFlags() == queenCastle || GetFlags() == doublePawnPush); } + bool IsPromo() { + return (GetFlags() >= knightPromotion); + } + void PrintMove() { std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; diff --git a/source/search.cpp b/source/search.cpp index 0b76e9d..d2c5e60 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -156,7 +156,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { } } - int bestScore = HCE::Evaluate(board); + int bestScore = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board)); TTEntry *entry = TT.GetRawEntry(board.hashKey); if (entry->hashKey == board.hashKey) { @@ -239,7 +239,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { if (depth <= 0) return Quiescence(board, alpha, beta, ply); - const int staticEval = HCE::Evaluate(board); + const int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board)); if (!board.InCheck()) { if (ply) {