Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cancel button #92

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/locales/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ stats_window:
label:
none:
en: None
cancel:
en: Cancel
clear:
en: Clear
select:
Expand Down
3 changes: 2 additions & 1 deletion raphael-cli/src/commands/solve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Args;
use game_data::{get_game_settings, CrafterStats, MEALS, POTIONS, RECIPES};
use simulator::SimulationState;
use solvers::MacroSolver;
use solvers::{AtomicFlag, MacroSolver};

#[derive(Args, Debug)]
pub struct SolveArgs {
Expand Down Expand Up @@ -258,6 +258,7 @@ pub fn execute(args: &SolveArgs) {
args.unsound,
Box::new(|_| {}),
Box::new(|_| {}),
AtomicFlag::new(),
);
let actions = solver.solve(state).expect("Failed to solve");

Expand Down
15 changes: 11 additions & 4 deletions solvers/examples/macro_solver_example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use simulator::{Action, ActionMask, Settings, SimulationState};
use solvers::MacroSolver;
use solvers::{AtomicFlag, MacroSolver};

fn main() {
#[cfg(feature = "env_logger")]
Expand Down Expand Up @@ -32,9 +32,16 @@ fn main() {
};

let state = SimulationState::new(&settings);
let actions = MacroSolver::new(settings, false, false, Box::new(|_| {}), Box::new(|_| {}))
.solve(state)
.unwrap();
let actions = MacroSolver::new(
settings,
false,
false,
Box::new(|_| {}),
Box::new(|_| {}),
AtomicFlag::new(),
)
.solve(state)
.unwrap();

let quality = SimulationState::from_macro(&settings, &actions)
.unwrap()
Expand Down
7 changes: 5 additions & 2 deletions solvers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod actions;
mod branch_pruning;
mod utils;

mod finish_solver;
use finish_solver::FinishSolver;
Expand All @@ -14,8 +13,11 @@ use step_lower_bound_solver::StepLowerBoundSolver;
mod macro_solver;
pub use macro_solver::MacroSolver;

mod utils;
pub use utils::AtomicFlag;

pub mod test_utils {
use crate::MacroSolver;
use crate::{utils::AtomicFlag, MacroSolver};
use simulator::*;

pub fn solve(
Expand All @@ -29,6 +31,7 @@ pub mod test_utils {
unsound_branch_pruning,
Box::new(|_| {}),
Box::new(|_| {}),
AtomicFlag::new(),
)
.solve(SimulationState::new(settings))
}
Expand Down
8 changes: 4 additions & 4 deletions solvers/src/macro_solver/fast_lower_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn fast_lower_bound(
settings: &Settings,
finish_solver: &mut FinishSolver,
upper_bound_solver: &mut QualityUpperBoundSolver,
) -> u16 {
) -> Option<u16> {
let _timer = NamedTimer::new("Fast lower bound");
let allowed_actions = settings.allowed_actions.intersection(SEARCH_ACTIONS);

Expand All @@ -29,7 +29,7 @@ pub fn fast_lower_bound(

let mut quality_lower_bound = 0;

search_queue.push(upper_bound_solver.quality_upper_bound(state), state);
search_queue.push(upper_bound_solver.quality_upper_bound(state)?, state);

while let Some((score, state)) = search_queue.pop() {
if score <= quality_lower_bound {
Expand All @@ -48,7 +48,7 @@ pub fn fast_lower_bound(
if action == Action::ByregotsBlessing {
continue;
}
let quality_upper_bound = upper_bound_solver.quality_upper_bound(state);
let quality_upper_bound = upper_bound_solver.quality_upper_bound(state)?;
if quality_upper_bound <= quality_lower_bound {
continue;
}
Expand All @@ -62,7 +62,7 @@ pub fn fast_lower_bound(
}

log::debug!("Fast quality lower bound: {}", quality_lower_bound);
std::cmp::min(settings.max_quality, quality_lower_bound)
Some(std::cmp::min(settings.max_quality, quality_lower_bound))
}

fn should_use_action(action: Action, state: &SimulationState, allowed_actions: ActionMask) -> bool {
Expand Down
24 changes: 18 additions & 6 deletions solvers/src/macro_solver/solver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use simulator::{Action, ActionMask, Condition, Settings, SimulationState};

use super::search_queue::SearchScore;
use crate::actions::{DURABILITY_ACTIONS, PROGRESS_ACTIONS, QUALITY_ACTIONS};
use crate::branch_pruning::{is_progress_only_state, strip_quality_effects};
use crate::macro_solver::fast_lower_bound::fast_lower_bound;
use crate::macro_solver::search_queue::SearchQueue;
use crate::utils::NamedTimer;
use crate::{
actions::{DURABILITY_ACTIONS, PROGRESS_ACTIONS, QUALITY_ACTIONS},
utils::AtomicFlag,
};
use crate::{FinishSolver, QualityUpperBoundSolver, StepLowerBoundSolver};

use std::vec::Vec;
Expand Down Expand Up @@ -36,6 +39,7 @@ pub struct MacroSolver<'a> {
step_lower_bound_solver: StepLowerBoundSolver,
solution_callback: Box<SolutionCallback<'a>>,
progress_callback: Box<ProgressCallback<'a>>,
interrupt_signal: AtomicFlag,
}

impl<'a> MacroSolver<'a> {
Expand All @@ -45,6 +49,7 @@ impl<'a> MacroSolver<'a> {
unsound_branch_pruning: bool,
solution_callback: Box<SolutionCallback<'a>>,
progress_callback: Box<ProgressCallback<'a>>,
interrupt_signal: AtomicFlag,
) -> MacroSolver<'a> {
MacroSolver {
settings,
Expand All @@ -55,14 +60,17 @@ impl<'a> MacroSolver<'a> {
settings,
backload_progress,
unsound_branch_pruning,
interrupt_signal.clone(),
),
step_lower_bound_solver: StepLowerBoundSolver::new(
settings,
backload_progress,
unsound_branch_pruning,
interrupt_signal.clone(),
),
solution_callback,
progress_callback,
interrupt_signal,
}
}

Expand All @@ -82,9 +90,9 @@ impl<'a> MacroSolver<'a> {
fn do_solve(&mut self, state: SimulationState) -> Option<Vec<Action>> {
let mut search_queue = {
let _timer = NamedTimer::new("Initial upper bound");
let quality_upper_bound = self.quality_upper_bound_solver.quality_upper_bound(state);
let quality_upper_bound = self.quality_upper_bound_solver.quality_upper_bound(state)?;
let step_lower_bound = if quality_upper_bound >= self.settings.max_quality {
self.step_lower_bound_solver.step_lower_bound(state)
self.step_lower_bound_solver.step_lower_bound(state)?
} else {
1 // quality dominates the search score, so no need to query the step solver
};
Expand All @@ -94,7 +102,7 @@ impl<'a> MacroSolver<'a> {
&self.settings,
&mut self.finish_solver,
&mut self.quality_upper_bound_solver,
);
)?;
let minimum_score = SearchScore::new(quality_lower_bound, u8::MAX, u8::MAX);
SearchQueue::new(state, initial_score, minimum_score)
};
Expand All @@ -103,6 +111,10 @@ impl<'a> MacroSolver<'a> {

let mut popped = 0;
while let Some((state, score, backtrack_id)) = search_queue.pop() {
if self.interrupt_signal.is_set() {
return None;
}

popped += 1;
if popped % (1 << 14) == 0 {
(self.progress_callback)(popped);
Expand Down Expand Up @@ -136,14 +148,14 @@ impl<'a> MacroSolver<'a> {
} else {
std::cmp::min(
score.quality,
self.quality_upper_bound_solver.quality_upper_bound(state),
self.quality_upper_bound_solver.quality_upper_bound(state)?,
)
};

let step_lb_hint = score.steps.saturating_sub(current_steps + 1);
let step_lower_bound = if quality_upper_bound >= self.settings.max_quality {
self.step_lower_bound_solver
.step_lower_bound_with_hint(state, step_lb_hint)
.step_lower_bound_with_hint(state, step_lb_hint)?
.saturating_add(current_steps + 1)
} else {
current_steps + 1
Expand Down
Loading