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