-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
898 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/main/java/com/fathzer/jchess/chesslib/ChessLibMoveData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 31 additions & 66 deletions
97
src/main/java/com/fathzer/jchess/chesslib/ai/eval/hbpg2/Hb2BasicState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,63 @@ | ||
package com.fathzer.jchess.chesslib.ai.eval.hbpg2; | ||
|
||
import static com.fathzer.chess.utils.Pieces.KING; | ||
|
||
|
||
import com.fathzer.chess.utils.adapters.BoardExplorer; | ||
import com.fathzer.jchess.chesslib.ai.eval.hbpg2.additional.ChessEvalAdditionalElems; | ||
import com.github.bhlangonijr.chesslib.Board; | ||
|
||
/** The state of the evaluator. | ||
*/ | ||
class Hb2BasicState extends Hb2FastPhaseDetector { | ||
int pointsMg; | ||
int pointsEg; | ||
int pointsPosMg; | ||
int pointsPosEg; | ||
int whiteKingIndex; | ||
int blackKingIndex; | ||
int computedPhase; | ||
Board board; | ||
|
||
class Hb2BasicState extends Hb2ElementaryBasicState { | ||
protected ChessEvalAdditionalElems chessEvalAdditionalElems; | ||
|
||
Hb2BasicState() { | ||
super(); | ||
} | ||
|
||
void copyTo(Hb2BasicState other) { | ||
public void copyTo(Hb2BasicState other) { | ||
super.copyTo(other); | ||
other.pointsMg = pointsMg; | ||
other.pointsEg= pointsEg; | ||
other.pointsPosMg = pointsPosMg; | ||
other.pointsPosEg= pointsPosEg; | ||
other.blackKingIndex = blackKingIndex; | ||
other.whiteKingIndex = whiteKingIndex; | ||
other.computedPhase = computedPhase; | ||
other.board = board; | ||
other.chessEvalAdditionalElems = new ChessEvalAdditionalElems(this.chessEvalAdditionalElems); | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
Hb2BasicState(BoardExplorer explorer, Board board) { | ||
this.board = board; | ||
this.pointsMg = 0; | ||
this.pointsEg = 0; | ||
this.pointsPosMg = 0; | ||
this.pointsPosEg = 0; | ||
this.computedPhase = 0; | ||
do { | ||
final int p = explorer.getPiece(); | ||
// add(p); | ||
final int kind = Math.abs(p); | ||
final int index = explorer.getIndex(); | ||
final boolean isBlack = p<0; | ||
if (kind!=KING) { | ||
int incMg = Hb2SimplifiedEvaluatorBase.getRawValueMg(kind); | ||
int incEg = Hb2SimplifiedEvaluatorBase.getRawValueEg(kind); | ||
if (isBlack) { | ||
pointsMg -= incMg; | ||
pointsEg -= incEg; | ||
} else { | ||
pointsMg += incMg; | ||
pointsEg += incEg; | ||
} | ||
} else if (isBlack) { | ||
this.blackKingIndex = index; | ||
} else { | ||
this.whiteKingIndex = index; | ||
} | ||
|
||
|
||
|
||
if (kind!=KING) { | ||
int incPosMg = Hb2SimplifiedEvaluatorBase.getPositionValueMg(kind, isBlack, index); | ||
int incPosEg = Hb2SimplifiedEvaluatorBase.getPositionValueEg(kind, isBlack, index); | ||
if (isBlack) { | ||
pointsPosMg -= incPosMg; | ||
pointsPosEg -= incPosEg; | ||
} else { | ||
pointsPosMg += incPosMg; | ||
pointsPosEg += incPosEg; | ||
} | ||
} | ||
computedPhase += Hb2Phase.getPhaseValue(kind); | ||
} while (explorer.next()); | ||
super(explorer, board); | ||
chessEvalAdditionalElems = new ChessEvalAdditionalElems( board); | ||
|
||
|
||
} | ||
|
||
public ChessEvalAdditionalElems getChessEvalAdditionalElems() { | ||
return chessEvalAdditionalElems; | ||
} | ||
|
||
public void setChessEvalAdditionalElems(ChessEvalAdditionalElems chessEvalAdditionalElems) { | ||
this.chessEvalAdditionalElems = chessEvalAdditionalElems; | ||
} | ||
|
||
|
||
int evaluateAsWhite() { | ||
|
||
// pointsMg = material only! The white material minus the black material in the middlegame. | ||
// pointsEg = material only! The white material minus the black material in the endgame. | ||
int phase = getPhaseForTaperedEval(computedPhase); | ||
// int phase = Hb2Phase.getPhaseForTaperedEval(computedPhase); | ||
// gets the borned phase: necessary for the tapered evaluation | ||
int phase= (computedPhase > Hb2Phase.PHASE_UPPER_BOUND?Hb2Phase.PHASE_UPPER_BOUND:computedPhase); | ||
// int pointsPosMg = Hb2SimplifiedEvaluatorBase.getPositionValueMg(board); | ||
// int pointsPosEg = Hb2SimplifiedEvaluatorBase.getPositionValueEg(board); | ||
int evalMg = pointsMg + pointsPosMg + Hb2SimplifiedEvaluatorBase.getKingPositionsValueMg(whiteKingIndex, blackKingIndex); | ||
int evalEg = pointsEg + pointsPosEg+ Hb2SimplifiedEvaluatorBase.getKingPositionsValueEg(whiteKingIndex, blackKingIndex); | ||
int posKingMg = Hb2SimplifiedEvaluatorBase.getKingPositionsValueMg(whiteKingIndex, blackKingIndex); | ||
int posKingEg = Hb2SimplifiedEvaluatorBase.getKingPositionsValueEg(whiteKingIndex, blackKingIndex); | ||
int evalMg = pointsMg + pointsPosMg + posKingMg + chessEvalAdditionalElems.getContribMg(); | ||
int evalEg = pointsEg + pointsPosEg+ posKingEg + chessEvalAdditionalElems.getContribEg(); | ||
|
||
return ((evalMg * phase + evalEg * (Hb2Phase.NB_INCR_PHASE-phase)) / Hb2Phase.NB_INCR_PHASE); | ||
} | ||
|
||
|
||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/fathzer/jchess/chesslib/ai/eval/hbpg2/Hb2BitboardsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.fathzer.jchess.chesslib.ai.eval.hbpg2; | ||
|
||
public class Hb2BitboardsUtils { | ||
|
||
// This class is built mainly thanks to shameful plagiarism from https://github.com/Luecx/Chess.git (GNU Public License) | ||
// That said, the guy took probably his inspiration from the C++ code described here: https://habr.com/ru/articles/682122/ | ||
|
||
public static final long H_FILE = 0x8080808080808080L; | ||
public static final long G_FILE = H_FILE >>> 1; | ||
public static final long F_FILE = H_FILE >>> 2; | ||
public static final long E_FILE = H_FILE >>> 3; | ||
public static final long D_FILE = H_FILE >>> 4; | ||
public static final long C_FILE = H_FILE >>> 5; | ||
public static final long B_FILE = H_FILE >>> 6; | ||
public static final long A_FILE = H_FILE >>> 7; | ||
|
||
public static final long RANK_1 = 0x00000000000000FFL; | ||
public static final long RANK_2 = RANK_1 << 8; | ||
public static final long RANK_3 = RANK_1 << 16; | ||
public static final long RANK_4 = RANK_1 << 24; | ||
public static final long RANK_5 = RANK_1 << 32; | ||
public static final long RANK_6 = RANK_1 << 40; | ||
public static final long RANK_7 = RANK_1 << 48; | ||
public static final long RANK_8 = RANK_1 << 56; | ||
|
||
public static final long NOT_A_FILE = ~A_FILE; | ||
public static final long NOT_H_FILE = ~H_FILE; | ||
public static final long NOT_RANK_1 = ~RANK_1; | ||
public static final long NOT_RANK_8 = ~RANK_8; | ||
|
||
public static final long[] WHITE_PASSED_PAWNS_MASK = new long[] { 0x0303030303030300L, 0x0707070707070700L, | ||
0x0e0e0e0e0e0e0e00L, 0x1c1c1c1c1c1c1c00L, 0x3838383838383800L, 0x7070707070707000L, 0xe0e0e0e0e0e0e000L, | ||
0xc0c0c0c0c0c0c000L, 0x0303030303030000L, 0x0707070707070000L, 0x0e0e0e0e0e0e0000L, 0x1c1c1c1c1c1c0000L, | ||
0x3838383838380000L, 0x7070707070700000L, 0xe0e0e0e0e0e00000L, 0xc0c0c0c0c0c00000L, 0x0303030303000000L, | ||
0x0707070707000000L, 0x0e0e0e0e0e000000L, 0x1c1c1c1c1c000000L, 0x3838383838000000L, 0x7070707070000000L, | ||
0xe0e0e0e0e0000000L, 0xc0c0c0c0c0000000L, 0x0303030300000000L, 0x0707070700000000L, 0x0e0e0e0e00000000L, | ||
0x1c1c1c1c00000000L, 0x3838383800000000L, 0x7070707000000000L, 0xe0e0e0e000000000L, 0xc0c0c0c000000000L, | ||
0x0303030000000000L, 0x0707070000000000L, 0x0e0e0e0000000000L, 0x1c1c1c0000000000L, 0x3838380000000000L, | ||
0x7070700000000000L, 0xe0e0e00000000000L, 0xc0c0c00000000000L, 0x0303000000000000L, 0x0707000000000000L, | ||
0x0e0e000000000000L, 0x1c1c000000000000L, 0x3838000000000000L, 0x7070000000000000L, 0xe0e0000000000000L, | ||
0xc0c0000000000000L, 0x0300000000000000L, 0x0700000000000000L, 0x0e00000000000000L, 0x1c00000000000000L, | ||
0x3800000000000000L, 0x7000000000000000L, 0xe000000000000000L, 0xc000000000000000L, 0x0000000000000000L, | ||
0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, | ||
0x0000000000000000L, 0x0000000000000000L }; | ||
|
||
public static final long[] BLACK_PASSED_PAWNS_MASK = new long[] { | ||
|
||
0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, | ||
0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000003L, 0x0000000000000007L, | ||
0x000000000000000eL, 0x000000000000001cL, 0x0000000000000038L, 0x0000000000000070L, 0x00000000000000e0L, | ||
0x00000000000000c0L, 0x0000000000000303L, 0x0000000000000707L, 0x0000000000000e0eL, 0x0000000000001c1cL, | ||
0x0000000000003838L, 0x0000000000007070L, 0x000000000000e0e0L, 0x000000000000c0c0L, 0x0000000000030303L, | ||
0x0000000000070707L, 0x00000000000e0e0eL, 0x00000000001c1c1cL, 0x0000000000383838L, 0x0000000000707070L, | ||
0x0000000000e0e0e0L, 0x0000000000c0c0c0L, 0x0000000003030303L, 0x0000000007070707L, 0x000000000e0e0e0eL, | ||
0x000000001c1c1c1cL, 0x0000000038383838L, 0x0000000070707070L, 0x00000000e0e0e0e0L, 0x00000000c0c0c0c0L, | ||
0x0000000303030303L, 0x0000000707070707L, 0x0000000e0e0e0e0eL, 0x0000001c1c1c1c1cL, 0x0000003838383838L, | ||
0x0000007070707070L, 0x000000e0e0e0e0e0L, 0x000000c0c0c0c0c0L, 0x0000030303030303L, 0x0000070707070707L, | ||
0x00000e0e0e0e0e0eL, 0x00001c1c1c1c1c1cL, 0x0000383838383838L, 0x0000707070707070L, 0x0000e0e0e0e0e0e0L, | ||
0x0000c0c0c0c0c0c0L, 0x0003030303030303L, 0x0007070707070707L, 0x000e0e0e0e0e0e0eL, 0x001c1c1c1c1c1c1cL, | ||
0x0038383838383838L, 0x0070707070707070L, 0x00e0e0e0e0e0e0e0L, 0x00c0c0c0c0c0c0c0L, }; | ||
|
||
public static final long shiftWest(long b) { | ||
return (b >>> 1) & NOT_H_FILE; | ||
} | ||
|
||
public static final long shiftEast(long b) { | ||
return (b << 1) & NOT_A_FILE; | ||
} | ||
|
||
public static final long shiftSouth(long b) { | ||
return b >>> 8; | ||
} | ||
|
||
public static final long shiftNorth(long b) { | ||
return b << 8; | ||
} | ||
|
||
public static final long shiftNorthEast(long b) { | ||
return (b << 9) & NOT_A_FILE; | ||
} | ||
|
||
public static final long shiftSouthEast(long b) { | ||
return (b >>> 7) & NOT_A_FILE; | ||
} | ||
|
||
public static final long shiftSouthWest(long b) { | ||
return (b >>> 9) & NOT_H_FILE; | ||
} | ||
|
||
public static final long shiftNorthWest(long b) { | ||
return (b << 7) & NOT_H_FILE; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/fathzer/jchess/chesslib/ai/eval/hbpg2/Hb2ChessConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fathzer.jchess.chesslib.ai.eval.hbpg2; | ||
|
||
public abstract class Hb2ChessConstants { | ||
public static final int NB_RANKS = 8; | ||
public static final int NB_FILES = 8; | ||
public static final int INDEX_MAX_RANK = NB_RANKS-1; | ||
public static final int INDEX_MAX_FILE = NB_FILES-1; | ||
|
||
|
||
|
||
public static final int NB_MAX_PAWNS_TAKEN_INTO_ACCOUNT_FOR_MALUS_DOUBLED_PAWNS = 2; // Beyond, it could be counterproductive...according to many famous chess engines specs | ||
|
||
} |
Oops, something went wrong.