Skip to content

Commit

Permalink
contempt, retune pawn structure that depend on rank, add knight too f…
Browse files Browse the repository at this point in the history
…ar from kings penalty
  • Loading branch information
tryingsomestuff committed Apr 13, 2020
1 parent 5d41fd2 commit af0a479
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 131 deletions.
6 changes: 5 additions & 1 deletion Source/definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef uint64_t u_int64_t;
#include <unistd.h>
#endif

const std::string MinicVersion = "2.06";
const std::string MinicVersion = "2.07";

// *** options
#define WITH_UCI
Expand Down Expand Up @@ -187,7 +187,11 @@ enum Sq : unsigned char { Sq_a1 = 0,Sq_b1,Sq_c1,Sq_d1,Sq_e1,Sq_f1,Sq_g1,Sq_h1
Sq_a8,Sq_b8,Sq_c8,Sq_d8,Sq_e8,Sq_f8,Sq_g8,Sq_h8};

enum File : unsigned char { File_a = 0,File_b,File_c,File_d,File_e,File_f,File_g,File_h};
inline File operator++(File & f){f=File(f+1); return f;}

enum Rank : unsigned char { Rank_1 = 0,Rank_2,Rank_3,Rank_4,Rank_5,Rank_6,Rank_7,Rank_8};
inline Rank operator++(Rank & r){r=Rank(r+1); return r;}


