fix
This commit is contained in:
parent
59dd47c67d
commit
b8a6349525
@ -5,5 +5,5 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num = "0.4"
|
||||
rand = "0.9.0"
|
||||
rayon = "1.10.0"
|
||||
rand = "0.9"
|
||||
rayon = "1.10"
|
||||
|
||||
@ -55,20 +55,22 @@ impl FutureMoves {
|
||||
let mut layers: Vec<Vec<Move>> = Vec::with_capacity(depth);
|
||||
layers.push(prob_space_idx(board, color, None).collect());
|
||||
|
||||
layers.extend(
|
||||
(0..depth)
|
||||
.flat_map(|i| layers.get(i))
|
||||
.map(|layers_at_depth| {
|
||||
layers_at_depth
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(parent_idx, parent_move)| {
|
||||
prob_space_idx(&parent_move.board, !parent_move.color, Some(parent_idx))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
for current_depth in 0..depth {
|
||||
let current_layer = &layers[current_depth];
|
||||
if current_layer.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
layers.push(
|
||||
current_layer
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(parent_idx, parent_move)| {
|
||||
prob_space_idx(&parent_move.board, !parent_move.color, Some(parent_idx))
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut tmp = Self {
|
||||
inner: layers,
|
||||
@ -79,29 +81,22 @@ impl FutureMoves {
|
||||
}
|
||||
|
||||
fn compute_values(&mut self) {
|
||||
let layers_len = self.inner.len();
|
||||
for depth in (0..layers_len).rev() {
|
||||
let layer_depth_1: Vec<Move> = if depth + 1 < layers_len {
|
||||
self.inner[depth + 1].clone()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
// could be overhauled via this: https://github.com/rust-lang/rust/issues/75027
|
||||
for depth in (0..self.inner.len()).rev() {
|
||||
let (curr, prev) = self.inner.split_at_mut(depth + 1);
|
||||
let curr = curr.last_mut().unwrap();
|
||||
let prev = prev.first_mut().map(|x| x.as_slice()).unwrap_or(&[]);
|
||||
|
||||
let current_layer = &mut self.inner[depth];
|
||||
|
||||
current_layer.iter_mut().for_each(|mv| {
|
||||
curr.iter_mut().for_each(|mv| {
|
||||
let self_value = mv.compute_self_value(self.color, depth + 1);
|
||||
let children_value = if depth + 1 < layers_len {
|
||||
// calculate average value of each move
|
||||
layer_depth_1
|
||||
.iter()
|
||||
.filter(|child| child.parent_index == Some(mv.parent_index.unwrap_or(0)))
|
||||
.map(|child| child.value)
|
||||
.sum::<i64>()
|
||||
/ layer_depth_1.len() as i64
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// calculate average value of each move
|
||||
let children_value = prev
|
||||
.iter()
|
||||
.filter(|child| child.parent_index == Some(mv.parent_index.unwrap_or(0)))
|
||||
.map(|child| child.value)
|
||||
.sum::<i64>()
|
||||
.checked_div(prev.len() as i64)
|
||||
.unwrap_or(0);
|
||||
mv.value = self_value + children_value;
|
||||
});
|
||||
}
|
||||
@ -114,26 +109,19 @@ impl FutureMoves {
|
||||
|
||||
pub struct ComplexAgent {
|
||||
color: Piece,
|
||||
future_moves: Option<FutureMoves>,
|
||||
}
|
||||
|
||||
impl ComplexAgent {
|
||||
#[allow(dead_code)]
|
||||
pub const fn new(color: Piece) -> Self {
|
||||
Self {
|
||||
color,
|
||||
future_moves: None,
|
||||
}
|
||||
Self { color }
|
||||
}
|
||||
}
|
||||
|
||||
impl Agent for ComplexAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||
const LOOPS: usize = 5;
|
||||
let layers = self
|
||||
.future_moves
|
||||
.take()
|
||||
.unwrap_or_else(|| FutureMoves::generate(self.color, board, LOOPS));
|
||||
let layers = FutureMoves::generate(self.color, board, LOOPS);
|
||||
|
||||
println!(
|
||||
"# of moves {} deep: {}",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user