move "is_terminal" and "is_better" logic from node.rs into bot.rs

This commit is contained in:
tali 2023-04-16 19:15:55 -04:00
parent 9067459eb1
commit 4ad8f38341
2 changed files with 7 additions and 19 deletions

View File

@ -269,7 +269,8 @@ impl SegmentedAStar {
let cand = open_set.pop().ok_or(None)?; let cand = open_set.pop().ok_or(None)?;
let cand = unsafe { cand.0.as_node() }; let cand = unsafe { cand.0.as_node() };
if cand.is_terminal() { if cand.queue().is_empty() || cand.rating().0 {
// terminal node; end search and back up its rating
return Err(Some(cand)); return Err(Some(cand));
} }
@ -293,7 +294,7 @@ impl SegmentedAStar {
#[cold] #[cold]
fn backup(&mut self, cand: &Node) -> bool { fn backup(&mut self, cand: &Node) -> bool {
if cand.is_better(self.best()) { if cand.rating() > self.best().rating() {
self.best = cand.into(); self.best = cand.into();
true true
} else { } else {
@ -302,13 +303,13 @@ impl SegmentedAStar {
} }
fn select(&mut self) { fn select(&mut self) {
let mut best = None; let mut best = None::<&Node>;
self.depth = 0; self.depth = 0;
for (depth, set) in self.open.iter().enumerate() { for (depth, set) in self.open.iter().enumerate() {
let Some(cand) = set.peek() else { continue }; let Some(cand) = set.peek() else { continue };
let cand = unsafe { cand.0.as_node() }; let cand = unsafe { cand.0.as_node() };
if best.map_or(true, |best| cand.is_better(best)) { if best.map_or(true, |best| cand.rating() > best.rating()) {
best = Some(cand); best = Some(cand);
self.depth = depth; self.depth = depth;
} }
@ -330,11 +331,8 @@ impl core::cmp::Ord for AStarNode {
fn cmp(&self, other: &Self) -> core::cmp::Ordering { fn cmp(&self, other: &Self) -> core::cmp::Ordering {
let lhs = unsafe { self.0.as_node() }; let lhs = unsafe { self.0.as_node() };
let rhs = unsafe { other.0.as_node() }; let rhs = unsafe { other.0.as_node() };
if lhs.is_better(rhs) { // TODO: tiebreaker strategy
core::cmp::Ordering::Greater lhs.rating().cmp(&rhs.rating())
} else {
core::cmp::Ordering::Less
}
} }
} }

View File

@ -74,16 +74,6 @@ impl Node {
self.rating self.rating
} }
// TODO: move this function to `bot.algorithm`
pub fn is_better(&self, other: &Node) -> bool {
self.rating > other.rating
}
// TODO: move this function to `bot.algorithm`
pub fn is_terminal(&self) -> bool {
self.queue().is_empty() || self.rating.0
}
/// Get the initial placement made after the root node which eventually arrives at /// Get the initial placement made after the root node which eventually arrives at
/// this node. /// this node.
pub fn root_placement(&self) -> Option<Piece> { pub fn root_placement(&self) -> Option<Piece> {