Rev 108 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 108 | Rev 154 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 2 | #include "evaluate.h" |
2 | #include "evaluate.h" |
| 3 | #include "data.h" |
3 | #include "data.h" |
| 4 | /* last modified |
4 | /* last modified 08/03/16 */ |
| 5 | /* |
5 | /* |
| 6 | ******************************************************************************* |
6 | ******************************************************************************* |
| 7 | * * |
7 | * * |
| 8 | * Evaluate() is used to evaluate the chess board. Broadly, it addresses * |
8 | * Evaluate() is used to evaluate the chess board. Broadly, it addresses * |
| 9 | * four (4) distinct areas: (1) material score which is simply a summing of * |
9 | * four (4) distinct areas: (1) material score which is simply a summing of * |
| Line 18... | Line 18... | ||
| 18 | */ |
18 | */ |
| 19 | int Evaluate(TREE * RESTRICT tree, int ply, int wtm, int alpha, int beta) { |
19 | int Evaluate(TREE * RESTRICT tree, int ply, int wtm, int alpha, int beta) { |
| 20 | PAWN_HASH_ENTRY *ptable; |
20 | PAWN_HASH_ENTRY *ptable; |
| 21 | PXOR *pxtable; |
21 | PXOR *pxtable; |
| 22 | int score, side, can_win = 3, phase, lscore, cutoff; |
22 | int score, side, can_win = 3, phase, lscore, cutoff; |
| 23 | int full = 0; |
- | |
| 24 | uint64_t *etable; |
- | |
| 25 | uint64_t temp_hashkey; |
- | |
| 26 | 23 | ||
| 27 | /* |
24 | /* |
| 28 | ************************************************************* |
25 | ************************************************************* |
| 29 | * * |
26 | * * |
| 30 | * First thing we do is if -DSKILL was passed in to the * |
27 | * First thing we do is if -DSKILL was passed in to the * |
| Line 41... | Line 38... | ||
| 41 | burner[j - 1] = burner[j - 1] * burner[j]; |
38 | burner[j - 1] = burner[j - 1] * burner[j]; |
| 42 | if (TimeCheck(tree, 1)) |
39 | if (TimeCheck(tree, 1)) |
| 43 | abort_search = 1; |
40 | abort_search = 1; |
| 44 | } |
41 | } |
| 45 | #endif |
42 | #endif |
| 46 | /* |
- | |
| 47 | ************************************************************ |
- | |
| 48 | * * |
- | |
| 49 | * Next we check to see if this position has been handled * |
- | |
| 50 | * before. If so, we can skip the work saved in the eval * |
- | |
| 51 | * hash table and just return the score found there. Note * |
- | |
| 52 | * that we only store FULL evaluations in the evaluation * |
- | |
| 53 | * hash, if an early (lazy) exit is taken, nothing is * |
- | |
| 54 | * stored. * |
- | |
| 55 | * * |
- | |
| 56 | ************************************************************ |
- | |
| 57 | */ |
- | |
| 58 | temp_hashkey = (wtm) ? HashKey : ~HashKey; |
- | |
| 59 | etable = eval_hash_table + (temp_hashkey & eval_hash_mask); |
- | |
| 60 | if (*etable >> 16 == temp_hashkey >> 16) { |
- | |
| 61 | score = (*etable & 0xffff) - 32768; |
- | |
| 62 | return (wtm) ? score : -score; |
- | |
| 63 | } |
- | |
| 64 | /* |
43 | /* |
| 65 | ************************************************************* |
44 | ************************************************************* |
| 66 | * * |
45 | * * |
| 67 | * First lazy cutoff attempt. If the material score is way * |
46 | * First lazy cutoff attempt. If the material score is way * |
| 68 | * below alpha or way above beta (way above means so far * |
47 | * below alpha or way above beta (way above means so far * |
| Line 141... | Line 120... | ||
| 141 | tree->score_eg = tree->score_eg / 16; |
120 | tree->score_eg = tree->score_eg / 16; |
| 142 | if (tree->score_eg < DrawScore(1) && !(can_win & 1)) |
121 | if (tree->score_eg < DrawScore(1) && !(can_win & 1)) |
| 143 | tree->score_eg = tree->score_eg / 16; |
122 | tree->score_eg = tree->score_eg / 16; |
| 144 | #if defined(SKILL) |
123 | #if defined(SKILL) |
| 145 | if (skill < 100) |
124 | if (skill < 100) |
| 146 | tree->score_eg = |
125 | tree->score_eg = |
| 147 |
|
126 | skill * tree->score_eg / 100 + ((100 - |
| 148 | skill) * PAWN_VALUE * (uint64_t) Random32() / |
127 | skill) * PAWN_VALUE * (uint64_t) Random32() / |
| 149 | 0x100000000ull) / 100 |
128 | 0x100000000ull) / 100; |
| 150 | #endif |
129 | #endif |
| 151 | return (wtm) ? tree->score_eg : -tree->score_eg; |
130 | return (wtm) ? tree->score_eg : -tree->score_eg; |
| 152 | } |
131 | } |
| 153 | } |
132 | } |
| 154 | /* |
133 | /* |
| Line 294... | Line 273... | ||
| 294 | */ |
273 | */ |
| 295 | if (lscore + cutoff > alpha && lscore - cutoff < beta) { |
274 | if (lscore + cutoff > alpha && lscore - cutoff < beta) { |
| 296 | tree->tropism[white] = 0; |
275 | tree->tropism[white] = 0; |
| 297 | tree->tropism[black] = 0; |
276 | tree->tropism[black] = 0; |
| 298 | for (side = black; side <= white; side++) |
277 | for (side = black; side <= white; side++) |
| - | 278 | if (Knights(side)) |
|
| 299 | EvaluateKnights(tree, side); |
279 | EvaluateKnights(tree, side); |
| 300 | for (side = black; side <= white; side++) |
280 | for (side = black; side <= white; side++) |
| - | 281 | if (Bishops(side)) |
|
| 301 | EvaluateBishops(tree, side); |
282 | EvaluateBishops(tree, side); |
| 302 | for (side = black; side <= white; side++) |
283 | for (side = black; side <= white; side++) |
| - | 284 | if (Rooks(side)) |
|
| 303 | EvaluateRooks(tree, side); |
285 | EvaluateRooks(tree, side); |
| 304 | for (side = black; side <= white; side++) |
286 | for (side = black; side <= white; side++) |
| - | 287 | if (Queens(side)) |
|
| 305 | EvaluateQueens(tree, side); |
288 | EvaluateQueens(tree, side); |
| 306 | for (side = black; side <= white; side++) |
289 | for (side = black; side <= white; side++) |
| 307 | EvaluateKing(tree, ply, side); |
290 | EvaluateKing(tree, ply, side); |
| 308 | full = 1; |
- | |
| 309 | } |
291 | } |
| 310 | /* |
292 | /* |
| 311 | ************************************************************* |
293 | ************************************************************* |
| 312 | * * |
294 | * * |
| 313 | * Caclulate the final score, which is interpolated between * |
295 | * Caclulate the final score, which is interpolated between * |
| Line 321... | Line 303... | ||
| 321 | */ |
303 | */ |
| 322 | score = ((tree->score_mg * phase) + (tree->score_eg * (62 - phase))) / 62; |
304 | score = ((tree->score_mg * phase) + (tree->score_eg * (62 - phase))) / 62; |
| 323 | score = EvaluateDraws(tree, ply, can_win, score); |
305 | score = EvaluateDraws(tree, ply, can_win, score); |
| 324 | #if defined(SKILL) |
306 | #if defined(SKILL) |
| 325 | if (skill < 100) |
307 | if (skill < 100) |
| 326 | score = |
308 | score = |
| 327 |
|
309 | skill * score / 100 + ((100 - |
| 328 | skill) * PAWN_VALUE * (uint64_t) Random32() / 0x100000000ull) / |
310 | skill) * PAWN_VALUE * (uint64_t) Random32() / 0x100000000ull) / |
| 329 | 100 |
311 | 100; |
| 330 | #endif |
312 | #endif |
| 331 | if (full) |
- | |
| 332 | *etable = (temp_hashkey & 0xffffffffffff0000) + score + 32768; |
- | |
| 333 | return (wtm) ? score : -score; |
313 | return (wtm) ? score : -score; |
| 334 | } |
314 | } |
| 335 | 315 | ||
| 336 | /* last modified 10/19/15 */ |
316 | /* last modified 10/19/15 */ |
| 337 | /* |
317 | /* |
| Line 412... | Line 392... | ||
| 412 | if (mobility < 0 && (pawn_attacks[enemy][square] & Pawns(side)) |
392 | if (mobility < 0 && (pawn_attacks[enemy][square] & Pawns(side)) |
| 413 | && (File(square) == FILEA || File(square) == FILEH)) |
393 | && (File(square) == FILEA || File(square) == FILEH)) |
| 414 | mobility -= 8; |
394 | mobility -= 8; |
| 415 | score_mg += mobility; |
395 | score_mg += mobility; |
| 416 | score_eg += mobility; |
396 | score_eg += mobility; |
| 417 | /* |
- | |
| 418 | ************************************************************ |
- | |
| 419 | * * |
- | |
| 420 | * Check for pawns on both wings, which makes a bishop * |
- | |
| 421 | * even more valuable against an enemy knight * |
- | |
| 422 | * * |
- | |
| 423 | ************************************************************ |
- | |
| 424 | */ |
- | |
| 425 | if (tree->all_pawns & mask_fgh && tree->all_pawns & mask_abc) { |
- | |
| 426 | score_mg += bishop_wing_pawns[mg]; |
- | |
| 427 | score_eg += bishop_wing_pawns[eg]; |
- | |
| 428 | } |
- | |
| 429 | /* |
397 | /* |
| 430 | ************************************************************ |
398 | ************************************************************ |
| 431 | * * |
399 | * * |
| 432 | * Adjust the tropism count for this piece. * |
400 | * Adjust the tropism count for this piece. * |
| 433 | * * |
401 | * * |
| Line 450... | Line 418... | ||
| 450 | ************************************************************ |
418 | ************************************************************ |
| 451 | */ |
419 | */ |
| 452 | if (TotalPieces(side, bishop) > 1) { |
420 | if (TotalPieces(side, bishop) > 1) { |
| 453 | score_mg += bishop_pair[mg]; |
421 | score_mg += bishop_pair[mg]; |
| 454 | score_eg += bishop_pair[eg]; |
422 | score_eg += bishop_pair[eg]; |
| - | 423 | } |
|
| - | 424 | /* |
|
| - | 425 | ************************************************************ |
|
| - | 426 | * * |
|
| - | 427 | * Check for pawns on both wings, which makes a bishop * |
|
| - | 428 | * even more valuable against an enemy knight * |
|
| - | 429 | * * |
|
| - | 430 | ************************************************************ |
|
| - | 431 | */ |
|
| - | 432 | else { |
|
| - | 433 | if (tree->all_pawns & mask_fgh && tree->all_pawns & mask_abc) { |
|
| - | 434 | score_mg += bishop_wing_pawns[mg]; |
|
| - | 435 | score_eg += bishop_wing_pawns[eg]; |
|
| - | 436 | } |
|
| 455 | } |
437 | } |
| 456 | tree->score_mg += sign[side] * score_mg; |
438 | tree->score_mg += sign[side] * score_mg; |
| 457 | tree->score_eg += sign[side] * score_eg; |
439 | tree->score_eg += sign[side] * score_eg; |
| 458 | } |
440 | } |
| 459 | 441 | ||
| Line 1287... | Line 1269... | ||
| 1287 | * edge squares to encourage capture toward the center, * |
1269 | * edge squares to encourage capture toward the center, * |
| 1288 | * the rest are neutral. * |
1270 | * the rest are neutral. * |
| 1289 | * * |
1271 | * * |
| 1290 | ************************************************************ |
1272 | ************************************************************ |
| 1291 | */ |
1273 | */ |
| 1292 | score_mg += pval |
1274 | score_mg += pval[side][square]; |
| 1293 | score_eg += pval[eg][side][square]; |
- | |
| 1294 | /* |
1275 | /* |
| 1295 | ************************************************************ |
1276 | ************************************************************ |
| 1296 | * * |
1277 | * * |
| 1297 | * Evaluate isolated pawns, which are penalized based on * |
1278 | * Evaluate isolated pawns, which are penalized based on * |
| 1298 | * which file they occupy. * |
1279 | * which file they occupy. * |
| Line 1350... | Line 1331... | ||
| 1350 | * * |
1331 | * * |
| 1351 | ************************************************************ |
1332 | ************************************************************ |
| 1352 | */ |
1333 | */ |
| 1353 | connected = Pawns(side) & mask_pawn_connected[side][square]; |
1334 | connected = Pawns(side) & mask_pawn_connected[side][square]; |
| 1354 | if (connected) { |
1335 | if (connected) { |
| 1355 | score_mg += pawn_connected[rank][file]; |
1336 | score_mg += pawn_connected[mg][rank][file]; |
| 1356 | score_eg += pawn_connected[rank][file]; |
1337 | score_eg += pawn_connected[eg][rank][file]; |
| 1357 | } |
1338 | } |
| 1358 | /* |
1339 | /* |
| 1359 | ************************************************************ |
1340 | ************************************************************ |
| 1360 | * * |
1341 | * * |
| 1361 | * Flag passed pawns for use later when we finally call * |
1342 | * Flag passed pawns for use later when we finally call * |