Skip to content

Commit 569bc75

Browse files
committed
Evaluation weights cleanup
Use a Weights[] array instead of named variables to store evaluation weights. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
1 parent 0544d6c commit 569bc75

2 files changed

Lines changed: 34 additions & 41 deletions

File tree

src/evaluate.cpp

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,21 @@ namespace {
4646
const int GrainSize = 8;
4747

4848
// Evaluation weights, initialized from UCI options
49-
Score WeightMobility, WeightPawnStructure;
50-
Score WeightPassedPawns, WeightSpace;
51-
Score WeightKingSafety[2];
49+
enum { Mobility, PawnStructure, PassedPawns, Space, KingSafetyUs, KingSafetyThem };
50+
Score Weights[6];
51+
52+
typedef Value V;
53+
#define S(mg, eg) make_score(mg, eg)
5254

5355
// Internal evaluation weights. These are applied on top of the evaluation
5456
// weights read from UCI parameters. The purpose is to be able to change
5557
// the evaluation weights while keeping the default values of the UCI
5658
// parameters at 100, which looks prettier.
5759
//
5860
// Values modified by Joona Kiiski
59-
const Score WeightMobilityInternal = make_score(248, 271);
60-
const Score WeightPawnStructureInternal = make_score(233, 201);
61-
const Score WeightPassedPawnsInternal = make_score(252, 259);
62-
const Score WeightSpaceInternal = make_score( 46, 0);
63-
const Score WeightKingSafetyInternal = make_score(247, 0);
64-
const Score WeightKingOppSafetyInternal = make_score(259, 0);
65-
66-
// Mobility and outposts bonus modified by Joona Kiiski
67-
68-
typedef Value V;
69-
#define S(mg, eg) make_score(mg, eg)
70-
71-
CACHE_LINE_ALIGNMENT
61+
const Score WeightsInternal[] = {
62+
S(248, 271), S(233, 201), S(252, 259), S(46, 0), S(247, 0), S(259, 0)
63+
};
7264

7365
// Knight mobility bonus in middle game and endgame, indexed by the number
7466
// of attacked squares not occupied by friendly piecess.
@@ -227,9 +219,9 @@ namespace {
227219
// Bonuses for safe checks
228220
const int QueenContactCheckBonus = 3;
229221
const int DiscoveredCheckBonus = 3;
230-
const int QueenCheckBonus = 2;
222+
const int QueenCheckBonus = 2;
231223
const int RookCheckBonus = 1;
232-
const int BishopCheckBonus = 1;
224+
const int BishopCheckBonus = 1;
233225
const int KnightCheckBonus = 1;
234226

235227
// Scan for queen contact mates?
@@ -338,7 +330,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
338330

339331
// Probe the pawn hash table
340332
ei.pi = PawnTable[threadID]->get_pawn_info(pos);
341-
ei.value += apply_weight(ei.pi->pawns_value(), WeightPawnStructure);
333+
ei.value += apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]);
342334

343335
// Initialize king attack bitboards and king attack zones for both sides
344336
ei.attackedBy[WHITE][KING] = pos.attacks_from<KING>(pos.king_square(WHITE));
@@ -404,7 +396,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
404396
}
405397

406398
// Mobility
407-
ei.value += apply_weight(ei.mobility, WeightMobility);
399+
ei.value += apply_weight(ei.mobility, Weights[Mobility]);
408400

409401
// If we don't already have an unusual scale factor, check for opposite
410402
// colored bishop endgames, and use a lower scale for those
@@ -487,22 +479,23 @@ void quit_eval() {
487479

488480
void read_weights(Color us) {
489481

490-
Color them = opposite_color(us);
482+
// King safety is asymmetrical. Our king safety is controled by "Cowardice"
483+
// UCI parameter, instead the opponent one by "Aggressiveness".
484+
const int kingSafetyUs = (us == WHITE ? KingSafetyUs : KingSafetyThem);
485+
const int kingSafetyThem = (us == WHITE ? KingSafetyThem : KingSafetyUs);
491486

492-
WeightMobility = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightMobilityInternal);
493-
WeightPawnStructure = weight_option("Pawn Structure (Middle Game)", "Pawn Structure (Endgame)", WeightPawnStructureInternal);
494-
WeightPassedPawns = weight_option("Passed Pawns (Middle Game)", "Passed Pawns (Endgame)", WeightPassedPawnsInternal);
495-
WeightSpace = weight_option("Space", "Space", WeightSpaceInternal);
496-
WeightKingSafety[us] = weight_option("Cowardice", "Cowardice", WeightKingSafetyInternal);
497-
WeightKingSafety[them] = weight_option("Aggressiveness", "Aggressiveness", WeightKingOppSafetyInternal);
487+
Weights[Mobility] = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightsInternal[Mobility]);
488+
Weights[PawnStructure] = weight_option("Pawn Structure (Middle Game)", "Pawn Structure (Endgame)", WeightsInternal[PawnStructure]);
489+
Weights[PassedPawns] = weight_option("Passed Pawns (Middle Game)", "Passed Pawns (Endgame)", WeightsInternal[PassedPawns]);
490+
Weights[Space] = weight_option("Space", "Space", WeightsInternal[Space]);
491+
Weights[kingSafetyUs] = weight_option("Cowardice", "Cowardice", WeightsInternal[KingSafetyUs]);
492+
Weights[kingSafetyThem] = weight_option("Aggressiveness", "Aggressiveness", WeightsInternal[KingSafetyThem]);
498493

