// Workspace: stats // File: stats/src/stat_algo.rs //! The module `stat_algo` contains algorithms for solving statistical affinity problems. //! # Usage //! //! - [find_all_fits]: Searches for all fits of a given mode in the character matrix and adds them to the solution. use crate::{ stat_io::{AffName, CharacCharMatrix}, stat_structs::Fit, }; use anyhow::{Result, anyhow}; use debug_print::debug_println; use std::{iter::Cycle, vec::IntoIter}; /// Fitness mode #[derive(Debug, Clone, Copy)] pub(super) enum Mode { /// A positive fitness, looking for stats the creature has affinity with Positive, /// A negative fitness, looking for stats the creature has no affinity with Negative, } /// Find all the fits for the solution (algo1) pub(super) fn find_all_fits( statmatrix: &mut CharacCharMatrix, mut sw: Cycle>, mode: Mode, ) -> Result<()> { let (mut fits, mut desired_fit_level) = match mode { 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 { (fits, desired_fit_level) = match mode { Mode::Positive => ( find_stat_fit(statmatrix, &mut sw, &fit_level, fits, mode), fit_level.lower(None), ), Mode::Negative => ( find_stat_fit(statmatrix, &mut sw, &fit_level, fits, mode), fit_level.upper(None), ), } } else { return Err(anyhow!( "Can't find any ({:?}) fit at any level, please examine the remaining unfitted characters", mode )); } } Ok(()) } // /// Find all the fits for the solution (algo1) // pub(super) fn find_all_negative_fits( // statmatrix: &mut CharacCharMatrix, // mut sw: Cycle>, // ) -> Result<()> { // let mut fits = statmatrix.size_unfited_loop(); // let mut desired_fit_level = Some(Fit::VeryWeak); // while fits > 0 { // if let Some(fit_level) = desired_fit_level { // fits = find_stat_unfit(statmatrix, &mut sw, &fit_level, fits); // desired_fit_level = fit_level.upper(); // } else { // return Err(anyhow!( // "Can't find any fit at any level, please examine the remaining unfitted characters" // )); // } // } // Ok(()) // } /// Number of value that couldn't have a fit type NbUnfit = usize; /// Find a stat that fits the given affinity fn find_stat_fit( statmatrix: &mut CharacCharMatrix, stat: &mut Cycle>, fit_level: &Fit, iterations: usize, mode: Mode, ) -> NbUnfit { let mut unfit = 0; // All iteration to do for _ in 0..iterations { let stat = stat.next().unwrap(); // Iterate every creature this part of the algorithm is unneeded for our fitness function // as the fitness doesn't depend on the creature if test_fit_for_all_creatures(statmatrix, stat, fit_level, mode).is_err() { unfit += 1; } } if statmatrix.verbose() { if unfit > 0 { println!( "[{}] stats couldn't be [{:?}] fitted at level {:?}", unfit, mode, fit_level ); } else { println!("All stats [{:?}] fitted at level [{:?}]", mode, fit_level); } } unfit } /// Iterate the fitness on every creature /// Useless for our fitness algorithm fn test_fit_for_all_creatures( statmatrix: &mut CharacCharMatrix, stat: AffName, fit_level: &Fit, mode: Mode, ) -> Result<()> { for _ in 0..statmatrix.get_len_affinity(stat) { debug_println!( "[{:?}] Testing fit for stat {:?} at level {:?}", mode, stat, fit_level ); let found = match mode { Mode::Negative => statmatrix.find_creature_for_stat_with_negative_fit(stat, fit_level), Mode::Positive => statmatrix.find_creature_for_stat_with_fit(stat, fit_level), }; if let Ok(creature_id) = found { debug_println!( " ==> Creature {} has affinity for stat {:?}", statmatrix.inner_creature(creature_id, stat).get_name(), stat ); statmatrix .add_affinity_to_creature(statmatrix.to_vec_id(creature_id, stat), stat, mode) .unwrap(); if statmatrix.verbose() { let creature = statmatrix.inner_creature(creature_id, stat); println!( "[{:?}] [{:?}] is a fit for stat {:?} [{:?}]", mode, creature.get_name(), stat, fit_level ); println!( " --> [{:?}] {:?}", creature.get_name(), creature.get_all_selected() ) } statmatrix.remove_creature_from_stat(creature_id, stat); return Ok(()); } } Err(anyhow!( "No creature can fit the charac {:?} at fitness {:?}", stat, fit_level )) }