evaluate.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 knight_value(game: &Game, side: Side) -> PosValue {
  84. let knights = game.get_piece(KNIGHT, side);
  85. let ks = BitboardIterator(knights);
  86. let mut k_attacks: u32 = 0;
  87. for k in ks {
  88. let targets = get_knight_targets(square(k));
  89. k_attacks += targets.count_ones() * 8;
  90. }
  91. let num_opp_pawns = game.get_piece(PAWN, !side).count_ones() as PosValue;
  92. let num_knights = game.get_piece(KNIGHT, side).count_ones() as PosValue;
  93. let center = (ROW_4 | ROW_5) & (FILE_D | FILE_E);
  94. let center_knights = (game.get_piece(KNIGHT, side) & center).count_ones() as PosValue;
  95. center_knights * 6 + k_attacks as PosValue + ((num_knights * 75 * num_opp_pawns) / 8) as PosValue
  96. }
  97. fn material_value(game: &Game, side: Side) -> PosValue {
  98. (game.get_piece(PAWN, side).count_ones() * 100
  99. + game.get_piece(KNIGHT, side).count_ones() * 220
  100. + game.get_piece(BISHOP, side).count_ones() * 320
  101. + game.get_piece(ROOK, side).count_ones() * 400
  102. + game.get_piece(QUEEN, side).count_ones() * 700) as PosValue
  103. }
  104. fn castling_rights(game: &Game, side: Side) -> PosValue {
  105. let castling_value = 15;
  106. (if side == WHITE {
  107. 0
  108. + if game.castling_rights[0] { 1 } else { 0 }
  109. + if game.castling_rights[1] { 1 } else { 0 }
  110. }
  111. else {
  112. 0
  113. + if game.castling_rights[2] { 1 } else { 0 }
  114. + if game.castling_rights[3] { 1 } else { 0 }
  115. }
  116. * castling_value) as PosValue
  117. }
  118. fn king_safety(game: &Game, side: Side) -> PosValue {
  119. let king = game.get_piece(KING, side);
  120. let area = north_one(king)
  121. | south_one(king)
  122. | east_one(king)
  123. | west_one(king)
  124. | northeast_one(king)
  125. | northwest_one(king)
  126. | southwest_one(king)
  127. | southeast_one(king);
  128. let guards = game.get_all_side(side) & area;
  129. let attackers = game.get_all_side(!side) & area;
  130. let kneighbors = guards.count_ones() as PosValue * 5 - attackers.count_ones() as PosValue * 15;
  131. let enemies = game.get_all_side(!side);
  132. let kingrow = indices_from_square(square(king)).1;
  133. let row_opponents = enemies & (ROW_1 << (kingrow * 8));
  134. let dang_row_opponents = row_opponents & (ROW_1 | ROW_2 | ROW_7 | ROW_8);
  135. let danger_value = (dang_row_opponents.count_ones() * 50) as PosValue;
  136. return kneighbors + danger_value;
  137. }
  138. fn king_there(game: &Game, side: Side) -> PosValue {
  139. if game.get_piece(KING, side) != 0 { 10000 } else { 0 }
  140. }
  141. ///
  142. /// The evaluate function of the engine
  143. ///
  144. pub fn evaluate(game: &Game) -> PosValue {
  145. let sv = side_value(game, game.turn) as PosValue - side_value(game, !game.turn) as PosValue;
  146. let material_value_us = material_value(game, game.turn);
  147. let material_value_them = material_value(game, !game.turn);
  148. let mat_val = (material_value_us) - (material_value_them) as PosValue;
  149. let kv = knight_value(game, game.turn) - knight_value(game, !game.turn);
  150. let king_safety = king_safety(game, game.turn) - king_safety(game, !game.turn);
  151. let king_there = king_there(game, game.turn) - king_there(game, !game.turn);
  152. let castling_rights = castling_rights(game, game.turn) - castling_rights(game, !game.turn);
  153. let mge = sv + kv + king_safety + king_there + castling_rights;
  154. let lge = late_game_eval(game);
  155. let lateness = game_lateness(game);
  156. let ge = (mge * (100 - lateness) + lge * lateness) / 100;
  157. return mat_val + ge;
  158. }
  159. pub fn game_lateness(game: &Game) -> i32 {
  160. let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
  161. let n_pieces = all_pieces.count_ones();
  162. let captured_pieces = 32 - n_pieces;
  163. if captured_pieces > 15 {
  164. let lateness = (captured_pieces - 15) * 100 / 10;
  165. return std::cmp::min(lateness as _, 100);
  166. }
  167. return 0;
  168. }
  169. pub fn late_game_eval(game: &Game) -> PosValue {
  170. let pp = pawn_push_value(game, game.turn) - pawn_push_value(game, !game.turn);
  171. let bp = blocked_pawns(game, game.turn) - blocked_pawns(game, !game.turn);
  172. pp + bp
  173. }
  174. pub fn pawn_push_value(game: &Game, side: Side) -> PosValue {
  175. let val = if side == WHITE {
  176. (game.get_piece(PAWN, side) & ROW_3).count_ones() * 10
  177. + (game.get_piece(PAWN, side) & ROW_4).count_ones() * 20
  178. + (game.get_piece(PAWN, side) & ROW_5).count_ones() * 30
  179. + (game.get_piece(PAWN, side) & ROW_6).count_ones() * 40
  180. + (game.get_piece(PAWN, side) & ROW_7).count_ones() * 50
  181. }
  182. else {
  183. (game.get_piece(PAWN, side) & ROW_6).count_ones() * 10
  184. + (game.get_piece(PAWN, side) & ROW_5).count_ones() * 20
  185. + (game.get_piece(PAWN, side) & ROW_4).count_ones() * 30
  186. + (game.get_piece(PAWN, side) & ROW_3).count_ones() * 40
  187. + (game.get_piece(PAWN, side) & ROW_2).count_ones() * 50
  188. };
  189. val as PosValue
  190. }
  191. pub fn blocked_pawns(game: &Game, side: Side) -> PosValue {
  192. let pawns = game.get_piece(PAWN, side);
  193. let pushed_pawns = if side == WHITE {
  194. north_one(pawns)
  195. } else {
  196. south_one(pawns)
  197. };
  198. let blocked_pawns = pushed_pawns & game.get_all_side(!side);
  199. blocked_pawns.count_ones() as _
  200. }
  201. #[cfg(test)]
  202. mod tests {
  203. use super::*;
  204. #[test]
  205. fn test_mate_values() {
  206. let mate_in_three = mate_in_p1(3);
  207. let mate_in_four = mate_in_p1(4);
  208. assert!(mate_in_three > mate_in_four);
  209. assert!(mate_in_three > mate_in_four);
  210. }
  211. }