Skip to main content

State

Struct State 

Source
pub struct State { /* private fields */ }
Expand description

The State is the container of variables and propagators.

State implements Clone, and cloning the State will create a fresh copy of the State. If the State is large, this may be extremely expensive.

Implementations§

Source§

impl State

Operations to create .

Source

pub fn new_constraint_tag(&mut self) -> ConstraintTag

Create a new ConstraintTag.

Source

pub fn new_literal(&mut self, name: Option<Arc<str>>) -> Literal

Creates a new Boolean (0-1) variable.

The name is used in solver traces to identify individual domains. They are required to be unique. If the state already contains a domain with the given name, then this function will panic.

Creation of new Literals is not influenced by the current checkpoint of the state. If a Literal is created at a non-zero checkpoint, then it will not ‘disappear’ when backtracking past the checkpoint where the domain was created.

Source

pub fn new_interval_variable( &mut self, lower_bound: i32, upper_bound: i32, name: Option<Arc<str>>, ) -> DomainId

Creates a new interval variable with the given lower and upper bound.

The name is used in solver traces to identify individual domains. They are required to be unique. If the state already contains a domain with the given name, then this function will panic.

Creation of new domains is not influenced by the current checkpoint of the state. If a domain is created at a non-zero checkpoint, then it will not ‘disappear’ when backtracking past the checkpoint where the domain was created.)

Source

pub fn new_sparse_variable( &mut self, values: Vec<i32>, name: Option<String>, ) -> DomainId

Creates a new sparse domain with the given values.

Note that this is implemented as an interval domain with explicit holes in the domain. For very sparse domains, this can result in a high memory overhead.

For more information on creation of domains, see State::new_interval_variable.

Source§

impl State

Operations to retrieve information about values

Source

pub fn lower_bound<Var>(&self, variable: Var) -> i32
where Var: IntegerVariable,

Returns the lower-bound of the given variable.

Source

pub fn upper_bound<Var>(&self, variable: Var) -> i32
where Var: IntegerVariable,

Returns the upper-bound of the given variable.

Source

pub fn contains<Var>(&self, variable: Var, value: i32) -> bool
where Var: IntegerVariable,

Returns whether the given variable contains the provided value.

Source

pub fn fixed_value<Var>(&self, variable: Var) -> Option<i32>
where Var: IntegerVariable,

If the given variable is fixed, then Some containing the assigned value is returned. Otherwise, None is returned.

Source

pub fn truth_value(&self, predicate: Predicate) -> Option<bool>

Returns the truth value of the provided Predicate.

If the Predicate is assigned in the current State then Some containing whether the Predicate is satisfied or falsified is returned. Otherwise, None is returned.

Source

pub fn get_checkpoint_for_predicate( &self, predicate: Predicate, ) -> Option<usize>

If the provided Predicate is satisfied then it returns Some containing the checkpoint at which the Predicate became satisfied. Otherwise, None is returned.

Source

pub fn get_literal_value(&self, literal: Literal) -> Option<bool>

Returns the truth value of the provided Literal.

If the Literal is assigned in the current State then Some containing whether the Literal is satisfied or falsified is returned. Otherwise, None is returned.

Source

pub fn get_checkpoint(&self) -> usize

Returns the number of created checkpoints.

Source§

impl State

Operations for retrieving information about trail

Source

pub fn is_on_trail(&self, predicate: Predicate) -> bool

Returns whether the provided Predicate is explicitly on the trail.

For example, if we post the Predicate [x >= v], then the predicate [x >= v - 1] is not explicity on the trail.

Source

pub fn trail_position(&self, predicate: Predicate) -> Option<usize>

Returns whether the trail position of the provided Predicate.

Source§

impl State

Operations for adding constraints.

Source

pub fn add_propagator<Constructor>( &mut self, constructor: Constructor, ) -> PropagatorHandle<<Constructor as PropagatorConstructor>::PropagatorImpl>
where Constructor: PropagatorConstructor, <Constructor as PropagatorConstructor>::PropagatorImpl: 'static,

Add a new propagator to the State. The constructor for that propagator should subscribe to the appropriate domain events so that the propagator is called when necessary.

While the propagator is added to the queue for propagation, this function does not trigger a round of propagation. An explicit call to State::propagate_to_fixed_point is necessary to run the new propagator for the first time.

Source

pub fn add_inference_checker( &mut self, inference_code: InferenceCode, checker: Box<dyn InferenceChecker<Predicate>>, )

