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
impl ConstraintWeights
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty set of weights, under which every constraint keeps multiplier 1.
Sourcepub fn set(
&mut self,
constraint: impl Into<String>,
multiplier: u32,
) -> &mut Self
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.
Sourcepub fn remove(&mut self, constraint: &str) -> Option<u32>
pub fn remove(&mut self, constraint: &str) -> Option<u32>
Removes the override for constraint, returning its previous multiplier.
Sourcepub fn get(&self, constraint: &str) -> Option<u32>
pub fn get(&self, constraint: &str) -> Option<u32>
Returns the explicit multiplier for constraint, or None when it is not listed.
Sourcepub fn multiplier_for(&self, constraint: &ConstraintRef) -> u32
pub fn multiplier_for(&self, constraint: &ConstraintRef) -> u32
Returns the multiplier for constraint, 1 when it is not listed.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, u32)>
pub fn iter(&self) -> impl Iterator<Item = (&str, u32)>
Iterates over (constraint full name, multiplier) pairs in name order.
Sourcepub fn validate<'a>(
&self,
known: impl IntoIterator<Item = &'a ConstraintRef>,
) -> Result<(), ConstraintWeightError>
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.