eval-simple.c: game phases, multiple PST

This commit is contained in:
2024-07-21 18:08:10 +02:00
parent 30ac647fe5
commit 14a2cae072
9 changed files with 548 additions and 143 deletions

View File

@@ -129,6 +129,8 @@ else # ifeq ($(build),dev)
# attack.c
#CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers
#CPPFLAGS += -DDEBUG_ATTACK_PINNERS # sq_pinners details
# eval.c eval-simple.c
CPPFLAGS += -DEVAL # eval
# old unused flags
#CPPFLAGS += -DDEBUG_POS # position.c
@@ -413,7 +415,7 @@ TEST += movedo-test perft-test tt-test
PIECE_OBJS := piece.o
FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \
hq.o attack.o hash.o init.o misc.o alloc.o move.o
hq.o attack.o hash.o init.o misc.o alloc.o move.o eval-simple.o
BB_OBJS := $(FEN_OBJS)
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
ATTACK_OBJS := $(MOVEGEN_OBJS)

View File

@@ -81,9 +81,11 @@ typedef enum {
/* game phases
*/
#define OPENING 0
#define MIDDLEGAME 1
#define ENDGAME 2
typedef enum {
MIDGAME,
ENDGAME,
PHASE_NB
} phase_t;
/* forward defs */
typedef struct __pos_s pos_t;
@@ -109,7 +111,7 @@ enum {
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
SQUARE_MAX = 64,
SQUARE_NB = 64,
SQUARE_NONE = 64
};
typedef u8 square_t;
@@ -117,14 +119,14 @@ typedef u8 square_t;
enum {
//_SFILE_ = -1, /* force signed enum */
FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
FILE_MAX,
FILE_NB,
};
typedef u8 file_t;
enum {
//_SRANK_ = -1, /* force signed enum */
RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
RANK_MAX,
RANK_NB,
};
typedef u8 rank_t;

View File

@@ -19,126 +19,507 @@
#include "eval-simple.h"
/*
* Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function
* Piece-square tables. For easier reading, they are defined for black side:
*
* Attention! Tables are black point of view (to be visually easier to read).
* {
* A8 .... H8
* ..........
* ..........
* A1 .... H1
* }
*/
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
const struct pc_sq pc_sq_def[] = {
{
/*
* rofchade:
* https://www.talkchess.com/forum3/viewtopic.php?f=2&t=68311&start=19
*/
"rofchade",
{
[PAWN] = {
{ /* midgame */
0, 0, 0, 0, 0, 0, 0, 0,
98, 134, 61, 95, 68, 126, 34, -11,
-6, 7, 26, 31, 65, 56, 25, -20,
-14, 13, 6, 21, 23, 12, 17, -23,
-27, -2, -5, 12, 17, 6, 10, -25,
-26, -4, -4, -10, 3, 3, 33, -12,
-35, -1, -20, -23, -15, 24, 38, -22,
0, 0, 0, 0, 0, 0, 0, 0,
},
{ /* endgame */
0, 0, 0, 0, 0, 0, 0, 0,
178, 173, 158, 134, 147, 132, 165, 187,
94, 100, 85, 67, 56, 53, 82, 84,
32, 24, 13, 5, -2, 4, 17, 17,
13, 9, -3, -7, -7, -8, 3, -1,
4, 7, -6, 1, 0, -5, -1, -8,
13, 8, 8, 10, 13, 0, 2, -7,
0, 0, 0, 0, 0, 0, 0, 0,
}
},
[KNIGHT] = {
{ /* midgame */
-167, -89, -34, -49, 61, -97, -15, -107,
-73, -41, 72, 36, 23, 62, 7, -17,
-47, 60, 37, 65, 84, 129, 73, 44,
-9, 17, 19, 53, 37, 69, 18, 22,
-13, 4, 16, 13, 28, 19, 21, -8,
-23, -9, 12, 10, 19, 17, 25, -16,
-29, -53, -12, -3, -1, 18, -14, -19,
-105, -21, -58, -33, -17, -28, -19, -23,
},
{ /* endgame */
-58, -38, -13, -28, -31, -27, -63, -99,
-25, -8, -25, -2, -9, -25, -24, -52,
-24, -20, 10, 9, -1, -9, -19, -41,
-17, 3, 22, 22, 22, 11, 8, -18,
-18, -6, 16, 25, 16, 17, 4, -18,
-23, -3, -1, 15, 10, -3, -20, -22,
-42, -20, -10, -5, -2, -20, -23, -44,
-29, -51, -23, -15, -22, -18, -50, -64,
}
},
[BISHOP] = {
{ /* midgame */
-29, 4, -82, -37, -25, -42, 7, -8,
-26, 16, -18, -13, 30, 59, 18, -47,
-16, 37, 43, 40, 35, 50, 37, -2,
-4, 5, 19, 50, 37, 37, 7, -2,
-6, 13, 13, 26, 34, 12, 10, 4,
0, 15, 15, 15, 14, 27, 18, 10,
4, 15, 16, 0, 7, 21, 33, 1,
-33, -3, -14, -21, -13, -12, -39, -21,
},
{ /* endgame */
-14, -21, -11, -8, -7, -9, -17, -24,
-8, -4, 7, -12, -3, -13, -4, -14,
2, -8, 0, -1, -2, 6, 0, 4,
-3, 9, 12, 9, 14, 10, 3, 2,
-6, 3, 13, 19, 7, 10, -3, -9,
-12, -3, 8, 10, 13, 3, -7, -15,
-14, -18, -7, -1, 4, -9, -15, -27,
-23, -9, -23, -5, -9, -16, -5, -17,
},
},
[ROOK] = {
{ /* midgame */
32, 42, 32, 51, 63, 9, 31, 43,
27, 32, 58, 62, 80, 67, 26, 44,
-5, 19, 26, 36, 17, 45, 61, 16,
-24, -11, 7, 26, 24, 35, -8, -20,
-36, -26, -12, -1, 9, -7, 6, -23,
-45, -25, -16, -17, 3, 0, -5, -33,
-44, -16, -20, -9, -1, 11, -6, -71,
-19, -13, 1, 17, 16, 7, -37, -26,
},
{ /* endgame */
13, 10, 18, 15, 12, 12, 8, 5,
11, 13, 13, 11, -3, 3, 8, 3,
7, 7, 7, 5, 4, -3, -5, -3,
4, 3, 13, 1, 2, 1, -1, 2,
3, 5, 8, 4, -5, -6, -8, -11,
-4, 0, -5, -1, -7, -12, -8, -16,
-6, -6, 0, 2, -9, -9, -11, -3,
-9, 2, 3, -1, -5, -13, 4, -20,
},
},
[QUEEN] = {
{ /* midgame */
-28, 0, 29, 12, 59, 44, 43, 45,
-24, -39, -5, 1, -16, 57, 28, 54,
-13, -17, 7, 8, 29, 56, 47, 57,
-27, -27, -16, -16, -1, 17, -2, 1,
-9, -26, -9, -10, -2, -4, 3, -3,
-14, 2, -11, -2, -5, 2, 14, 5,
-35, -8, 11, 2, 8, 15, -3, 1,
-1, -18, -9, 10, -15, -25, -31, -50,
},
{ /* endgame */
-9, 22, 22, 27, 27, 19, 10, 20,
-17, 20, 32, 41, 58, 25, 30, 0,
-20, 6, 9, 49, 47, 35, 19, 9,
3, 22, 24, 45, 57, 40, 57, 36,
-18, 28, 19, 47, 31, 34, 39, 23,
-16, -27, 15, 6, 9, 17, 10, 5,
-22, -23, -30, -16, -16, -23, -36, -32,
-33, -28, -22, -43, -5, -32, -20, -41,
},
},
[KING] = { /* midgame */
{
-65, 23, 16, -15, -56, -34, 2, 13,
29, -1, -20, -7, -8, -4, -38, -29,
-9, 24, 2, -16, -20, 6, 22, -22,
-17, -20, -12, -27, -30, -25, -14, -36,
-49, -1, -27, -39, -46, -44, -33, -51,
-14, -14, -22, -46, -44, -30, -15, -27,
1, 7, -8, -64, -43, -16, 9, 8,
-15, 36, 12, -54, 8, -28, 24, 14,
},
{ /* endgame */
-74, -35, -18, -18, -11, 15, 4, -17,
-12, 17, 14, 17, 17, 38, 23, 11,
10, 17, 23, 15, 20, 45, 44, 13,
-8, 22, 24, 27, 26, 33, 26, 3,
-18, -4, 21, 24, 27, 23, 9, -11,
-19, -3, 11, 21, 23, 16, 7, -9,
-27, -11, 4, 13, 14, 4, -5, -17,
-53, -34, -21, -11, -28, -14, -24, -43
}
}
}
}, /* CPW */
{
/*
* CPW:
* https://www.chessprogramming.org/Simplified_Evaluation_Function
* Note: ≠ https://github.com/nescitus/cpw-engine
*/
"cpw",
{
[PAWN] = {
{ /* midgame */
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,
},
{ /* endgame */
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,
},
},
[KNIGHT] = {
{ /* midgame */
-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
},
{ /* endgame */
-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
},
},
[BISHOP] = {
{ /* midgame */
-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,
},
{ /* endgame */
-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,
},
},
[ROOK] = {
{ /* midgame */
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,
},
{ /* endgame */
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,
},
},
[QUEEN] = {
{ /* midgame */
-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,
},
{ /* endgame */
-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,
},
},
[KING] = { /* midgame */
{
-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,
},
{ /* endgame */
-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,
}
}
}
}, /* CPW */
{
/*
* sjeng: https://github.com/gcp/sjeng
* Rook and Queen from CPW.
*/
"sjeng",
{
[PAWN] = {
{ /* midgame */
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 15, 20, 20, 15, 10, 5,
4, 8, 12, 16, 16, 12, 8, 4,
3, 6, 9, 14, 14, 9, 6, 3,
2, 4, 6, 12, 12, 6, 4, 2,
1, 2, 3, 10, 10, 3, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
},
{ /* endgame */
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 15, 20, 20, 15, 10, 5,
4, 8, 12, 16, 16, 12, 8, 4,
3, 6, 9, 14, 14, 9, 6, 3,
2, 4, 6, 12, 12, 6, 4, 2,
1, 2, 3, 10, 10, 3, 2, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
},
},
[KNIGHT] = {
{ /* midgame */
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 0, 3, 3, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 3, 3, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20,
},
{ /* endgame */
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 0, 3, 3, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 3, 3, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20,
},
},
[BISHOP] = {
{ /* midgame */
-2, -2, -2, -2, -2, -2, -2, -2,
-2, 8, 5, 5, 5, 5, 8, -2,
-2, 3, 3, 5, 5, 3, 3, -2,
-2, 2, 5, 4, 4, 5, 2, -2,
-2, 2, 5, 4, 4, 5, 2, -2,
-2, 3, 3, 5, 5, 3, 3, -2,
-2, 8, 5, 5, 5, 5, 8, -2,
-2, -2, -2, -2, -2, -2, -2, -2,
},
{ /* endgame */
-2, -2, -2, -2, -2, -2, -2, -2,
-2, 8, 5, 5, 5, 5, 8, -2,
-2, 3, 3, 5, 5, 3, 3, -2,
-2, 2, 5, 4, 4, 5, 2, -2,
-2, 2, 5, 4, 4, 5, 2, -2,
-2, 3, 3, 5, 5, 3, 3, -2,
-2, 8, 5, 5, 5, 5, 8, -2,
-2, -2, -2, -2, -2, -2, -2, -2,
},
},
[ROOK] = {
{ /* midgame */
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,
},
{ /* endgame */
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,
},
},
[QUEEN] = {
{ /* midgame */
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 5, 0, 0, 0, 0, -10,
-10, 5, 5, 5, 5, 5, 0, -10,
0, 0, 5, 5, 5, 5, 0, -5,
-5, 0, 5, 5, 5, 5, 0, -5,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20,
},
{ /* endgame */
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 5, 0, 0, 0, 0, -10,
-10, 5, 5, 5, 5, 5, 0, -10,
0, 0, 5, 5, 5, 5, 0, -5,
-5, 0, 5, 5, 5, 5, 0, -5,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20,
},
},
[KING] = { /* midgame */
{
-55, -55, -89, -89, -89, -89, -55, -55,
-34, -34, -55, -55, -55, -55, -34, -34,
-21, -21, -34, -34, -34, -34, -21, -21,
-13, -13, -21, -21, -21, -21, -13, -13,
-8, -8, -13, -13, -13, -13, -8, -8,
-5, -5, -8, -8, -8, -8, -5, -5,
-3, -5, -6, -6, -6, -6, -5, -3,
2, 14, 0, 0, 0, 9, 14, 2,
},
{ /* endgame */
-5, -3, -1, 0, 0, -1, -3, -5,
-3, 10, 10, 10, 10, 10, 10, -3,
-1, 10, 25, 25, 25, 25, 10, -1,
0, 10, 25, 30, 30, 25, 10, 0,
0, 10, 25, 30, 30, 25, 10, 0,
-1, 10, 25, 25, 25, 25, 10, -1,
-3, 10, 10, 10, 10, 10, 10, -3,
-5, -3, -1, 0, 0, -1, -3, -5,
}
}
},
}, /* sjeng */
};
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
};
const int nb_pc_sq = ARRAY_SIZE(pc_sq_def); /* # of predefined pc_sq tables */
int pc_sq_current = 0;
int pc_sq_mg[COLOR_NB][PT_NB][SQUARE_NB];
int pc_sq_eg[COLOR_NB][PT_NB][SQUARE_NB];
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
};
/* phase calculation from Fruit:
* https://github.com/Warpten/Fruit-2.1
*/
int phase;
static u16 piece_phase[] = { 0, 1, 1, 2, 4, 0 };
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 const int p_phase = 0;
static const int n_phase = 1;
static const int b_phase = 1;
static const int r_phase = 2;
static const int q_phase = 4;
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 const int total_phase = p_phase * 16 + n_phase * 4 +
b_phase * 4 + r_phase * 4 + q_phase * 2;
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
};
int calc_phase(pos_t *pos)
{
phase = total_phase;
phase -= p_phase * popcount64(pos->bb[WHITE][PAWN] | pos->bb[BLACK][PAWN]);
phase -= n_phase * popcount64(pos->bb[WHITE][KNIGHT] | pos->bb[BLACK][KNIGHT]);
phase -= b_phase * popcount64(pos->bb[WHITE][BISHOP] | pos->bb[BLACK][BISHOP]);
phase -= r_phase * popcount64(pos->bb[WHITE][ROOK] | pos->bb[BLACK][ROOK]);
phase -= q_phase * popcount64(pos->bb[WHITE][QUEEN] | pos->bb[BLACK][QUEEN]);
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
};
phase = max(phase, 0);
return (phase * 256 + (total_phase / 2)) / total_phase;
}
static int *mg_tables[] = {
NULL,
mg_pawn,
mg_knight,
mg_bishop,
mg_rook,
mg_queen,
mg_king
};
int eval_simple_find(char *str)
{
for (int i = 0; i < nb_pc_sq; ++i)
if (!strcmp(pc_sq_def[i].name, str))
return i;
return -1;
}
static int *eg_tables[] = {
NULL,
mg_pawn,
mg_knight,
mg_bishop,
mg_rook,
mg_queen,
eg_king
};
void eval_simple_set(int set)
{
const struct pc_sq *cur = pc_sq_def + set;
# ifdef DEBUG_EVAL
printf("initializing piece-square tables %d\n", set);
# endif
pc_sq_current = set;
for (piece_type_t pt = PAWN; pt < PT_NB; ++pt) {
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
pc_sq_mg[BLACK][pt][sq] = cur->val[MIDGAME][pt][sq];
pc_sq_mg[WHITE][pt][sq] = cur->val[MIDGAME][pt][FLIP_V(sq)];
/* 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];
pc_sq_eg[BLACK][pt][sq] = cur->val[ENDGAME][pt][sq];
pc_sq_eg[WHITE][pt][sq] = cur->val[ENDGAME][pt][FLIP_V(sq)];
}
}
}
void eval_simple_init(void)
{
# ifdef DEBUG_EVAL
log_f(1, "initializing piece tables\n");
printf("initializing eval_simple\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_set(pc_sq_current);
}
/**
@@ -154,41 +535,36 @@ void eval_simple_init(void)
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;
eval_t mg_eval[2], eg_eval[2];
//struct pc_sq = sq_ int (*gg)[6 + 2][64] = eg? pc_sq_eg: pc_sq_mg;
//pos->eval_simple_phase = ENDGAME;
# ifdef DEBUG_EVAL
log_f(5, "phase = %s.\n", eg? "endgame": "midgame");
log_f(5, "phase = %d\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];
for (color_t color = WHITE; color < COLOR_NB; ++color) {
mg_eval[color] = 0;
eg_eval[color] = 0;
for (piece_type_t pt = PAWN; pt < KING; pt++) {
bitboard_t bb = pos->bb[color][pt];
while (bb) {
square_t sq = bb_next(&bb);
mg_eval[color] += pc_sq_mg[color][pt][sq];
eg_eval[color] += pc_sq_eg[color][pt][sq];
}
# ifdef DEBUG_EVAL
log(5, "\n");
printf("c=%d p=%d mg=%ld eg=%ld\n", color, pt,
popcount64(pos->bb[color][pt]),
mg_eval[color], eg_eval[color]);
# endif
}
}
# ifdef DEBUG_EVAL
log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK],
eval[WHITE], eval[BLACK]);
printf("phase:%d mg[WHITE]:%d mg[BLACK]:%d eg[WHITE]:%d eg[BLACK]:%d\n",
mg_eval[WHITE], mg_eval[BLACK], eg_eval[WHITE], eg_eval[BLACK]);
# endif
return eval[WHITE] - eval[BLACK];

View File

@@ -16,6 +16,22 @@
#include "chessdefs.h"
#include "piece.h"
struct pc_sq {
char *name; /* one word only, no spaces */
int val[PIECE_TYPE_NB][PHASE_NB][SQUARE_NB]; /* MG then EG */
};
/**
* pc_sq - pre-defined piece-square tables.
*/
extern const struct pc_sq pc_sq_def[];
extern const int nb_pc_sq; /* # of pc_sq_def */
extern int pc_sq_current;
extern int pc_sq_mg[COLOR_NB][PT_NB][SQUARE_NB];
extern int pc_sq_eg[COLOR_NB][PT_NB][SQUARE_NB];
/* no queen on board */
#define simple_no_queen(p, c) \
@@ -41,6 +57,9 @@
simple_no_rook(p, BLACK) && \
simple_one_minor_piece(p, BLACK))) )
int calc_phase(pos_t *pos);
int eval_simple_find(char *str);
void eval_simple_set(int set);
void eval_simple_init(void);
eval_t eval_simple(pos_t *pos);

