Skip to content

Commit

Permalink
Merge pull request #18 from dsekercioglu/research_tm
Browse files Browse the repository at this point in the history
Research tm
  • Loading branch information
jnlt3 authored Dec 7, 2021
2 parents 46707da + d1fe239 commit 8d8a315
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/bm/bm_runner/ab_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 15 additions & 8 deletions src/bm/bm_runner/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -42,6 +43,8 @@ pub struct TimeManager {
max_duration: AtomicU32,
normal_duration: AtomicU32,
target_duration: AtomicU32,

same_move_depth: AtomicU32,
prev_move: Mutex<Option<ChessMove>>,
board: Mutex<Board>,

Expand All @@ -62,6 +65,7 @@ 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),
Expand Down Expand Up @@ -100,22 +104,24 @@ impl TimeManager {
}
*prev_move = Some(current_move);

let move_factor = if move_changed { 3.0 } else { -1.0 };
let move_change_depth = if move_changed {
self.same_move_depth.store(0, Ordering::SeqCst);
0
} else {
self.same_move_depth.fetch_add(1, Ordering::SeqCst)
};

let mut eval_diff = (current_eval as f32 - last_eval as f32) / 25.0;
let eval_diff = (current_eval as f32 - last_eval as f32).abs() / 25.0;

if eval_diff < 0.0 {
eval_diff *= 2.0;
};
eval_diff = eval_diff.max(-4.0).min(2.0).abs();
time *= 1.05_f32.powf(eval_diff.min(1.0));

time *= 1.05_f32.powf(eval_diff + move_factor);
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
.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);
}

Expand Down Expand Up @@ -218,6 +224,7 @@ 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);
Expand Down

0 comments on commit 8d8a315

Please sign in to comment.