dpv007 commited on
Commit
4b99b2d
·
verified ·
1 Parent(s): ba7b4c0

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. neural_engine/src/main.rs +68 -5
neural_engine/src/main.rs CHANGED
@@ -235,6 +235,7 @@ fn alpha_beta(
235
  root_cache: &Option<RootKVCache>,
236
  root_history_len: usize,
237
  stop_flag: &AtomicBool,
 
238
  ) -> f32 {
239
  if stop_flag.load(Ordering::Relaxed) { return 0.0; }
240
 
@@ -255,6 +256,24 @@ fn alpha_beta(
255
  return 0.0;
256
  }
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  let hash = board.zobrist_hash::<Zobrist64>(shakmaty::EnPassantMode::Legal).0;
259
 
260
  let mut tt_move = None;
@@ -296,13 +315,29 @@ fn alpha_beta(
296
  let mut best_move = None;
297
  let old_alpha = alpha;
298
 
 
 
299
  for m in &legals {
 
300
  let uci = m.to_uci(CastlingMode::Standard);
301
  let mut next_board = board.clone();
302
  next_board.play_unchecked(m);
303
  history.push(uci.to_string());
304
 
305
- let score = -alpha_beta(&next_board, depth - 1, -beta, -alpha, history, session, vocab, tt, tablebases, root_cache, root_history_len, stop_flag);
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  history.pop();
307
 
308
  if score > best_val {
@@ -449,13 +484,19 @@ fn main() {
449
  } else { None };
450
 
451
  let max_depth = 100;
 
452
  for depth in 1..=max_depth {
453
  if t_stop_flag.load(Ordering::Relaxed) { break; }
454
 
455
- let alpha = -2.0;
456
- let beta = 2.0;
457
 
458
- alpha_beta(
 
 
 
 
 
459
  &t_board,
460
  depth,
461
  alpha,
@@ -467,8 +508,30 @@ fn main() {
467
  &t_tb,
468
  &t_root_cache,
469
  root_history_len,
470
- &t_stop_flag
 
471
  );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
  }
473
  });
474
  handles.push(handle);
 
235
  root_cache: &Option<RootKVCache>,
236
  root_history_len: usize,
237
  stop_flag: &AtomicBool,
238
+ is_null: bool,
239
  ) -> f32 {
240
  if stop_flag.load(Ordering::Relaxed) { return 0.0; }
241
 
 
256
  return 0.0;
257
  }
258
 
259
+ if !is_null && depth >= 3 && !board.is_check() && board.board().occupied().count() > 6 {
260
+ use shakmaty::FromSetup;
261
+ let mut setup = board.clone().into_setup(shakmaty::EnPassantMode::Legal);
262
+ setup.turn = match setup.turn {
263
+ shakmaty::Color::White => shakmaty::Color::Black,
264
+ shakmaty::Color::Black => shakmaty::Color::White,
265
+ };
266
+ setup.ep_square = None;
267
+ setup.halfmoves += 1;
268
+ if let Ok(null_board) = Chess::from_setup(setup, shakmaty::CastlingMode::Standard) {
269
+ let r_depth = if depth > 3 { depth - 3 } else { 0 };
270
+ let null_score = -alpha_beta(&null_board, r_depth, -beta, -beta + 0.01, history, session, vocab, tt, tablebases, root_cache, root_history_len, stop_flag, true);
271
+ if null_score >= beta {
272
+ return beta;
273
+ }
274
+ }
275
+ }
276
+
277
  let hash = board.zobrist_hash::<Zobrist64>(shakmaty::EnPassantMode::Legal).0;
278
 
279
  let mut tt_move = None;
 
315
  let mut best_move = None;
316
  let old_alpha = alpha;
317
 
318
+ let mut move_count = 0;
319
+
320
  for m in &legals {
321
+ move_count += 1;
322
  let uci = m.to_uci(CastlingMode::Standard);
323
  let mut next_board = board.clone();
324
  next_board.play_unchecked(m);
325
  history.push(uci.to_string());
326
 
327
+ let is_tactical = m.is_capture() || m.is_promotion() || next_board.is_check();
328
+ let is_tt = Some(uci.clone()) == tt_move;
329
+
330
+ let mut score;
331
+ if depth >= 3 && move_count >= 4 && !is_tactical && !is_tt {
332
+ let r_depth = depth - 2;
333
+ score = -alpha_beta(&next_board, r_depth, -beta, -alpha, history, session, vocab, tt, tablebases, root_cache, root_history_len, stop_flag, false);
334
+ if score > alpha {
335
+ score = -alpha_beta(&next_board, depth - 1, -beta, -alpha, history, session, vocab, tt, tablebases, root_cache, root_history_len, stop_flag, false);
336
+ }
337
+ } else {
338
+ score = -alpha_beta(&next_board, depth - 1, -beta, -alpha, history, session, vocab, tt, tablebases, root_cache, root_history_len, stop_flag, false);
339
+ }
340
+
341
  history.pop();
342
 
343
  if score > best_val {
 
484
  } else { None };
485
 
486
  let max_depth = 100;
487
+ let mut prev_score = 0.0;
488
  for depth in 1..=max_depth {
489
  if t_stop_flag.load(Ordering::Relaxed) { break; }
490
 
491
+ let mut alpha = -2.0;
492
+ let mut beta = 2.0;
493
 
494
+ if depth >= 3 {
495
+ alpha = prev_score - 0.2;
496
+ beta = prev_score + 0.2;
497
+ }
498
+
499
+ let mut score = alpha_beta(
500
  &t_board,
501
  depth,
502
  alpha,
 
508
  &t_tb,
509
  &t_root_cache,
510
  root_history_len,
511
+ &t_stop_flag,
512
+ false
513
  );
514
+
515
+ if score <= alpha || score >= beta {
516
+ alpha = -2.0;
517
+ beta = 2.0;
518
+ score = alpha_beta(
519
+ &t_board,
520
+ depth,
521
+ alpha,
522
+ beta,
523
+ &mut t_history,
524
+ &mut t_session,
525
+ &t_vocab,
526
+ &t_tt,
527
+ &t_tb,
528
+ &t_root_cache,
529
+ root_history_len,
530
+ &t_stop_flag,
531
+ false
532
+ );
533
+ }
534
+ prev_score = score;
535
  }
536
  });
537
  handles.push(handle);