Skip to content

Commit

Permalink
Adapts to games-core changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Jan 19, 2024
1 parent cdd92ed commit 79e9ad6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import com.fathzer.chess.utils.Pieces;
import com.fathzer.chess.utils.adapters.BoardExplorerBuilder;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.ai.evaluation.AbstractEvaluator;
import com.fathzer.games.ai.evaluation.Evaluator;
import com.fathzer.games.ai.evaluation.ZeroSumEvaluator;
import com.fathzer.games.util.Stack;

/** A naive <a href="https://www.chessprogramming.org/Incremental_Updates">incremental</a> evaluator that only uses the <a href="https://en.wikipedia.org/wiki/Chess_piece_relative_value">standard valuation of pieces</a>.
* @param <M> The type of moves
* @param <B> The type of chess board
*/
public abstract class AbstractNaiveEvaluator<M, B extends MoveGenerator<M>> extends AbstractEvaluator<M, B> implements Evaluator<M, B>, BoardExplorerBuilder<B> {
public abstract class AbstractNaiveEvaluator<M, B extends MoveGenerator<M>> implements ZeroSumEvaluator<M, B>, Evaluator<M, B>, BoardExplorerBuilder<B> {
private final Stack<Integer> scores;
private int toCommit;

Expand Down Expand Up @@ -64,9 +64,7 @@ public int evaluateAsWhite(B board) {

@Override
public AbstractNaiveEvaluator<M, B> fork() {
final AbstractNaiveEvaluator<M, B> result = fork(scores.get());
result.viewPoint = this.viewPoint;
return result;
return fork(scores.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.fathzer.chess.utils.adapters.MoveData;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.ai.evaluation.Evaluator;
import com.fathzer.games.ai.evaluation.ZeroSumEvaluator;
import com.fathzer.games.util.Stack;

/** An incremental implementation of the simplified evaluator described at <a href="https://www.chessprogramming.org/Simplified_Evaluation_Function">https://www.chessprogramming.org/Simplified_Evaluation_Function</a>
* <br>This only works with 8*8 games and exactly one king per Color.
*/
public abstract class AbstractIncrementalSimplifiedEvaluator<M, B extends MoveGenerator<M>> extends SimplifiedEvaluatorBase<M, B> implements Evaluator<M,B>, Supplier<MoveData<M,B>> {
public abstract class AbstractIncrementalSimplifiedEvaluator<M, B extends MoveGenerator<M>> extends SimplifiedEvaluatorBase<M, B> implements ZeroSumEvaluator<M,B>, Supplier<MoveData<M,B>> {
private final Stack<IncrementalState> states;
private IncrementalState toCommit;
private MoveData<M, B> moveData;
Expand All @@ -35,9 +36,7 @@ protected AbstractIncrementalSimplifiedEvaluator(IncrementalState state) {

@Override
public Evaluator<M, B> fork() {
final AbstractIncrementalSimplifiedEvaluator<M, B> result = fork(states.get());
result.viewPoint = this.viewPoint;
return result;
return fork(states.get());
}

/** Creates a new instance initialized with current state that will become the initial state of created instance.
Expand Down Expand Up @@ -80,7 +79,7 @@ public void unmakeMove() {
}

@Override
protected int evaluateAsWhite(B board) {
public int evaluateAsWhite(B board) {
return states.get().evaluateAsWhite();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import com.fathzer.chess.utils.Pieces;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.ai.evaluation.AbstractEvaluator;

abstract class SimplifiedEvaluatorBase<M, B extends MoveGenerator<M>> extends AbstractEvaluator<M, B> implements BoardExplorerBuilder<B> {
abstract class SimplifiedEvaluatorBase<M, B extends MoveGenerator<M>> implements BoardExplorerBuilder<B> {
private static final int[] PIECE_VALUES = {0, 100, 320, 330, 500, 900, 20000};
private static final int[] KING_MID_GAME_EVAL = new int[] {
-30,-40,-40,-50,-50,-40,-40,-30,
Expand Down Expand Up @@ -84,6 +83,10 @@ abstract class SimplifiedEvaluatorBase<M, B extends MoveGenerator<M>> extends Ab
-20,-10,-10, -5, -5,-10,-10,-20
}};

SimplifiedEvaluatorBase() {
super();
}

static int getPositionValue(int type, boolean black, int index) {
return getPositionValue(PIECE_POSITION_VALUES[type], index, black);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fathzer.chess.utils.adapters.chesslib.BasicMoveDecoder;
import com.fathzer.chess.utils.adapters.chesslib.ChessLibBoardExplorer;
import com.fathzer.chess.utils.adapters.chesslib.ChessLibMoveGenerator;
import com.fathzer.games.Color;
import com.fathzer.games.MoveGenerator.MoveConfidence;
import com.fathzer.games.ai.evaluation.Evaluator;
import com.github.bhlangonijr.chesslib.Piece;
Expand Down Expand Up @@ -51,11 +50,10 @@ private static class ATest {
private final ChessLibNaiveEvaluator eval;
private final ChessLibMoveGenerator mvg;

private ATest(String fen, Color viewPoint, int expectedEval) {
private ATest(String fen, int expectedEval) {
this.mvg = FENUtils.from(fen);
this.eval = new ChessLibNaiveEvaluator();
this.eval.init(mvg);
eval.setViewPoint(viewPoint);
assertEquals(expectedEval, eval.evaluate(mvg));
}

Expand All @@ -70,58 +68,11 @@ private void test(Move move, int expectedEval) {
}
assertEquals (expectedEval, incEvaluation, "Error for move "+move+" on "+mvg.getBoard().getFen());
}

private int unmakeMove() {
mvg.unmakeMove();
eval.unmakeMove();
return eval.evaluate(mvg);
}
}

@Test
void testBlack() {
ATest test = new ATest("rn1qkb1r/1ppb1ppp/4pn2/pP1p4/3P1B2/4P3/P1P2PPP/RN1QKBNR w KQkq a6 0 6", Color.BLACK, 0);
// En passant from white
test.test(new Move(B5, A6), -100);
// No capture from black
test.test(new Move(F8, D6), -100);
// Capture from white
test.test(new Move(F4, D6), -400);
// Capture from black
test.test(new Move(C7, D6), -100);
// No Capture from white
test.test(new Move(B1, C3), -100);
// Castling from black
test.test(new Move(E8, G8), -100);
// Unmake moves
assertEquals(-100,test.unmakeMove());
assertEquals(-100,test.unmakeMove());
assertEquals(-400,test.unmakeMove());
assertEquals(-100,test.unmakeMove());
assertEquals(-100,test.unmakeMove());
assertEquals(0,test.unmakeMove());
}

@Test
void testWhite() {
ATest test = new ATest("r2qkb1r/1ppb1ppp/4pn2/pP1p4/3P1B2/4P3/P1P2PPP/RN1QKBNR w KQkq a6 0 6", Color.WHITE, 300);
// En passant from white
test.test(new Move(B5, A6), 400);
// No capture from black
test.test(new Move(F8, D6), 400);
// Capture from white
test.test(new Move(F4, D6), 700);
// Capture from black
test.test(new Move(C7, D6), 400);
// No Capture from white
test.test(new Move(B1,B3), 400);
// Castling from black
test.test(new Move(E8, G8), 400);
}

@Test
void testCurrentPlayer() {
ATest test = new ATest("rn1qkb1r/1ppb1ppp/4pn2/pP1p4/3P1B2/4P3/P1P2PPP/RN1QKBNR w KQkq a6 0 6", null, 0);
ATest test = new ATest("rn1qkb1r/1ppb1ppp/4pn2/pP1p4/3P1B2/4P3/P1P2PPP/RN1QKBNR w KQkq a6 0 6", 0);
// En passant from white
test.test(new Move(B5, A6), -100);
// No capture from black
Expand All @@ -138,28 +89,27 @@ void testCurrentPlayer() {

@Test
void theBlackPromotionCase() {
ATest test = new ATest("8/4P1n1/8/5P2/8/QK5k/1P3p2/8 b - - 0 1", Color.WHITE, 800);
ATest test = new ATest("8/4P1n1/8/8/8/QK5k/1P3p2/8 b - - 0 1", -700);
// En passant from white
test.test(new Move(F2, F1, Piece.BLACK_QUEEN), 0);
test.test(new Move(F2, F1, Piece.BLACK_QUEEN), -100);
}

@Test
void testFork() {
ChessLibMoveGenerator mvg = FENUtils.from("r2qkb1r/1ppb1ppp/4pn2/pP1p4/3P1B2/4P3/P1P2PPP/RN1QKBNR w KQkq a6 0 6");
Evaluator<Move, ChessLibMoveGenerator> eval = new ChessLibNaiveEvaluator();
eval.init(mvg);
eval.setViewPoint(Color.BLACK);
assertEquals(-300, eval.evaluate(mvg));
assertEquals(300, eval.evaluate(mvg));

ChessLibMoveGenerator mvg2 = (ChessLibMoveGenerator) mvg.fork();
Evaluator<Move, ChessLibMoveGenerator> eval2 = eval.fork();
assertEquals(-300, eval2.evaluate(mvg2));
assertEquals(300, eval2.evaluate(mvg2));
// En passant from white
Move move = new Move(B5, A6);
eval2.prepareMove(mvg, move);
eval2.prepareMove(mvg2, move);
assertTrue(mvg2.makeMove(move, MoveConfidence.UNSAFE));
eval2.commitMove();
assertEquals(-400, eval2.evaluate(mvg2));
assertEquals(-300, eval.evaluate(mvg));
assertEquals(300, eval.evaluate(mvg));
}
}

0 comments on commit 79e9ad6

Please sign in to comment.