View File

@@ -19,7 +19,7 @@
#include "bitboard.h"
#include "hq.h"
#include "eval-simple.h"
#include "hash.h"
#include "hist.h"
@@ -52,5 +52,9 @@ void init_all(void)
printff("transposition tables... ");
tt_create(HASH_SIZE_DEFAULT);
/* eval tables */
printf("eval data... ");
eval_simple_init();
printf("done.\n");
}

View File

@@ -24,7 +24,7 @@
/**
* piece_details
*/
const struct piece_details piece_details[PIECE_MAX] = {
const struct piece_details piece_details[PIECE_NB] = {
/* cap low fen sym name midgame val endgame val */
[EMPTY] = { "", "", "", "", "", 0, 0 },
[W_PAWN] = { "", "", "P", "", "Pawn", P_VAL_MID, P_VAL_END },

View File

@@ -26,7 +26,7 @@
*/
enum {
WHITE, BLACK,
COLOR_MAX
COLOR_NB
};
typedef u8 color_t;
@@ -34,7 +34,8 @@ enum {
ALL_PIECES = 0, /* 'all pieces' bitboard */
NO_PIECE_TYPE = 0,
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
PIECE_TYPE_MAX = 7 /* bit 4 */
PIECE_TYPE_NB,
PT_NB = PIECE_TYPE_NB
};
typedef u8 piece_type_t;
@@ -43,7 +44,7 @@ enum __piece_e {
NO_PIECE = 0,
W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
B_PAWN = PAWN | 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
PIECE_MAX
PIECE_NB
};
typedef u8 piece_t;
/* default values for midgame, endgame
@@ -82,7 +83,7 @@ extern const struct piece_details {
char *name; /* piece name */
s16 mid_value; /* value midgame */
s16 end_value; /* value endgame */
} piece_details[PIECE_MAX];
} piece_details[PIECE_NB];
extern const char pieces_str[6+6+1]; /* to search from fen/user input */

View File

@@ -49,8 +49,9 @@ typedef struct __pos_s {
hkey_t key;
/* 16 bits */
u16 plycount; /* plies so far, start from 1 */
move_t move;
u16 plycount; /* plies so far, start from 1 */
s16 phase;
/* 8 bits */
square_t en_passant;
@@ -63,7 +64,7 @@ typedef struct __pos_s {
bitboard_t pinners; /* opponent pinners */
bitboard_t blockers; /* pieces blocking pin */
piece_t board[BOARDSIZE];
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
bitboard_t bb[2][PT_NB]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
square_t king[2]; /* dup with bb, faster retrieval */
} pos_t;

View File

@@ -244,7 +244,7 @@ struct fentest {
* - https://www.chessprogramming.net/perfect-perft/ *
*****************************************************/
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
"\"kiwipete\"",
"kiwipete",
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
},
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,