Skip to content

Commit

Permalink
fix(encodings): dpw reserve don't build unnecessary structure
Browse files Browse the repository at this point in the history
if the encoding has a single input, don't build an encoding structure
  when reserving variables, since the input can be used to enforce
  bounds
  • Loading branch information
chrjabs committed Feb 11, 2025
1 parent 80f41b7 commit 880aa0d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/encodings/pb/dpw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ impl IterWeightedInputs for DynamicPolyWatchdog {

impl EncodeIncremental for DynamicPolyWatchdog {
fn reserve(&mut self, var_manager: &mut dyn ManageVars) {
if self.structure.is_none() && !self.in_lits.is_empty() {
// Special case for a single input literal, don't need an encoding structure
if self.in_lits.len() <= 1 {
return;
}
if self.structure.is_none() {
self.structure = Some(build_structure(
&mut self.weight_queue,
self.prec_div,
Expand Down Expand Up @@ -1391,4 +1395,18 @@ mod tests {
dpw.encode_ub(0..3, &mut cnf, &mut var_manager).unwrap();
assert_eq!(var_manager.n_used(), 23);
}

#[test]
fn reserve_with_single_input() {
let mut dpw: DynamicPolyWatchdog = [(lit![0], 96833)].into_iter().collect();
let mut var_manager = BasicVarManager::from_next_free(var![1]);
dpw.reserve(&mut var_manager);
let mut cnf = Cnf::new();
dpw.encode_ub_change(96832..=96832, &mut cnf, &mut var_manager)
.unwrap();
assert!(cnf.is_empty());
let assumps = dpw.enforce_ub(96832).unwrap();
assert_eq!(assumps.len(), 1);
assert_eq!(assumps[0], !lit![0]);
}
}

0 comments on commit 880aa0d

Please sign in to comment.