generated from PlexSheep/rs-base
improve naive solver #13
|
@ -4,14 +4,15 @@ use libpt::log::{debug, error, info, trace};
|
||||||
|
|
||||||
use crate::error::{SolverError, WResult};
|
use crate::error::{SolverError, WResult};
|
||||||
use crate::game::evaluation::{Evaluation, EvaluationUnit};
|
use crate::game::evaluation::{Evaluation, EvaluationUnit};
|
||||||
|
use crate::game::response::Status;
|
||||||
use crate::wlist::word::{Word, WordData};
|
use crate::wlist::word::{Word, WordData};
|
||||||
use crate::wlist::WordList;
|
use crate::wlist::WordList;
|
||||||
|
|
||||||
use super::{AnyBuiltinSolver, Solver, Status};
|
|
||||||
|
|
||||||
mod states;
|
mod states;
|
||||||
use states::*;
|
use states::*;
|
||||||
|
|
||||||
|
use super::{AnyBuiltinSolver, Solver};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct NaiveSolver<'wl, WL> {
|
pub struct NaiveSolver<'wl, WL> {
|
||||||
wl: &'wl WL,
|
wl: &'wl WL,
|
||||||
|
@ -37,9 +38,10 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> {
|
||||||
let mut state: SolverState = SolverState::new();
|
let mut state: SolverState = SolverState::new();
|
||||||
let responses = game.responses().iter().enumerate();
|
let responses = game.responses().iter().enumerate();
|
||||||
for (_idx, response) in responses {
|
for (_idx, response) in responses {
|
||||||
let mut already_found_amounts: HashMap<char, usize> = HashMap::new();
|
let mut abs_freq: HashMap<char, usize> = HashMap::new();
|
||||||
let evaluation: &Evaluation = response.evaluation();
|
let evaluation: &Evaluation = response.evaluation();
|
||||||
for (idx, p) in evaluation.clone().into_iter().enumerate() {
|
for (idx, p) in evaluation.clone().into_iter().enumerate() {
|
||||||
|
state.start_step();
|
||||||
match p.1 {
|
match p.1 {
|
||||||
Status::Matched => {
|
Status::Matched => {
|
||||||
pattern.replace_range(idx..idx + 1, &p.0.to_string());
|
pattern.replace_range(idx..idx + 1, &p.0.to_string());
|
||||||
|
@ -49,7 +51,7 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> {
|
||||||
.entry(p.0)
|
.entry(p.0)
|
||||||
.or_insert(CharInfo::new(game.length()))
|
.or_insert(CharInfo::new(game.length()))
|
||||||
.found_at(idx);
|
.found_at(idx);
|
||||||
*already_found_amounts.entry(p.0).or_default() += 1;
|
*abs_freq.entry(p.0).or_default() += 1;
|
||||||
}
|
}
|
||||||
Status::Exists => {
|
Status::Exists => {
|
||||||
let cinfo = state
|
let cinfo = state
|
||||||
|
@ -57,16 +59,18 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> {
|
||||||
.entry(p.0)
|
.entry(p.0)
|
||||||
.or_insert(CharInfo::new(game.length()));
|
.or_insert(CharInfo::new(game.length()));
|
||||||
cinfo.tried_but_failed(idx);
|
cinfo.tried_but_failed(idx);
|
||||||
*already_found_amounts.entry(p.0).or_default() += 1;
|
*abs_freq.entry(p.0).or_default() += 1;
|
||||||
cinfo.min_occurences(already_found_amounts[&p.0]);
|
|
||||||
}
|
}
|
||||||
Status::None => state
|
Status::None => {
|
||||||
|
let cinfo = state
|
||||||
.char_map_mut()
|
.char_map_mut()
|
||||||
.entry(p.0)
|
.entry(p.0)
|
||||||
.or_insert(CharInfo::new(game.length()))
|
.or_insert(CharInfo::new(game.length()));
|
||||||
.max_occurences(*already_found_amounts.entry(p.0).or_default()),
|
cinfo.tried_but_failed(idx);
|
||||||
}
|
}
|
||||||
trace!("absolute frequencies: {already_found_amounts:?}");
|
}
|
||||||
|
trace!("absolute frequencies: {abs_freq:?}");
|
||||||
|
state.finish_step(&abs_freq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl SolverState {
|
||||||
pub(crate) fn get_all_known_contained(&self) -> Vec<(&char, &CharInfo)> {
|
pub(crate) fn get_all_known_contained(&self) -> Vec<(&char, &CharInfo)> {
|
||||||
self.char_map
|
self.char_map
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(key, value)| value.part_of_solution())
|
.filter(|(_key, value)| value.part_of_solution())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +49,28 @@ impl SolverState {
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn start_step(&mut self) {}
|
||||||
|
|
||||||
|
pub(crate) fn finish_step(&mut self, abs_freq: &HashMap<char, usize>) {
|
||||||
|
for (k, v) in abs_freq {
|
||||||
|
if *v == 0 {
|
||||||
|
self.char_map
|
||||||
|
.get_mut(k)
|
||||||
|
.expect(
|
||||||
|
"char in abs_freq was not added to the char_map before finalizing the step",
|
||||||
|
)
|
||||||
|
.max_occurences(0);
|
||||||
|
} else {
|
||||||
|
self.char_map
|
||||||
|
.get_mut(k)
|
||||||
|
.expect(
|
||||||
|
"char in abs_freq was not added to the char_map before finalizing the step",
|
||||||
|
)
|
||||||
|
.min_occurences(*v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CharInfo {
|
impl CharInfo {
|
||||||
|
|
Loading…
Reference in New Issue