Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6c0054b
Rewrite how the board is stored and moves are made
razvanfilea Nov 20, 2019
1c41cd3
Remove 'ToList' MoveGen template arg, make some fixes
razvanfilea Nov 22, 2019
2bfdff3
Fix a critical error with Board::doMove()
razvanfilea Nov 24, 2019
9db8df9
Fix getPhase always returning ENDGAME
razvanfilea Nov 24, 2019
8fd4e8c
Improve the Evaluation function performance
razvanfilea Nov 24, 2019
bee2ee1
Fix Board::hasValidState and update Pawn PSQT
razvanfilea Nov 26, 2019
68d9853
Switch to the previous Multi Threading model
razvanfilea Nov 28, 2019
7154003
Remove KING_DANGER MoveGen
razvanfilea Nov 28, 2019
eb1963c
Add age attribute to the TranspositionTable
razvanfilea Nov 29, 2019
2da9711
Fix crash when generating legal moves
razvanfilea Nov 30, 2019
f922ceb
Rewrite how the UI processes positions
razvanfilea Nov 30, 2019
220b4d7
Rename NegaMax class to Search
razvanfilea Nov 30, 2019
ee721fd
Work on the ThreadPool
razvanfilea Nov 30, 2019
9a9e5ac
Solve a bug with Castling and disable EnPassant for now
razvanfilea Nov 30, 2019
faba2c2
Add Generated Nodes to Stats
razvanfilea Dec 1, 2019
d73bb5c
Remake the Settings Screen
razvanfilea Dec 1, 2019
9b46c3e
Clean ThreadSafeQueue and ThreadPool
razvanfilea Dec 1, 2019
d5d5ae0
Revert to the old Pawn PSQT
razvanfilea Dec 8, 2019
1b60b38
Re-enable EnPassant
razvanfilea Dec 9, 2019
c0be29b
Add Platform Specific Implementations for BSF and BSR
razvanfilea Dec 10, 2019
8a64bbc
Remove StackVector
razvanfilea Dec 10, 2019
7889cfa
Fix compile errors
razvanfilea Dec 10, 2019
e35ab03
Remake the Pawn Evaluation add Connected Bonus
razvanfilea Dec 11, 2019
459400d
Merge Rays.h into Bitboard.h
razvanfilea Dec 11, 2019
9be2e38
Fix Pawn Move Generation from last commit
razvanfilea Dec 12, 2019
9a37891
Remove Light Theme, add Rook on Queen File Evaluation
razvanfilea Dec 15, 2019
b81a38c
Rewrite Piece class to only use 1 byte
razvanfilea Dec 15, 2019
3a122a9
New icon
razvanfilea Dec 15, 2019
99a93b5
Release 1.0
razvanfilea Dec 15, 2019
b8651a6
Fix issues with the Pull Request
razvanfilea Dec 15, 2019
8c8a8d9
Delete MoveOrdering.h
razvanfilea Dec 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Switch to the previous Multi Threading model
  • Loading branch information
razvanfilea committed Nov 28, 2019
commit 68d9853cc625b632eb890586405b600d0d0033fe
204 changes: 48 additions & 156 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/NegaMax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "../threads/NegaMaxThreadPool.h"

bool NegaMax::s_QuiescenceSearchEnabled{};
std::size_t NegaMax::s_ThreadCount{};
TranspositionTable NegaMax::s_SearchCache(1);
short NegaMax::s_BestMoveFound{};

Expand All @@ -24,7 +23,6 @@ RootMove NegaMax::findBestMove(const Board &board, const Settings &settings)
// Apply Settings
short depth = settings.getBaseSearchDepth() - 1;
const auto threadCount = settings.getThreadCount();
s_ThreadCount = threadCount;
s_QuiescenceSearchEnabled = settings.performQuiescenceSearch();

// If the Transposition Table wasn't resized, clean it
Expand All @@ -45,35 +43,24 @@ short NegaMax::getBestMoveFound()

