evaluate.rs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. use game::*;
  2. use bitboard::*;
  3. use movegen::*;
  4. use std::i32;
  5. ///
  6. /// type that is returned by the evaluate funcion
  7. ///
  8. pub type PosValue = i32;
  9. pub const MIN_VALUE: PosValue = i32::MIN + 1;
  10. pub const MAX_VALUE: PosValue = i32::MAX;
  11. const MATE_VALUE: PosValue = 1i32 << 26;
  12. const MATE_CUTOFF: PosValue = 1i32 << 24;
  13. pub fn mate() -> PosValue {
  14. mate_in_p1(1)
  15. }
  16. ///
  17. /// constructs a PosValue that indicates that from a given position mate
  18. /// can be achieved in turns-1 moves (not halfmoves)
  19. ///
  20. pub fn mate_in_p1(turns: i32) -> PosValue {
  21. if turns < 0 {
  22. return -mate_in_p1(-turns);
  23. }
  24. else {
  25. return MATE_VALUE - turns;
  26. }
  27. }
  28. ///
  29. /// returns Some(turns) if the pos_val indicates that mate
  30. /// can be reached in turns-1 moves
  31. ///
  32. /// turns is negative if the moving side is getting mated
  33. /// in turns moves
  34. ///
  35. pub fn is_mate_in_p1(pos_val: PosValue) -> Option<i32> {
  36. if pos_val > MATE_CUTOFF && pos_val <= MATE_VALUE {
  37. Some(-pos_val + MATE_VALUE)
  38. }
  39. else if pos_val < -MATE_CUTOFF {
  40. Some(-pos_val - MATE_VALUE)
  41. }
  42. else {
  43. None
  44. }
  45. }
  46. pub fn increase_mate_in(mut val: PosValue) -> PosValue {
  47. if let Some(turns) = is_mate_in_p1(val) {
  48. if turns < 0 {
  49. val = mate_in_p1(turns - 1);
  50. }
  51. if turns > 0 {
  52. val = mate_in_p1(turns + 1);
  53. }
  54. if turns == 0 {
  55. if val < 0 {
  56. val = mate_in_p1(-1);
  57. }
  58. else {
  59. val = mate_in_p1(1);
  60. }
  61. }
  62. }
  63. val
  64. }
  65. pub fn decrease_mate_in(mut val: PosValue) -> PosValue {
  66. if let Some(turns) = is_mate_in_p1(val) {
  67. if turns < 0 {
  68. val = mate_in_p1(turns + 1);
  69. }
  70. if turns > 0 {
  71. val = mate_in_p1(turns - 1);
  72. }
  73. }
  74. val
  75. }
  76. fn side_value(game: &Game, side: Side) -> u32 {
  77. let adv_pawn_mask = match side { WHITE => ROW_7, BLACK => ROW_2 };
  78. let semi_adv_pawn_mask = match side { WHITE => ROW_6, BLACK => ROW_3 };
  79. let advanced_pawns = (game.get_piece(PAWN, side) & adv_pawn_mask).count_ones() * 200;
  80. let semi_advanced_pawns = (game.get_piece(PAWN, side) & semi_adv_pawn_mask).count_ones() * 50;
  81. advanced_pawns + semi_advanced_pawns
  82. }
  83. fn pawn_structure(game: &Game, side: Side) -> PosValue {
  84. let pawns = game.pawns(side);
  85. let all_own_pieces = game.get_all_side(side);
  86. let all_pieces = all_own_pieces | game.get_all_side(!side);
  87. let pawn_protect = if side == WHITE {
  88. northeast_one(pawns) | northwest_one(pawns)
  89. }
  90. else {
  91. southeast_one(pawns) | southwest_one(pawns)
  92. };
  93. let pawn_pushes = if side == WHITE { north_one(pawns) } else { south_one(pawns) };
  94. let protections = (pawn_protect & all_own_pieces).count_ones();
  95. let pushable_pawns = (pawn_pushes & !all_pieces).count_ones();
  96. return (protections * 15 + pushable_pawns * 10) as _;
  97. }
  98. fn knight_value(game: &Game, side: Side) -> PosValue {
  99. let knights = game.get_piece(KNIGHT, side);
  100. let ks = BitboardIterator(knights);
  101. let mut k_attacks: u32 = 0;
  102. for k in ks {
  103. let targets = get_knight_targets(square(k));
  104. k_attacks += targets.count_ones() * 8;
  105. }
  106. let num_opp_pawns = game.get_piece(PAWN, !side).count_ones() as PosValue;
  107. let num_knights = game.get_piece(KNIGHT, side).count_ones() as PosValue;
  108. let center = (ROW_4 | ROW_5) & (FILE_D | FILE_E);
  109. let center_knights = (game.get_piece(KNIGHT, side) & center).count_ones() as PosValue;
  110. center_knights * 6 + k_attacks as PosValue + ((num_knights * 75 * num_opp_pawns) / 8) as PosValue
  111. }
  112. pub fn material_value(game: &Game, side: Side) -> PosValue {
  113. (game.get_piece(PAWN, side).count_ones() * 100
  114. + game.get_piece(KNIGHT, side).count_ones() * 220
  115. + game.get_piece(BISHOP, side).count_ones() * 320
  116. + game.get_piece(ROOK, side).count_ones() * 400
  117. + game.get_piece(QUEEN, side).count_ones() * 800) as PosValue
  118. }
  119. fn castling_rights(game: &Game, side: Side) -> PosValue {
  120. let castling_value = 15;
  121. (if side == WHITE {
  122. 0
  123. + if game.castling_rights[0] { 1 } else { 0 }
  124. + if game.castling_rights[1] { 1 } else { 0 }
  125. }
  126. else {
  127. 0
  128. + if game.castling_rights[2] { 1 } else { 0 }
  129. + if game.castling_rights[3] { 1 } else { 0 }
  130. }
  131. * castling_value) as PosValue
  132. }
  133. fn king_safety(game: &Game, side: Side) -> PosValue {
  134. let king = game.get_piece(KING, side);
  135. let area = north_one(king)
  136. | south_one(king)
  137. | northeast_one(king)
  138. | northwest_one(king)
  139. | southwest_one(king)
  140. | southeast_one(king);
  141. let guards = game.get_all_side(side) & area;
  142. let attackers = game.get_all_side(!side) & area;
  143. let backrow = if king & (ROW_1 | ROW_8) != 0 { 50 } else { 0 };
  144. let kneighbors = guards.count_ones() as PosValue * 8 - attackers.count_ones() as PosValue * 15;
  145. let enemies = game.get_all_side(!side);
  146. let row_opponents = enemies & (populate_rows(king));
  147. let dang_row_opponents = row_opponents & (ROW_1 | ROW_2 | ROW_7 | ROW_8);
  148. let danger_value = (dang_row_opponents.count_ones() * 50) as PosValue;
  149. return kneighbors + danger_value + backrow;
  150. }
  151. fn king_there(game: &Game, side: Side) -> PosValue {
  152. if game.get_piece(KING, side) != 0 { 10000 } else { 0 }
  153. }
  154. ///
  155. /// The evaluate function of the engine
  156. ///
  157. pub fn evaluate(game: &Game) -> PosValue {
  158. let sv = side_value(game, game.turn) as PosValue - side_value(game, !game.turn) as PosValue;
  159. let material_value_us = material_value(game, game.turn);
  160. let material_value_them = material_value(game, !game.turn);
  161. let mat_val = (material_value_us) - (material_value_them) as PosValue;
  162. let kv = knight_value(game, game.turn) - knight_value(game, !game.turn);
  163. let king_safety = king_safety(game, game.turn) - king_safety(game, !game.turn);
  164. let king_there = king_there(game, game.turn) - king_there(game, !game.turn);
  165. let castling_rights = castling_rights(game, game.turn) - castling_rights(game, !game.turn);
  166. let pawn_structure = pawn_structure(game, game.turn) - pawn_structure(game, !game.turn);
  167. let bishop_moves = (bishop_moves(game, game.turn) - bishop_moves(game, !game.turn)) * 3;
  168. let queen_moves = (queen_moves(game, game.turn) - queen_moves(game, !game.turn)) * 3;
  169. let mge = sv + kv + king_safety + king_there + castling_rights + pawn_structure + bishop_moves + queen_moves;
  170. let lge = late_game_eval(game);
  171. let lateness = game_lateness(game);
  172. let ge = (mge * (100 - lateness) + lge * lateness) / 100;
  173. return mat_val + ge;
  174. }
  175. pub fn game_lateness(game: &Game) -> i32 {
  176. let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
  177. let n_pieces = all_pieces.count_ones();
  178. let captured_pieces = 32 - n_pieces;
  179. if captured_pieces > 15 {
  180. let lateness = (captured_pieces - 15) * 100 / 10;
  181. return std::cmp::min(lateness as _, 100);
  182. }
  183. return 0;
  184. }
  185. ///
  186. /// counts the number of moves the rooks of side can do
  187. ///
  188. fn rook_moves(game: &Game, side: Side) -> PosValue {
  189. let rooks = game.rooks(side);
  190. let rook_moves = count_sliding_move_bitboard(
  191. game.get_all_side(side),
  192. game.get_all_side(!side),
  193. rooks,
  194. true, false,
  195. false);
  196. return rook_moves as _;
  197. }
  198. ///
  199. /// counts the number of moves the bishops of side can do
  200. ///
  201. fn bishop_moves(game: &Game, side: Side) -> PosValue {
  202. let bishops = game.bishops(side);
  203. let bishop_moves = count_sliding_move_bitboard(
  204. game.get_all_side(side),
  205. game.get_all_side(!side),
  206. bishops,
  207. false, true,
  208. false);
  209. return bishop_moves as _;
  210. }
  211. ///
  212. /// counts the number of moves the queens of side can do
  213. ///
  214. fn queen_moves(game: &Game, side: Side) -> PosValue {
  215. let queens = game.queens(side);
  216. let queen_moves = count_sliding_move_bitboard(
  217. game.get_all_side(side),
  218. game.get_all_side(!side),
  219. queens,
  220. true, true,
  221. false);
  222. return queen_moves as _;
  223. }
  224. pub fn late_game_eval(game: &Game) -> PosValue {
  225. let pp = pawn_push_value(game, game.turn) - pawn_push_value(game, !game.turn);
  226. let bp = blocked_pawns(game, game.turn) - blocked_pawns(game, !game.turn);
  227. let rook_moves = rook_moves(game, game.turn) - rook_moves(game, !game.turn);
  228. pp + bp + rook_moves * 3
  229. }
  230. pub fn pawn_push_value(game: &Game, side: Side) -> PosValue {
  231. let val = if side == WHITE {
  232. (game.get_piece(PAWN, side) & ROW_3).count_ones() * 10
  233. + (game.get_piece(PAWN, side) & ROW_4).count_ones() * 20
  234. + (game.get_piece(PAWN, side) & ROW_5).count_ones() * 30
  235. + (game.get_piece(PAWN, side) & ROW_6).count_ones() * 40
  236. + (game.get_piece(PAWN, side) & ROW_7).count_ones() * 50
  237. }
  238. else {
  239. (game.get_piece(PAWN, side) & ROW_6).count_ones() * 10
  240. + (game.get_piece(PAWN, side) & ROW_5).count_ones() * 20
  241. + (game.get_piece(PAWN, side) & ROW_4).count_ones() * 30
  242. + (game.get_piece(PAWN, side) & ROW_3).count_ones() * 40
  243. + (game.get_piece(PAWN, side) & ROW_2).count_ones() * 50
  244. };
  245. val as PosValue
  246. }
  247. pub fn blocked_pawns(game: &Game, side: Side) -> PosValue {
  248. let pawns = game.get_piece(PAWN, side);
  249. let pushed_pawns = if side == WHITE {
  250. north_one(pawns)
  251. } else {
  252. south_one(pawns)
  253. };
  254. let blocked_pawns = pushed_pawns & game.get_all_side(!side);
  255. blocked_pawns.count_ones() as _
  256. }
  257. #[cfg(test)]
  258. mod tests {
  259. use super::*;
  260. #[test]
  261. fn test_mate_values() {
  262. let mate_in_three = mate_in_p1(3);
  263. let mate_in_four = mate_in_p1(4);
  264. assert!(mate_in_three > mate_in_four);
  265. assert!(mate_in_three > mate_in_four);
  266. }
  267. }