diff --git a/src/eval-simple.c b/src/eval-simple.c index 7d10e31..358cd2a 100644 --- a/src/eval-simple.c +++ b/src/eval-simple.c @@ -1,201 +1,195 @@ -/* - * /\* eval-simple.c - simple position evaluation. - * * - * * Copyright (C) 2023 Bruno Raoult ("br") - * * Licensed under the GNU General Public License v3.0 or later. - * * Some rights reserved. See COPYING. - * * - * * You should have received a copy of the GNU General Public License along with this - * * program. If not, see . - * * - * * SPDX-License-Identifier: GPL-3.0-or-later - * * - * *\/ +/* eval-simple.c - simple position evaluation. * - * #include "br.h" - * #include "debug.h" + * Copyright (C) 2023 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. * - * #include "piece.h" - * #include "eval-simple.h" - * #include "position.h" + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . * - * /\* - * * Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function - * * - * * Attention! Tables are black point of view (to be visually easier to read). - * *\/ + * SPDX-License-Identifier: GPL-3.0-or-later * - * static int mg_pawn[] = { - * 0, 0, 0, 0, 0, 0, 0, 0, - * 50, 50, 50, 50, 50, 50, 50, 50, - * 10, 10, 20, 30, 30, 20, 10, 10, - * 5, 5, 10, 25, 25, 10, 5, 5, - * 0, 0, 0, 20, 20, 0, 0, 0, - * 5, -5, -10, 0, 0, -10, -5, 5, - * 5, 10, 10, -20, -20, 10, 10, 5, - * 0, 0, 0, 0, 0, 0, 0, 0 - * }; - * - * static int mg_knight[] = { - * -50, -40, -30, -30, -30, -30, -40, -50, - * -40, -20, 0, 0, 0, 0, -20, -40, - * -30, 0, 10, 15, 15, 10, 0, -30, - * -30, 5, 15, 20, 20, 15, 5, -30, - * -30, 0, 15, 20, 20, 15, 0, -30, - * -30, 5, 10, 15, 15, 10, 5, -30, - * -40, -20, 0, 5, 5, 0, -20, -40, - * -50, -40, -30, -30, -30, -30, -40, -50 - * }; - * - * static int mg_bishop[] = { - * -20, -10, -10, -10, -10, -10, -10, -20, - * -10, 0, 0, 0, 0, 0, 0, -10, - * -10, 0, 5, 10, 10, 5, 0, -10, - * -10, 5, 5, 10, 10, 5, 5, -10, - * -10, 0, 10, 10, 10, 10, 0, -10, - * -10, 10, 10, 10, 10, 10, 10, -10, - * -10, 5, 0, 0, 0, 0, 5, -10, - * -20, -10, -10, -10, -10, -10, -10, -20 - * }; - * - * static int mg_rook[] = { - * 0, 0, 0, 0, 0, 0, 0, 0, - * 5, 10, 10, 10, 10, 10, 10, 5, - * -5, 0, 0, 0, 0, 0, 0, -5, - * -5, 0, 0, 0, 0, 0, 0, -5, - * -5, 0, 0, 0, 0, 0, 0, -5, - * -5, 0, 0, 0, 0, 0, 0, -5, - * -5, 0, 0, 0, 0, 0, 0, -5, - * 0, 0, 0, 5, 5, 0, 0, 0 - * }; - * - * static int mg_queen[] = { - * -20, -10, -10, -5, -5, -10, -10, -20, - * -10, 0, 0, 0, 0, 0, 0, -10, - * -10, 0, 5, 5, 5, 5, 0, -10, - * -5, 0, 5, 5, 5, 5, 0, -5, - * 0, 0, 5, 5, 5, 5, 0, -5, - * -10, 5, 5, 5, 5, 5, 0, -10, - * -10, 0, 5, 0, 0, 0, 0, -10, - * -20, -10, -10, -5, -5, -10, -10, -20 - * }; - * - * static int mg_king[] = { - * -30, -40, -40, -50, -50, -40, -40, -30, - * -30, -40, -40, -50, -50, -40, -40, -30, - * -30, -40, -40, -50, -50, -40, -40, -30, - * -30, -40, -40, -50, -50, -40, -40, -30, - * -20, -30, -30, -40, -40, -30, -30, -20, - * -10, -20, -20, -20, -20, -20, -20, -10, - * 20, 20, 0, 0, 0, 0, 20, 20, - * 20, 30, 10, 0, 0, 10, 30, 20 - * }; - * - * static int eg_king[] = { - * -50, -40, -30, -20, -20, -30, -40, -50, - * -30, -20, -10, 0, 0, -10, -20, -30, - * -30, -10, 20, 30, 30, 20, -10, -30, - * -30, -10, 30, 40, 40, 30, -10, -30, - * -30, -10, 30, 40, 40, 30, -10, -30, - * -30, -10, 20, 30, 30, 20, -10, -30, - * -30, -30, 0, 0, 0, 0, -30, -30, - * -50, -30, -30, -30, -30, -30, -30, -50 - * }; - * - * /\* as pieces bitboard tables start at position 2; we make these tables - * * bigger. - * *\/ - * static int *mg_tables[] = { - * NULL, - * NULL, - * mg_pawn, - * mg_knight, - * mg_bishop, - * mg_rook, - * mg_queen, - * mg_king - * }; - * - * static int *eg_tables[] = { - * NULL, - * NULL, - * mg_pawn, - * mg_knight, - * mg_bishop, - * mg_rook, - * mg_queen, - * eg_king - * }; - * - * /\* to flip vertically a square, we need to XOR it with 56 - * *\/ - * static int mg_table[2][6 + 2][64]; - * static int eg_table[2][6 + 2][64]; - * - * void eval_simple_init(void) - * { - * # ifdef DEBUG_EVAL - * log_f(1, "initializing piece tables\n"); - * # endif - * for (int piece = BB_PAWN; piece <= BB_KING; ++piece) { - * for (int square = 0; square < 64; ++square) { - * mg_table[WHITE][piece][square] = mg_tables[piece][FLIP_V(square)]; - * eg_table[WHITE][piece][square] = eg_tables[piece][FLIP_V(square)]; - * mg_table[BLACK][piece][square] = mg_tables[piece][square]; - * eg_table[BLACK][piece][square] = eg_tables[piece][square]; - * } - * } - * } - * - * /\** - * * eval_simple() - simple and fast position evaluation - * * @pos: &position to evaluate - * * - * * This function is normally used only during initialization, - * * or when changing phase (middlegame <--> endgame), as the eval - * * will be done increntally when doing moves. - * * - * * @return: the @pos evaluation in centipawns - * *\/ - * eval_t eval_simple(pos_t *pos) - * { - * eval_t eval[2] = { 0, 0 }; - * int eg = simple_is_endgame(pos); - * int (*gg)[6 + 2][64]= eg? eg_table: mg_table; - * - * pos->eval_simple_phase = ENDGAME; - * # ifdef DEBUG_EVAL - * log_f(5, "phase = %s.\n", eg? "endgame": "midgame"); - * # endif - * - * for (int color = WHITE; color <= BLACK; ++color) { - * for (uint piece = PAWN; piece <= KING; piece <<= 1) { - * int bb = PIECETOBB(piece), cur; - * u64 _t; - * - * # ifdef DEBUG_EVAL - * log_f(5, "p=%u bb=%d %s %s: count=%d val=%ld ", piece, bb, color? "black": "white", - * P_SYM(piece), popcount64(pos->bb[color][bb]), - * popcount64(pos->bb[color][bb]) * P_VALUE(piece)); - * # endif - * - * eval[color] += popcount64(pos->bb[color][bb]) * P_LETTER(piece); - * bit_for_each64(cur, _t, pos->bb[color][bb]) { - * # ifdef DEBUG_EVAL - * log(5, "sq=%d:%d ", cur, gg[color][bb][cur]); - * # endif - * eval[color] += gg[color][bb][cur]; - * } - * # ifdef DEBUG_EVAL - * log(5, "\n"); - * # endif - * } - * } - * # ifdef DEBUG_EVAL - * log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK], - * eval[WHITE], eval[BLACK]); - * # endif - * - * return eval[WHITE] - eval[BLACK]; - * } */ + +#include + +#include "chessdefs.h" +#include "piece.h" +#include "position.h" +#include "eval-simple.h" + +/* + * Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function + * + * Attention! Tables are black point of view (to be visually easier to read). + */ + +static int mg_pawn[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 50, 50, 50, 50, 50, 50, 50, 50, + 10, 10, 20, 30, 30, 20, 10, 10, + 5, 5, 10, 25, 25, 10, 5, 5, + 0, 0, 0, 20, 20, 0, 0, 0, + 5, -5, -10, 0, 0, -10, -5, 5, + 5, 10, 10, -20, -20, 10, 10, 5, + 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static int mg_knight[] = { + -50, -40, -30, -30, -30, -30, -40, -50, + -40, -20, 0, 0, 0, 0, -20, -40, + -30, 0, 10, 15, 15, 10, 0, -30, + -30, 5, 15, 20, 20, 15, 5, -30, + -30, 0, 15, 20, 20, 15, 0, -30, + -30, 5, 10, 15, 15, 10, 5, -30, + -40, -20, 0, 5, 5, 0, -20, -40, + -50, -40, -30, -30, -30, -30, -40, -50 +}; + +static int mg_bishop[] = { + -20, -10, -10, -10, -10, -10, -10, -20, + -10, 0, 0, 0, 0, 0, 0, -10, + -10, 0, 5, 10, 10, 5, 0, -10, + -10, 5, 5, 10, 10, 5, 5, -10, + -10, 0, 10, 10, 10, 10, 0, -10, + -10, 10, 10, 10, 10, 10, 10, -10, + -10, 5, 0, 0, 0, 0, 5, -10, + -20, -10, -10, -10, -10, -10, -10, -20 +}; + +static int mg_rook[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 5, 10, 10, 10, 10, 10, 10, 5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + -5, 0, 0, 0, 0, 0, 0, -5, + 0, 0, 0, 5, 5, 0, 0, 0 +}; + +static int mg_queen[] = { + -20, -10, -10, -5, -5, -10, -10, -20, + -10, 0, 0, 0, 0, 0, 0, -10, + -10, 0, 5, 5, 5, 5, 0, -10, + -5, 0, 5, 5, 5, 5, 0, -5, + 0, 0, 5, 5, 5, 5, 0, -5, + -10, 5, 5, 5, 5, 5, 0, -10, + -10, 0, 5, 0, 0, 0, 0, -10, + -20, -10, -10, -5, -5, -10, -10, -20 +}; + +static int mg_king[] = { + -30, -40, -40, -50, -50, -40, -40, -30, + -30, -40, -40, -50, -50, -40, -40, -30, + -30, -40, -40, -50, -50, -40, -40, -30, + -30, -40, -40, -50, -50, -40, -40, -30, + -20, -30, -30, -40, -40, -30, -30, -20, + -10, -20, -20, -20, -20, -20, -20, -10, + 20, 20, 0, 0, 0, 0, 20, 20, + 20, 30, 10, 0, 0, 10, 30, 20 +}; + +static int eg_king[] = { + -50, -40, -30, -20, -20, -30, -40, -50, + -30, -20, -10, 0, 0, -10, -20, -30, + -30, -10, 20, 30, 30, 20, -10, -30, + -30, -10, 30, 40, 40, 30, -10, -30, + -30, -10, 30, 40, 40, 30, -10, -30, + -30, -10, 20, 30, 30, 20, -10, -30, + -30, -30, 0, 0, 0, 0, -30, -30, + -50, -30, -30, -30, -30, -30, -30, -50 +}; + +static int *mg_tables[] = { + NULL, + mg_pawn, + mg_knight, + mg_bishop, + mg_rook, + mg_queen, + mg_king +}; + +static int *eg_tables[] = { + NULL, + mg_pawn, + mg_knight, + mg_bishop, + mg_rook, + mg_queen, + eg_king +}; + +/* to flip vertically a square, we need to XOR it with 56 + */ +static int mg_table[2][6 + 2][64]; +static int eg_table[2][6 + 2][64]; + +void eval_simple_init(void) +{ +# ifdef DEBUG_EVAL + log_f(1, "initializing piece tables\n"); +# endif + for (int piece = PAWN; piece <= KING; ++piece) { + for (int square = 0; square < 64; ++square) { + mg_table[WHITE][piece][square] = mg_tables[piece][FLIP_V(square)]; + eg_table[WHITE][piece][square] = eg_tables[piece][FLIP_V(square)]; + mg_table[BLACK][piece][square] = mg_tables[piece][square]; + eg_table[BLACK][piece][square] = eg_tables[piece][square]; + } + } +} + +/** + * eval_simple() - simple and fast position evaluation + * @pos: &position to evaluate + * + * This function is normally used only during initialization, + * or when changing phase (middlegame <--> endgame), as the eval + * will be done incrementally when doing moves. + * + * @return: the @pos evaluation in centipawns + */ +eval_t eval_simple(pos_t *pos) +{ + eval_t eval[2] = { 0, 0 }; + int eg = simple_is_endgame(pos); + int (*gg)[6 + 2][64]= eg? eg_table: mg_table; + + //pos->eval_simple_phase = ENDGAME; +# ifdef DEBUG_EVAL + log_f(5, "phase = %s.\n", eg? "endgame": "midgame"); +# endif + + for (color_t color = WHITE; color <= BLACK; ++color) { + for (piece_type_t pt = PAWN; pt <= KING; pt++) { + //int bb = PIECETOBB(piece), cur; + u64 _t; + int cur; + +# ifdef DEBUG_EVAL + log_f(5, "p=%u bb=%d %s %s: count=%d val=%ld ", piece, bb, color? "black": "white", + P_SYM(piece), popcount64(pos->bb[color][bb]), + popcount64(pos->bb[color][bb]) * P_VALUE(piece)); +# endif + + eval[color] += popcount64(pos->bb[color][pt]) * piece_val(pt); + bit_for_each64(cur, _t, pos->bb[color][pt]) { +# ifdef DEBUG_EVAL + log(5, "sq=%d:%d ", cur, gg[color][bb][cur]); +# endif + eval[color] += gg[color][pt][cur]; + } +# ifdef DEBUG_EVAL + log(5, "\n"); +# endif + } + } +# ifdef DEBUG_EVAL + log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK], + eval[WHITE], eval[BLACK]); +# endif + + return eval[WHITE] - eval[BLACK]; +} diff --git a/src/eval-simple.h b/src/eval-simple.h index 3ca92ab..4bbc34d 100644 --- a/src/eval-simple.h +++ b/src/eval-simple.h @@ -1,6 +1,6 @@ /* eval-simple.h - simple position evaluation. * - * Copyright (C) 2021 Bruno Raoult ("br") + * Copyright (C) 2021-2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. * Some rights reserved. See COPYING. * @@ -19,16 +19,16 @@ /* no queen on board */ #define simple_no_queen(p, c) \ - ( !(p)->bb[c][BB_QUEEN] ) + ( !(p)->bb[c][QUEEN] ) #define simple_one_queen(p, c) \ - ( popcount64((p)->bb[c][BB_QUEEN]) == 1 ) + ( popcount64((p)->bb[c][QUEEN]) == 1 ) #define simple_no_rook(p, c) \ - (!(p)->bb[c][BB_ROOK]) + (!(p)->bb[c][ROOK]) #define simple_one_minor_piece(p, c) \ - (popcount64((p)->bb[c][BB_KNIGHT] | (p)->bb[c][BB_BISHOP]) == 1) + (popcount64((p)->bb[c][KNIGHT] | (p)->bb[c][BISHOP]) == 1) #define simple_is_endgame(p) \ ( (simple_no_queen(p, WHITE) || \ diff --git a/src/eval.c b/src/eval.c index 69e606d..3a47ae0 100644 --- a/src/eval.c +++ b/src/eval.c @@ -1,97 +1,99 @@ +/* eval.c - static position evaluation. + * + * Copyright (C) 2021-2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include + +#include + +#include "chessdefs.h" +#include "position.h" +#include "eval.h" +#include "eval-simple.h" + +inline eval_t eval_material(pos_t *pos, bool color) +{ + eval_t res = 0; + + /* I need to do something about the king, if it can be potentially taken + * if pseudo-moves include a pinned piece on King. + */ + for (piece_type_t pt = PAWN; pt < KING; ++pt) { +# ifdef DEBUG_EVAL + log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n", + color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]), + P_VALUE(piece)); +# endif + res += popcount64(pos->bb[color][pt]) * piece_val(pt); + } + return res; +} + + + /* - * /\* eval.c - static position evaluation. - * * - * * Copyright (C) 2021-2023 Bruno Raoult ("br") - * * Licensed under the GNU General Public License v3.0 or later. - * * Some rights reserved. See COPYING. - * * - * * You should have received a copy of the GNU General Public License along with this - * * program. If not, see . - * * - * * SPDX-License-Identifier: GPL-3.0-or-later - * * - * *\/ - * - * #include - * - * #include - * #include - * - * #include "position.h" - * #include "eval.h" - * #include "eval-simple.h" - * - * inline eval_t eval_material(pos_t *pos, bool color) - * { - * eval_t res = 0; - * - * /\* I need to do something about the king, if it can be potentially taken - * * if pseudo-moves include a pinned piece on King. - * *\/ - * for (uint piece = PAWN; piece < KING; piece <<= 1) { - * uint bb = PIECETOBB(piece); - * # ifdef DEBUG_EVAL - * log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n", - * color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]), - * P_VALUE(piece)); - * # endif - * /\* attention here *\/ - * res += popcount64(pos->bb[color][bb]) * P_VALUE(piece); - * } - * return res; - * } - * * inline eval_t eval_mobility(pos_t *pos, bool color) * { * return pos->mobility[color]; * } * + * * inline eval_t eval_square_control(pos_t *pos, bool color) * { * return popcount64(pos->controlled[color]); * } - * - * eval_t eval(pos_t *pos) - * { - * eval_t simple = 0, control[2] = {0}; - * - * if (pos->eval != EVAL_INVALID) - * return pos->eval; - * - * /\* 1) pieces value *\/ - * //material[WHITE] = eval_material(pos, WHITE); - * //material[BLACK] = eval_material(pos, BLACK); - * simple = eval_simple(pos); - * - * # ifdef DEBUG_EVAL - * log_f(2, "eval_simple=%d\n", simple); - * # endif - * - * /\* 2) square control: 10 square controls diff = 1 pawn *\/ - * control[WHITE] = eval_square_control(pos, WHITE); - * control[BLACK] = eval_square_control(pos, BLACK); - * - * # ifdef DEBUG_EVAL - * log_f(2, "square control: W:%d B:%d diff=%d\n", - * control[WHITE], control[BLACK], - * (control[WHITE] - control[BLACK]) * 10); - * # endif - * - * /\* 3) mobility: 10 mobility diff = 1 pawn - * *\/ - * # ifdef DEBUG_EVAL - * log_f(2, "mobility: W:%u B:%u diff=%d\n", - * pos->mobility[WHITE], pos->mobility[BLACK], - * (pos->mobility[WHITE] - pos->mobility[BLACK]) * 10); - * # endif - * - * eval_t res = simple + - * (control[WHITE] - control[BLACK]) * 10 + - * (pos->mobility[WHITE] - pos->mobility[BLACK]) * 10; - * # ifdef DEBUG_EVAL - * log_f(2, "eval: %d\n", res); - * # endif - * pos->eval = res; - * return res; - * } */ + +eval_t eval(pos_t *pos) +{ + //eval_t simple = 0, control[2] = {0}; + eval_t simple = 0; + + //if (pos->eval != EVAL_INVALID) + // return pos->eval; + + /* 1) pieces value */ + //material[WHITE] = eval_material(pos, WHITE); + //material[BLACK] = eval_material(pos, BLACK); + simple = eval_simple(pos); + +# ifdef DEBUG_EVAL + log_f(2, "eval_simple=%d\n", simple); +# endif + + /* 2) square control: 10 square controls diff = 1 pawn */ + //control[WHITE] = eval_square_control(pos, WHITE); + //control[BLACK] = eval_square_control(pos, BLACK); + +# ifdef DEBUG_EVAL + log_f(2, "square control: W:%d B:%d diff=%d\n", + control[WHITE], control[BLACK], + (control[WHITE] - control[BLACK]) * 10); +# endif + + /* 3) mobility: 10 mobility diff = 1 pawn + */ +# ifdef DEBUG_EVAL + log_f(2, "mobility: W:%u B:%u diff=%d\n", + pos->mobility[WHITE], pos->mobility[BLACK], + (pos->mobility[WHITE] - pos->mobility[BLACK]) * 10); +# endif + + //eval_t res = simple + + // (control[WHITE] - control[BLACK]) * 10 + + // (pos->mobility[WHITE] - pos->mobility[BLACK]) * 10; +# ifdef DEBUG_EVAL + log_f(2, "eval: %d\n", res); +# endif + pos->eval = simple; + return simple; +} diff --git a/src/eval.h b/src/eval.h index 07158ee..77f6f79 100644 --- a/src/eval.h +++ b/src/eval.h @@ -17,16 +17,16 @@ #include #include "chessdefs.h" -#include "piece.h" -/* max pieces eval is KING_VALUE + 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE - * + 2*KNIGHT_VALUE which around 30000. - * We are on secure side with -50000/+50000 +/* max pieces eval is 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE + * + 2*KNIGHT_VALUE which is (for a pawn valued at 100) well less than 10,000. */ -#define EVAL_MAX (50000) +#define EVAL_MAX (SHRT_MAX) /* 32767 */ #define EVAL_MIN (-EVAL_MAX) -#define EVAL_INVALID INT_MIN -#define EVAL_MATE EVAL_MAX + +#define EVAL_INVALID EVAL_MIN + +#define EVAL_MATE 30000 eval_t eval_material(pos_t *pos, bool color); eval_t eval_mobility(pos_t *pos, bool color);