Skip to content

Commit

Permalink
Fine tune late move reduction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyde committed May 11, 2024
1 parent 188e653 commit affd96e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/kelseyde/calvin/engine/EngineConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ public class EngineConfig {
boolean searchCancelled = false;

int maxDepth;
int maxPossibleMoves = 250;
int aspMargin;
int aspFailMargin;
int nmpDepth;
int fpDepth;
int rfpDepth;
int lmrDepth;
float lmrBase;
float lmrDivisor;
int lmrMinSearchedMoves;
int lmpDepth;
int lmpMultiplier;
int iirDepth;
int nmpMargin;
int dpMargin;
int[][] lmrReductions;
int[] fpMargin;
int[] rfpMargin;

Expand Down Expand Up @@ -72,4 +77,19 @@ public class EngineConfig {

int tempoBonus;

public void postInitialise() {
calculateLmrReductions();
}

private void calculateLmrReductions() {
// Credit to Lynx (https://github.com/lynx-chess/Lynx) for this formula for determining the optimal late move reduction depth
lmrReductions = new int[maxDepth][];
for (int depth = 1; depth < maxDepth; ++depth) {
lmrReductions[depth] = new int[maxPossibleMoves];
for (int movesSearched = 1; movesSearched < maxPossibleMoves; ++movesSearched) {
lmrReductions[depth][movesSearched] = (int) Math.round(lmrBase + (Math.log(movesSearched) * Math.log(depth) / lmrDivisor));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class EngineInitializer {

public static Engine loadEngine() {
EngineConfig config = loadDefaultConfig();
config.postInitialise();
OpeningBook book = loadDefaultOpeningBook();
Supplier<MoveGeneration> moveGenerator = MoveGenerator::new;
Supplier<MoveOrdering> moveOrderer = MoveOrderer::new;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/kelseyde/calvin/search/Searcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -276,8 +277,8 @@ public int search(int depth, int ply, int alpha, int beta, boolean allowNull) {
// capture or promotion, let's assume it's less likely to be good, and fully skip searching that move.
if (!pvNode
&& !isInCheck
&& depth <= config.getLmpDepth()
&& isQuiet
&& depth <= config.getLmpDepth()
&& i >= depth * config.getLmpMultiplier()) {
board.unmakeMove();
continue;
Expand All @@ -286,9 +287,14 @@ public int search(int depth, int ply, int alpha, int beta, boolean allowNull) {
// capture or promotion, let's save time by assuming it's less likely to be good, and reduce the search depth.
int reduction = 0;
if (depth >= config.getLmrDepth()
&& i >= 2
&& i >= (pvNode ? config.getLmrMinSearchedMoves() : config.getLmrMinSearchedMoves() - 1)
&& isQuiet) {
reduction = i < 5 ? 1 : depth / 3;
reduction = config.getLmrReductions()[depth][i];
if (pvNode || isInCheck) {
reduction--;
}
//System.out.printf("depth %s, moves searched %s, reduction %s%n", depth, i, reduction);
//reduction = i < 5 ? 1 : depth / 3;
}

// For all other moves apart from the principal variation, search with a null window (-alpha - 1, -alpha),
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/engine_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"fpDepth":6,
"rfpDepth":4,
"lmrDepth":3,
"lmrMinSearchedMoves": 3,
"lmrBase": 0.85,
"lmrDivisor": 3.12,
"lmpDepth":2,
"lmpMultiplier":10,
"iirDepth":4,
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/kelseyde/calvin/utils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ private static EngineConfig loadConfig(String configLocation) {
ObjectMapper mapper = new ObjectMapper();
Path path = Paths.get(configLocation);
String json = Files.readString(path);
return mapper.readValue(json, EngineConfig.class);
EngineConfig config = mapper.readValue(json, EngineConfig.class);
config.postInitialise();
return config;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ timestamp,performance
2024-05-09T12:55:01Z,PT8.864766S
2024-05-09T12:55:14Z,PT8.680054S
2024-05-09T13:01:47Z,PT6.410241S
2024-05-11T17:49:51Z,PT4.587096S

0 comments on commit affd96e

Please # to comment.