RootMove NegaMax::negaMaxRoot(const std::vector<RootMove> &validMoves, const unsigned jobCount, const short ply)
{
short alpha = VALUE_MIN;
RootMove bestMove = validMoves.front();

for (const RootMove &move : validMoves)
{
const short moveScore = -negaMax(move.board, ply, VALUE_MIN, -alpha, 1, false);

if (moveScore > alpha)
{
alpha = moveScore;
bestMove = move;
}
}
/*std::vector<ThreadPool::TaskFuture<void>> futures;
std::vector<ThreadPool::TaskFuture<void>> futures;
futures.reserve(jobCount);

std::mutex mutex;
RootMove bestMove = validMoves.front();
auto currentMove = validMoves.begin();
const auto lastMove = validMoves.end();
short alpha = VALUE_MIN;

const auto doWork = [&] {
mutex.lock();

while (!validMoves.empty())
while (currentMove != lastMove)
{
// Make a copy of the needed variables while locked
const RootMove &move = *currentMove;
++currentMove;
const short beta = -alpha;
const RootMove move = validMoves.front();
validMoves.pop_front();

mutex.unlock(); // Process the result asynchronously
const short result = -negaMax(move.board, ply, VALUE_MIN, beta, 1, false);
Expand All @@ -91,7 +78,7 @@ RootMove NegaMax::negaMaxRoot(const std::vector<RootMove> &validMoves, const uns

futures.emplace_back(NegaMaxThreadPool::submitJob(doWork));

if (jobCount != 1) {
if (jobCount > 1) {
// Wait until an alpha bound has been found
while (alpha == VALUE_MIN)
std::this_thread::yield();
Expand All @@ -101,7 +88,7 @@ RootMove NegaMax::negaMaxRoot(const std::vector<RootMove> &validMoves, const uns
}

for (auto &future : futures)
future.get(); // Wait for the Search to finish*/
future.get(); // Wait for the Search to finish

s_BestMoveFound = alpha;

Expand Down Expand Up @@ -142,161 +129,66 @@ short NegaMax::negaMax(const Board &board, const short ply, short alpha, short b
++Stats::nodesSearched;

const auto validMoves = board.listValidMoves<Board>();
auto currentMove = validMoves.begin();
const auto lastMove = validMoves.end();
short bestScore = VALUE_MIN;
short movesCount = 0;

if (ply == 4)
for (const Board &move : validMoves)
{
const auto jobCount = std::min<unsigned>(s_ThreadCount, validMoves.size());
std::vector<ThreadPool::TaskFuture<>> futures;
futures.reserve(jobCount);
std::mutex mutex;
bool keepSearching = true;

const auto doWork = [&] {
mutex.lock();

while (keepSearching && currentMove != lastMove)
if (move.state == State::WINNER_WHITE || move.state == State::WINNER_BLACK)
{
// Mate Pruning
const short mateValue = VALUE_WINNER_WHITE - depth;
if (mateValue < beta)
{
const Board &move = *currentMove;
++currentMove;

if (move.state == State::WINNER_WHITE || move.state == State::WINNER_BLACK)
{
// Mate Pruning
const short mateValue = VALUE_WINNER_WHITE - depth;
if (mateValue < beta)
{
beta = mateValue;
if (alpha >= mateValue)
{
bestScore = mateValue;
keepSearching = false;
break;
}
}

if (mateValue > alpha)
{
alpha = mateValue;
if (beta <= mateValue)
{
bestScore = mateValue;
keepSearching = false;
break;
}
}

if (bestScore > mateValue)
bestScore = mateValue;
continue;
}

// Make a copy of the needed variables while locked
const short alphaCopy = alpha;
const short betaCopy = beta;

mutex.unlock(); // Process the result asynchronously
const short moveScore = -negaMax(move, ply - 1, -betaCopy, -alphaCopy, depth + 1, false);
mutex.lock();

if (moveScore > bestScore)
bestScore = moveScore;

// Alpha-Beta Pruning
if (bestScore >= beta)
beta = mateValue;
if (alpha >= mateValue)
{
keepSearching = false;
bestScore = mateValue;
break;
}
if (bestScore > alpha)
alpha = bestScore;

++movesCount;
}

mutex.unlock();
};

futures.emplace_back(NegaMaxThreadPool::submitJob(doWork));

if (jobCount > 1)
{
// Wait until the first move has been searched
while (keepSearching && movesCount < 1)
std::this_thread::yield();

if (keepSearching) // Don't start the other threads if a cut-off has happened
if (mateValue > alpha)
{
for (unsigned i = 1u; i < jobCount; ++i)
futures.emplace_back(NegaMaxThreadPool::submitJob(doWork));
}
}

for (auto &future : futures)
future.get(); // Wait for the Search to finish
futures.clear();
} else {
for (const Board &move : validMoves)
{
if (move.state == State::WINNER_WHITE || move.state == State::WINNER_BLACK)
{
// Mate Pruning
const short mateValue = VALUE_WINNER_WHITE - depth;
if (mateValue < beta)
alpha = mateValue;
if (beta <= mateValue)
{
beta = mateValue;
if (alpha >= mateValue)
{
bestScore = mateValue;
break;
}
}

if (mateValue > alpha)
{
alpha = mateValue;
if (beta <= mateValue)
{
bestScore = mateValue;
break;
}
}

if (bestScore > mateValue)
bestScore = mateValue;
continue;
break;
}
}

short moveScore = alpha + 1;
if (bestScore > mateValue)
bestScore = mateValue;
continue;
}

short moveScore = alpha + 1;

// Late Move Reductions
if (!moveCountPruning &&
movesCount > 4 &&
depth >= 3 &&
ply >= 3 &&
move.state != State::WHITE_IN_CHECK &&
move.state != State::BLACK_IN_CHECK &&
!move.isCapture &&
!move.isPromotion)
moveScore = -negaMax(move, ply - 2, -moveScore, -alpha, depth + 1, true);
// Late Move Reductions
if (!moveCountPruning &&
movesCount > 4 &&
depth >= 3 &&
ply >= 3 &&
move.state != State::WHITE_IN_CHECK &&
move.state != State::BLACK_IN_CHECK &&
!move.isCapture &&
!move.isPromotion)
moveScore = -negaMax(move, ply - 2, -moveScore, -alpha, depth + 1, true);

if (moveScore > alpha)
moveScore = -negaMax(move, ply - 1, -beta, -alpha, depth + 1, false);
if (moveScore > alpha)
moveScore = -negaMax(move, ply - 1, -beta, -alpha, depth + 1, false);

if (moveScore > bestScore)
bestScore = moveScore;
if (moveScore > bestScore)
bestScore = moveScore;

// Alpha-Beta Pruning
if (bestScore >= beta)
break;
if (bestScore > alpha)
alpha = bestScore;
// Alpha-Beta Pruning
if (bestScore >= beta)
break;
if (bestScore > alpha)
alpha = bestScore;

++movesCount;
}
++movesCount;
}

// Store the result in the transposition table
Expand Down
7 changes: 4 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/data/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,13 @@ std::vector<Board> Board::listQuiescenceMoves() const
const Piece &selectedPiece = pair.second;
U64 possibleMoves = selectedPiece.getPossibleCaptures(startSq, *this);

assert(possibleMoves == (possibleMoves & allPieces[oppositeColor(colorToMove)]));
// Make sure we are not capturing the king
possibleMoves &= getType(colorToMove, KING);

while (possibleMoves)
{
const byte destSq = Bitboard::findNextSquare(possibleMoves);
const Piece &destPiece = getPiece(destSq);
if (!destPiece || destPiece.type == PieceType::KING)
continue;

Board board = *this;
board.doMove(startSq, destSq);
Expand Down
6 changes: 3 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/data/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ std::vector<T> Board::listValidMoves() const noexcept
const Piece &selectedPiece = pair.second;
U64 possibleMoves = selectedPiece.getPossibleMoves(startSq, *this);

// Make sure we are not capturing the king
possibleMoves &= getType(colorToMove, KING);

while (possibleMoves)
{
const byte destSq = Bitboard::findNextSquare(possibleMoves);
const auto &destPiece = getPiece(destSq);
if (destPiece.type == PieceType::KING)
continue;

Board board = *this;
board.doMove(startSq, destSq);
Expand Down
5 changes: 1 addition & 4 deletions ChessAndroid/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.60'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

Expand Down