initial rewrite of lazy_children logic
This commit is contained in:
parent
9976368730
commit
0adb59dc7f
@ -12,8 +12,8 @@ pub struct ComplexAgent {
|
||||
#[allow(dead_code)]
|
||||
impl ComplexAgent {
|
||||
pub const fn new(color: Piece) -> Self {
|
||||
const MAX_DEPTH: usize = 9;
|
||||
const NON_LAZY_DEPTH: usize = 9;
|
||||
const MAX_DEPTH: usize = 18;
|
||||
const NON_LAZY_DEPTH: usize = 5;
|
||||
Self {
|
||||
color,
|
||||
future_moves: FutureMoves::new(color, MAX_DEPTH, NON_LAZY_DEPTH),
|
||||
|
||||
@ -113,7 +113,7 @@ impl FutureMoves {
|
||||
|
||||
// use [`Board::all_positions`] here instead of [`Board::possible_moves`]
|
||||
// because we use [`Board::what_if`] later and we want to reduce calls to [`Board::propegate_from_dry`]
|
||||
let mut new: Vec<Move> = Board::all_positions()
|
||||
let new: Vec<Move> = Board::all_positions()
|
||||
.flat_map(|(i, j)| {
|
||||
parent
|
||||
.board
|
||||
@ -133,37 +133,24 @@ impl FutureMoves {
|
||||
})
|
||||
.collect();
|
||||
|
||||
// negative, because we want the max value to be at the first index
|
||||
// abs because we want the most EXTREME possible outcome (win or lose for example)
|
||||
new.sort_by_key(|x| -x.self_value.abs());
|
||||
|
||||
// keep the TOP_K children of their magnitude
|
||||
const TOP_K_CHILDREN: usize = 2;
|
||||
|
||||
// we want to keep only the greatest magnitude moves
|
||||
if lazy_children && new.len() > TOP_K_CHILDREN {
|
||||
new.drain(TOP_K_CHILDREN..);
|
||||
}
|
||||
const TOP_K_CHILDREN: usize = 10;
|
||||
|
||||
let start_idx = self.arena.len();
|
||||
if parent.lazy_children && !lazy_children {
|
||||
self.arena[parent_idx].lazy_children = false;
|
||||
// this move's children are being regenerated after lazy child expiration, don't append first node
|
||||
if new.len() > TOP_K_CHILDREN {
|
||||
self.arena.extend(new.drain(TOP_K_CHILDREN..));
|
||||
} else {
|
||||
// nothing will be appended
|
||||
// even though it was sorted the first time around
|
||||
// there's still no more than one element (which is already in the arena)
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
self.arena.extend(new);
|
||||
}
|
||||
|
||||
let new_indices = start_idx..self.arena.len();
|
||||
|
||||
self.arena[parent_idx].children.extend(new_indices.clone());
|
||||
let mut parent_copy = self.arena[parent_idx].clone();
|
||||
parent_copy.sort_children(self.arena.as_mut_slice());
|
||||
self.arena[parent_idx] = parent_copy;
|
||||
|
||||
if lazy_children && new_indices.clone().count() > TOP_K_CHILDREN {
|
||||
for i in new_indices.clone().skip(TOP_K_CHILDREN) {
|
||||
self.arena[i].lazy_children = true;
|
||||
}
|
||||
}
|
||||
|
||||
Some(new_indices)
|
||||
}
|
||||
|
||||
@ -86,4 +86,11 @@ impl Move {
|
||||
|
||||
BVM.board_value(&self.board, agent_color)
|
||||
}
|
||||
|
||||
/// Sort children of the [`Move`] by their self_value in `arena`
|
||||
pub fn sort_children(&mut self, arena: &[Move]) {
|
||||
// negative, because we want the max value to be at the first index
|
||||
// abs because we want the most EXTREME possible outcome (win or lose for example)
|
||||
self.children.sort_by_key(|&i| -arena[i].self_value.abs());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user