Add an inference checker to the state.

The inference checker will be used to check propagations performed during Self::propagate_to_fixed_point, if the check-propagations feature is enabled.

Multiple inference checkers may be added for the same inference code. In that case, if any checker accepts the inference, the inference is accepted.

Source§

impl State

Operations for retrieving propagators.

Source

pub fn get_propagator<P>(&self, handle: PropagatorHandle<P>) -> Option<&P>
where P: Propagator,

Get a reference to the propagator identified by the given handle.

For an exclusive reference, use State::get_propagator_mut.

Source

pub fn get_propagator_mut<P>( &mut self, handle: PropagatorHandle<P>, ) -> Option<&mut P>
where P: Propagator,

Get an exclusive reference to the propagator identified by the given handle.

Source§

impl State

Operations for modifying the state.

Source

pub fn post(&mut self, predicate: Predicate) -> Result<bool, EmptyDomain>

Apply a Predicate to the State.

Returns true if a change to a domain occured, and false if the given Predicate was already true.

If a domain becomes empty due to this operation, an EmptyDomain error is returned.

This method does not perform any propagation. For that, an explicit call to State::propagate_to_fixed_point is required. This allows the posting of multiple predicates before the entire propagation engine is invoked.

A call to State::restore_to that goes past the checkpoint at which a Predicate was posted will undo the effect of that Predicate. See the documentation of State::new_checkpoint and State::restore_to for more information.

Source

pub fn new_checkpoint(&mut self)

Create a checkpoint of the current State, that can be returned to with State::restore_to.

The current checkpoint can be retrieved using the method State::get_checkpoint.

If the state is not at fixed-point, then this method will panic.

§Example
use pumpkin_core::predicate;
use pumpkin_core::state::State;

let mut state = State::default();
let variable = state.new_interval_variable(1, 10, Some("x1".into()));

assert_eq!(state.get_checkpoint(), 0);

state.new_checkpoint();

assert_eq!(state.get_checkpoint(), 1);

state
    .post(predicate![variable <= 5])
    .expect("The lower bound is 1 so no conflict");
assert_eq!(state.upper_bound(variable), 5);

state.restore_to(0);

assert_eq!(state.get_checkpoint(), 0);
assert_eq!(state.upper_bound(variable), 10);
Source

pub fn restore_to(&mut self, checkpoint: usize) -> Vec<(DomainId, i32)>

Restore to the given checkpoint and return the DomainIds which were fixed before restoring, with their assigned values.

If the provided checkpoint is equal to the current checkpoint, this is a no-op. If the provided checkpoint is larger than the current checkpoint, this method will panic.

See State::new_checkpoint for an example.

Source

pub fn propagate_to_fixed_point(&mut self) -> Result<(), Conflict>

Performs fixed-point propagation using the propagators defined in the State.

The posted Predicates (using State::post) and added propagators (using State::add_propagator) cause propagators to be enqueued when the events that they have subscribed to are triggered. As propagation causes more changes to be made, more propagators are enqueued. This continues until applying all (enqueued) propagators leads to no more domain changes.

It could be that the current State implies a conflict by propagation. In that case, an error with Conflict is returned.

Once the State is conflicting, then the only operation that is defined is State::restore_to. All other operations and queries on the state are unspecified.

Source§

impl State

Source

pub fn get_propagation_reason( &mut self, predicate: Predicate, reason_buffer: &mut (impl Extend<Predicate> + AsRef<[Predicate]>), current_nogood: CurrentNogood<'_>, ) -> Option<usize>

Get the reason for a predicate being true and store it in reason_buffer; additionally, if the provided Predicate is explicitly on the trail, this method will return the corresponding trail index.

The provided current_nogood can be used by the propagator to provide a different reason; use CurrentNogood::empty otherwise.

All the predicates in the returned slice will evaluate to true.

If the provided predicate is not true, then this method will panic.

Source§

impl State

Source

pub fn get_domains(&mut self) -> Domains<'_>

Source

pub fn get_propagation_context(&mut self) -> PropagationContext<'_>

Trait Implementations§

Source§

impl Clone for State

Source§

fn clone(&self) -> State

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for State

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for State

Source§

fn default() -> State

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for State

§

impl !RefUnwindSafe for State

§

impl !Send for State

§

impl !Sync for State

§

impl Unpin for State

§

impl !UnwindSafe for State

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V