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 2 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
16 changes: 6 additions & 10 deletions solvers/src/macro_solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct MacroSolver<'a> {
step_lower_bound_solver: StepLowerBoundSolver,
solution_callback: Box<SolutionCallback<'a>>,
progress_callback: Box<ProgressCallback<'a>>,
flag: AtomicFlag,
interrupt_signal: AtomicFlag,
}

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

Expand All @@ -88,10 +88,6 @@ impl<'a> MacroSolver<'a> {
}

fn do_solve(&mut self, state: SimulationState) -> Option<Vec<Action>> {
if self.flag.is_set() {
return None;
}

let mut search_queue = {
let _timer = NamedTimer::new("Initial upper bound");
let quality_upper_bound = self.quality_upper_bound_solver.quality_upper_bound(state)?;
Expand All @@ -115,7 +111,7 @@ impl<'a> MacroSolver<'a> {

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

Expand Down
50 changes: 28 additions & 22 deletions solvers/src/quality_upper_bound_solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct QualityUpperBoundSolver {
solver_settings: SolverSettings,
solved_states: HashMap<ReducedState, ParetoFrontId>,
pareto_front_builder: ParetoFrontBuilder<u16, u16>,
flag: AtomicFlag,
interrupt_signal: AtomicFlag,
// pre-computed branch pruning values
waste_not_1_min_cp: i16,
waste_not_2_min_cp: i16,
Expand All @@ -42,7 +42,7 @@ impl QualityUpperBoundSolver {
settings: Settings,
backload_progress: bool,
unsound_branch_pruning: bool,
flag: AtomicFlag,
interrupt_signal: AtomicFlag,
) -> Self {
log::trace!(
"ReducedState (QualityUpperBoundSolver) - size: {}, align: {}",
Expand Down Expand Up @@ -83,7 +83,7 @@ impl QualityUpperBoundSolver {
settings.max_progress,
settings.max_quality,
),
flag,
interrupt_signal,
waste_not_1_min_cp: waste_not_min_cp(56, 4, durability_cost),
waste_not_2_min_cp: waste_not_min_cp(98, 8, durability_cost),
}
Expand All @@ -92,7 +92,7 @@ impl QualityUpperBoundSolver {
/// Returns an upper-bound on the maximum Quality achievable from this state while also maxing out Progress.
/// There is no guarantee on the tightness of the upper-bound.
pub fn quality_upper_bound(&mut self, state: SimulationState) -> Option<u16> {
if self.flag.is_set() {
if self.interrupt_signal.is_set() {
return None;
}

Expand Down Expand Up @@ -137,19 +137,19 @@ impl QualityUpperBoundSolver {
))
}

fn solve_state(&mut self, state: ReducedState) {
if self.flag.is_set() {
return;
fn solve_state(&mut self, state: ReducedState) -> Option<()> {
if self.interrupt_signal.is_set() {
return None;
}

if state.data.combo() == Combo::None {
self.solve_normal_state(state);
self.solve_normal_state(state)
} else {
self.solve_combo_state(state)
}
}

fn solve_normal_state(&mut self, state: ReducedState) {
fn solve_normal_state(&mut self, state: ReducedState) -> Option<()> {
self.pareto_front_builder.push_empty();
let search_actions = if state.data.progress_only() {
PROGRESS_SEARCH_ACTIONS.intersection(self.simulator_settings.allowed_actions)
Expand All @@ -160,7 +160,7 @@ impl QualityUpperBoundSolver {
if !self.should_use_action(state, action) {
continue;
}
self.build_child_front(state, action);
self.build_child_front(state, action)?;
if self.pareto_front_builder.is_max() {
// stop early if both Progress and Quality are maxed out
// this optimization would work even better with better action ordering
Expand All @@ -170,33 +170,37 @@ impl QualityUpperBoundSolver {
}
let id = self.pareto_front_builder.save().unwrap();
self.solved_states.insert(state, id);

Some(())
}

fn solve_combo_state(&mut self, state: ReducedState) {
fn solve_combo_state(&mut self, state: ReducedState) -> Option<()> {
match self.solved_states.get(&state.drop_combo()) {
Some(id) => self.pareto_front_builder.push_from_id(*id),
None => self.solve_normal_state(state.drop_combo()),
None => self.solve_normal_state(state.drop_combo())?,
}
match state.data.combo() {
Combo::None => unreachable!(),
Combo::SynthesisBegin => {
self.build_child_front(state, Action::MuscleMemory);
self.build_child_front(state, Action::Reflect);
self.build_child_front(state, Action::TrainedEye);
self.build_child_front(state, Action::MuscleMemory)?;
self.build_child_front(state, Action::Reflect)?;
self.build_child_front(state, Action::TrainedEye)?;
}
Combo::BasicTouch => {
self.build_child_front(state, Action::RefinedTouch);
self.build_child_front(state, Action::StandardTouch);
self.build_child_front(state, Action::RefinedTouch)?;
self.build_child_front(state, Action::StandardTouch)?;
}
Combo::StandardTouch => {
self.build_child_front(state, Action::AdvancedTouch);
self.build_child_front(state, Action::AdvancedTouch)?;
}
}

Some(())
}

fn build_child_front(&mut self, state: ReducedState, action: Action) {
if self.flag.is_set() {
return;
fn build_child_front(&mut self, state: ReducedState, action: Action) -> Option<()> {
if self.interrupt_signal.is_set() {
return None;
}

if let Ok((new_state, action_progress, action_quality)) =
Expand All @@ -205,7 +209,7 @@ impl QualityUpperBoundSolver {
if new_state.data.cp() >= self.solver_settings.durability_cost {
match self.solved_states.get(&new_state) {
Some(id) => self.pareto_front_builder.push_from_id(*id),
None => self.solve_state(new_state),
None => self.solve_state(new_state)?,
}
self.pareto_front_builder.map(move |value| {
value.first += action_progress;
Expand All @@ -222,6 +226,8 @@ impl QualityUpperBoundSolver {
self.pareto_front_builder.merge();
}
}

Some(())
}

fn should_use_action(&self, state: ReducedState, action: Action) -> bool {
Expand Down
46 changes: 26 additions & 20 deletions solvers/src/step_lower_bound_solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub struct StepLowerBoundSolver {
bonus_durability_restore: i8,
solved_states: HashMap<ReducedState, ParetoFrontId>,
pareto_front_builder: ParetoFrontBuilder<u16, u16>,
flag: AtomicFlag,
interrupt_signal: AtomicFlag,
}

impl StepLowerBoundSolver {
pub fn new(
mut settings: Settings,
backload_progress: bool,
unsound_branch_pruning: bool,
flag: AtomicFlag,
interrupt_signal: AtomicFlag,
) -> Self {
log::trace!(
"ReducedState (StepLowerBoundSolver) - size: {}, align: {}",
Expand All @@ -59,7 +59,7 @@ impl StepLowerBoundSolver {
settings.max_progress,
settings.max_quality,
),
flag,
interrupt_signal,
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ impl StepLowerBoundSolver {
}

fn quality_upper_bound(&mut self, state: SimulationState, step_budget: u8) -> Option<u16> {
if self.flag.is_set() {
if self.interrupt_signal.is_set() {
return None;
}

Expand Down Expand Up @@ -127,26 +127,26 @@ impl StepLowerBoundSolver {
))
}

fn solve_state(&mut self, reduced_state: ReducedState) {
if self.flag.is_set() {
return;
fn solve_state(&mut self, reduced_state: ReducedState) -> Option<()> {
if self.interrupt_signal.is_set() {
return None;
}

if reduced_state.combo == Combo::None {
self.solve_normal_state(reduced_state);
self.solve_normal_state(reduced_state)
} else {
self.solve_combo_state(reduced_state)
}
}

fn solve_normal_state(&mut self, reduced_state: ReducedState) {
fn solve_normal_state(&mut self, reduced_state: ReducedState) -> Option<()> {
self.pareto_front_builder.push_empty();
let search_actions = match reduced_state.progress_only {
false => FULL_SEARCH_ACTIONS,
true => PROGRESS_SEARCH_ACTIONS,
};
for action in search_actions.actions_iter() {
self.build_child_front(reduced_state, action);
self.build_child_front(reduced_state, action)?;
if self.pareto_front_builder.is_max() {
// stop early if both Progress and Quality are maxed out
// this optimization would work even better with better action ordering
Expand All @@ -156,32 +156,36 @@ impl StepLowerBoundSolver {
}
let id = self.pareto_front_builder.save().unwrap();
self.solved_states.insert(reduced_state, id);

Some(())
}

fn solve_combo_state(&mut self, reduced_state: ReducedState) {
fn solve_combo_state(&mut self, reduced_state: ReducedState) -> Option<()> {
match self.solved_states.get(&reduced_state.to_non_combo()) {
Some(id) => self.pareto_front_builder.push_from_id(*id),
None => self.solve_normal_state(reduced_state.to_non_combo()),
None => self.solve_normal_state(reduced_state.to_non_combo())?,
}
match reduced_state.combo {
Combo::None => unreachable!(),
Combo::SynthesisBegin => {
self.build_child_front(reduced_state, Action::MuscleMemory);
self.build_child_front(reduced_state, Action::Reflect);
self.build_child_front(reduced_state, Action::TrainedEye);
self.build_child_front(reduced_state, Action::MuscleMemory)?;
self.build_child_front(reduced_state, Action::Reflect)?;
self.build_child_front(reduced_state, Action::TrainedEye)?;
}
Combo::BasicTouch => {
if !reduced_state.progress_only {
self.build_child_front(reduced_state, Action::RefinedTouch);
self.build_child_front(reduced_state, Action::RefinedTouch)?;
}
}
Combo::StandardTouch => unreachable!(),
}

Some(())
}

fn build_child_front(&mut self, reduced_state: ReducedState, action: Action) {
if self.flag.is_set() {
return;
fn build_child_front(&mut self, reduced_state: ReducedState, action: Action) -> Option<()> {
if self.interrupt_signal.is_set() {
return None;
}

if let Ok(new_full_state) =
Expand Down Expand Up @@ -212,7 +216,7 @@ impl StepLowerBoundSolver {
if new_reduced_state.steps_budget != 0 && new_reduced_state.durability > 0 {
match self.solved_states.get(&new_reduced_state) {
Some(id) => self.pareto_front_builder.push_from_id(*id),
None => self.solve_state(new_reduced_state),
None => self.solve_state(new_reduced_state)?,
}
self.pareto_front_builder.map(move |value| {
value.first += action_progress;
Expand All @@ -226,6 +230,8 @@ impl StepLowerBoundSolver {
self.pareto_front_builder.merge();
}
}

Some(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion solvers/src/utils/atomic_flag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
atomic::{AtomicBool, Ordering},
Arc,
};

Expand Down
Loading