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

Fix the in-place negation of Xor in the TseitinEncoder #68

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
53 changes: 51 additions & 2 deletions crates/pindakaas/src/propositional_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,13 @@ impl<DB: ClauseDatabase> Encoder<DB, Formula> for TseitinEncoder {
let neg_els: Formula = !*els.clone();
self.encode(&mut cdb, &neg_els)
}
Formula::Xor(sub) => {
Formula::Equiv(sub) if sub.len() == 2 => {
self.encode(db, &Formula::Xor(sub.clone()))
}
Formula::Xor(sub) if sub.len() == 2 => {
self.encode(db, &Formula::Equiv(sub.clone()))
}
Formula::Xor(sub) if sub.len() % 2 != 0 => {
let neg_sub = sub.iter().map(|f| !(f.clone())).collect();
self.encode(db, &Formula::Xor(neg_sub))
}
Expand Down Expand Up @@ -339,7 +345,7 @@ impl<DB: ClauseDatabase> Encoder<DB, Formula> for TseitinEncoder {
#[cfg(test)]
mod tests {
use crate::{
helpers::tests::{assert_enc_sol, lits},
helpers::tests::{assert_enc_sol, assert_sol, lits},
Encoder, Formula, Lit, TseitinEncoder,
};

Expand Down Expand Up @@ -507,6 +513,49 @@ mod tests {
=> vec![lits![-1, -3, -5], lits![1, 3, -5], lits![-1, 3, 5], lits![1, -3, 5], lits![-2, -4, -5], lits![2, -4, 5], lits![2, 4, -5], lits![-2, 4, 5]],
vec![lits![-1, -2, -3, -4], lits![-1, -2, 3, 4], lits![-1, 2, -3, 4], lits![-1, 2, 3, -4], lits![1, -2, -3, 4], lits![1, -2, 3, -4], lits![1, 2, -3, -4], lits![1, 2, 3, 4]]
);
// Regression test: negated XOR (into equiv)
assert_enc_sol!(
TseitinEncoder,
2,
&Formula::Not(Box::new(Formula::Xor(vec![
Formula::Atom(1.into()),
Formula::Atom(2.into()),
])))
=> vec![lits![-1, 2,], lits![1,-2]],
vec![lits![-1, -2],lits![1, 2]]
);
// Regression test: negated XOR (negated args)
assert_sol!(
TseitinEncoder,
3,
&Formula::Not(Box::new(Formula::Xor(vec![
Formula::Atom(1.into()),
Formula::Atom(2.into()),
Formula::Atom(3.into()),
])))
=> vec![lits![-1, -2, -3], lits![1, 2, -3], lits![-1, 2, 3], lits![1, -2, 3]]
);
// Regression test: negated XOR (negated binding)
assert_sol!(
TseitinEncoder,
4,
&Formula::Not(Box::new(Formula::Xor(vec![
Formula::Atom(1.into()),
Formula::Atom(2.into()),
Formula::Atom(3.into()),
Formula::Atom(4.into()),
])))
=> vec![
lits![-1, -2, -3, -4],
lits![1, 2, -3, -4],
lits![1, -2, 3, -4],
lits![1, -2, -3, 4],
lits![-1, 2, 3, -4],
lits![-1, 2, -3, 4],
lits![-1, -2, 3, 4],
lits![1, 2, 3, 4]
]
);
}

#[test]
Expand Down
Loading