Skip to content

Commit

Permalink
Cleanup history stats
Browse files Browse the repository at this point in the history
And other assorted trivia.

No functional change.
  • Loading branch information
mcostalba committed Oct 24, 2015
1 parent 5575834 commit 307a5a4
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 112 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### Overview [![Build Status](https://travis-ci.org/official-stockfish/Stockfish.svg?branch=master)](https://travis-ci.org/official-stockfish/Stockfish)
### Overview

[![Build Status](https://travis-ci.org/official-stockfish/Stockfish.svg?branch=master)](https://travis-ci.org/official-stockfish/Stockfish)

Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
not a complete chess program and requires some UCI-compatible GUI
Expand Down
26 changes: 12 additions & 14 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ namespace {

// PassedFile[File] contains a bonus according to the file of a passed pawn.
const Score PassedFile[] = {
S( 12, 10), S( 3, 10), S( 1, -8), S(-27, -12),
S(-27, -12), S( 1, -8), S( 3, 10), S( 12, 10)
S( 12, 10), S( 3, 10), S( 1, -8), S(-27, -12),
S(-27, -12), S( 1, -8), S( 3, 10), S( 12, 10)
};

const Score ThreatenedByHangingPawn = S(40, 60);
Expand Down Expand Up @@ -677,20 +677,18 @@ namespace {
// evaluate_initiative() computes the initiative correction value for the
// position, i.e. second order bonus/malus based on the known attacking/defending
// status of the players.
Score evaluate_initiative(const Position& pos, const EvalInfo& ei, const Score positional_score) {
Score evaluate_initiative(const Position& pos, int asymmetry, Value eg) {

int king_separation = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));
int pawns = pos.count<PAWN>(WHITE) + pos.count<PAWN>(BLACK);
int asymmetry = ei.pi->pawn_asymmetry();
int kingDistance = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));
int pawns = pos.count<PAWN>(WHITE) + pos.count<PAWN>(BLACK);

// Compute the initiative bonus for the attacking side
int attacker_bonus = 8 * (pawns + asymmetry + king_separation) - 120;
int initiative = 8 * (pawns + asymmetry + kingDistance - 15);

// Now apply the bonus: note that we find the attacking side by extracting the sign
// of the endgame value of "positional_score", and that we carefully cap the bonus so
// that the endgame score with the correction will never be divided by more than two.
int eg = eg_value(positional_score);
int value = ((eg > 0) - (eg < 0)) * std::max(attacker_bonus, -abs(eg / 2));
// Now apply the bonus: note that we find the attacking side by extracting
// the sign of the endgame value, and that we carefully cap the bonus so
// that the endgame score will never be divided by more than two.
int value = ((eg > 0) - (eg < 0)) * std::max(initiative, -abs(eg / 2));

return make_score(0, value);
}
Expand Down Expand Up @@ -777,8 +775,8 @@ Value Eval::evaluate(const Position& pos) {
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 12222)
score += (evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei)) * Weights[Space];

// Evaluate initiative
score += evaluate_initiative(pos, ei, score);
// Evaluate position potential for the winning side
score += evaluate_initiative(pos, ei.pi->pawn_asymmetry(), eg_value(score));

// Scale winning side if position is more drawish than it appears
Color strongSide = eg_value(score) > VALUE_DRAW ? WHITE : BLACK;
Expand Down
20 changes: 8 additions & 12 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ namespace {
/// ordering is at the current node.

MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
const CounterMovesHistoryStats& cmh, Move cm, Search::Stack* s)
: pos(p), history(h), counterMovesHistory(cmh), ss(s), countermove(cm), depth(d) {
const CounterMovesStats& cmh, Move cm, Search::Stack* s)
: pos(p), history(h), counterMovesHistory(&cmh), ss(s), countermove(cm), depth(d) {

assert(d > DEPTH_ZERO);

Expand All @@ -78,9 +78,9 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
endMoves += (ttMove != MOVE_NONE);
}

MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
const CounterMovesHistoryStats& cmh, Square s)
: pos(p), history(h), counterMovesHistory(cmh) {
MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
const HistoryStats& h, Square s)
: pos(p), history(h), counterMovesHistory(nullptr) {

assert(d <= DEPTH_ZERO);

Expand All @@ -104,9 +104,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
endMoves += (ttMove != MOVE_NONE);
}

MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h,
const CounterMovesHistoryStats& cmh, Value th)
: pos(p), history(h), counterMovesHistory(cmh), threshold(th) {
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, Value th)
: pos(p), history(h), counterMovesHistory(nullptr), threshold(th) {

assert(!pos.checkers());

Expand Down Expand Up @@ -141,12 +140,9 @@ void MovePicker::score<CAPTURES>() {
template<>
void MovePicker::score<QUIETS>() {

Square prevSq = to_sq((ss-1)->currentMove);
const HistoryStats& cmh = counterMovesHistory[pos.piece_on(prevSq)][prevSq];

for (auto& m : *this)
m.value = history[pos.moved_piece(m)][to_sq(m)]
+ cmh[pos.moved_piece(m)][to_sq(m)];
+ (*counterMovesHistory)[pos.moved_piece(m)][to_sq(m)];
}

template<>
Expand Down
32 changes: 13 additions & 19 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
/// Countermoves store the move that refute a previous one. Entries are stored
/// using only the moving piece and destination square, hence two moves with
/// different origin but same destination and piece will be considered identical.
template<typename T>
template<typename T, bool CM = false>
struct Stats {

static const Value Max = Value(1<<28);
static const Value Max = Value(1 << 28);

const T* operator[](Piece pc) const { return table[pc]; }
T* operator[](Piece pc) { return table[pc]; }
Expand All @@ -51,29 +51,23 @@ struct Stats {
table[pc][to] = m;
}

void updateH(Piece pc, Square to, Value v) {
void update(Piece pc, Square to, Value v) {

if (abs(int(v)) >= 324)
if (abs(int(v)) >= 324)
return;
table[pc][to] -= table[pc][to] * abs(int(v)) / 324;
table[pc][to] += int(v) * 32;
}

void updateCMH(Piece pc, Square to, Value v) {

if (abs(int(v)) >= 324)
return;
table[pc][to] -= table[pc][to] * abs(int(v)) / 512;
table[pc][to] += int(v) * 64;
table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 512 : 324);
table[pc][to] += int(v) * (CM ? 64 : 32);
}

private:
T table[PIECE_NB][SQUARE_NB];
};

typedef Stats<Value> HistoryStats;
typedef Stats<Move> MovesStats;
typedef Stats<HistoryStats> CounterMovesHistoryStats;
typedef Stats<Value, false> HistoryStats;
typedef Stats<Value, true> CounterMovesStats;
typedef Stats<CounterMovesStats> CounterMovesHistoryStats;


/// MovePicker class is used to pick one pseudo legal move at a time from the
Expand All @@ -88,9 +82,9 @@ class MovePicker {
MovePicker(const MovePicker&) = delete;
MovePicker& operator=(const MovePicker&) = delete;

MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesHistoryStats&, Square);
MovePicker(const Position&, Move, const HistoryStats&, const CounterMovesHistoryStats&, Value);
MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesHistoryStats&, Move, Search::Stack*);
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
MovePicker(const Position&, Move, const HistoryStats&, Value);
MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesStats&, Move, Search::Stack*);

Move next_move();

Expand All @@ -102,7 +96,7 @@ class MovePicker {

const Position& pos;
const HistoryStats& history;
const CounterMovesHistoryStats& counterMovesHistory;
const CounterMovesStats* counterMovesHistory;
Search::Stack* ss;
Move countermove;
Depth depth;
Expand Down
Loading

0 comments on commit 307a5a4

Please sign in to comment.