-
Notifications
You must be signed in to change notification settings - Fork 32
feat(pumpkin-solver): Implement consistency checker infrastructure #449
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
Draft
maartenflippo
wants to merge
13
commits into
main
Choose a base branch
from
feat/consistency-checkers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2be6e8d
Started reworking multiplication for consistency checkers.
maartenflippo 4ca596a
feat(pumpkin-solver): Implement consistency checker infrastructure
maartenflippo f7c9b69
Fix multiline test command syntax
maartenflippo 11511e2
Implement domain consistency checker
maartenflippo 70a2c31
Avoid copying domains when doing domain consistency checks
maartenflippo 1d32565
Refactor PropagatorConstructor to remove `add_inference_checkers`
maartenflippo a9324b9
Fix conditional compilation errors and split up reified propagator
maartenflippo 5fc57f9
Fix formatting
maartenflippo dee4887
Implement the checkers for nogood propagator
maartenflippo 1c9f258
Cleanup code
maartenflippo 893989d
Various cleanup
maartenflippo 2d999b7
Rename 'ConsistencyChecker' to 'RetentionChecker'
maartenflippo 2bbdd59
Explicitly introduce the concept of 'PropagationChecker'
maartenflippo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| mod propagation_checker; | ||
| mod retention_checker; | ||
| mod scope; | ||
| mod self_disabling; | ||
| mod store; | ||
| mod strong_retention_checker; | ||
| pub mod support; | ||
|
|
||
| pub use propagation_checker::*; | ||
| pub use retention_checker::*; | ||
| pub use scope::*; | ||
| pub use self_disabling::*; | ||
| pub use store::*; | ||
| pub use strong_retention_checker::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| use pumpkin_checking::BoxedChecker; | ||
| use pumpkin_checking::VariableState; | ||
|
|
||
| use crate::predicates::Predicate; | ||
| use crate::propagation::Domains; | ||
| use crate::propagation::ReadDomains; | ||
| use crate::variables::DomainId; | ||
|
|
||
| /// Tests whether an inference is correct given the solver state. | ||
| /// | ||
| /// An inference is correct when: | ||
| /// 1. All premises are satisfied. | ||
| /// 2. The conjunction of the premises and negation of the consequent is consistent. | ||
| /// 3. The consequent is logically entailed given the inference code. | ||
| #[derive(Clone, Debug)] | ||
| pub struct PropagationChecker { | ||
| inference_checker: BoxedChecker<Predicate>, | ||
| } | ||
|
|
||
| impl PropagationChecker { | ||
| /// Create a new propagation checker given an inference checker and inference code. | ||
| pub fn new(inference_checker: BoxedChecker<Predicate>) -> PropagationChecker { | ||
| PropagationChecker { inference_checker } | ||
| } | ||
|
|
||
| /// Run the propagation checker for the given inference. | ||
| pub fn check( | ||
| &self, | ||
| premises: &[Predicate], | ||
| consequent: Option<Predicate>, | ||
| domains: Domains<'_>, | ||
| ) -> Result<(), InvalidInference> { | ||
| let premises_satisfied = premises | ||
| .iter() | ||
| .all(|&premise| domains.evaluate_predicate(premise) == Some(true)); | ||
|
|
||
| if !premises_satisfied { | ||
| return Err(InvalidInference::UnsatisfiedPremises); | ||
| } | ||
|
|
||
| let variable_state = | ||
| VariableState::prepare_for_conflict_check(premises.iter().copied(), consequent) | ||
| .map_err(InvalidInference::InconsistentPredicates)?; | ||
|
|
||
| if self | ||
| .inference_checker | ||
| .check(variable_state, &premises, consequent.as_ref()) | ||
| { | ||
| Ok(()) | ||
| } else { | ||
| Err(InvalidInference::Unsound) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum InvalidInference { | ||
| /// Not all premises are true given the current state. | ||
| UnsatisfiedPremises, | ||
| /// The predicates that make up the inference are trivially inconsistent. | ||
| InconsistentPredicates(DomainId), | ||
| /// Cannot establish that the inference is sound. | ||
| Unsound, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| use std::fmt::Debug; | ||
|
|
||
| use dyn_clone::DynClone; | ||
|
|
||
| use crate::checkers::Scope; | ||
| use crate::propagation::Domains; | ||
|
|
||
| /// A runtime verifier that determines whether domains are sufficiently pruned. | ||
| pub trait RetentionChecker: Debug + DynClone { | ||
| /// Ensure the domains do not have values that should have been removed by propagation. | ||
| /// | ||
| /// Returns `true` if the domains are sufficiently pruned, or `false` otherwise. | ||
| fn check_retention(&mut self, scope: &Scope, domains: Domains<'_>) -> bool; | ||
| } | ||
|
|
||
| /// Wrapper around `Box<dyn RetentionChecker>` that implements [`Clone`]. | ||
| #[derive(Debug)] | ||
| pub struct BoxedRetentionChecker(Box<dyn RetentionChecker>); | ||
|
|
||
| impl Clone for BoxedRetentionChecker { | ||
| fn clone(&self) -> Self { | ||
| BoxedRetentionChecker(dyn_clone::clone_box(&*self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl<T> From<T> for BoxedRetentionChecker | ||
| where | ||
| T: RetentionChecker + 'static, | ||
| { | ||
| fn from(value: T) -> Self { | ||
| BoxedRetentionChecker(Box::new(value)) | ||
| } | ||
| } | ||
|
|
||
| impl BoxedRetentionChecker { | ||
| pub fn check_retention(&mut self, scope: &Scope, domains: Domains<'_>) -> bool { | ||
| self.0.check_retention(scope, domains) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use crate::containers::HashMap; | ||
| use crate::propagation::LocalId; | ||
| use crate::variables::DomainId; | ||
|
|
||
| /// The scope of a constraint is the collection of variables involved in the relation. | ||
| #[derive(Clone, Debug, Default)] | ||
| pub struct Scope { | ||
| domains: HashMap<LocalId, DomainId>, | ||
| } | ||
|
|
||
| impl FromIterator<(LocalId, DomainId)> for Scope { | ||
| fn from_iter<T: IntoIterator<Item = (LocalId, DomainId)>>(iter: T) -> Self { | ||
| Scope { | ||
| domains: iter.into_iter().collect(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Scope { | ||
| /// Add a new domain to the scope with the given local id. | ||
| /// | ||
| /// Any previous occurrance of this local id will be overridden. | ||
| pub fn add_domain(&mut self, local_id: LocalId, domain_id: DomainId) { | ||
| let _ = self.domains.insert(local_id, domain_id); | ||
| } | ||
|
|
||
| /// The integer domains in the scope with the [`LocalId`]s they are registered. | ||
| pub fn domains(&self) -> impl ExactSizeIterator<Item = (LocalId, DomainId)> { | ||
| self.domains.iter().map(|(lid, did)| (*lid, *did)) | ||
| } | ||
|
|
||
| /// Returns a copy of this scope with the entry for `local_id` removed. | ||
| pub fn without(&self, local_id: LocalId) -> Scope { | ||
| let mut scope = self.clone(); | ||
| let _ = scope.domains.remove(&local_id); | ||
| scope | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_scope_from_tuple { | ||
| ($($lid_name:ident,$var_name:ident : $ty_name:ident),+) => { | ||
| impl<$($ty_name),+> From<($((LocalId, &$ty_name)),+)> for Scope | ||
| where | ||
| $($ty_name: ScopeItem),+ | ||
| { | ||
| fn from( | ||
| ($(($lid_name, $var_name)),+): ($((LocalId, &$ty_name)),+), | ||
| ) -> Self { | ||
| let mut scope = Scope::default(); | ||
|
|
||
| $($var_name.add_to_scope(&mut scope, $lid_name);)+ | ||
|
|
||
| scope | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| impl_scope_from_tuple!(la,va: VA, lb,vb: VB); | ||
| impl_scope_from_tuple!(la,va: VA, lb,vb: VB, lc,vc: VC); | ||
|
|
||
| pub trait ScopeItem { | ||
| /// Adds self to the given scope with the given [`LocalId`]. | ||
| fn add_to_scope(&self, scope: &mut Scope, local_id: LocalId); | ||
| } | ||
|
|
||
| impl ScopeItem for i32 { | ||
| fn add_to_scope(&self, _: &mut Scope, _: LocalId) { | ||
| // Do nothing | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use std::sync::Arc; | ||
| use std::sync::atomic::AtomicBool; | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use super::RetentionChecker; | ||
| use super::Scope; | ||
| use crate::propagation::Domains; | ||
|
|
||
| /// A [`ConsistencyChecker`] wrapper that skips the inner check when the associated constraint has | ||
| /// been deleted. | ||
| /// | ||
| /// The deletion flag is shared with the constraint owner (e.g. the nogood propagator). Setting the | ||
| /// flag to `true` causes the checker to become a permanent no-op. | ||
| #[derive(Debug, Clone)] | ||
| pub struct SelfDisablingChecker<T> { | ||
| inner: T, | ||
| is_deleted: Arc<AtomicBool>, | ||
| } | ||
|
|
||
| impl<T> SelfDisablingChecker<T> { | ||
| /// Create a new self-disabling checker. | ||
| /// | ||
| /// The deletion flag can be obtained with [`SelfDisablingChecker::deletion_flag`]. | ||
| pub fn new(checker: T) -> Self { | ||
| SelfDisablingChecker { | ||
| inner: checker, | ||
| is_deleted: Arc::new(AtomicBool::new(false)), | ||
| } | ||
| } | ||
|
|
||
| /// The deletion flag for this self-disabling checker. | ||
| pub fn deletion_flag(&self) -> Arc<AtomicBool> { | ||
| Arc::clone(&self.is_deleted) | ||
| } | ||
| } | ||
|
|
||
| impl<T: RetentionChecker + Clone> RetentionChecker for SelfDisablingChecker<T> { | ||
| fn check_retention(&mut self, scope: &Scope, domains: Domains<'_>) -> bool { | ||
| if self.is_deleted.load(Ordering::Relaxed) { | ||
| return true; | ||
| } | ||
| self.inner.check_retention(scope, domains) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.