-
Notifications
You must be signed in to change notification settings - Fork 23
feat(pumpkin-core): Introduce runtime consistency verification of propagators #374
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
Open
maartenflippo
wants to merge
10
commits into
main
Choose a base branch
from
feat/propagation-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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2e9cba5
Initial implementation of consistency checkers
maartenflippo aacd712
Run the consistency checkers when enabled
maartenflippo ece98c7
Implement bound consistency checker
maartenflippo 5013527
Give access to domains in witness generator
maartenflippo c7a3b9c
Implement checker for absolute value
maartenflippo 5fbad25
Remove redundant min
maartenflippo fd04f2e
Include the value to support in linear witness generator
maartenflippo 0e7329e
Don't add to supported values of the domain being witnessed
maartenflippo 5c7a9d1
Proper mechanism to add to the scope
maartenflippo 8b143be
Update todo notes
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
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 |
|---|---|---|
|
|
@@ -14,6 +14,11 @@ use crate::engine::predicates::predicate_constructor::PredicateConstructor; | |
| use crate::engine::variables::DomainId; | ||
| use crate::engine::variables::IntegerVariable; | ||
| use crate::math::num_ext::NumExt; | ||
| use crate::propagation::LocalId; | ||
| use crate::propagation::checkers::ScopeBuilder; | ||
| use crate::propagation::checkers::SingleVariableAssignment; | ||
| use crate::propagation::checkers::ValueToWitness; | ||
| use crate::propagation::checkers::WitnessedVariable; | ||
|
|
||
| /// Models the constraint `y = ax + b`, by expressing the domain of `y` as a transformation of the | ||
| /// domain of `x`. | ||
|
|
@@ -406,6 +411,24 @@ enum Rounding { | |
| Down, | ||
| } | ||
|
|
||
| impl<Inner: WitnessedVariable> WitnessedVariable for AffineView<Inner> { | ||
| fn add_to_scope(&self, scope: &mut ScopeBuilder, local_id: LocalId) { | ||
| self.inner.add_to_scope(scope, local_id); | ||
| } | ||
|
|
||
| fn unpack_value(&self, value: ValueToWitness) -> i32 { | ||
| let inner_value = self.inner.unpack_value(value); | ||
|
|
||
| self.map(inner_value) | ||
| } | ||
|
|
||
| fn assign(&self, value: i32) -> SingleVariableAssignment { | ||
| assert_eq!((value - self.offset) % self.scale, 0); | ||
| let inner_assignment = (value - self.offset) / self.scale; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the |
||
| self.inner.assign(inner_assignment) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
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
34 changes: 34 additions & 0 deletions
34
pumpkin-crates/core/src/propagation/checkers/bound_consistency_checker.rs
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,34 @@ | ||
| use pumpkin_checking::InferenceChecker; | ||
|
|
||
| use crate::predicates::Predicate; | ||
| use crate::propagation::Domains; | ||
| use crate::propagation::checkers::ConsistencyChecker; | ||
| use crate::propagation::checkers::Scope; | ||
| use crate::propagation::checkers::WitnessGenerator; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct BoundConsistencyChecker<C> { | ||
| witness_generator: C, | ||
| } | ||
|
|
||
| impl<C> BoundConsistencyChecker<C> { | ||
| pub fn new(witness_generator: C) -> Self { | ||
| BoundConsistencyChecker { witness_generator } | ||
| } | ||
| } | ||
|
|
||
| impl<C> ConsistencyChecker for BoundConsistencyChecker<C> | ||
| where | ||
| C: WitnessGenerator + InferenceChecker<Predicate> + Clone, | ||
| { | ||
| fn check_consistency(&self, domains: Domains<'_>, scope: &Scope) -> bool { | ||
| super::assert_consistency( | ||
| domains, | ||
| scope, | ||
| &self.witness_generator, | ||
| super::Consistency::Bounds, | ||
| ); | ||
|
|
||
| true | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
pumpkin-crates/core/src/propagation/checkers/consistency_checker.rs
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::propagation::Domains; | ||
| use crate::propagation::checkers::Scope; | ||
|
|
||
| /// A consistency checker ensures that a propagator is at a certain level of consistency. | ||
| /// | ||
| /// Given a current set of domains and a scope, the checker identifies whether the desired | ||
| /// consistency level is reached. | ||
| pub trait ConsistencyChecker: Debug + DynClone { | ||
| /// Returns `true` if the variables in `scope` are at the required consistency in | ||
| /// `domains`. | ||
| fn check_consistency(&self, domains: Domains<'_>, scope: &Scope) -> bool; | ||
| } | ||
|
|
||
| /// Wrapper around `Box<dyn ConsistencyChecker>` that implements [`Clone`]. | ||
| #[derive(Debug)] | ||
| pub struct BoxedConsistencyChecker(Box<dyn ConsistencyChecker>); | ||
|
|
||
| impl Clone for BoxedConsistencyChecker { | ||
| fn clone(&self) -> Self { | ||
| BoxedConsistencyChecker(dyn_clone::clone_box(&*self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl From<Box<dyn ConsistencyChecker>> for BoxedConsistencyChecker { | ||
| fn from(value: Box<dyn ConsistencyChecker>) -> Self { | ||
| BoxedConsistencyChecker(value) | ||
| } | ||
| } | ||
|
|
||
| impl BoxedConsistencyChecker { | ||
| /// See [`ConsistencyChecker::check_consistency`]. | ||
| pub fn check_consistency(&self, domains: Domains<'_>, scope: &Scope) -> bool { | ||
| self.0.check_consistency(domains, scope) | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
pumpkin-crates/core/src/propagation/checkers/domain_consistency_checker.rs
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,34 @@ | ||
| use pumpkin_checking::InferenceChecker; | ||
|
|
||
| use crate::predicates::Predicate; | ||
| use crate::propagation::Domains; | ||
| use crate::propagation::checkers::ConsistencyChecker; | ||
| use crate::propagation::checkers::Scope; | ||
| use crate::propagation::checkers::WitnessGenerator; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct DomainConsistencyChecker<C> { | ||
| witness_generator: C, | ||
| } | ||
|
|
||
| impl<C> DomainConsistencyChecker<C> { | ||
| pub fn new(witness_generator: C) -> Self { | ||
| DomainConsistencyChecker { witness_generator } | ||
| } | ||
| } | ||
|
|
||
| impl<C> ConsistencyChecker for DomainConsistencyChecker<C> | ||
| where | ||
| C: WitnessGenerator + InferenceChecker<Predicate> + Clone, | ||
| { | ||
| fn check_consistency(&self, domains: Domains<'_>, scope: &Scope) -> bool { | ||
| super::assert_consistency( | ||
| domains, | ||
| scope, | ||
| &self.witness_generator, | ||
| super::Consistency::Domain, | ||
| ); | ||
|
|
||
| true | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add this directly in
constructor.add_inference? It seems like this is doing the same thing as for theInferenceChecker