-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluation.h
executable file
·185 lines (152 loc) · 6.63 KB
/
evaluation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef EVALUATION_H
#define EVALUATION_H
#include "defines.h"
#include "constants.h"
#include "utils.h"
#include "king.h"
#include "piecesquaretables.h"
#include "compassrose.h"
#include "board.h"
#include "pawntable.h"
#include <cassert>
#include <cmath>
namespace Napoleon
{
class Piece;
namespace Evaluation
{
int Evaluate(Board&);
Score EvaluatePiece(Piece, Square, BitBoard, Board&);
Score PieceSquareValue(Piece, Square);
int KingSafety(Board&);
void PrintEval(Board&);
Score evaluatePawn(Color, Square, Board&);
Score evaluateOutpost(Color, Square, Board&);
int interpolate(Score, int);
void formatParam(std::string, int, int);
void formatParam(std::string, Score, Score, int);
inline void updateScore(Score&, int, int);
inline void updateScore(Score&, int);
inline void updateScore(Score&, Score);
extern int multiPawnP[8]; // penalization for doubled, tripled... pawns
extern int isolatedPawnP[8]; // number
extern int passedPawn[3][8]; // phase, file
extern int candidatePawn[3][8]; // phase, rank
extern int supportedPassed[3]; // phase
extern int pawnIslandsP[3][5]; // phase, number
extern int mobilityBonus[3][7][Constants::QueenMaxMoves + 1];
extern int backwardPawnP[3]; // phase
extern BitBoard pawnAttacks[2]; // color
extern BitBoard unpinnedKnightAttacks[2]; // color
extern PawnTable pawnTable;
extern int hangingValue[2]; // color
extern int kingAttacks[200]; // number of weighted attacks
}
INLINE void Evaluation::updateScore(std::pair<int, int>& scores, int openingBonus, int endBonus)
{
scores.first += openingBonus;
scores.second += endBonus;
}
INLINE void Evaluation::updateScore(std::pair<int, int>& scores, int openingBonus)
{
scores.first += openingBonus;
scores.second += openingBonus;
}
INLINE void Evaluation::updateScore(std::pair<int, int>& scores, Score bonus)
{
scores.first += bonus.first;
scores.second += bonus.second;
}
inline int Evaluation::interpolate(Score score, int phase)
{
return ((score.first * (Constants::Eval::MaxPhase - phase)) + (score.second * phase)) / Constants::Eval::MaxPhase; // linear-interpolated score
}
inline Score Evaluation::PieceSquareValue(Piece piece, Square square)
{
square = piece.Color == PieceColor::White ? square : Utils::Square::MirrorSquare(square);
return std::make_pair(PieceSquareTable[piece.Type][Opening][square], PieceSquareTable[piece.Type][EndGame][square]);
}
inline Score Evaluation::evaluatePawn(Color color, Square square, Board& board)
{
using namespace PieceColor;
using namespace Utils::BitBoard;
using namespace Utils::Square;
Score score(0,0);
BitBoard pawns[2] = { // color
board.Pieces(White, Pawn), board.Pieces(Black, Pawn)
};
//int tropism = 1;
Color enemy = Utils::Piece::GetOpposite(color);
if ((MoveDatabase::FrontSpan[color][square] & pawns[color]) == 0) // NO OWN PAWNS IN FRONT
{
Rank rrank = RelativeRank(color, GetRankIndex(square));
if ((MoveDatabase::PasserSpan[color][square] & pawns[enemy]) == 0) // NO ENEMY PAWNS (i.e. PASSED PAWN)
{
updateScore(score, passedPawn[Opening][rrank], passedPawn[EndGame][rrank]);
//SetBit(entry->passers[color], square);
//tropism = 2;
}
else if ((MoveDatabase::FrontSpan[color][square] & pawns[enemy]) == 0) // NO ENEMY PAWNS IN FRONT (i.e. OPEN PAWN)
{
int defenders = PopCount(MoveDatabase::CandidateDefenders[color][square] & pawns[color]);
if (defenders > 0)
{
int attackers = PopCount(MoveDatabase::AttackFrontSpan[color][square] & pawns[enemy]);
if (defenders >= attackers) // CANDIDATE PASSER
{
updateScore(score, candidatePawn[Opening][rrank], candidatePawn[EndGame][rrank]);
//SetBit(entry->passers[color], square);
}
}
/* BACKWARD PAWN DISABLED: no elo gains
if (rrank <= 4) // OPEN PAWNS ON SECOND, THIRD or FOURTH RANK (color relative)
{
BitBoard stop = MoveDatabase::PawnStop[color][square];
// BACKWARD PAWN DEFINITION:
// * - open pawn
// * - pawn on second, third or fourth rank (color relative)
// * - stop square attacked by enemy pawns
// * - stop square not defended by own pawns
// * TODO: consider opposing backward pawns (the more advanced is not backward)
// * TODO: consider backward pawns for king-pawn tropism evaluation
if ((stop & pawnAttacks[enemy]) && !(stop & pawnAttacks[color])) // BACKWARD PAWN
{
updateScore(score, -backwardPawnP[Opening], -backwardPawnP[EndGame]);
}
}
*/
}
}
//int enemy_king_distance = MoveDatabase::Distance[square][board.KingSquare(enemy)];
//int pawn_file = GetFileIndex(square);
//int king_file = GetFileIndex(board.KingSquare(enemy));
//pawn storm evaluation:
//we penalize enemy pawns near own king
//updateScore(score, 7 - enemy_king_distance, 0);
// NEXT TO TRY:
// ONLY CONDIDER PAWNS ON ADJACENT FILES TO KING
//if (std::abs(pawn_file - king_file) <= 1) // i.e. on the same or adjacent file to king
//updateScore(score, 3 - enemy_king_distance/2, 0);
return color == White ? score : -score;
}
inline Score Evaluation::evaluateOutpost(Color color, Square square, Board& board)
{
using namespace PieceColor;
using namespace Utils::BitBoard;
using namespace Utils::Square;
Score bonus(0,0);
Rank rrank = RelativeRank(color, square);
BitBoard square_mask = Constants::Masks::SquareMask[square];
if (rrank < Constants::Ranks::Int4) return bonus;
Color enemy = Utils::Piece::GetOpposite(color);
if (pawnAttacks[color] & square_mask) // & ~pawAttacks not needed
{
if((MoveDatabase::AttackFrontSpan[color][square] & board.Pieces(enemy, Pawn)) == 0)
{
updateScore(bonus, 10, 8);
}
}
return bonus;
}
}
#endif // EVALUATION_H