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