feat(lib): add async solver capabilities
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::{env::args, path::Path};
|
||||
|
||||
use indicatif::ProgressBar;
|
||||
use wordle_solver::{game::Game, solver::Solver, word_list::WordList};
|
||||
|
||||
pub fn do_simulation(valid_words: WordList, target_words: WordList, opener: Option<&str>) -> u64 {
|
||||
@@ -24,14 +25,16 @@ pub fn do_simulation(valid_words: WordList, target_words: WordList, opener: Opti
|
||||
}
|
||||
|
||||
loop {
|
||||
let best_word = solver
|
||||
.evaluate_all_words()
|
||||
.unwrap()
|
||||
.first()
|
||||
.unwrap()
|
||||
.word
|
||||
.clone();
|
||||
println!("{}", solver.best_word_format(5));
|
||||
let bar = ProgressBar::new(0);
|
||||
let best_words = solver
|
||||
.evaluate_all_words_async(move |i, n| {
|
||||
bar.set_position(i as u64);
|
||||
bar.set_length(n as u64);
|
||||
})
|
||||
.wait()
|
||||
.unwrap();
|
||||
let best_word = best_words.first().unwrap().word.clone();
|
||||
println!("{}", Solver::best_word_format(best_words, 5));
|
||||
|
||||
match game.guess(&best_word) {
|
||||
Ok(pat) => {
|
||||
|
||||
@@ -39,9 +39,11 @@ pub fn main() -> io::Result<()> {
|
||||
io::stdin().read_line(&mut buf)?;
|
||||
let pat_str = buf.trim_ascii().to_ascii_lowercase();
|
||||
|
||||
solver.apply_guess(Pattern::try_new(&guess, pat_from_str(&pat_str)).unwrap());
|
||||
solver.evaluate_all_words();
|
||||
solver
|
||||
.apply_guess(Pattern::try_new(&guess, pat_from_str(&pat_str)).unwrap())
|
||||
.unwrap();
|
||||
let best_words = solver.evaluate_all_words().unwrap();
|
||||
println!("\n{}\n", solver.tabular_format());
|
||||
println!("{}", solver.best_word_format(10));
|
||||
println!("{}", Solver::best_word_format(best_words, 10));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ pub enum CharStatus {
|
||||
GREEN,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
pub struct Pattern {
|
||||
chars: Vec<char>,
|
||||
stats: Vec<CharStatus>,
|
||||
|
||||
+60
-8
@@ -1,3 +1,8 @@
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
mpsc, Arc,
|
||||
};
|
||||
|
||||
use crate::pattern::Pattern;
|
||||
use indicatif::ParallelProgressIterator;
|
||||
use rayon::iter::{ParallelBridge, ParallelIterator};
|
||||
@@ -7,16 +12,29 @@ use crate::word_list::WordList;
|
||||
use derive_more::Display;
|
||||
use tabular::{Row, Table};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Guess {
|
||||
pub pattern: Pattern,
|
||||
pub information_gained: f64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Solver {
|
||||
valid_words: WordList,
|
||||
possible_solutions: WordList,
|
||||
guesses: Vec<Guess>,
|
||||
best_words: Vec<WordStats>,
|
||||
}
|
||||
|
||||
pub struct SolverJob {
|
||||
receiver: mpsc::Receiver<Result<Vec<WordStats>, String>>,
|
||||
}
|
||||
|
||||
impl SolverJob {
|
||||
pub fn wait(self) -> Result<Vec<WordStats>, String> {
|
||||
self.receiver
|
||||
.recv()
|
||||
.map_err(|_| "calculation task cancelled".to_string())?
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Display, Clone)]
|
||||
@@ -40,7 +58,6 @@ impl Solver {
|
||||
valid_words: word_list.clone(),
|
||||
possible_solutions: word_list,
|
||||
guesses: vec![],
|
||||
best_words: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +66,6 @@ impl Solver {
|
||||
valid_words,
|
||||
possible_solutions,
|
||||
guesses: vec![],
|
||||
best_words: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,8 +115,44 @@ impl Solver {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn evaluate_all_words_async<F>(&self, progress: F) -> SolverJob
|
||||
where
|
||||
F: Fn(usize, usize) + Send + Sync + 'static,
|
||||
{
|
||||
let solver_locked = (*self).clone();
|
||||
|
||||
let (tx, rc) = mpsc::channel();
|
||||
|
||||
let completed = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
rayon::spawn_fifo(move || {
|
||||
let mut result = solver_locked
|
||||
.valid_words
|
||||
.words()
|
||||
.par_bridge()
|
||||
.map(|w| {
|
||||
let r = solver_locked.evaluate_word(w);
|
||||
let completed = completed.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
progress(completed, solver_locked.valid_words.len());
|
||||
r
|
||||
})
|
||||
.collect::<Result<Vec<WordStats>, String>>();
|
||||
|
||||
if let Ok(result) = &mut result {
|
||||
result.sort_by(|a, b| {
|
||||
a.expected_score_after_guess
|
||||
.total_cmp(&b.expected_score_after_guess)
|
||||
});
|
||||
}
|
||||
|
||||
let _ = tx.send(result);
|
||||
});
|
||||
|
||||
SolverJob { receiver: rc }
|
||||
}
|
||||
|
||||
pub fn evaluate_all_words(&mut self) -> Result<Vec<WordStats>, String> {
|
||||
self.best_words = self
|
||||
let mut best_words: Vec<WordStats> = self
|
||||
.valid_words
|
||||
.words()
|
||||
.par_bridge()
|
||||
@@ -108,15 +160,15 @@ impl Solver {
|
||||
.map(|w| self.evaluate_word(w))
|
||||
.collect::<Result<_, String>>()?;
|
||||
|
||||
self.best_words.sort_by(|a, b| {
|
||||
best_words.sort_by(|a, b| {
|
||||
a.expected_score_after_guess
|
||||
.total_cmp(&b.expected_score_after_guess)
|
||||
});
|
||||
|
||||
Ok(self.best_words.clone())
|
||||
Ok(best_words)
|
||||
}
|
||||
|
||||
pub fn best_word_format(&self, n: usize) -> String {
|
||||
pub fn best_word_format(best_words: Vec<WordStats>, n: usize) -> String {
|
||||
let mut table = Table::new("#{:<} {:>} | {:<} {:<} {:<}");
|
||||
|
||||
table.add_row(
|
||||
@@ -127,7 +179,7 @@ impl Solver {
|
||||
.with_cell("E[I]")
|
||||
.with_cell("E[score]"),
|
||||
);
|
||||
for (i, ws) in self.best_words.iter().take(n).enumerate() {
|
||||
for (i, ws) in best_words.iter().take(n).enumerate() {
|
||||
table.add_row(
|
||||
Row::new()
|
||||
.with_cell(i + 1)
|
||||
|
||||
Reference in New Issue
Block a user