little reorg
This commit is contained in:
parent
f1d81ef05e
commit
1316bd2861
@ -1,10 +1,6 @@
|
||||
use crate::{agent::Agent, board::Board, piece::Piece};
|
||||
use rayon::prelude::*;
|
||||
|
||||
pub struct ComplexAgent {
|
||||
color: Piece,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Move {
|
||||
/// `i` value of move
|
||||
@ -21,7 +17,7 @@ struct Move {
|
||||
winner: Option<Piece>,
|
||||
/// Move's parent index in [`FutureMoves`]
|
||||
parent_index: Option<usize>,
|
||||
/// Value determined in [`Move::compute_self_value`]
|
||||
/// Value determined in [`FutureMoves::compute_values`]
|
||||
value: i64,
|
||||
}
|
||||
|
||||
@ -55,42 +51,24 @@ struct FutureMoves {
|
||||
}
|
||||
|
||||
impl FutureMoves {
|
||||
fn from_problem_space(board: &Board, color: Piece, parent_index: Option<usize>) -> Vec<Move> {
|
||||
problem_space(board, color)
|
||||
.map(|mut m| {
|
||||
m.parent_index = parent_index;
|
||||
m.value = 0;
|
||||
m
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub fn generate(color: Piece, board: &Board, depth: usize) -> Self {
|
||||
let mut layers = Vec::with_capacity(depth);
|
||||
let initial_moves: Vec<Move> = Self::from_problem_space(board, color, None);
|
||||
let mut layers: Vec<Vec<Move>> = Vec::with_capacity(depth);
|
||||
layers.push(prob_space_idx(board, color, None).collect());
|
||||
|
||||
layers.push(initial_moves);
|
||||
|
||||
for current_depth in 0..depth {
|
||||
let current_layer = &layers[current_depth];
|
||||
if current_layer.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
let next_moves: Vec<Move> = current_layer
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(|(parent_idx, parent_move)| {
|
||||
Self::from_problem_space(
|
||||
&parent_move.board,
|
||||
!parent_move.color,
|
||||
Some(parent_idx),
|
||||
)
|
||||
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();
|
||||
|
||||
layers.push(next_moves);
|
||||
}
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
let mut tmp = Self {
|
||||
inner: layers,
|
||||
@ -111,7 +89,7 @@ impl FutureMoves {
|
||||
|
||||
let current_layer = &mut self.inner[depth];
|
||||
|
||||
current_layer.par_iter_mut().for_each(|mv| {
|
||||
current_layer.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
|
||||
@ -134,24 +112,37 @@ 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 }
|
||||
Self {
|
||||
color,
|
||||
future_moves: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Agent for ComplexAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||
const LOOPS: usize = 5;
|
||||
let layers = FutureMoves::generate(self.color, board, LOOPS);
|
||||
let layers = self
|
||||
.future_moves
|
||||
.take()
|
||||
.unwrap_or_else(|| FutureMoves::generate(self.color, board, LOOPS));
|
||||
|
||||
println!(
|
||||
"# of moves {} deep: {}",
|
||||
LOOPS,
|
||||
layers.inner().iter().map(Vec::len).sum::<usize>()
|
||||
);
|
||||
|
||||
layers.inner()[0]
|
||||
.par_iter()
|
||||
.iter()
|
||||
.max_by_key(|m| m.value)
|
||||
.map(|m| (m.i, m.j))
|
||||
}
|
||||
@ -182,3 +173,15 @@ fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> +
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn prob_space_idx(
|
||||
board: &Board,
|
||||
color: Piece,
|
||||
parent_index: Option<usize>,
|
||||
) -> Box<dyn Iterator<Item = Move> + '_> {
|
||||
Box::new(problem_space(board, color).map(move |mut m| {
|
||||
m.parent_index = parent_index;
|
||||
m.value = 0;
|
||||
m
|
||||
}))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user