ConstraintFactory

Struct ConstraintFactory 

Source
pub struct ConstraintFactory<S, Sc: Score> { /* private fields */ }
Expand description

Factory for creating constraint streams.

ConstraintFactory is parameterized by the solution type S and score type Sc. It serves as the entry point for defining constraints using the fluent API.

§Example

use solverforge_scoring::stream::ConstraintFactory;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::score::SimpleScore;

#[derive(Clone)]
struct Solution {
    values: Vec<Option<i32>>,
}

let factory = ConstraintFactory::<Solution, SimpleScore>::new();

let constraint = factory
    .for_each(|s: &Solution| &s.values)
    .filter(|v: &Option<i32>| v.is_none())
    .penalize(SimpleScore::of(1))
    .as_constraint("Unassigned");

let solution = Solution { values: vec![Some(1), None, None] };
assert_eq!(constraint.evaluate(&solution), SimpleScore::of(-2));

Implementations§

Source§

impl<S, Sc> ConstraintFactory<S, Sc>
where S: Send + Sync + 'static, Sc: Score + 'static,

Source

pub fn new() -> Self

Creates a new constraint factory.

Source

pub fn for_each<A, E>( self, extractor: E, ) -> UniConstraintStream<S, A, E, TrueFilter, Sc>
where A: Clone + Send + Sync + 'static, E: Fn(&S) -> &[A] + Send + Sync,

Creates a zero-erasure uni-constraint stream over entities extracted from the solution.

The extractor function receives a reference to the solution and returns a slice of entities to iterate over. The extractor type is preserved as a concrete generic for full zero-erasure.

Source

pub fn for_each_unique_pair<A, E, K, KA>( self, extractor: E, joiner: EqualJoiner<KA, KA, K>, ) -> BiConstraintStream<S, A, K, E, KA, TrueFilter, Sc>
where A: Clone + Hash + PartialEq + Send + Sync + 'static, E: Fn(&S) -> &[A] + Send + Sync, K: Eq + Hash + Clone + Send + Sync, KA: Fn(&A) -> K + Send + Sync,

Creates a zero-erasure bi-constraint stream over unique pairs of entities.

This is equivalent to for_each(extractor).join_self(joiner) but provides a more concise API for the common case of self-joins with key-based grouping.

Pairs are ordered (i, j) where i < j to avoid duplicates and self-pairs.

§Example
use solverforge_scoring::stream::{ConstraintFactory, joiner::equal};
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::score::SimpleScore;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct Task { team: u32, priority: u32 }

#[derive(Clone)]
struct Solution { tasks: Vec<Task> }

let factory = ConstraintFactory::<Solution, SimpleScore>::new();

// Penalize when two tasks on the same team conflict
let constraint = factory
    .for_each_unique_pair(
        |s: &Solution| s.tasks.as_slice(),
        equal(|t: &Task| t.team)
    )
    .penalize(SimpleScore::of(1))
    .as_constraint("Team conflict");

let solution = Solution {
    tasks: vec![
        Task { team: 1, priority: 1 },
        Task { team: 1, priority: 2 },  // Same team as first
        Task { team: 2, priority: 1 },
    ],
};

// One pair on same team: (0, 1) = -1 penalty
assert_eq!(constraint.evaluate(&solution), SimpleScore::of(-1));

Trait Implementations§

Source§

impl<S, Sc: Score> Clone for ConstraintFactory<S, Sc>

Source§

fn clone(&self) -> Self

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<S, Sc: Score> Debug for ConstraintFactory<S, Sc>

Source§

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

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

impl<S, Sc> Default for ConstraintFactory<S, Sc>
where S: Send + Sync + 'static, Sc: Score + 'static,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<S, Sc> Freeze for ConstraintFactory<S, Sc>

§

impl<S, Sc> RefUnwindSafe for ConstraintFactory<S, Sc>

§

impl<S, Sc> Send for ConstraintFactory<S, Sc>
where S: Send,

§

impl<S, Sc> Sync for ConstraintFactory<S, Sc>
where S: Sync,

§

impl<S, Sc> Unpin for ConstraintFactory<S, Sc>
where S: Unpin, Sc: Unpin,

§

impl<S, Sc> UnwindSafe for ConstraintFactory<S, Sc>
where S: UnwindSafe, Sc: UnwindSafe,

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.