Skip to main content

ConstraintWeights

Struct ConstraintWeights 

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

Per-constraint multipliers keyed by ConstraintRef::full_name().

A constraint that is not listed keeps multiplier 1, its coded behavior. Multiplier 0 disables the constraint and n scales its contribution n times. An override applies to every terminal carrying that constraint name.

§Example

use solverforge_core::{ConstraintRef, ConstraintWeights};

let mut weights = ConstraintWeights::new();
weights.set("Travel time", 10).set("Overtime", 0);

assert_eq!(weights.get("Travel time"), Some(10));
assert_eq!(weights.multiplier_for(&ConstraintRef::new("", "Overtime")), 0);
assert_eq!(weights.multiplier_for(&ConstraintRef::new("", "Unlisted")), 1);

let known = [
    ConstraintRef::new("", "Travel time"),
    ConstraintRef::new("", "Overtime"),
];
assert!(weights.validate(&known).is_ok());

weights.set("Travle time", 2);
let error = weights.validate(&known).unwrap_err();
assert!(error.to_string().contains("did you mean \"Travel time\""));

Implementations§

Source§

impl ConstraintWeights

Source

pub fn new() -> Self

Creates an empty set of weights, under which every constraint keeps multiplier 1.

Source

pub fn set( &mut self, constraint: impl Into<String>, multiplier: u32, ) -> &mut Self

Sets the multiplier for the constraint whose full name is constraint, replacing any previous value.

The multiplier is only range-checked by validate.

Source

pub fn remove(&mut self, constraint: &str) -> Option<u32>

Removes the override for constraint, returning its previous multiplier.

Source

pub fn get(&self, constraint: &str) -> Option<u32>

Returns the explicit multiplier for constraint, or None when it is not listed.

Source

pub fn multiplier_for(&self, constraint: &ConstraintRef) -> u32

Returns the multiplier for constraint, 1 when it is not listed.

Source

pub fn is_empty(&self) -> bool

Returns true when no constraint has an override.

Source

pub fn len(&self) -> usize

Returns the number of constraints with an override.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, u32)>

Iterates over (constraint full name, multiplier) pairs in name order.

Source

pub fn validate<'a>( &self, known: impl IntoIterator<Item = &'a ConstraintRef>, ) -> Result<(), ConstraintWeightError>

Checks every listed name against the known constraints and every multiplier against MAX_CONSTRAINT_WEIGHT_MULTIPLIER.

Names are checked in sorted order, so the reported error is deterministic.

Trait Implementations§

Source§

impl Clone for ConstraintWeights

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ConstraintWeights

Source§

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

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

impl Default for ConstraintWeights

Source§

fn default() -> Self

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

impl Eq for ConstraintWeights

Source§

impl PartialEq for ConstraintWeights

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ConstraintWeights

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.