Bladeren bron

some eval tweaks

Nicolas Winkler 2 jaren geleden
bovenliggende
commit
b1694ac9ef
6 gewijzigde bestanden met toevoegingen van 51 en 9 verwijderingen
  1. 18 4
      src/evaluate.rs
  2. 1 0
      src/game.rs
  3. 8 2
      src/hash.rs
  4. 5 1
      src/interface.rs
  5. 15 1
      src/movegen.rs
  6. 4 1
      src/search.rs

+ 18 - 4
src/evaluate.rs

@@ -160,8 +160,6 @@ fn king_safety(game: &Game, side: Side) -> PosValue {
     let king = game.get_piece(KING, side);
     let area = north_one(king)
              | south_one(king)
-             | east_one(king)
-             | west_one(king)
              | northeast_one(king)
              | northwest_one(king)
              | southwest_one(king)
@@ -170,6 +168,8 @@ fn king_safety(game: &Game, side: Side) -> PosValue {
     let guards = game.get_all_side(side) & area;
     let attackers = game.get_all_side(!side) & area;
 
+    let backrow =  if king & (ROW_1 | ROW_8) != 0 { 50 } else { 0 };
+
     let kneighbors = guards.count_ones() as PosValue * 5 - attackers.count_ones() as PosValue * 15;
 
     let enemies = game.get_all_side(!side);
@@ -179,7 +179,7 @@ fn king_safety(game: &Game, side: Side) -> PosValue {
     let dang_row_opponents = row_opponents & (ROW_1 | ROW_2 | ROW_7 | ROW_8);
     let danger_value = (dang_row_opponents.count_ones() * 50) as PosValue;
 
-    return kneighbors + danger_value;
+    return kneighbors + danger_value + backrow;
 }
 
 fn king_there(game: &Game, side: Side) -> PosValue {
@@ -226,11 +226,25 @@ pub fn game_lateness(game: &Game) -> i32 {
     return 0;
 }
 
+fn rook_moves(game: &Game, side: Side) -> PosValue {
+    let rooks = game.rooks(side);
+    let rook_moves = count_sliding_move_bitboard(
+        game,
+        game.get_all_side(side),
+        game.get_all_side(!side),
+        rooks,
+        true, false,
+        false);
+    
+    return rook_moves as _;
+}
+
 pub fn late_game_eval(game: &Game) -> PosValue {
     let pp = pawn_push_value(game, game.turn) - pawn_push_value(game, !game.turn);
     let bp = blocked_pawns(game, game.turn) - blocked_pawns(game, !game.turn);
+    let rook_moves = rook_moves(game, game.turn) - rook_moves(game, !game.turn);
 
-    pp + bp
+    pp + bp + rook_moves * 3
 }
 
 pub fn pawn_push_value(game: &Game, side: Side) -> PosValue {

+ 1 - 0
src/game.rs

@@ -701,6 +701,7 @@ impl Game {
                     self.en_passant = None;
                 }
                 else {
+                    info!("internal en passant error");
                     panic!("internal en passant error");
                 }
             },

+ 8 - 2
src/hash.rs

@@ -4,7 +4,7 @@ use std::collections::{HashMap};
 use log::info;
 use zobrist;
 
-use crate::movegen::Move;
+use crate::{movegen::Move, game};
 
 #[derive(Clone)]
 pub enum EntryType {
@@ -69,10 +69,16 @@ impl Cache {
     }
 
     pub fn lookup(&self, game_pos: &Game) -> Option<CacheEntry> {
+        if game_pos.zobrist.is_none() {
+            info!("invalid zobrist");
+        }
         self.hashmap.get(&game_pos.zobrist.as_ref().unwrap().1).map(|x| x.clone())
     }
     
     pub fn cache(&mut self, game_pos: &Game, ce: CacheEntry) {
+        if game_pos.zobrist.is_none() {
+            info!("invalid zobrist");
+        }
         if let Some(c) = self.lookup(game_pos) {
             if c.depth > ce.depth {
                 return;
@@ -99,7 +105,7 @@ impl Cache {
 impl RepetitionTable {
     pub fn new() -> Self {
         RepetitionTable {
-            hashmap: HashMap::with_capacity(256)
+            hashmap: HashMap::with_capacity(1024)
         }
     }
 

+ 5 - 1
src/interface.rs

@@ -62,7 +62,11 @@ fn cmd_position(mut args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<Eng
     }
     else if position == "fen" {
         let fen_parts: Vec<&str> = Vec::from(&args[0..6]).into_iter().collect::<Vec<&str>>();
-        game = Game::from_fen(fen_parts.as_slice()).unwrap();
+        game = Game::from_fen(fen_parts.as_slice()).unwrap_or_else(|| {
+            info!("invalid fen");
+            println!("invalid fen");
+            Game::default()
+        });
         args.drain(0..6);
     }
     let moves = 

+ 15 - 1
src/movegen.rs

@@ -204,7 +204,7 @@ pub fn generate_legal_moves(game: &mut Game, side: Side) -> Vec<Move> {
 }
 
 
-pub fn generate_legal_sorted_moves(game: &mut Game, hash: &mut Cache, killers: &[Move], ce: Option<CacheEntry>, side: Side) -> Vec<Move> {
+pub fn generate_legal_sorted_moves(game: &mut Game, _hash: &mut Cache, killers: &[Move], ce: Option<CacheEntry>, side: Side) -> Vec<Move> {
     let moves = generate_moves(game, side);
 
     let illegal_value = crate::evaluate::MAX_VALUE;
@@ -520,6 +520,20 @@ fn generate_queen_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, capt
     generate_sliding_moves(game, friends, others, game.queens(side), QUEEN, true, true, move_list, captures_only);
 }
 
+
+pub fn count_sliding_move_bitboard(game: &Game, friends: Bitboard, others: Bitboard, pieces: Bitboard, straight: bool, diagonal: bool, captures_only: bool) -> u32 {
+    let pieces_iter = BitboardIterator(pieces);
+
+    let mask = if captures_only { others } else { !(0 as Bitboard) };
+    let mut sum = 0;
+    for piece in pieces_iter {
+        let destinations = generate_sliding_destinations(friends, others, piece, straight, diagonal);
+        
+        sum += (destinations & mask).count_ones();
+    }
+    return sum;
+}
+
 fn generate_sliding_moves(game: &Game, friends: Bitboard, others: Bitboard, pieces: Bitboard, piece_type: PieceType,
                           straight: bool, diagonal: bool, move_list: &mut Vec<Move>, captures_only: bool) {
     let pieces_iter = BitboardIterator(pieces);

+ 4 - 1
src/search.rs

@@ -6,6 +6,10 @@ use rand::prelude::*;
 use hash::*;
 
 
+
+///
+/// struct to contain data for a search
+/// 
 pub struct SearchControl<'a> {
     /// node counter
     pub nodes: usize,
@@ -28,7 +32,6 @@ pub enum SearchResult {
     Invalid
 }
 
-
 impl<'a> SearchControl<'a> {
     pub fn new(check: &'a mut dyn FnMut() -> bool, move_history: &'a mut RepetitionTable, depth: i32) -> Self {
         SearchControl {