diff --git a/Cargo.toml b/Cargo.toml index 3e58724..2e83686 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" anyhow = "1.0.82" clap = { version = "4.5.4", features = ["derive"] } csv = "1.3.0" +debug_print = "1.0.0" dynfmt = { version = "0.1.5", features = ["curly"] } enum_dispatch = "0.3.13" glob = "0.3.1" diff --git a/Todo.md b/Todo.md index 5d46061..c9808a5 100644 --- a/Todo.md +++ b/Todo.md @@ -11,6 +11,13 @@ * Have interactive mode ? * Dissociate Model from generation +## StatGen +* Get rid of cycle and have a struct that gives stats according to % and keeps tracks of % +* Test VecDeque +* Add testing +* Add the swap algo (swap a bad fit creature with another) +* Save/load states + ## Test Units diff --git a/src/stat_algo.rs b/src/stat_algo.rs index fdb6f82..84c7367 100644 --- a/src/stat_algo.rs +++ b/src/stat_algo.rs @@ -1,12 +1,12 @@ //! The stat repartition algorithm -use std::{iter::Cycle, vec::IntoIter}; - use crate::{ stat_io::{AffName, CharacCharMatrix}, stat_structs::Fit, }; use anyhow::{anyhow, Result}; +use debug_print::debug_println; +use std::{iter::Cycle, vec::IntoIter}; /// Fitness mode #[derive(Debug, Clone, Copy)] @@ -24,8 +24,14 @@ pub(crate) fn find_all_fits( mode: Mode, ) -> Result<()> { let (mut fits, mut desired_fit_level) = match mode { - Mode::Positive => (statmatrix.size_loop(), Some(Fit::Strong)), - Mode::Negative => (statmatrix.size_unfited_loop(), Some(Fit::VeryWeak)), + Mode::Positive => { + statmatrix.sort(); + (statmatrix.size_loop(), Some(Fit::Strong)) + } + Mode::Negative => { + statmatrix.reverse_sort(); + (statmatrix.size_unfited_loop(), Some(Fit::VeryWeak)) + } }; while fits > 0 { if let Some(fit_level) = desired_fit_level { @@ -111,9 +117,11 @@ fn test_fit_for_all_creatures( mode: Mode, ) -> Result<()> { for _ in 0..statmatrix.get_len_affinity(stat) { - println!( + debug_println!( "[{:?}] Testing fit for stat {:?} at level {:?}", - mode, stat, fit_level + mode, + stat, + fit_level ); let found = match mode { Mode::Negative => statmatrix.find_creature_for_stat_with_negative_fit(stat, fit_level), @@ -121,7 +129,7 @@ fn test_fit_for_all_creatures( }; if let Ok(creature_id) = found { - println!( + debug_println!( " ==> Creature {} has affinity for stat {:?}", statmatrix.inner_creature(creature_id, stat).get_name(), stat @@ -145,6 +153,7 @@ fn test_fit_for_all_creatures( creature.get_all_selected() ) } + statmatrix.remove_creature_from_stat(creature_id, stat); return Ok(()); } } diff --git a/src/stat_io.rs b/src/stat_io.rs index e280fc7..77b9212 100644 --- a/src/stat_io.rs +++ b/src/stat_io.rs @@ -1,11 +1,11 @@ //! All the struct needed for IO purposes use crate::{stat_algo::Mode, stat_structs::Fit, stats::StatConfig}; use anyhow::anyhow; -use std::{fmt::Display, fs::File, iter::zip, path::Path}; - use anyhow::{Context, Result}; use csv::StringRecord; +use debug_print::debug_println; use serde::{Deserialize, Serialize}; +use std::{fmt::Display, fs::File, iter::zip, path::Path}; /// How to represent mathematically the affinity for a stat type Affinity = f64; @@ -166,7 +166,7 @@ impl Creature { /// This test only works if the algo is done in the correct order #[deprecated] fn can_pick(&self, stat: AffName) -> bool { - println!(" ==> Testing if {} can fit {:?}", self.name, stat); + debug_println!(" ==> Testing if {} can fit {:?}", self.name, stat); self.selected_affinities.len() < 2 && !self.selected_affinities.contains(&stat) } @@ -174,7 +174,7 @@ impl Creature { /// This test only works if the algo is done in the correct order #[deprecated] fn can_pick_negatively(&self, stat: AffName) -> bool { - println!(" ==> Testing if {} can fit {:?}", self.name, stat); + debug_println!(" ==> Testing if {} can fit {:?}", self.name, stat); self.selected_negative_affinities.is_empty() && !self.selected_affinities.contains(&stat) } } @@ -274,9 +274,7 @@ pub(crate) struct CharacCharMatrix { impl CharacCharMatrix { /// Initialize the charac matrix by sorting it fn init(&mut self) { - for v in self.inner.iter_mut() { - v.sort_unstable_by(|(_, v1), (_, v2)| v1.total_cmp(v2)) - } + // self.sort() } /// Find a creature having an affinity for the given stat @@ -301,7 +299,7 @@ impl CharacCharMatrix { stat: AffName, fit_level: &Fit, ) -> Result { - for (i, (creature, _)) in self.inner[stat as usize].iter().enumerate().rev() { + for (i, (creature, _)) in self.inner[stat as usize].iter().enumerate() { if self.available_negative(*creature, stat, *fit_level).is_ok() { return Ok(i.into()); } @@ -446,6 +444,34 @@ impl CharacCharMatrix { pub(crate) fn get_config(&self) -> &StatConfig { &self.config } + + /// Sort the underlying data + pub(crate) fn sort(&mut self) { + for v in self.inner.iter_mut() { + v.sort_unstable_by(|(_, v1), (_, v2)| v1.total_cmp(v2)) + } + } + /// Sort the underlying data in the reverse order + pub(crate) fn reverse_sort(&mut self) { + for v in self.inner.iter_mut() { + v.sort_unstable_by(|(_, v1), (_, v2)| v2.total_cmp(v1)) + } + } + + /// Remove the creature to avoid doing incessant lookups + pub(crate) fn remove_creature_from_stat( + &mut self, + creature_id: CreatureInnerId, + stat: AffName, + ) { + let _rem = self.inner[stat as usize].remove(creature_id.id()); + // println!( + // " ==> Removing stat {:?} on creature {} (was: {:?})", + // stat, + // self.creature_name(_rem.0), + // _rem.1 + // ); + } } // impl From> for CharacCharMatrix { diff --git a/src/stats.rs b/src/stats.rs index dba68ba..50e2dd7 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -31,8 +31,8 @@ pub(crate) fn stat_solver(args: &StatConfig, config: &Config) -> Result<()> { solution_from_empty(&mut statmatrix).unwrap(); if args.verbosity { println!("{:?}", statmatrix.print_weights()); - println!("{:?}", statmatrix.print_solutions()); } + statmatrix.print_solutions(); Ok(()) }