Skip to content

Commit

Permalink
Version 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincan0 committed Aug 23, 2019
1 parent 8513981 commit bcf8681
Show file tree
Hide file tree
Showing 22 changed files with 401 additions and 283 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ OLDWFLAGS = -DNDEBUG -Wall -Wcast-qual -Wextra -Wshadow -pedantic -std=c++11 -m
DFLAGS = -g -Wall -Wcast-qual -Wextra -Wshadow -pedantic -std=c++11 -m64 -msse3 -mpopcnt -flto -D__DEBUG__
TARGET = Defenchess
OPT = -O3
VERSION = 2.1
VERSION = 2.2
OBJECTS = bitboard.o data.o eval.o move.o move_utils.o params.o position.o pst.o search.o see.o target.o test.o timecontrol.o thread.o tt.o tune.o uci.o magic.o main.o movegen.o tb.o fathom/tbprobe.o

all: $(TARGET)
Expand Down
4 changes: 4 additions & 0 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ inline bool more_than_one(Bitboard b) {
return b & (b - 1);
}

inline bool only_one(Bitboard b) {
return b && !more_than_one(b);
}

inline Bitboard shift(Bitboard b, int offset) {
if (offset > 0) {
return b << offset;
Expand Down
31 changes: 12 additions & 19 deletions src/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,15 @@ const uint64_t _knight_targets = 0x0000005088008850;
const uint64_t _king_targets = 0x0000000070507000;

const int
KNOWN_WIN = 10000,
MATE = 32000,
INFINITE = 32001,
UNDEFINED = 32002,
TIMEOUT = 32003,

MATE_IN_MAX_PLY = MATE - MAX_PLY,
MATED_IN_MAX_PLY = -MATE + MAX_PLY;
MATED_IN_MAX_PLY = -MATE + MAX_PLY,

TB_WIN = MATE_IN_MAX_PLY - MAX_PLY - 1;

const int RANK_1 = 0,
RANK_2 = 1,
Expand Down Expand Up @@ -307,12 +308,13 @@ typedef struct CopiedInfo {
const int info_size = sizeof(CopiedInfo);

typedef struct Metadata {
int ply;
Move current_move;
int static_eval;
Move killers[2];
Move pv[MAX_PLY + 1];
Move excluded_move;
int ply;
Move current_move;
int static_eval;
Move killers[2];
Move pv[MAX_PLY + 1];
Move excluded_move;
Piece moved_piece;
} Metadata;

struct Position {
Expand Down Expand Up @@ -353,14 +355,7 @@ typedef struct Evaluation {

enum EndgameType {
NORMAL_ENDGAME,
DRAW_ENDGAME,
KNOWN_ENDGAME_KPK,
KNOWN_ENDGAME_KXK
};

enum ScalingFactorType {
NO_SCALING,
KRPKR_SCALING
DRAW_ENDGAME
};

const int SCALE_NORMAL = 32;
Expand All @@ -369,7 +364,6 @@ typedef struct Material {
int phase;
int score;
EndgameType endgame_type;
ScalingFactorType scaling_factor_type;
} Material;

const int material_balance[NUM_PIECE] ={
Expand Down Expand Up @@ -443,7 +437,6 @@ struct MoveGen {
Position *position;
Move tte_move;
Move counter_move;
Move prev_move;
int stage;
uint8_t head;
uint8_t tail;
Expand All @@ -456,7 +449,6 @@ const MoveGen blank_movegen = {
nullptr, // Position
no_move, // tte_move
no_move, // counter move
no_move, // prev move
0, // stage
0, // head
0, // tail
Expand All @@ -474,6 +466,7 @@ struct SearchThread {
Move counter_moves[NUM_PIECE][64];
int history[2][64][64];
int counter_move_history[NUM_PIECE][64][NUM_PIECE][64];
int followup_history[NUM_PIECE][64][NUM_PIECE][64];
int selply;
PawnTTEntry pawntt[pawntt_size];
int depth;
Expand Down
13 changes: 1 addition & 12 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ uint64_t polyglotCombined[NUM_PIECE][64];
int num_threads = 1;
int move_overhead = 100;

SearchThread main_thread;
SearchThread *search_threads;

int mvvlva_values[12][NUM_PIECE];
Expand Down Expand Up @@ -467,11 +468,8 @@ void init_imbalance(){
int all_minor = white_minor + black_minor;
int all_major = white_major + black_major;
bool no_pawns = wp == 0 && bp == 0;
bool only_one_pawn = wp + bp == 1;

// Default endgame and scaling factor types
material->endgame_type = NORMAL_ENDGAME;
material->scaling_factor_type = NO_SCALING;

if (wp + bp + all_minor + all_major == 0) {
material->endgame_type = DRAW_ENDGAME;
Expand All @@ -481,15 +479,6 @@ void init_imbalance(){
}
else if (no_pawns && all_major == 0 && all_minor == 2 && (wn == 2 || bn == 2)) {
material->endgame_type = DRAW_ENDGAME;
}
else if (only_one_pawn && !all_minor && !all_major) {
material->endgame_type = KNOWN_ENDGAME_KPK;
}
else if (no_pawns && !all_minor && all_major == 1) {
material->endgame_type = KNOWN_ENDGAME_KXK;
}
else if (br == 1 && wr == 1 && only_one_pawn && !all_minor && wq == 0 && bq == 0) {
material->scaling_factor_type = KRPKR_SCALING;
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,36 @@ bool scored_move_compare_greater(ScoredMove lhs, ScoredMove rhs);

inline bool is_main_thread(Position *p) {return p->my_thread->thread_id == 0;}

extern SearchThread main_thread;
extern SearchThread *search_threads;

inline SearchThread *get_thread(int thread_id) { return thread_id == 0 ? &main_thread : &search_threads[thread_id - 1]; }

extern int num_threads;
extern int move_overhead;

inline void initialize_nodes() {
for (int i = 0; i < num_threads; ++i) {
search_threads[i].nodes = 0;
search_threads[i].tb_hits = 0;
SearchThread *t = get_thread(i);
t->nodes = 0;
t->tb_hits = 0;
}
}

inline uint64_t sum_nodes() {
uint64_t s = 0;
for (int i = 0; i < num_threads; ++i) {
s += search_threads[i].nodes;
SearchThread *t = get_thread(i);
s += t->nodes;
}
return s;
}

inline uint64_t sum_tb_hits() {
uint64_t s = 0;
for (int i = 0; i < num_threads; ++i) {
s += search_threads[i].tb_hits;
SearchThread *t = get_thread(i);
s += t->tb_hits;
}
return s;
}
Expand Down
86 changes: 64 additions & 22 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,36 @@ void evaluate_pawns(Evaluation *eval, Position *p) {
Score evaluate_pieces(Evaluation *eval, Position *p, Color color) {
Score piece_score = {0, 0};

Bitboard capturable, king_threats;

Bitboard my_bishops = p->bbs[bishop(color)];
Bitboard my_knights = p->bbs[knight(color)];
Bitboard my_rooks = p->bbs[rook(color)];
Bitboard my_queens = p->bbs[queen(color)];
Bitboard pinned = p->info->pinned[color];

Bitboard opponent_bishops = p->bbs[bishop(~color)];
Bitboard opponent_knights = p->bbs[knight(~color)];
Bitboard opponent_rooks = p->bbs[rook(~color)];
Bitboard opponent_queens = p->bbs[queen(~color)];

eval->targets[bishop(color)] = 0;
while (my_bishops) {
Square sq = pop(&my_bishops);
Bitboard bishop_targets = generate_bishop_targets(p->board ^ p->bbs[queen(color)], sq);
if (pinned & bfi[sq]) {
bishop_targets &= BETWEEN_MASK[p->king_index[color]][sq];
bishop_targets &= FROMTO_MASK[p->king_index[color]][sq];
}

int mobility = count(bishop_targets & eval->mobility_area[color]);
capturable = opponent_bishops | opponent_rooks | opponent_queens;
int mobility = count(bishop_targets & (eval->mobility_area[color] | capturable));
piece_score += mobility_bonus[BISHOP][mobility];

// Bishop with same colored pawns
piece_score -= bishop_pawn_penalty * count(COLOR_MASKS[TILE_COLOR[sq]] & p->bbs[pawn(color)]);

// King threats
Bitboard king_threats = bishop_targets & eval->king_zone[~color];
king_threats = bishop_targets & eval->king_zone[~color];
if (king_threats) {
++eval->num_king_attackers[~color];
eval->king_zone_score[~color] += ATTACK_VALUES[BISHOP];
Expand All @@ -171,14 +179,16 @@ Score evaluate_pieces(Evaluation *eval, Position *p, Color color) {
Bitboard knight_targets = generate_knight_targets(sq);

if (pinned & bfi[sq]) {
knight_targets &= BETWEEN_MASK[p->king_index[color]][sq];
// Pinned knights cannot move
knight_targets = 0;
}

int mobility = count(knight_targets & eval->mobility_area[color]);
capturable = opponent_knights | opponent_bishops | opponent_rooks | opponent_queens;
int mobility = count(knight_targets & (eval->mobility_area[color] | capturable));
piece_score += mobility_bonus[KNIGHT][mobility];

// King threats
Bitboard king_threats = knight_targets & eval->king_zone[~color];
king_threats = knight_targets & eval->king_zone[~color];
if (king_threats) {
++eval->num_king_attackers[~color];
eval->king_zone_score[~color] += ATTACK_VALUES[KNIGHT];
Expand All @@ -194,10 +204,11 @@ Score evaluate_pieces(Evaluation *eval, Position *p, Color color) {
Bitboard rook_targets = generate_rook_targets(p->board ^ (p->bbs[queen(color)] | p->bbs[rook(color)]), sq);

if (pinned & bfi[sq]) {
rook_targets &= BETWEEN_MASK[p->king_index[color]][sq];
rook_targets &= FROMTO_MASK[p->king_index[color]][sq];
}

int mobility = count(rook_targets & eval->mobility_area[color]);
capturable = opponent_rooks | opponent_queens;
int mobility = count(rook_targets & (eval->mobility_area[color] | capturable));
piece_score += mobility_bonus[ROOK][mobility];

// Bonus for being on a semiopen or open file
Expand All @@ -206,7 +217,7 @@ Score evaluate_pieces(Evaluation *eval, Position *p, Color color) {
}

// King threats
Bitboard king_threats = rook_targets & eval->king_zone[~color];
king_threats = rook_targets & eval->king_zone[~color];
if (king_threats) {
++eval->num_king_attackers[~color];
eval->king_zone_score[~color] += ATTACK_VALUES[ROOK];
Expand All @@ -222,14 +233,15 @@ Score evaluate_pieces(Evaluation *eval, Position *p, Color color) {
Bitboard queen_targets = generate_queen_targets(p->board, sq);

if (pinned & bfi[sq]) {
queen_targets &= BETWEEN_MASK[p->king_index[color]][sq];
queen_targets &= FROMTO_MASK[p->king_index[color]][sq];
}

int mobility = count(queen_targets & eval->mobility_area[color]);
capturable = opponent_queens;
int mobility = count(queen_targets & (eval->mobility_area[color] | capturable));
piece_score += mobility_bonus[QUEEN][mobility];

// King threats
Bitboard king_threats = queen_targets & eval->king_zone[~color];
king_threats = queen_targets & eval->king_zone[~color];
if (king_threats) {
++eval->num_king_attackers[~color];
eval->king_zone_score[~color] += ATTACK_VALUES[QUEEN];
Expand All @@ -256,6 +268,10 @@ Score evaluate_king(Evaluation *eval, Position *p, Color color) {
king_score.endgame -= pawn_distance_penalty * pawn_distance;
}

Bitboard flank_attacks = eval->targets[~color] & flank_ranks[color] & flank_files[file_of(king_sq)];
Bitboard flank_attacks2 = flank_attacks & eval->double_targets[~color];
king_score.midgame -= king_flank_penalty * (count(flank_attacks) + count(flank_attacks2));

if (eval->num_king_attackers[color] > (1 - eval->num_queens[~color])) {
Bitboard weak = eval->targets[king(color)] & eval->targets[~color] & ~eval->double_targets[color];
Bitboard weak_zone = eval->targets[~color] & ~eval->targets[color] & eval->king_zone[color] & ~p->bbs[~color];
Expand Down Expand Up @@ -307,24 +323,30 @@ Score evaluate_threat(Evaluation *eval, Position *p, Color color) {
Bitboard very_supported = eval->targets[pawn(~color)] | (eval->double_targets[~color] & ~eval->double_targets[color]);
Bitboard not_supported = p->bbs[~color] & ~very_supported & eval->targets[color];

Bitboard attacked;
if (opponent_non_pawns | not_supported) {
Bitboard attacked_by_minor = (opponent_non_pawns | not_supported) & (eval->targets[knight(color)] | eval->targets[bishop(color)]);
while (attacked_by_minor) {
Square sq = pop(&attacked_by_minor);
attacked = (opponent_non_pawns | not_supported) & (eval->targets[knight(color)] | eval->targets[bishop(color)]);
while (attacked) {
Square sq = pop(&attacked);
threat_score += minor_threat_bonus[piece_type(p->pieces[sq])];
}

Bitboard attacked_by_rook = (p->bbs[queen(~color)] | not_supported) & eval->targets[rook(color)];
while (attacked_by_rook) {
Square sq = pop(&attacked_by_rook);
attacked = (p->bbs[queen(~color)] | not_supported) & eval->targets[rook(color)];
while (attacked) {
Square sq = pop(&attacked);
threat_score += rook_threat_bonus[piece_type(p->pieces[sq])];
}

Bitboard attacked_by_pawn = opponent_non_pawns & eval->targets[pawn(color)];
while (attacked_by_pawn) {
Square sq = pop(&attacked_by_pawn);
attacked = opponent_non_pawns & eval->targets[pawn(color)];
while (attacked) {
Square sq = pop(&attacked);
threat_score += pawn_threat_bonus[piece_type(p->pieces[sq])];
}

attacked = not_supported & eval->targets[king(color)];
if (attacked) {
threat_score += king_threat_bonus[more_than_one(attacked)];
}
}

Bitboard pawn_moves = (color == white ? p->bbs[pawn(color)] << 8 : p->bbs[pawn(color)] >> 8) & ~p->board;
Expand Down Expand Up @@ -405,6 +427,25 @@ void pre_eval(Evaluation *eval, Position *p) {
eval->num_queens[white] = eval->num_queens[black] = 0;
}

int scaling_factor(Position *p) {
if (only_one(p->bbs[white_bishop]) &&
only_one(p->bbs[black_bishop]) &&
only_one(COLOR_MASKS[white] & (p->bbs[white_bishop] | p->bbs[black_bishop]))) {
if (p->info->non_pawn_material[white] == BISHOP_MID && p->info->non_pawn_material[black] == BISHOP_MID) {
return SCALE_PURE_OCB;
} else {
return SCALE_OCB_WITH_PIECES;
}
}

Color winner = p->score.endgame > 0 ? white : black;
if (!p->bbs[pawn(winner)] && p->info->non_pawn_material[winner] <= p->info->non_pawn_material[~winner] + piece_values[white_bishop]) {
return SCALE_NO_PAWNS;
}

return SCALE_NORMAL;
}

int evaluate(Position *p) {
assert(!is_checked(p));
Evaluation eval;
Expand All @@ -426,6 +467,7 @@ int evaluate(Position *p) {
evaluate_threat(&eval, p, white) - evaluate_threat(&eval, p, black) +
evaluate_passers(&eval, p, white) - evaluate_passers(&eval, p, black);

int ret = (eval.score.midgame * eval_material->phase + eval.score.endgame * (256 - eval_material->phase)) / 256;
int scale = scaling_factor(p);
int ret = (eval.score.midgame * eval_material->phase + eval.score.endgame * (256 - eval_material->phase) * scale / SCALE_NORMAL) / 256;
return (p->color == white ? ret : -ret) + tempo;
}
20 changes: 20 additions & 0 deletions src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@

#include "const.h"

const Bitboard flank_ranks[2] = {
RANK_1BB | RANK_2BB | RANK_3BB | RANK_4BB,
RANK_8BB | RANK_7BB | RANK_6BB | RANK_5BB
};

const Bitboard queenside_flank = FILE_ABB | FILE_BBB | FILE_CBB | FILE_DBB;
const Bitboard center_flank = FILE_CBB | FILE_DBB | FILE_EBB | FILE_FBB;
const Bitboard kingside_flank = FILE_EBB | FILE_FBB | FILE_GBB | FILE_HBB;

const Bitboard flank_files[8] = {
queenside_flank,
queenside_flank,
queenside_flank,
center_flank,
center_flank,
kingside_flank,
kingside_flank,
kingside_flank
};

int evaluate(Position *p);

#endif
Loading

0 comments on commit bcf8681

Please sign in to comment.