Predicate

Struct Predicate 

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

System requirements expressed as the inequality ax≤ b.

See the predicate module for more information on the semantics of this data type.

Implementations§

Source§

impl Predicate

Source

pub fn new() -> Self

Create a new empty predicate equivalent to 0 ≤ 0.

§Examples
use banquo::Predicate;
let mut p = Predicate::new();
Source

pub fn get(&self, name: &str) -> Option<f64>

Return a variable coefficient from the left-hand side of the inequality if it has been set, otherwise return None.

§Examples
use banquo::Predicate;

let p = Predicate::from([
    ("x", 1.0), ("y", 3.0), ("z", 2.0),
]);

p.get("x");  // Some(3.0)
p.get("a");  // None
Source

pub fn constant(&self) -> f64

Return the constant from the right-hand side of the inequality.

§Examples
use banquo::Predicate;

let p = Predicate::from([
    (None, 5.0), (Some("x"), 1.0), (Some("y"), 2.0), (None, 10.0),
]);

p.constant();  // 15
Source§

impl Predicate

Source

pub fn coefficients(&self) -> Coefficients<'_>

Create an iterator over the coefficient terms of the Predicate.

A coefficient term is a term that has a variable. This function does not make any guarantees about the order of iteration of the terms.

§Example
let p = banquo::predicate!{ x + 2.0 * y + 3.3 * z <= 4.2 };

for (name, coefficient) in p.coefficients() {
    // ...
}
Source§

impl Predicate

Source

pub fn evaluate_state<State>( &self, state: &State, ) -> Result<f64, EvaluationError>
where State: VariableSet,

Evaluate a system state into a robustness value.

The successful output of this function is a f64 value representing the distance of the state from making the inequality represented by the Predicate false. A positive output value indicates that the inequality was not violated, while a negative value indicates the inequality was violated. The unsuccessful output of this function is a [PredicateError] which indicates the nature of the evaluation error.

Any value that implements the VariableSet trait can be evaluated with this method.

§Examples
use std::collections::HashMap;

use banquo::predicate::{Predicate, Term};

let state = HashMap::from([
     ("x", 1.0), ("y", 3.0), ("z", f64::NAN),
]);

// BTreeMap also implements the VariableSet trait
// let state = BTreeMap::from([
//     ("x", 1.0), ("y", 3.0), ("z", f64::NAN),
// ]);

let p = Predicate::from([
    Term::from(("x", 1.0)), Term::from(("y", 2.0)), Term::from(10.0)
]);

p.evaluate_state(&state);  // Ok -> 3.0

let p = Predicate::from([
    Term::from(("x", 1.0)), Term::from(("a", 2.0)), Term::from(10.0)
]);

p.evaluate_state(&state);  // Error -> Missing variable "a"

let p = Predicate::from([
    Term::from(("x", 1.0)), Term::from(("z", 2.0)), Term::from(10.0)
]);

p.evaluate_state(&state);  // Error -> Variable "z" has NaN value

let p = Predicate::from([
    Term::from(("x", 1.0)), Term::from(("y", f64::NAN)), Term::from(10.0)
]);

p.evaluate_state(&state);  // Error -> Variable "y" has NaN coefficient

Trait Implementations§

Source§

impl<T> AddAssign<T> for Predicate
where T: Into<Term>,

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl Clone for Predicate

Source§

fn clone(&self) -> Predicate

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 Predicate

Source§

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

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

impl Default for Predicate

Source§

fn default() -> Predicate

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

impl<State> Formula<State> for Predicate
where State: VariableSet,

Evaluate a Trace of state values by evaluating each state individually.

Source§

type Metric = f64

The output metric from this formula
Source§

type Error = FormulaError

The type of error that may be produced during evaluation
Source§

fn evaluate( &self, trace: &Trace<State>, ) -> Result<Trace<Self::Metric>, Self::Error>

Evaluate a given trace of system states into either a trace of metric values or an error.
Source§

impl<T, const N: usize> From<[T; N]> for Predicate
where T: Into<Term>,

Source§

fn from(terms: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for Predicate
where T: Into<Term>,

Source§

fn from_iter<I>(terms: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl Index<&str> for Predicate

Source§

fn index(&self, index: &str) -> &Self::Output

Returns a reference to a coefficient in the predicate

§Panics

Panics if the variable does not have a coefficient set

Source§

type Output = f64

The returned type after indexing.
Source§

impl Neg for Predicate

Source§

type Output = Predicate

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Predicate

Source§

fn eq(&self, other: &Predicate) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> SubAssign<T> for Predicate
where T: Into<Term>,

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl StructuralPartialEq for Predicate

Auto Trait Implementations§

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> 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> 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.