const Rank PromRank[2] = { Rank_8 , Rank_1 };
const Rank EPRank[2] = { Rank_6 , Rank_3 };
Expand Down
2 changes: 2 additions & 0 deletions Source/dynamicConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ namespace DynamicConfig{
bool FRC = false;
bool UCIPonder = false;
unsigned int multiPV = 1;
ScoreType contempt = 0;
ScoreType contemptMG = 0;
}
2 changes: 2 additions & 0 deletions Source/dynamicConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ namespace DynamicConfig{
extern bool FRC ;
extern bool UCIPonder ;
extern unsigned int multiPV ;
extern ScoreType contempt ;
extern ScoreType contemptMG ;
}
72 changes: 56 additions & 16 deletions Source/eval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ inline ScoreType eval(const Position & p, EvalData & data, Searcher &context){
// PST, attack
evalPawn<Co_White>(p.pieces_const<P_wp>(Co_White),pe.score);
evalPawn<Co_Black>(p.pieces_const<P_wp>(Co_Black),pe.score);

// danger in king zone
pe.danger[Co_White] -= countBit(pe.pawnTargets[Co_White] & kingZone[Co_White]) * EvalConfig::kingAttWeight[EvalConfig::katt_defence][0];
pe.danger[Co_White] += countBit(pe.pawnTargets[Co_Black] & kingZone[Co_White]) * EvalConfig::kingAttWeight[EvalConfig::katt_attack] [0];
pe.danger[Co_Black] -= countBit(pe.pawnTargets[Co_Black] & kingZone[Co_Black]) * EvalConfig::kingAttWeight[EvalConfig::katt_defence][0];
pe.danger[Co_Black] += countBit(pe.pawnTargets[Co_White] & kingZone[Co_Black]) * EvalConfig::kingAttWeight[EvalConfig::katt_attack] [0];

// pawn passer
evalPawnPasser<Co_White>(p,pe.passed[Co_White],pe.score);
evalPawnPasser<Co_Black>(p,pe.passed[Co_Black],pe.score);
Expand All @@ -259,21 +261,42 @@ inline ScoreType eval(const Position & p, EvalData & data, Searcher &context){
// pawn candidate
evalPawnCandidate<Co_White>(candidates[Co_White],pe.score);
evalPawnCandidate<Co_Black>(candidates[Co_Black],pe.score);
// pawn backward
pe.score -= EvalConfig::backwardPawnMalus[EvalConfig::Close] * countBit(backward[Co_White] & ~semiOpenPawn[Co_White]);
pe.score -= EvalConfig::backwardPawnMalus[EvalConfig::SemiOpen] * countBit(backward[Co_White] & semiOpenPawn[Co_White]);
pe.score += EvalConfig::backwardPawnMalus[EvalConfig::Close] * countBit(backward[Co_Black] & ~semiOpenPawn[Co_Black]);
pe.score += EvalConfig::backwardPawnMalus[EvalConfig::SemiOpen] * countBit(backward[Co_Black] & semiOpenPawn[Co_Black]);
// double pawn malus
pe.score -= EvalConfig::doublePawnMalus[EvalConfig::Close] * countBit(doubled[Co_White] & ~semiOpenPawn[Co_White]);
pe.score -= EvalConfig::doublePawnMalus[EvalConfig::SemiOpen] * countBit(doubled[Co_White] & semiOpenPawn[Co_White]);
pe.score += EvalConfig::doublePawnMalus[EvalConfig::Close] * countBit(doubled[Co_Black] & ~semiOpenPawn[Co_Black]);
pe.score += EvalConfig::doublePawnMalus[EvalConfig::SemiOpen] * countBit(doubled[Co_Black] & semiOpenPawn[Co_Black]);
// isolated pawn malus
pe.score -= EvalConfig::isolatedPawnMalus[EvalConfig::Close] * countBit(isolated[Co_White] & ~semiOpenPawn[Co_White]);
pe.score -= EvalConfig::isolatedPawnMalus[EvalConfig::SemiOpen] * countBit(isolated[Co_White] & semiOpenPawn[Co_White]);
pe.score += EvalConfig::isolatedPawnMalus[EvalConfig::Close] * countBit(isolated[Co_Black] & ~semiOpenPawn[Co_Black]);
pe.score += EvalConfig::isolatedPawnMalus[EvalConfig::SemiOpen] * countBit(isolated[Co_Black] & semiOpenPawn[Co_Black]);

// bad pawns
const BitBoard backwardOpenW = backward[Co_White] & semiOpenPawn[Co_White];
const BitBoard backwardCloseW = backward[Co_White] & ~semiOpenPawn[Co_White];
const BitBoard backwardOpenB = backward[Co_Black] & semiOpenPawn[Co_Black];
const BitBoard backwardCloseB = backward[Co_Black] & ~semiOpenPawn[Co_Black];
const BitBoard doubledOpenW = doubled[Co_White] & semiOpenPawn[Co_White];
const BitBoard doubledCloseW = doubled[Co_White] & ~semiOpenPawn[Co_White];
const BitBoard doubledOpenB = doubled[Co_Black] & semiOpenPawn[Co_Black];
const BitBoard doubledCloseB = doubled[Co_Black] & ~semiOpenPawn[Co_Black];
const BitBoard isolatedOpenW = isolated[Co_White] & semiOpenPawn[Co_White];
const BitBoard isolatedCloseW = isolated[Co_White] & ~semiOpenPawn[Co_White];
const BitBoard isolatedOpenB = isolated[Co_Black] & semiOpenPawn[Co_Black];
const BitBoard isolatedCloseB = isolated[Co_Black] & ~semiOpenPawn[Co_Black];
for (Rank r = Rank_1 ; r <= Rank_8 ; ++r){
const int ir = r-1;
// pawn backward
pe.score -= EvalConfig::backwardPawnMalus[ir][EvalConfig::Close] * countBit(backwardCloseW & ranks[ir]);
pe.score -= EvalConfig::backwardPawnMalus[ir][EvalConfig::SemiOpen] * countBit(backwardOpenW & ranks[ir]);
// double pawn malus
pe.score -= EvalConfig::doublePawnMalus[ir][EvalConfig::Close] * countBit(doubledCloseW & ranks[ir]);
pe.score -= EvalConfig::doublePawnMalus[ir][EvalConfig::SemiOpen] * countBit(doubledOpenW & ranks[ir]);
// isolated pawn malus
pe.score -= EvalConfig::isolatedPawnMalus[ir][EvalConfig::Close] * countBit(isolatedCloseW & ranks[ir]);
pe.score -= EvalConfig::isolatedPawnMalus[ir][EvalConfig::SemiOpen] * countBit(isolatedOpenW & ranks[ir]);
// pawn backward
pe.score += EvalConfig::backwardPawnMalus[7-ir][EvalConfig::Close] * countBit(backwardCloseB & ranks[ir]);
pe.score += EvalConfig::backwardPawnMalus[7-ir][EvalConfig::SemiOpen] * countBit(backwardOpenB & ranks[ir]);
// double pawn malus
pe.score += EvalConfig::doublePawnMalus[7-ir][EvalConfig::Close] * countBit(doubledCloseB & ranks[ir]);
pe.score += EvalConfig::doublePawnMalus[7-ir][EvalConfig::SemiOpen] * countBit(doubledOpenB & ranks[ir]);
// isolated pawn malus
pe.score += EvalConfig::isolatedPawnMalus[7-ir][EvalConfig::Close] * countBit(isolatedCloseB & ranks[ir]);
pe.score += EvalConfig::isolatedPawnMalus[7-ir][EvalConfig::SemiOpen] * countBit(isolatedOpenB & ranks[ir]);
}

// pawn shield (PST and king troppism alone is not enough)
const int pawnShieldW = countBit(kingShield[Co_White] & pawns[Co_White]);
const int pawnShieldB = countBit(kingShield[Co_Black] & pawns[Co_Black]);
Expand Down Expand Up @@ -358,6 +381,18 @@ inline ScoreType eval(const Position & p, EvalData & data, Searcher &context){
score[sc_Outpost] += EvalConfig::outpost * countBit(pe.holes[Co_Black] & p.whiteKnight() & pe.pawnTargets[Co_White]);
score[sc_Outpost] -= EvalConfig::outpost * countBit(pe.holes[Co_White] & p.blackKnight() & pe.pawnTargets[Co_Black]);

// knight far from both kings gets a penalty
BitBoard knights = p.whiteKnight();
while(knights){
const Square knighSq = popBit(knights);
score[sc_knightFar] += EvalConfig::knightTooFar[std::min(chebyshevDistance(p.king[Co_White],knighSq),chebyshevDistance(p.king[Co_Black],knighSq))];
}
knights = p.blackKnight();
while(knights){
const Square knighSq = popBit(knights);
score[sc_knightFar] -= EvalConfig::knightTooFar[std::min(chebyshevDistance(p.king[Co_White],knighSq),chebyshevDistance(p.king[Co_Black],knighSq))];
}

// reward safe checks
for (Piece pp = P_wp ; pp < P_wk ; ++pp) {
kdanger[Co_White] += EvalConfig::kingAttSafeCheck[pp-1] * countBit( checkers[Co_Black][pp-1] & safeSquare[Co_White] );
Expand Down Expand Up @@ -461,7 +496,7 @@ inline ScoreType eval(const Position & p, EvalData & data, Searcher &context){
if ( blackQueenSquare != INVALIDSQUARE ) score[sc_QueenNearKing] -= EvalConfig::queenNearKing * (7 - chebyshevDistance(p.king[Co_White], blackQueenSquare) );
if ( whiteQueenSquare != INVALIDSQUARE ) score[sc_QueenNearKing] += EvalConfig::queenNearKing * (7 - chebyshevDistance(p.king[Co_Black], whiteQueenSquare) );

// number of pawn and piece type value
// number of pawn and piece type value ///@todo closedness instead ?
score[sc_Adjust] += EvalConfig::adjRook [p.mat[Co_White][M_p]] * ScoreType(p.mat[Co_White][M_r]);
score[sc_Adjust] -= EvalConfig::adjRook [p.mat[Co_Black][M_p]] * ScoreType(p.mat[Co_Black][M_r]);
score[sc_Adjust] += EvalConfig::adjKnight[p.mat[Co_White][M_p]] * ScoreType(p.mat[Co_White][M_n]);
Expand All @@ -483,9 +518,14 @@ inline ScoreType eval(const Position & p, EvalData & data, Searcher &context){
score[sc_initiative][MG] += sgn(score.score[MG]) * std::max(initiativeBonus[MG], ScoreType(-std::abs(score.score[MG])));
score[sc_initiative][EG] += sgn(score.score[EG]) * std::max(initiativeBonus[EG], ScoreType(-std::abs(score.score[EG])));

///@todo complexity

// tempo
score[sc_Tempo] += EvalConfig::tempo*(white2Play?+1:-1);

// contempt
score[sc_Contempt] += context.contempt;

if ( display ) score.Display(p,data.gp);
ScoreType ret = (white2Play?+1:-1)*score.Score(p,data.gp); // scale both phase and 50 moves rule
STOP_AND_SUM_TIMER(Eval)
Expand Down
Loading

0 comments on commit af0a479

Please sign in to comment.