Skip to content

Commit

Permalink
Some statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaAtWork committed Dec 27, 2024
1 parent dbd8552 commit b7ddbee
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 22 deletions.
61 changes: 50 additions & 11 deletions src/logic/depth_first/simulation/d_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use super::{
d_node_id::DNodeId,
node::{DNode, DNodeStatus},
};
use crate::logic::depth_first::game::d_direction::DDirection;
use std::{
collections::BTreeMap,
fmt::Display,
os::macos::raw::stat,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -66,9 +64,11 @@ where
}

fn simulate(&mut self) -> DSimulationStatus {
loop {
let mut simulation_status = DSimulationStatus::default();
'simulation: loop {
if self.time.is_timed_out() {
return DSimulationStatus::TimedOut;
simulation_status = DSimulationStatus::TimedOut;
break 'simulation;
}
match self.queue.pop() {
Some(parent_id) => {
Expand All @@ -81,16 +81,24 @@ where
DNodeStatus::Alive => {
self.queue.push(id);
}
DNodeStatus::TimedOut => {
simulation_status = DSimulationStatus::TimedOut;
break 'simulation;
}
_ => (),
}
}
}
_ => (),
}
}
None => return DSimulationStatus::Finished,
None => {
simulation_status = DSimulationStatus::Finished;
break 'simulation;
}
}
}
simulation_status
}

fn calc_children(&mut self, id: &DNodeId) -> Vec<(DNodeId, DNodeStatus)> {
Expand All @@ -107,10 +115,38 @@ where
}
result
}

fn statistic_states(&self) -> usize {
let mut states = 0;
for (_, node) in self.nodes.iter() {
states += node.statistics().states.unwrap_or(1);
}
states
}

fn statistic_nodes(&self) -> usize {
self.nodes.len()
}

fn statistics_time(&self) -> Duration {
self.time.start.elapsed()
}

fn statistics_depth(&self) -> usize {
let mut depth = 0;
for (_, node) in self.nodes.iter() {
if node.status() != DNodeStatus::TimedOut {
depth = depth.max(node.id().len());
}
}
depth
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
enum DSimulationStatus {
#[default]
Unknown,
TimedOut,
Finished,
}
Expand All @@ -128,8 +164,13 @@ impl<Node: DNode> Default for DTree<Node> {
impl<Node: DNode> Display for DTree<Node> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (id, node) in &self.nodes {
writeln!(f, "{} {:?}", id, node.status())?;
writeln!(f, "{}", node.info())?;
}
writeln!(f, "")?;
writeln!(f, "Nodes: {}", self.statistic_nodes())?;
writeln!(f, "States: {}", self.statistic_states())?;
writeln!(f, "Depth: {}", self.statistics_depth())?;
writeln!(f, "Time: {:?}", self.statistics_time())?;
Ok(())
}
}
Expand Down Expand Up @@ -209,12 +250,10 @@ mod tests {
let root = DFullSimulationNode::new(
DNodeId::default(),
vec![state],
DTreeTime::default(),
DTreeTime::new(Duration::from_millis(200)),
DNodeStatus::default(),
);
let mut tree = DTree::default()
.root(root)
.time(Duration::from_millis(100000));
let mut tree = DTree::default().root(root);
let status = tree.simulate();
println!("{}", tree);
println!("{:?}", status);
Expand Down
37 changes: 26 additions & 11 deletions src/logic/depth_first/simulation/node/d_full_simulation_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ use std::{cell::Cell, fmt::Display};
use arrayvec::ArrayVec;
use itertools::Itertools;

use crate::logic::{
depth_first::{
game::{
d_direction::{DDirection, D_DIRECTION_LIST},
d_field::DFastField,
d_game_state::DGameState,
},
simulation::{d_node_id::DNodeId, d_tree::DTreeTime},
use crate::logic::depth_first::{
game::{
d_direction::{DDirection, D_DIRECTION_LIST},
d_field::DFastField,
d_game_state::DGameState,
},
legacy::shared::e_snakes::SNAKES,
simulation::{d_node_id::DNodeId, d_tree::DTreeTime},
};

use super::{DNode, DNodeStatus};
use super::{DNode, DNodeStatistics, DNodeStatus};

#[derive(Default)]
pub struct DFullSimulationNode {
Expand Down Expand Up @@ -66,6 +63,14 @@ impl DNode for DFullSimulationNode {
let mut states = [Vec::new(), Vec::new(), Vec::new(), Vec::new()];
let mut statuses = [DNodeStatus::Alive; 4];
for state in self.states.iter() {
if self.time.is_timed_out() {
for i in 0..4 {
if statuses[i] == DNodeStatus::Alive {
statuses[i] = DNodeStatus::TimedOut;
}
}
break;
}
let possible_moves = state.possible_moves().generate();
if possible_moves.is_empty() {
self.status.set(DNodeStatus::DeadEnd);
Expand All @@ -85,7 +90,7 @@ impl DNode for DFullSimulationNode {
}
}
for i in 0..4 {
if states[i].is_empty() {
if states[i].is_empty() && statuses[i] != DNodeStatus::TimedOut {
statuses[i] = DNodeStatus::Dead;
}
}
Expand All @@ -102,6 +107,16 @@ impl DNode for DFullSimulationNode {
}
result
}

fn info(&self) -> String {
format!("{} {:?} {}", self.id, self.status(), self.states.len())
}

fn statistics(&self) -> DNodeStatistics {
let mut statistics = DNodeStatistics::default();
statistics.states = Some(self.states.len());
statistics
}
}

impl Display for DFullSimulationNode {
Expand Down
11 changes: 11 additions & 0 deletions src/logic/depth_first/simulation/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub trait DNode {
fn id(&self) -> &DNodeId;
fn calc_children(&self) -> Vec<Box<Self>>;
fn status(&self) -> DNodeStatus;
fn info(&self) -> String {
format!("{} {:?}", self.id(), self.status())
}
fn statistics(&self) -> DNodeStatistics {
DNodeStatistics::default()
}
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
Expand All @@ -23,3 +29,8 @@ pub enum DNodeStatus {
TimedOut,
DeadEnd,
}

#[derive(Default)]
pub struct DNodeStatistics {
pub states: Option<usize>,
}

0 comments on commit b7ddbee

Please sign in to comment.