Skip to content

Commit

Permalink
Trail integer consequences before literal assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekker1 committed Sep 10, 2024
1 parent 92b81c5 commit 596142d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
43 changes: 29 additions & 14 deletions crates/huub/src/solver/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ impl IpasirPropagator for Engine {
return;
}
}
if self.state.trail.assign_sat(lit).is_some() {
if self.state.trail.get_sat_value(lit).is_some() || self.state.conflict.is_some() {
return;
}

// Enqueue propagators, if no conflict has been found
if self.state.conflict.is_none() {
self.state.enqueue_propagators(lit, None);
}
// Enqueue propagators and process integer consequences
self.state.enqueue_propagators(lit, None);
// Trail the SAT assignment
let _ = self.state.trail.assign_sat(lit).is_some();
}

fn notify_new_decision_level(&mut self) {
Expand Down Expand Up @@ -233,26 +233,21 @@ impl IpasirPropagator for Engine {
.collect::<Vec<i32>>(),
"propagate"
);
for &lit in queue.iter() {
self.state.enqueue_propagators(lit, None);
}
// Debug helper to ensure that any reason is based on known true literals
#[cfg(debug_assertions)]
{
for lit in queue.iter() {
if let Some(reason) = self.state.reason_map.get(lit).cloned() {
let clause: Vec<_> = reason.to_clause(&mut self.propagators, &mut self.state);
for l in &clause {
let val = self.state.trail.get_sat_value(!l);
if !val.unwrap_or(false) {
let val = self.state.reason_lit_known_positive(!l);
if !val {
tracing::error!(lit_prop = i32::from(*lit), lit_reason= i32::from(!l), reason_val = ?val, "invalid reason");
}
debug_assert!(
val.unwrap_or(false),
val,
"Literal {} in Reason for {} is {:?}, but should be known true",
!l,
lit,
val
!l, lit, val

Check warning on line 250 in crates/huub/src/solver/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/huub/src/solver/engine.rs#L250

Added line #L250 was not covered by tests
);
}
}
Expand Down Expand Up @@ -547,6 +542,26 @@ impl State {
}
}

#[cfg(debug_assertions)]
fn reason_lit_known_positive(&self, lit: RawLit) -> bool {
if let Some(val) = self.trail.get_sat_value(lit) {
val
} else if let Some((iv, meaning)) = self.bool_to_int.get(lit.var()) {
let meaning = meaning
.map(|l| if lit.is_negated() { !l } else { l })

Check warning on line 551 in crates/huub/src/solver/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/huub/src/solver/engine.rs#L551

Added line #L551 was not covered by tests
.unwrap_or_else(|| self.int_vars[iv].lit_meaning(lit));
let (lb, ub) = self.int_vars[iv].get_bounds(&self.trail);
match meaning {
LitMeaning::Eq(v) => lb == v && v == ub,
LitMeaning::NotEq(_) => false, // TODO: Is this possible?
LitMeaning::GreaterEq(v) => lb >= v,
LitMeaning::Less(v) => ub < v,

Check warning on line 558 in crates/huub/src/solver/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/huub/src/solver/engine.rs#L556-L558

Added lines #L556 - L558 were not covered by tests
}
} else {
false

Check warning on line 561 in crates/huub/src/solver/engine.rs

View check run for this annotation

Codecov / codecov/patch

crates/huub/src/solver/engine.rs#L561

Added line #L561 was not covered by tests
}
}

fn register_reason(&mut self, lit: RawLit, built_reason: Result<Reason, bool>) {
match built_reason {
Ok(reason) => {
Expand Down
1 change: 1 addition & 0 deletions crates/huub/src/solver/engine/solving_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl PropagationActions for SolvingContext<'_> {
trace!(lit = i32::from(propagated_lit), reason = ?reason, "propagate bool");
self.state.register_reason(propagated_lit, reason);
self.state.propagation_queue.push(propagated_lit);
self.state.enqueue_propagators(propagated_lit, None);
let prev = self.state.trail.assign_sat(propagated_lit);
debug_assert!(prev.is_none());
Ok(())
Expand Down

0 comments on commit 596142d

Please sign in to comment.