Skip to content

Commit

Permalink
Adds support for hashfull in info reply
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Apr 4, 2024
1 parent c4fb738 commit 616afa3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>com.fathzer</groupId>
<artifactId>games-core</artifactId>
<version>0.0.10-SNAPSHOT</version>
<version>0.0.11-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/fathzer/jchess/uci/ClassicalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ private ClassicalOptions() {
super();
}

/** Gets the standard option to accept Chess 960 games.
* @param trigger The consumer to call when value is changed.
* @return The standard {@value ClassicalOptions#CHESS960_NAME} option.
*/
public static CheckOption chess960(Consumer<Boolean> trigger) {
return new CheckOption(CHESS960_NAME, trigger, false);
}
Expand All @@ -50,10 +54,20 @@ public static CheckOption ponder(Consumer<Boolean> trigger, boolean defaultValue
return new CheckOption(PONDER_NAME, trigger, defaultValue);
}

/** Gets an spin option to set the engine strength.
* @param trigger The consumer to call when value is changed.
* @param maxValue The maximum value of the option.
* @return an option whose name is {@value ClassicalOptions#LEVEL_NAME}, the default value is {@code maxValue} and minimal value is 0.
*/
public static SpinOption<Integer> level(Consumer<Integer> trigger, int maxValue) {
return new IntegerSpinOption(LEVEL_NAME, trigger, maxValue, 0, maxValue);
}

/** Gets an spin option to set the number of CPU threads used for searching a position.
* @param trigger The consumer to call when value is changed.
* @param maxValue The default value of the option.
* @return an option whose name is {@value ClassicalOptions#THREADS_NAME}, the minimal value is 1 and and maximal value is the number of available processors reported by {@code Runtime.getRuntime().availableProcessors()}
*/
public static SpinOption<Integer> threads(Consumer<Integer> trigger, int defaultValue) {
return new IntegerSpinOption(THREADS_NAME, trigger, defaultValue, 1, Runtime.getRuntime().availableProcessors());
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/fathzer/jchess/uci/GoReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static class Info {
private List<UCIMove> extraMoves;
private Function<UCIMove, Optional<List<UCIMove>>> pvBuilder;
private Function<UCIMove, Optional<Score>> scoreBuilder;
private int hashFull;

/** Constructor.
* @param depth The search depth.
Expand All @@ -68,11 +69,23 @@ public Info(int depth) {
this.extraMoves = Collections.emptyList();
this.pvBuilder = m -> Optional.empty();
this.scoreBuilder = m -> Optional.empty();
this.hashFull = -1;
}

public int getDepth() {
return depth;
}

public int getHashFull() {
return hashFull;
}

/** Sets the transposition table occupancy in per mill.
* @param hashFull An integer. -1 if the occupancy is unknown
*/
public void setHashFull(int hashFull) {
this.hashFull = hashFull;
}

public List<UCIMove> getExtraMoves() {
return extraMoves;
Expand Down Expand Up @@ -155,6 +168,12 @@ public Optional<String> getMainInfoString() {
}
builder.append("score ").append(score.get().toUCI());
}
if (info.hashFull>0) {
if (!builder.isEmpty()) {
builder.append(' ');
}
builder.append("hashfull ").append(info.hashFull);
}
final Optional<List<UCIMove>> pv = info.pvBuilder.apply(bestMove);
if (pv.isPresent()) {
if (!builder.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ public GoReply get() {
final EvaluatedMove<M> move = best.get().move();
final GoReply goReply = new GoReply(toUCI(move.getContent()));
final Info info = new Info(best.get().depth());
final TranspositionTable<M> tt = engine.getTranspositionTable();
final int entryCount = tt.getEntryCount();
if (entryCount>0) {
info.setHashFull((int)(1000L*entryCount/tt.getSize()));
}
info.setScoreBuilder(m -> toScore(toMove(m), move));
info.setPvBuilder(m -> {
final List<UCIMove> list = engine.getTranspositionTable().collectPV(board, toMove(m), info.getDepth()).stream().map(x -> toUCI(x)).toList();
final List<UCIMove> list = tt.collectPV(board, toMove(m), info.getDepth()).stream().map(x -> toUCI(x)).toList();
return list.isEmpty() ? Optional.empty() : Optional.of(list);
});
goReply.setInfo(info);
Expand Down

0 comments on commit 616afa3

Please sign in to comment.