eval.c, eval-simple.c: start to migrate from 0x88 (does not compile)

This commit is contained in:
2024-06-30 10:00:35 +02:00
parent 4d1870ffb3
commit 58c985f52f
4 changed files with 290 additions and 294 deletions

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
* *
* * SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
* *
* *\/
/* 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* /\*
* * 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 <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
* 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 <brlib.h>
#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];
}

View File

@@ -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) || \

View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <brlib.h>
#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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
* *
* * SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
* *
* *\/
*
* #include <stdio.h>
*
* #include <list.h>
* #include <debug.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 (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;
}

View File

@@ -17,16 +17,16 @@
#include <limits.h>
#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);