From 34a76d6a697cdd07ab5c8bc7da6def56311ec0df Mon Sep 17 00:00:00 2001 From: Fathzer Date: Sat, 6 Jan 2024 10:16:48 +0100 Subject: [PATCH] Simplifies UCI code by using jchess-uci enhancements --- .../jchess/lichess/DefaultOpenings.java | 2 +- .../com/fathzer/jchess/uci/JChessUCI.java | 3 +- .../fathzer/jchess/uci/JChessUCIEngine.java | 77 ++++++------------- .../uci/UCIEngineSearchConfiguration.java | 59 -------------- 4 files changed, 28 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/com/fathzer/jchess/uci/UCIEngineSearchConfiguration.java diff --git a/src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java b/src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java index abb4b28..12d6403 100644 --- a/src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java +++ b/src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java @@ -61,6 +61,6 @@ public Move apply(Board board) { return null; } final LibraryMove move = opening.getMoves().get(RND.nextInt(opening.getMoves().size())); - return JChessUCIEngine.toMove(board.getCoordinatesSystem(), UCIMove.from(move.getCoord()), board.getActiveColor()); + return JChessUCIEngine.toMove(board, UCIMove.from(move.getCoord())); } } diff --git a/src/main/java/com/fathzer/jchess/uci/JChessUCI.java b/src/main/java/com/fathzer/jchess/uci/JChessUCI.java index efd49b6..1552b28 100644 --- a/src/main/java/com/fathzer/jchess/uci/JChessUCI.java +++ b/src/main/java/com/fathzer/jchess/uci/JChessUCI.java @@ -23,10 +23,11 @@ import com.fathzer.jchess.ai.evaluator.BasicEvaluator; import com.fathzer.jchess.ai.evaluator.simple.SimpleEvaluator; import com.fathzer.jchess.fen.FENUtils; +import com.fathzer.jchess.uci.extended.ExtendedUCI; import com.fathzer.plugin.loader.jar.JarPluginLoader; import com.fathzer.plugin.loader.utils.FileUtils; -public class JChessUCI extends UCI { +public class JChessUCI extends ExtendedUCI { public static void main(String[] args) { try (UCI uci = new JChessUCI()) { final JarPluginLoader loader = new JarPluginLoader(); diff --git a/src/main/java/com/fathzer/jchess/uci/JChessUCIEngine.java b/src/main/java/com/fathzer/jchess/uci/JChessUCIEngine.java index b797aa1..e5c99c0 100644 --- a/src/main/java/com/fathzer/jchess/uci/JChessUCIEngine.java +++ b/src/main/java/com/fathzer/jchess/uci/JChessUCIEngine.java @@ -6,10 +6,8 @@ import java.util.Optional; import java.util.function.Supplier; -import com.fathzer.games.Color; -import com.fathzer.games.MoveGenerator; -import com.fathzer.games.MoveGenerator.MoveConfidence; import com.fathzer.games.ai.evaluation.Evaluator; +import com.fathzer.games.ai.time.BasicTimeManager; import com.fathzer.games.perft.TestableMoveGeneratorBuilder; import com.fathzer.games.util.PhysicalCores; import com.fathzer.jchess.Board; @@ -22,12 +20,16 @@ import com.fathzer.jchess.fen.FENUtils; import com.fathzer.jchess.generic.BasicMove; import com.fathzer.jchess.lichess.DefaultOpenings; +import com.fathzer.jchess.time.VuckovicSolakOracle; +import com.fathzer.jchess.uci.extended.Displayable; +import com.fathzer.jchess.uci.helper.AbstractEngine; import com.fathzer.jchess.uci.option.ComboOption; import com.fathzer.jchess.uci.option.Option; import com.fathzer.jchess.uci.option.SpinOption; -import com.fathzer.jchess.uci.parameters.GoParameters; -public class JChessUCIEngine implements Engine, MoveGeneratorSupplier, TestableMoveGeneratorBuilder>, MoveToUCIConverter { +public class JChessUCIEngine extends AbstractEngine> implements TestableMoveGeneratorBuilder>, Displayable { + private static final BasicTimeManager> TIME_MANAGER = new BasicTimeManager<>(VuckovicSolakOracle.INSTANCE); + private static final int SILLY_LEVEL_DEPTH = 4; private static final int AVERAGE_LEVEL_DEPTH = 6; private static final int BEST_LEVEL_DEPTH = 14; @@ -38,11 +40,11 @@ public class JChessUCIEngine implements Engine, MoveGeneratorSupplier, Tes private static final String NAIVE_EVALUATOR = "naive"; private static final String SIMPLIFIED_EVALUATOR = "simplified"; - private Board board; private final JChessEngine engine; public JChessUCIEngine() { - engine = new JChessEngine(SimpleEvaluator::new, AVERAGE_LEVEL_DEPTH); + super (new JChessEngine(SimpleEvaluator::new, AVERAGE_LEVEL_DEPTH), TIME_MANAGER); + engine = (JChessEngine) this.getEngine(); engine.setOpenings(DefaultOpenings.INSTANCE); engine.getDeepeningPolicy().setDeepenOnForced(false); } @@ -103,15 +105,16 @@ public boolean isChess960Supported() { } @Override - public void move(UCIMove move) { - board.makeMove(toMove(board.getCoordinatesSystem(), move, board.getActiveColor()), MoveConfidence.UNSAFE); + protected Move toMove(UCIMove move) { + return toMove(board, move); } - - public static Move toMove(CoordinatesSystem cs, UCIMove move, Color color) { + + public static Move toMove(Board board, UCIMove move) { + final CoordinatesSystem cs = board.getCoordinatesSystem(); final int from = cs.getIndex(move.getFrom()); final int to = cs.getIndex(move.getTo()); String promotion = move.getPromotion(); - if (promotion!=null && Color.WHITE.equals(color)) { + if (promotion!=null && board.isWhiteToMove()) { // Warning the promotion code is always in lowercase promotion = promotion.toUpperCase(); } @@ -130,56 +133,26 @@ private static Move toMove(int from, int to, String promotionAsFen) { } } } - - @Override - public void setStartPosition(String fen) { - board = FENUtils.from(fen); - board.setMoveComparatorBuilder(engine.getMoveComparatorSupplier()); - } - - @Override - public MoveGenerator fromFEN(String fen) { - return FENUtils.from(fen); - } - - @Override - public LongRunningTask go(GoParameters options) { - return new LongRunningTask<>() { - @Override - public BestMoveReply get() { - final UCIEngineSearchConfiguration c = new UCIEngineSearchConfiguration(); - final UCIEngineSearchConfiguration.EngineConfiguration previous = c.configure(engine, options, board); - final Move move = engine.apply(board); - c.set(engine, previous); - return new BestMoveReply(toMove(board.getCoordinatesSystem(), move)); - } - - @Override - public void stop() { - super.stop(); - engine.interrupt(); - } - }; - } - private static UCIMove toMove(CoordinatesSystem cs, Move move) { + private static UCIMove toUCIMove(CoordinatesSystem cs, Move move) { final String promotion = move.getPromotion()==null ? null : move.getPromotion().getNotation().toLowerCase(); return new UCIMove(cs.getAlgebraicNotation(move.getFrom()), cs.getAlgebraicNotation(move.getTo()), promotion); } - @Override - public MoveGenerator get() { - return board.fork(); - } - @Override public UCIMove toUCI(Move move) { - return toMove(board.getCoordinatesSystem(), move); + return toUCIMove(board.getCoordinatesSystem(), move); } @Override - public String getBoardAsString() { - return board==null ? "no position defined" : board.toString(); + public void setStartPosition(String fen) { + board = FENUtils.from(fen); + board.setMoveComparatorBuilder(engine.getMoveComparatorSupplier()); + } + + @Override + public Board fromFEN(String fen) { + return FENUtils.from(fen); } @Override diff --git a/src/main/java/com/fathzer/jchess/uci/UCIEngineSearchConfiguration.java b/src/main/java/com/fathzer/jchess/uci/UCIEngineSearchConfiguration.java deleted file mode 100644 index 59ee467..0000000 --- a/src/main/java/com/fathzer/jchess/uci/UCIEngineSearchConfiguration.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.fathzer.jchess.uci; - -import com.fathzer.games.Color; -import com.fathzer.games.ai.iterativedeepening.IterativeDeepeningEngine; -import com.fathzer.games.ai.time.BasicTimeManager; -import com.fathzer.games.clock.CountDownState; -import com.fathzer.jchess.Board; -import com.fathzer.jchess.Move; -import com.fathzer.jchess.ai.JChessEngine; -import com.fathzer.jchess.time.VuckovicSolakOracle; -import com.fathzer.jchess.uci.parameters.GoParameters; -import com.fathzer.jchess.uci.parameters.GoParameters.PlayerClockData; -import com.fathzer.jchess.uci.parameters.GoParameters.TimeOptions; - -/** A class that configures the engine before executing the go command - */ -public class UCIEngineSearchConfiguration { - private static final BasicTimeManager> TIME_MANAGER = new BasicTimeManager<>(VuckovicSolakOracle.INSTANCE); - - public static class EngineConfiguration { - private long maxTime; - private int depth; - - private EngineConfiguration(IterativeDeepeningEngine engine) { - maxTime = engine.getDeepeningPolicy().getMaxTime(); - depth = engine.getDeepeningPolicy().getDepth(); - } - } - - public EngineConfiguration configure(JChessEngine engine, GoParameters options, Board board) { - final EngineConfiguration result = new EngineConfiguration(engine); - final TimeOptions timeOptions = options.getTimeOptions(); - if (options.isPonder() || !options.getMoveToSearch().isEmpty() || options.getMate()>0 || options.getNodes()>0 || timeOptions.isInfinite()) { - //TODO some options are not supported - } - if (options.getDepth()>0) { - engine.getDeepeningPolicy().setDepth(options.getDepth()); - } - if (timeOptions.getMoveTimeMs()>0) { - engine.getDeepeningPolicy().setMaxTime(timeOptions.getMoveTimeMs()); - } else { - final Color c = board.getActiveColor(); - final PlayerClockData engineClock = c==Color.WHITE ? timeOptions.getWhiteClock() : timeOptions.getBlackClock(); - if (engineClock.getRemainingMs()>0) { - engine.getDeepeningPolicy().setMaxTime(getMaxTime(board, engineClock.getRemainingMs(), engineClock.getIncrementMs(), timeOptions.getMovesToGo())); - } - } - return result; - } - - public void set(JChessEngine engine, EngineConfiguration c) { - engine.getDeepeningPolicy().setMaxTime(c.maxTime); - engine.getDeepeningPolicy().setDepth(c.depth); - } - - public long getMaxTime(Board board, long remainingMs, long incrementMs, int movesToGo) { - return TIME_MANAGER.getMaxTime(board, new CountDownState(remainingMs, incrementMs, movesToGo)); - } -}