From c031e044222f7e0462c92445b1a15bcee68c9221 Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 01:11:53 +0300 Subject: [PATCH 1/7] aspiration research based TM --- src/bm/bm_runner/ab_runner.rs | 18 +++++++++--------- src/bm/bm_runner/time.rs | 31 +++---------------------------- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/bm/bm_runner/ab_runner.rs b/src/bm/bm_runner/ab_runner.rs index e2a07262..5b923bab 100644 --- a/src/bm/bm_runner/ab_runner.rs +++ b/src/bm/bm_runner/ab_runner.rs @@ -361,6 +361,15 @@ impl AbRunner { } local_context.window.set(score); local_context.eval = score; + + shared_context.time_manager.deepen( + thread, + depth, + nodes, + local_context.eval, + best_move.unwrap_or(make_move.unwrap()), + search_start.elapsed(), + ); if (score > alpha && score < beta) || score.is_mate() { best_move = make_move; eval = Some(score); @@ -406,15 +415,6 @@ impl AbRunner { } } depth += 1; - - shared_context.time_manager.deepen( - thread, - depth, - nodes, - local_context.eval, - best_move.unwrap(), - search_start.elapsed(), - ); } if let Some(evaluation) = eval { debugger.complete(); diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index c6c70fc9..eec0d093 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -42,7 +42,6 @@ pub struct TimeManager { max_duration: AtomicU32, normal_duration: AtomicU32, target_duration: AtomicU32, - prev_move: Mutex>, board: Mutex, infinite: AtomicBool, @@ -62,7 +61,6 @@ impl TimeManager { max_duration: AtomicU32::new(0), normal_duration: AtomicU32::new(0), target_duration: AtomicU32::new(0), - prev_move: Mutex::new(None), board: Mutex::new(Board::default()), abort_now: AtomicBool::new(false), infinite: AtomicBool::new(true), @@ -74,15 +72,7 @@ impl TimeManager { } impl TimeManager { - pub fn deepen( - &self, - _: u8, - depth: u32, - _: u32, - eval: Evaluation, - current_move: ChessMove, - _: Duration, - ) { + pub fn deepen(&self, _: u8, depth: u32, _: u32, eval: Evaluation, _: ChessMove, _: Duration) { if depth <= 4 || self.no_manage.load(Ordering::SeqCst) { return; } @@ -91,25 +81,11 @@ impl TimeManager { let last_eval = self.last_eval.load(Ordering::SeqCst); let mut time = (self.normal_duration.load(Ordering::SeqCst) * 1000) as f32; - let mut move_changed = false; - let prev_move = &mut *self.prev_move.lock().unwrap(); - if let Some(prev_move) = prev_move { - if *prev_move != current_move { - move_changed = true; - } - } - *prev_move = Some(current_move); - - let move_factor = if move_changed { 3.0 } else { -1.0 }; - let mut eval_diff = (current_eval as f32 - last_eval as f32) / 25.0; - if eval_diff < 0.0 { - eval_diff *= 2.0; - }; - eval_diff = eval_diff.max(-4.0).min(2.0).abs(); + eval_diff = eval_diff.abs().min(1.0); - time *= 1.05_f32.powf(eval_diff + move_factor); + time *= 1.05_f32.powf(eval_diff); let time = time.min(self.max_duration.load(Ordering::SeqCst) as f32 * 1000.0); self.normal_duration @@ -217,7 +193,6 @@ impl TimeManager { } pub fn clear(&self) { - *self.prev_move.lock().unwrap() = None; self.abort_now.store(false, Ordering::SeqCst); self.no_manage.store(false, Ordering::SeqCst); let expected_moves = self.expected_moves.load(Ordering::SeqCst); From 0f324306054e173f429bcb919ec4e1794544468c Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 07:36:55 +0300 Subject: [PATCH 2/7] increase expected moves --- src/bm/bm_runner/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index eec0d093..f52c7f63 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, AtomicI16, AtomicU32, Ordering}; use std::sync::Mutex; use std::time::{Duration, Instant}; -const EXPECTED_MOVES: u32 = 40; +const EXPECTED_MOVES: u32 = 50; const TIME_DEFAULT: Duration = Duration::from_secs(0); const INC_DEFAULT: Duration = Duration::from_secs(0); From d516f6dfc56f087de048f91b748f6ca4041e68ba Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 10:13:07 +0300 Subject: [PATCH 3/7] Revert back to 40 expected moves --- src/bm/bm_runner/time.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index f52c7f63..958c2bca 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, AtomicI16, AtomicU32, Ordering}; use std::sync::Mutex; use std::time::{Duration, Instant}; -const EXPECTED_MOVES: u32 = 50; +const EXPECTED_MOVES: u32 = 40; const TIME_DEFAULT: Duration = Duration::from_secs(0); const INC_DEFAULT: Duration = Duration::from_secs(0); @@ -85,8 +85,6 @@ impl TimeManager { eval_diff = eval_diff.abs().min(1.0); - time *= 1.05_f32.powf(eval_diff); - let time = time.min(self.max_duration.load(Ordering::SeqCst) as f32 * 1000.0); self.normal_duration .store((time * 0.001) as u32, Ordering::SeqCst); @@ -195,7 +193,10 @@ impl TimeManager { pub fn clear(&self) { self.abort_now.store(false, Ordering::SeqCst); self.no_manage.store(false, Ordering::SeqCst); - let expected_moves = self.expected_moves.load(Ordering::SeqCst); + let expected_moves = self + .expected_moves + .load(Ordering::SeqCst) + .min(EXPECTED_MOVES); self.expected_moves .store(expected_moves.saturating_sub(1), Ordering::SeqCst); } From ff526a65a76604ba86685728d8b5ad229d3efee0 Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 15:04:46 +0300 Subject: [PATCH 4/7] Punish high eval fluctuations more --- src/bm/bm_runner/time.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index 958c2bca..f0fd26c6 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -83,7 +83,8 @@ impl TimeManager { let mut eval_diff = (current_eval as f32 - last_eval as f32) / 25.0; - eval_diff = eval_diff.abs().min(1.0); + eval_diff = eval_diff.abs().min(3.0); + time *= 1.05_f32.powf(eval_diff); let time = time.min(self.max_duration.load(Ordering::SeqCst) as f32 * 1000.0); self.normal_duration From c55919528751d37d25c4511eff28e10f602f6299 Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 18:32:55 +0300 Subject: [PATCH 5/7] Think less when certain about move --- src/bm/bm_runner/time.rs | 52 +++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index f0fd26c6..d31e7ca0 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -6,6 +6,7 @@ use std::sync::Mutex; use std::time::{Duration, Instant}; const EXPECTED_MOVES: u32 = 40; +const MOVE_CHANGE_MARGIN: u32 = 9; const TIME_DEFAULT: Duration = Duration::from_secs(0); const INC_DEFAULT: Duration = Duration::from_secs(0); @@ -42,6 +43,9 @@ pub struct TimeManager { max_duration: AtomicU32, normal_duration: AtomicU32, target_duration: AtomicU32, + + same_move_depth: AtomicU32, + prev_move: Mutex>, board: Mutex, infinite: AtomicBool, @@ -61,6 +65,8 @@ impl TimeManager { max_duration: AtomicU32::new(0), normal_duration: AtomicU32::new(0), target_duration: AtomicU32::new(0), + same_move_depth: AtomicU32::new(0), + prev_move: Mutex::new(None), board: Mutex::new(Board::default()), abort_now: AtomicBool::new(false), infinite: AtomicBool::new(true), @@ -72,7 +78,15 @@ impl TimeManager { } impl TimeManager { - pub fn deepen(&self, _: u8, depth: u32, _: u32, eval: Evaluation, _: ChessMove, _: Duration) { + pub fn deepen( + &self, + _: u8, + depth: u32, + _: u32, + eval: Evaluation, + current_move: ChessMove, + _: Duration, + ) { if depth <= 4 || self.no_manage.load(Ordering::SeqCst) { return; } @@ -81,16 +95,37 @@ impl TimeManager { let last_eval = self.last_eval.load(Ordering::SeqCst); let mut time = (self.normal_duration.load(Ordering::SeqCst) * 1000) as f32; - let mut eval_diff = (current_eval as f32 - last_eval as f32) / 25.0; + let mut move_changed = false; + let prev_move = &mut *self.prev_move.lock().unwrap(); + if let Some(prev_move) = prev_move { + if *prev_move != current_move { + move_changed = true; + } + } + *prev_move = Some(current_move); + + let move_change_depth = if move_changed { + self.same_move_depth.fetch_add(1, Ordering::SeqCst) + } else { + self.same_move_depth.store(0, Ordering::SeqCst); + 0 + }; - eval_diff = eval_diff.abs().min(3.0); - time *= 1.05_f32.powf(eval_diff); + let eval_diff = (current_eval as f32 - last_eval as f32).abs() / 25.0; + + time *= 1.05_f32.powf(eval_diff.min(1.0)); + + let move_change_factor = if move_change_depth > MOVE_CHANGE_MARGIN { + 0.8 + } else { + 1.0 + }; let time = time.min(self.max_duration.load(Ordering::SeqCst) as f32 * 1000.0); self.normal_duration .store((time * 0.001) as u32, Ordering::SeqCst); self.target_duration - .store((time * 0.001) as u32, Ordering::SeqCst); + .store((time * 0.001 * move_change_factor) as u32, Ordering::SeqCst); self.last_eval.store(current_eval, Ordering::SeqCst); } @@ -192,12 +227,11 @@ impl TimeManager { } pub fn clear(&self) { + *self.prev_move.lock().unwrap() = None; + self.same_move_depth.store(0, Ordering::SeqCst); self.abort_now.store(false, Ordering::SeqCst); self.no_manage.store(false, Ordering::SeqCst); - let expected_moves = self - .expected_moves - .load(Ordering::SeqCst) - .min(EXPECTED_MOVES); + let expected_moves = self.expected_moves.load(Ordering::SeqCst); self.expected_moves .store(expected_moves.saturating_sub(1), Ordering::SeqCst); } From 09afc557cb08e21cfe7726d6eefe0b9944f78edc Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 20:45:41 +0300 Subject: [PATCH 6/7] Fix bug --- src/bm/bm_runner/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index d31e7ca0..1542113c 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -105,10 +105,10 @@ impl TimeManager { *prev_move = Some(current_move); let move_change_depth = if move_changed { - self.same_move_depth.fetch_add(1, Ordering::SeqCst) - } else { self.same_move_depth.store(0, Ordering::SeqCst); 0 + } else { + self.same_move_depth.fetch_add(1, Ordering::SeqCst) }; let eval_diff = (current_eval as f32 - last_eval as f32).abs() / 25.0; From d1fe239d667e7860c6c22632a387af631c95d25f Mon Sep 17 00:00:00 2001 From: doruk Date: Tue, 7 Dec 2021 21:42:47 +0300 Subject: [PATCH 7/7] Play faster if certain --- src/bm/bm_runner/time.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/bm/bm_runner/time.rs b/src/bm/bm_runner/time.rs index 1542113c..94451ba9 100644 --- a/src/bm/bm_runner/time.rs +++ b/src/bm/bm_runner/time.rs @@ -115,11 +115,7 @@ impl TimeManager { time *= 1.05_f32.powf(eval_diff.min(1.0)); - let move_change_factor = if move_change_depth > MOVE_CHANGE_MARGIN { - 0.8 - } else { - 1.0 - }; + let move_change_factor = 1.05_f32.powf(MOVE_CHANGE_MARGIN as f32 - move_change_depth as f32).max(0.4); let time = time.min(self.max_duration.load(Ordering::SeqCst) as f32 * 1000.0); self.normal_duration