pub struct Assignment {
pub pos: HashSet<Variable>,
pub neg: HashSet<Variable>,
}
Expand description
An Assignment
stores a set of positive and negative Variable
s.
Assignment
stores all variables in HashSet
s, this allows for a fast
evaluation of formulas. This might introduce additional costs when creating
an assignment. An opposite approach has the Model
data-structure. It also
stores an assignment, but uses vectors instead of sets. Making it easier to
create, but slower to use.
§Conversion to Model
and vice-versa
Depending of your use case it might be better to have a Model
or an
Assignment
. Both data-structures implement the From
trait, such that you
can easily swap between both.
Convert from Model
to Assignment
:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let model = Model::new(vec![a], vec![b]);
let assignment = Assignment::from(model);
/// Convert from Assignment
to Model
:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment = Assignment::from_variables(&[a], &[b]);
let model = Model::from(assignment);
Fields§
§pos: HashSet<Variable>
Set of all positive variables of this assignment.
neg: HashSet<Variable>
Set of all negative variables of this assignment.
Implementations§
Source§impl Assignment
impl Assignment
Sourcepub const fn new(pos: HashSet<Variable>, neg: HashSet<Variable>) -> Self
pub const fn new(pos: HashSet<Variable>, neg: HashSet<Variable>) -> Self
Creates a new assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let pos = HashSet::from([a]);
let neg = HashSet::from([b]);
let assignment = Assignment::new(pos, neg);
Sourcepub fn from_variables(pos: &[Variable], neg: &[Variable]) -> Self
pub fn from_variables(pos: &[Variable], neg: &[Variable]) -> Self
Creates a new assignment from slices.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment = Assignment::from_variables(&[a], &[b]);
Sourcepub fn from_lit(lit: Literal) -> Self
pub fn from_lit(lit: Literal) -> Self
Converts a literal into an assignment.
A positive literal is added to the positive variables, and a negative literal to the negative variables.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.lit("a", true);
let b = f.lit("b", false);
let assignment1 = Assignment::from_lit(a);
let assignment2 = Assignment::from_lit(b);
assert!(assignment1.contains_pos(a.variable()));
assert!(assignment2.contains_neg(b.variable()));
Sourcepub fn from_literals(literals: &[Literal]) -> Self
pub fn from_literals(literals: &[Literal]) -> Self
Creates an assignment from a single vector of literals.
All positive literals are added to the positive variables, and all negative literals to the negative variables.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.lit("a", true);
let b = f.lit("b", false);
let assignment = Assignment::from_literals(&[a, b]);
assert!(assignment.contains_pos(a.variable()));
assert!(assignment.contains_neg(b.variable()));
Sourcepub fn from_set(set: BTreeSet<Literal>) -> Self
pub fn from_set(set: BTreeSet<Literal>) -> Self
Creates an assignment from a single set of literals.
All positive literals are added to the positive variables, and all negative literals to the negative variables.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.lit("a", true);
let b = f.lit("b", false);
let set = BTreeSet::from([a, b]);
let assignment = Assignment::from_set(set);
assert!(assignment.contains_pos(a.variable()));
assert!(assignment.contains_neg(b.variable()));
Sourcepub fn from_names(
pos: &[&str],
neg: &[&str],
f: &FormulaFactory,
) -> Result<Self, String>
pub fn from_names( pos: &[&str], neg: &[&str], f: &FormulaFactory, ) -> Result<Self, String>
Creates a new assignment from slices of names.
Uses names of already existing variables and adds those variables to the assignment. If a name has no existing variable in the formula factory, the function will return an error.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment = Assignment::from_names(&["a"], &["b"], &f)?;
assert!(assignment.contains_pos(a));
assert!(assignment.contains_neg(b));
Sourcepub const fn pos(&self) -> &HashSet<Variable>
pub const fn pos(&self) -> &HashSet<Variable>
Returns all positive variables of this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let pos = HashSet::from([a]);
let neg = HashSet::from([b]);
let assignment = Assignment::new(pos.clone(), neg);
assert_eq!(assignment.pos(), &pos);
Sourcepub const fn neg(&self) -> &HashSet<Variable>
pub const fn neg(&self) -> &HashSet<Variable>
Returns all negative variables of this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let pos = HashSet::from([a]);
let neg = HashSet::from([b]);
let assignment = Assignment::new(pos, neg.clone());
assert_eq!(assignment.neg(), &neg);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the overall number of positive and negative variables.
A variable added as positive and negative is counted twice.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if there is no variable in this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment1 = Assignment::from_variables(&[a], &[b]);
let assignment2 = Assignment::from_variables(&[], &[]);
assert_eq!(assignment1.is_empty(), false);
assert_eq!(assignment2.is_empty(), true);
Sourcepub fn contains_pos(&self, var: Variable) -> bool
pub fn contains_pos(&self, var: Variable) -> bool
Returns true
if the given variable is a positive variable in this
assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let c = f.var("c");
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.contains_pos(a), true);
assert_eq!(assignment.contains_pos(b), false);
assert_eq!(assignment.contains_pos(c), false);
Sourcepub fn contains_neg(&self, var: Variable) -> bool
pub fn contains_neg(&self, var: Variable) -> bool
Returns true
if the given variable is a negative variable in this
assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let c = f.var("c");
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.contains_neg(a), false);
assert_eq!(assignment.contains_neg(b), true);
assert_eq!(assignment.contains_neg(c), false);
Sourcepub fn contains(&self, lit: Literal) -> bool
pub fn contains(&self, lit: Literal) -> bool
Returns true
if the variable of the given literal is a variable with
the same phase in this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let c = f.var("c");
let lit1 = f.lit("a", true);
let lit2 = f.lit("b", true);
let lit3 = f.lit("c", false);
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.contains(lit1), true);
assert_eq!(assignment.contains(lit2), false);
assert_eq!(assignment.contains(lit3), false);
Sourcepub fn evaluate_lit(&self, lit: Literal) -> bool
pub fn evaluate_lit(&self, lit: Literal) -> bool
Evaluates the given literals on this assignment.
It will return true
, if the literal is positive and it’s variable is
added as positive, or the literal is negative and it’s variable is added
as negative or not contained in this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let c = f.var("c");
let lit1 = f.lit("a", true);
let lit2 = f.lit("b", true);
let lit3 = f.lit("c", false);
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.evaluate_lit(lit1), true);
assert_eq!(assignment.evaluate_lit(lit2), false);
assert_eq!(assignment.evaluate_lit(lit3), true);
Sourcepub fn restrict_lit(&self, lit: Literal) -> EncodedFormula
pub fn restrict_lit(&self, lit: Literal) -> EncodedFormula
Restricts the given literal in this assignment.
It will return a EncodedFormula
, which is either a constant, if the
variable is contained in the assignment, or it is the passed literal as
a formula, if the variable is not in the assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let c = f.var("c");
let lit1 = f.lit("a", true);
let lit2 = f.lit("b", true);
let lit3 = f.lit("c", false);
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.restrict_lit(lit1), EncodedFormula::constant(true));
assert_eq!(assignment.restrict_lit(lit2), EncodedFormula::constant(false));
assert_eq!(assignment.restrict_lit(lit3), EncodedFormula::from(lit3));
Sourcepub fn blocking_clause(
&self,
f: &FormulaFactory,
variables: Option<&[Variable]>,
) -> EncodedFormula
pub fn blocking_clause( &self, f: &FormulaFactory, variables: Option<&[Variable]>, ) -> EncodedFormula
Creates the blocking clause for this assignment.
Sourcepub fn literals(&self) -> Vec<Literal>
pub fn literals(&self) -> Vec<Literal>
Returns a vector of Literal
representing this assignment.
All positive variables are returned as positive literals and all negative variables are returned as negative literals. The vector first consists of all positive literals and then of all negative literals.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let lit1 = f.lit("a", true);
let lit2 = f.lit("b", false);
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.literals(), vec![lit1, lit2]);
Sourcepub fn string_literals<'a>(
&self,
f: &'a FormulaFactory,
) -> Vec<StringLiteral<'a>>
pub fn string_literals<'a>( &self, f: &'a FormulaFactory, ) -> Vec<StringLiteral<'a>>
Returns a vector of StringLiteral
representing this assignment.
All positive variables are returned as positive literals and all negative variables are returned as negative literals. The vector first consists of all positive literals and then of all negative literals.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let lit1 = f.lit("a", true);
let lit2 = f.lit("b", false);
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.string_literals(&f), vec![lit1.to_string_lit(&f), lit2.to_string_lit(&f)]);
Sourcepub fn to_string(&self, f: &FormulaFactory) -> String
pub fn to_string(&self, f: &FormulaFactory) -> String
Creates a string representation of this assignment.
§Examples
Basic usage:
let f = FormulaFactory::new();
let a = f.var("a");
let b = f.var("b");
let assignment = Assignment::from_variables(&[a], &[b]);
assert_eq!(assignment.to_string(&f), "Assignment{pos=[a], neg=[b]}");
Trait Implementations§
Source§impl Clone for Assignment
impl Clone for Assignment
Source§fn clone(&self) -> Assignment
fn clone(&self) -> Assignment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Assignment
impl Debug for Assignment
Source§impl From<Assignment> for Model
impl From<Assignment> for Model
Source§fn from(assignment: Assignment) -> Self
fn from(assignment: Assignment) -> Self
Source§impl Hash for Assignment
impl Hash for Assignment
Source§impl PartialEq for Assignment
impl PartialEq for Assignment
impl Eq for Assignment
Auto Trait Implementations§
impl Freeze for Assignment
impl RefUnwindSafe for Assignment
impl Send for Assignment
impl Sync for Assignment
impl Unpin for Assignment
impl UnwindSafe for Assignment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.