Skip to content

Commit

Permalink
TT move and score extraction fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MetatransApps committed Jul 30, 2022
1 parent 1940e22 commit e65917d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public void clearHistoryHeuristics() {
Arrays.fill(HH_MOVES[BLACK], 0);
Arrays.fill(BF_MOVES[WHITE], 1);
Arrays.fill(BF_MOVES[BLACK], 1);
Arrays.fill(LMR_ALL[WHITE], 1);
Arrays.fill(LMR_ALL[BLACK], 1);
Arrays.fill(LMR_ALL[WHITE], 0);
Arrays.fill(LMR_ALL[BLACK], 0);
Arrays.fill(LMR_ABOVE_ALPHA[WHITE], 0);
Arrays.fill(LMR_ABOVE_ALPHA[BLACK], 0);

Expand Down Expand Up @@ -158,29 +158,43 @@ public void addLMR_AboveAlpha(final int color, final int move, final int depth)
}


public int getLMR_Rate(final int color, final int move) {

int fromToIndex = MoveUtil.getFromToIndex(move);

return LMR_STAT_MULTIPLIER * LMR_ABOVE_ALPHA[color][fromToIndex] / LMR_ALL[color][fromToIndex];
}


public VarStatistic updateLMRAboveAlpha_Stats(final int color, VarStatistic stats) {
public VarStatistic updateLMRStats(final int color, VarStatistic stats) {

stats.clear();

for (int fromToIndex = 0; fromToIndex < LMR_ALL[color].length; fromToIndex++) {

int rate = LMR_STAT_MULTIPLIER * LMR_ABOVE_ALPHA[color][fromToIndex] / LMR_ALL[color][fromToIndex];
boolean has_stats = LMR_ALL[color][fromToIndex] != 0;

stats.addValue(rate);
if (has_stats) { //Prevent adding zeros for from_to squares without statistics yet

stats.addValue(getLMR_Rate_internal(color, fromToIndex));
}
}

return stats;
}


public int getLMR_Rate(final int color, final int move) {

int fromToIndex = MoveUtil.getFromToIndex(move);

return getLMR_Rate_internal(color, fromToIndex);
}


private int getLMR_Rate_internal(final int color, final int fromToIndex) {

if (LMR_ALL[color][fromToIndex] == 0) {

return 0;
}

return LMR_STAT_MULTIPLIER * LMR_ABOVE_ALPHA[color][fromToIndex] / LMR_ALL[color][fromToIndex];
}


public void addKillerMove(final int move, final int ply) {
if (EngineConstants.ENABLE_KILLER_MOVES) {
if (KILLER_MOVE_1[ply] != move) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ protected boolean isDraw() {
}


protected boolean isDrawPV(int depth) {
protected boolean isDrawPV(int ply) {

//Skip the draw check for the root, we need at least one move in the pv
if (depth == 0) {
if (ply == 0) {

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ public class Search_PVS_NWS extends SearchImpl {
private long lastSentMinorInfo_timestamp;
private long lastSentMinorInfo_nodesCount;


private VarStatistic historyAVGScores;

private VarStatistic lmrAboveAlphaAVGScores_white;
private VarStatistic lmrAboveAlphaAVGScores_black;

private static final boolean USE_LMR_ABOVE_ALPHA = false;


private boolean USE_DTZ_CACHE = false;


private IEvalEntry temp_cache_entry;


Expand Down Expand Up @@ -777,17 +783,20 @@ If the engine needs to know the DTZ value (which is only necessary when a TB roo

if (EngineConstants.ENABLE_LMR && reduction != 1) {

moveGen.addLMR_All(cb.colorToMoveInverse, move, depth);
if (USE_LMR_ABOVE_ALPHA) moveGen.addLMR_All(cb.colorToMoveInverse, move, depth);

score = -search(mediator, info, pvman, evaluator, cb, moveGen, ply + 1, depth - reduction, -alpha - 1, -alpha, false, 0);

if (score > alpha) {

moveGen.addLMR_AboveAlpha(cb.colorToMoveInverse, move, depth);
if (USE_LMR_ABOVE_ALPHA) {

moveGen.updateLMRAboveAlpha_Stats(cb.colorToMoveInverse, getLMRStats(cb.colorToMoveInverse));

//System.out.println(getLMRStats(cb.colorToMoveInverse).getEntropy());
if (score > alpha) {

moveGen.addLMR_AboveAlpha(cb.colorToMoveInverse, move, depth);

moveGen.updateLMRStats(cb.colorToMoveInverse, getLMRStats(cb.colorToMoveInverse));

//System.out.println("COLOR " + cb.colorToMoveInverse + " ENTROPY=" + getLMRStats(cb.colorToMoveInverse).getEntropy() + " DISPERSE=" + getLMRStats(cb.colorToMoveInverse).getDisperse());
}
}
}

Expand Down Expand Up @@ -887,14 +896,11 @@ If the engine needs to know the DTZ value (which is only necessary when a TB roo
}


if (!SearchUtils.isMateVal(bestScore)) {
if (env.getTPT() != null) {

if (env.getTPT() != null) {

env.getTPT().put(hashkey, depth, bestScore, alphaOrig, beta, bestMove);
}
env.getTPT().put(hashkey, depth, bestScore, alphaOrig, beta, bestMove);
}


if (bestScore != node.eval) {

Expand Down Expand Up @@ -938,7 +944,7 @@ public int qsearch(ISearchMediator mediator, PVManager pvman, IEvaluator evaluat
if (isDraw()) {

node.eval = getDrawScores(-1);
return node.eval;
}

Expand Down Expand Up @@ -1141,12 +1147,9 @@ public int qsearch(ISearchMediator mediator, PVManager pvman, IEvaluator evaluat
}

//TODO: Maybe this puts are too often and SMP version could have [problems 10 times lower NPS - to check
if (!SearchUtils.isMateVal(alpha)) {

if (env.getTPT() != null) {
if (env.getTPT() != null) {

env.getTPT().put(cb.zobristKey, 0, bestScore, alphaOrig, beta, bestMove);
}
env.getTPT().put(cb.zobristKey, 0, bestScore, alphaOrig, beta, bestMove);
}

} else {
Expand Down Expand Up @@ -1209,15 +1212,15 @@ private boolean extractFromTT(int ply, PVNode result, ITTEntry entry, ISearchInf
}

result.leaf = true;

if (ply > 0 && isDraw()) {

result.eval = getDrawScores(-1); //EvalConstants.SCORE_DRAW;
if ((isPv && isDrawPV(ply)) || (ply > 0 && !isPv && isDraw())) {

result.eval = getDrawScores(-1);

result.bestmove = 0;

return true;
}
}


if (info != null && info.getSelDepth() < ply) {
Expand All @@ -1235,34 +1238,37 @@ private boolean extractFromTT(int ply, PVNode result, ITTEntry entry, ISearchInf

if (isPv) {

if (!env.getBitboard().isPossible(result.bestmove)) {

throw new IllegalStateException("!env.getBitboard().isPossible(result.bestmove)");
}

env.getBitboard().makeMoveForward(result.bestmove);

ply++;

env.getTPT().get(env.getBitboard().getHashKey(), tt_entries_per_ply[ply]);

if (!tt_entries_per_ply[ply].isEmpty()) {
if (ply < ISearch.MAX_DEPTH) {

if (!env.getBitboard().isPossible(result.bestmove)) {

throw new IllegalStateException("!env.getBitboard().isPossible(result.bestmove)");
}

draw = extractFromTT(ply, result.child, tt_entries_per_ply[ply], info, isPv);
env.getBitboard().makeMoveForward(result.bestmove);

env.getTPT().get(env.getBitboard().getHashKey(), tt_entries_per_ply[ply]);

if (draw) {
if (!tt_entries_per_ply[ply].isEmpty()) {

result.eval = getDrawScores(-1);

} else {
draw = extractFromTT(ply, result.child, tt_entries_per_ply[ply], info, isPv);


result.leaf = false;
if (draw) {

result.eval = getDrawScores(-1);

} else {

result.leaf = false;
}
}

env.getBitboard().makeMoveBackward(result.bestmove);
}

env.getBitboard().makeMoveBackward(result.bestmove);
}
}

Expand All @@ -1271,8 +1277,6 @@ private boolean extractFromTT(int ply, PVNode result, ITTEntry entry, ISearchInf
}




private Stack<Integer> stack = new Stack<Integer>();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import bagaturchess.bitboard.impl1.internal.Assert;
import bagaturchess.bitboard.impl1.internal.EngineConstants;
import bagaturchess.bitboard.impl1.internal.Util;
import bagaturchess.search.impl.alg.SearchUtils;
import bagaturchess.uci.api.ChannelManager;


Expand Down Expand Up @@ -135,6 +136,11 @@ public void get(long key, ITTEntry entry) {
@Override
public void put(long hashkey, int depth, int eval, int alpha, int beta, int bestmove) {

if (SearchUtils.isMateVal(eval)) {

return;
}

int flag = ITTEntry.FLAG_EXACT;

if (eval >= beta) {
Expand Down

0 comments on commit e65917d

Please sign in to comment.