Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup complexity #2591

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,28 +701,7 @@ namespace {
template<Tracing T>
Score Evaluation<T>::initiative(Score score) const {

int outflanking = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK))
- distance<Rank>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));

bool pawnsOnBothFlanks = (pos.pieces(PAWN) & QueenSide)
&& (pos.pieces(PAWN) & KingSide);

bool almostUnwinnable = !pe->passed_count()
&& outflanking < 0
&& !pawnsOnBothFlanks;

bool infiltration = rank_of(pos.square<KING>(WHITE)) > RANK_4
|| rank_of(pos.square<KING>(BLACK)) < RANK_5;

// Compute the initiative bonus for the attacking side
int complexity = 9 * pe->passed_count()
+ 11 * pos.count<PAWN>()
+ 9 * outflanking
+ 21 * pawnsOnBothFlanks
+ 24 * infiltration
+ 51 * !pos.non_pawn_material()
- 43 * almostUnwinnable
-110 ;
int complexity = pe->get_complexity(pos) + 51 * !pos.non_pawn_material();

Value mg = mg_value(score);
Value eg = eg_value(score);
Expand Down
33 changes: 32 additions & 1 deletion src/pawns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,38 @@ Score Entry::evaluate_shelter(const Position& pos, Square ksq) {
return bonus;
}

/// Entry::get_complexity() calculates position complexity. It is called only
/// when king square changes, which is about 20% of total king_safety() calls.

int Entry::do_complexity(const Position& pos) {

kingSquares[WHITE] = pos.square<KING>(WHITE);
kingSquares[BLACK] = pos.square<KING>(BLACK);

int outflanking = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK))
- distance<Rank>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));

bool pawnsOnBothFlanks = (pos.pieces(PAWN) & QueenSide)
&& (pos.pieces(PAWN) & KingSide);

bool almostUnwinnable = !passed_count()
&& outflanking < 0
&& !pawnsOnBothFlanks;

bool infiltration = rank_of(pos.square<KING>(WHITE)) > RANK_4
|| rank_of(pos.square<KING>(BLACK)) < RANK_5;

// Compute the initiative bonus for the attacking side
int complex = 9 * passed_count()
+ 11 * pos.count<PAWN>()
+ 9 * outflanking
+ 21 * pawnsOnBothFlanks
+ 24 * infiltration
- 43 * almostUnwinnable
-110 ;

return complex;
}

/// Entry::do_king_safety() calculates a bonus for king safety. It is called only
/// when king square changes, which is about 20% of total king_safety() calls.
Expand All @@ -222,7 +254,6 @@ template<Color Us>
Score Entry::do_king_safety(const Position& pos) {

Square ksq = pos.square<KING>(Us);
kingSquares[Us] = ksq;
castlingRights[Us] = pos.castling_rights(Us);
auto compare = [](Score a, Score b) { return mg_value(a) < mg_value(b); };

Expand Down
9 changes: 9 additions & 0 deletions src/pawns.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ struct Entry {
template<Color Us>
Score evaluate_shelter(const Position& pos, Square ksq);

int get_complexity(const Position& pos) {
return kingSquares[WHITE] == pos.square<KING>(WHITE)
&& kingSquares[BLACK] == pos.square<KING>(BLACK)
? complexity : (complexity = do_complexity(pos));
}

int do_complexity(const Position& pos);

Key key;
Score scores[COLOR_NB];
Bitboard passedPawns[COLOR_NB];
Expand All @@ -59,6 +67,7 @@ struct Entry {
Square kingSquares[COLOR_NB];
Score kingSafety[COLOR_NB];
int castlingRights[COLOR_NB];
int complexity;
};

typedef HashTable<Entry, 131072> Table;
Expand Down