From 2108fe2bc8b07c4f85d85904bcf1acb3f39d4168 Mon Sep 17 00:00:00 2001 From: Mark Bluemer Date: Sat, 29 Apr 2017 14:43:43 -0400 Subject: [PATCH 1/3] Some very basic variable depth --- include/Heuristic.hpp | 5 ++++- include/MinimaxSearch.h | 4 +++- src/Heuristic.cpp | 31 ++++++++++++++++++++++++++++++- src/MinimaxSearch.cpp | 24 +++++++++++++++++++----- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/include/Heuristic.hpp b/include/Heuristic.hpp index 37b43f5..2178dfe 100644 --- a/include/Heuristic.hpp +++ b/include/Heuristic.hpp @@ -6,7 +6,7 @@ class Heuristic { // Feature coefficients - int coeff[10] = {1,1,1,1,1,1,1,1,1,1}; + int coeff[11] = {1,1,1,1,1,1,1,1,1,1,1}; // Feature Functions int pawnCount() const; @@ -29,6 +29,9 @@ class Heuristic public: // Heuristic(int *co[10]) { coeff = *co;}; int evaluate(bup &bitboard); + + // Needed public for variable depth search + int jumpeablePieces() const; }; #endif diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h index be8d9ce..b40af94 100644 --- a/include/MinimaxSearch.h +++ b/include/MinimaxSearch.h @@ -13,12 +13,14 @@ typedef std::unique_ptr mup; class MinimaxSearch { private: - static const int maxDepth = 8; + static const int MIN_DEPTH = 3; + static const int MAX_DEPTH = 9; Heuristic heuristic; double minValue(std::unique_ptr &board, int currentDepth); double maxValue(std::unique_ptr &board, int currentDepth); double alphaBeta(std::unique_ptr &board, double alpha, double beta, int depth); + bool shouldStop(std::unique_ptr &board, int depth); public: diff --git a/src/Heuristic.cpp b/src/Heuristic.cpp index 208ba35..4bc150b 100644 --- a/src/Heuristic.cpp +++ b/src/Heuristic.cpp @@ -32,7 +32,8 @@ int Heuristic::evaluate(bup &bitboard){ coeff[6] * attackerCount() + coeff[7] * openPromotionCount() + coeff[8] * moveablePawns() + - coeff[9] * moveableKings(); + coeff[9] * moveableKings() + + coeff[10] * jumpeablePieces(); } // Number of pawns @@ -133,4 +134,32 @@ int Heuristic::moveableKings() const return (isBlacksTurn ? 1 : -1) * count(accumK); } +int Heuristic::jumpeablePieces() const +{ + uint32_t accumP = 0; + uint32_t temp = 0; + uint32_t kings = active & k; + uint32_t emptyPos = ~(b | w); + if (isBlacksTurn) { + temp = (board->rotr32(emptyPos, 7) & BB::downRight) & w; + accumP |= (board->rotr32(temp, 7) & BB::downRight) & b; + temp = (board->rotr32(emptyPos, 1) & BB::downLeft) & w; + accumP |= (board->rotr32(temp, 1) & BB::downLeft) & b; + temp = (board->rotl32(emptyPos, 7) & BB::upLeft) & w; + accumP |= (board->rotr32(temp, 7) & BB::upLeft) & kings; + temp = (board->rotl32(emptyPos, 1) & BB::upRight) & w; + accumP |= (board->rotl32(temp, 1) & BB::upRight) & kings; + } else { + temp = (board->rotl32(emptyPos, 7) & BB::upLeft) & b; + accumP |= (board->rotr32(temp, 7) & BB::upLeft) & w; + temp = (board->rotl32(emptyPos, 1) & BB::upRight) & b; + accumP |= (board->rotl32(temp, 1) & BB::upRight) & w; + temp = (board->rotr32(emptyPos, 7) & BB::downRight) & b; + accumP |= (board->rotr32(temp, 7) & BB::downRight) & kings; + temp = (board->rotr32(emptyPos, 1) & BB::downLeft) & b; + accumP |= (board->rotr32(temp, 1) & BB::downLeft) & kings; + } + return (isBlacksTurn ? 1 : -1) * count(accumP); +} + diff --git a/src/MinimaxSearch.cpp b/src/MinimaxSearch.cpp index b2ffda6..a832e5b 100644 --- a/src/MinimaxSearch.cpp +++ b/src/MinimaxSearch.cpp @@ -14,7 +14,7 @@ typedef std::unique_ptr mup; double MinimaxSearch::minValue(bup &board, int currentDepth) { - if (currentDepth == maxDepth) return heuristic.evaluate(board); + if (currentDepth == MAX_DEPTH) return heuristic.evaluate(board); double v = std::numeric_limits::max(); for (mup &move : board->actions()) { bup result = board->result(move); @@ -25,7 +25,7 @@ double MinimaxSearch::minValue(bup &board, int currentDepth) double MinimaxSearch::maxValue(bup &board, int currentDepth) { - if (currentDepth == maxDepth) return heuristic.evaluate(board); + if (currentDepth == MAX_DEPTH) return heuristic.evaluate(board); double v = std::numeric_limits::lowest(); for (mup &move : board->actions()) { bup result = board->result(move); @@ -35,9 +35,9 @@ double MinimaxSearch::maxValue(bup &board, int currentDepth) } double MinimaxSearch::alphaBeta(bup &board, double alpha, double beta, int depth) -{ - if (depth == maxDepth) return heuristic.evaluate(board); - +{ + if (shouldStop(board, depth)) return heuristic.evaluate(board); +// // black is max player here double v; if (board->player() == "black") { @@ -60,6 +60,20 @@ double MinimaxSearch::alphaBeta(bup &board, double alpha, double beta, int depth return v; } + +bool MinimaxSearch::shouldStop(bup &board, int depth) +{ + // always return at some max depth regardless + if (depth == MAX_DEPTH) return true; + + bool jumpAvailable = heuristic.jumpeablePieces() != 0; + + if (jumpAvailable) return false; + else if (depth >= MIN_DEPTH) return true; + else return false; + +} + /* Public Functions */ mup MinimaxSearch::minimaxDecision(bup &board) From 0b12a6370256d765d4a08b82701939a49811273c Mon Sep 17 00:00:00 2001 From: Mark Bluemer Date: Sat, 29 Apr 2017 15:33:04 -0400 Subject: [PATCH 2/3] fixed bug and change min depth to 5 --- include/BitBoard.h | 1 + include/MinimaxSearch.h | 2 +- src/BitBoard.cpp | 40 +++++++++++++++++++++++++++++++++------- src/MinimaxSearch.cpp | 2 +- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/include/BitBoard.h b/include/BitBoard.h index 7f8f290..3d1ffbf 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -89,6 +89,7 @@ class BitBoard vmup actions() const; bup result(mup &move) const; void printState() const; + std::string toString() const; }; #endif diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h index b40af94..b92ffec 100644 --- a/include/MinimaxSearch.h +++ b/include/MinimaxSearch.h @@ -13,7 +13,7 @@ typedef std::unique_ptr mup; class MinimaxSearch { private: - static const int MIN_DEPTH = 3; + static const int MIN_DEPTH = 5; static const int MAX_DEPTH = 9; Heuristic heuristic; diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 07316ab..ec78925 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -125,18 +125,32 @@ BitBoard BitBoard::boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo) c int diff = std::abs((double)pieceLoc - moveLoc); int avg = ((pieceLoc + moveLoc) / 2); if (board.m_isBlacksTurn) { - if (diff == 2 || diff == 14) white &= ~(1<= MAX_DEPTH) return true; bool jumpAvailable = heuristic.jumpeablePieces() != 0; From c03815ae0ede2a2fb1f2c3d3a0869bceaad7d637 Mon Sep 17 00:00:00 2001 From: Mark Bluemer Date: Sat, 29 Apr 2017 15:35:33 -0400 Subject: [PATCH 3/3] simplify variable depth function --- src/MinimaxSearch.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/MinimaxSearch.cpp b/src/MinimaxSearch.cpp index 2cd45a3..8e066d1 100644 --- a/src/MinimaxSearch.cpp +++ b/src/MinimaxSearch.cpp @@ -65,10 +65,7 @@ bool MinimaxSearch::shouldStop(bup &board, int depth) { // always return at some max depth regardless if (depth >= MAX_DEPTH) return true; - - bool jumpAvailable = heuristic.jumpeablePieces() != 0; - - if (jumpAvailable) return false; + else if (heuristic.jumpeablePieces() != 0) return false; else if (depth >= MIN_DEPTH) return true; else return false;