FutureMoves: initial move to not storing board
This commit is contained in:
parent
6958df9df4
commit
efd762e5d2
@ -19,6 +19,8 @@ pub struct FutureMoves {
|
||||
agent_color: Piece,
|
||||
|
||||
config: FutureMoveConfig,
|
||||
|
||||
board: Board,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@ -87,6 +89,7 @@ impl FutureMoves {
|
||||
current_depth: 0,
|
||||
agent_color,
|
||||
config,
|
||||
board: Board::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -196,13 +199,15 @@ impl FutureMoves {
|
||||
let parent = &self.arena[parent_idx];
|
||||
|
||||
let new_color = !parent.color;
|
||||
let parent_board = self
|
||||
.get_board_from_idx(parent_idx)
|
||||
.expect("unable to get board");
|
||||
|
||||
// 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()
|
||||
.flat_map(|coord| {
|
||||
parent
|
||||
.board
|
||||
parent_board
|
||||
.what_if(coord, new_color)
|
||||
.map(move |x| (coord, x))
|
||||
})
|
||||
@ -212,7 +217,7 @@ impl FutureMoves {
|
||||
.collect();
|
||||
|
||||
if new.is_empty() {
|
||||
new.push(Move::new(None, parent.board, new_color, self.agent_color));
|
||||
new.push(Move::new(None, parent_board, new_color, self.agent_color));
|
||||
}
|
||||
|
||||
let start_idx = self.arena.len();
|
||||
@ -321,7 +326,7 @@ impl FutureMoves {
|
||||
|
||||
fn get_board_from_idx(&self, idx: usize) -> Option<Board> {
|
||||
if let Some(hist) = self.move_history(idx) {
|
||||
let mut board = self.arena[self.current_root.unwrap()].board;
|
||||
let mut board = self.board;
|
||||
for (m, c) in hist {
|
||||
if let Some(m) = m {
|
||||
board.place(m, c).expect("move would not propegate");
|
||||
@ -361,7 +366,8 @@ impl FutureMoves {
|
||||
// match the agent_color usually root or great-grand child
|
||||
.filter(|&idx| self.depth_of(idx) % 2 == 0)
|
||||
.find(|&idx| {
|
||||
&self.arena[idx].board == board && self.arena[idx].color == !self.agent_color
|
||||
self.get_board_from_idx(idx).as_ref() == Some(board)
|
||||
&& self.arena[idx].color == !self.agent_color
|
||||
});
|
||||
|
||||
if let Some(curr_board_idx) = curr_board {
|
||||
@ -577,7 +583,6 @@ mod tests {
|
||||
|
||||
futm.arena.push(Move {
|
||||
coord: None,
|
||||
board: Board::new(),
|
||||
winner: Winner::None,
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
@ -664,7 +669,6 @@ mod tests {
|
||||
|
||||
futm.arena.push(Move {
|
||||
coord: None,
|
||||
board: Board::new(),
|
||||
winner: Winner::None,
|
||||
parent: None,
|
||||
children: vec![],
|
||||
@ -705,7 +709,6 @@ mod tests {
|
||||
|
||||
futm.arena.push(Move {
|
||||
coord: None,
|
||||
board: Board::new(),
|
||||
winner: Winner::None,
|
||||
parent: None,
|
||||
children: vec![1],
|
||||
|
||||
@ -7,9 +7,6 @@ pub struct Move {
|
||||
/// Coordinates (i, j) of the move (if it exists)
|
||||
pub coord: Option<CoordPair>,
|
||||
|
||||
/// [`Board`] state after move is made
|
||||
pub board: Board,
|
||||
|
||||
/// Current winner of the match
|
||||
pub winner: Winner,
|
||||
|
||||
@ -41,7 +38,6 @@ impl Move {
|
||||
pub fn new(coord: Option<CoordPair>, board: Board, color: Piece, agent_color: Piece) -> Self {
|
||||
let mut m = Move {
|
||||
coord,
|
||||
board,
|
||||
winner: board.game_winner(),
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
@ -51,11 +47,11 @@ impl Move {
|
||||
self_value: 0,
|
||||
tried_children: false,
|
||||
};
|
||||
m.self_value = m.compute_self_value(agent_color);
|
||||
m.self_value = m.compute_self_value(agent_color, &board);
|
||||
m
|
||||
}
|
||||
|
||||
fn compute_self_value(&self, agent_color: Piece) -> i16 {
|
||||
fn compute_self_value(&self, agent_color: Piece, board: &Board) -> i16 {
|
||||
if self.winner == Winner::Player(!agent_color) {
|
||||
// if this board results in the opponent winning, MAJORLY negatively weigh this move
|
||||
// NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move.
|
||||
@ -72,7 +68,7 @@ impl Move {
|
||||
|
||||
// I guess ignore Ties here, don't give them an explicit value,
|
||||
// because even in the case of ties, we want to have a higher score
|
||||
BVM.board_value(&self.board, agent_color)
|
||||
BVM.board_value(board, agent_color)
|
||||
}
|
||||
|
||||
/// Sort children of the [`Move`] by their self_value in `arena`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user