499494
// If running in analysis mode, make sure we use symmetrical king safety. We do this
500-
// by replacing both WeightKingSafety[us] and WeightKingSafety[them] by their average.
495+
// by replacing both Weights[kingSafetyUs] and Weights[kingSafetyThem] by their average.
501496
if (get_option_value_bool("UCI_AnalyseMode"))
502-
{
503-
WeightKingSafety[us] = (WeightKingSafety[us] + WeightKingSafety[them]) / 2;
504-
WeightKingSafety[them] = WeightKingSafety[us];
505-
}
497+
Weights[kingSafetyUs] = Weights[kingSafetyThem] = (Weights[kingSafetyUs] + Weights[kingSafetyThem]) / 2;
498+
506499
init_safety();
507500
}
508501

@@ -863,13 +856,13 @@ namespace {
863856
attackUnits = Min(99, Max(0, attackUnits));
864857

865858
// Finally, extract the king safety score from the SafetyTable[] array.
866-
// Add the score to the evaluation, and also to ei.futilityMargin. The
867-
// reason for adding the king safety score to the futility margin is
868-
// that the king safety scores can sometimes be very big, and that
859+
// Subtract the score from evaluation, and set ei.futilityMargin[].
860+
// The reason for storing the king safety score to futility margin
861+
// is that the king safety scores can sometimes be very big, and that
869862
// capturing a single attacking piece can therefore result in a score
870863
// change far bigger than the value of the captured piece.
871864
ei.value -= Sign[Us] * SafetyTable[Us][attackUnits];
872-
ei.futilityMargin[Us] += mg_value(SafetyTable[Us][attackUnits]);
865+
ei.futilityMargin[Us] = mg_value(SafetyTable[Us][attackUnits]);
873866
}
874867
}
875868

@@ -967,8 +960,8 @@ namespace {
967960
ebonus -= ebonus / 4;
968961
}
969962

970-
// Add the scores for this pawn to the middle game and endgame eval.
971-
ei.value += Sign[Us] * apply_weight(make_score(mbonus, ebonus), WeightPassedPawns);
963+
// Add the scores for this pawn to the middle game and endgame eval
964+
ei.value += Sign[Us] * apply_weight(make_score(mbonus, ebonus), Weights[PassedPawns]);
972965

973966
} // while
974967
}
@@ -1155,7 +1148,7 @@ namespace {
11551148
int space = count_1s_max_15<HasPopCnt>(safeSquares)
11561149
+ count_1s_max_15<HasPopCnt>(behindFriendlyPawns & safeSquares);
11571150

1158-
ei.value += Sign[Us] * apply_weight(make_score(space * ei.mi->space_weight(), 0), WeightSpace);
1151+
ei.value += Sign[Us] * apply_weight(make_score(space * ei.mi->space_weight(), 0), Weights[Space]);
11591152
}
11601153

11611154

@@ -1233,6 +1226,6 @@ namespace {
12331226
// Then apply the weights and get the final SafetyTable[] array
12341227
for (Color c = WHITE; c <= BLACK; c++)
12351228
for (int i = 0; i < 100; i++)
1236-
SafetyTable[c][i] = apply_weight(make_score(t[i], 0), WeightKingSafety[c]);
1229+
SafetyTable[c][i] = apply_weight(make_score(t[i], 0), Weights[KingSafetyUs + c]);
12371230
}
12381231
}

src/evaluate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct EvalInfo {
9292
// mateThreat[color] is a move for the given side which gives a direct mate.
9393
Move mateThreat[2];
9494

95-
// Middle game and endgame mobility scores.
95+
// Middle game and endgame mobility scores
9696
Score mobility;
9797

9898
// Extra futility margin. This is added to the standard futility margin

0 commit comments

Comments
 (0)