Skip to main content

Csp

Struct Csp 

Source
pub struct Csp<D: Domain> {
    pub variables: Vec<Variable<D>>,
    /* private fields */
}
Expand description

The main CSP solver struct.

Generic over the domain type D. Build a problem by adding variables and constraints, call finalize() to build the adjacency graph, then solve().

Fields§

§variables: Vec<Variable<D>>

Implementations§

Source§

impl<D: Domain> Csp<D>

Source

pub fn new() -> Self

Create a new empty CSP.

Source

pub fn add_variable(&mut self, domain: D) -> VarId

Add a variable with the given domain. Returns its VarId.

Source

pub fn add_variables(&mut self, domain: &D, count: usize) -> Vec<VarId>

Add multiple variables sharing the same domain. Returns their VarIds.

Source

pub fn add_constraint(&mut self, c: impl Constraint<D> + 'static)

Add a custom constraint (wrapped in the Custom enum variant).

Source

pub fn add_constraint_enum(&mut self, c: ConstraintEnum<D>)

Add a pre-typed constraint enum directly (avoids boxing for built-in types).

Source

pub fn add_soft_constraint(&mut self, c: SoftLambdaConstraint<D>)

Add a soft constraint (contributes penalty cost when violated, never prunes).

Source

pub fn add_not_equal(&mut self, x: VarId, y: VarId)

Add a not-equal constraint (devirtualized fast path).

Source

pub fn add_all_different(&mut self, vars: Vec<VarId>)

Add an all-different constraint (devirtualized fast path).

Source

pub fn add_equals(&mut self, var: VarId, value: D::Value)
where D: 'static,

Fix a variable to a specific value.

Source

pub fn add_less_than(&mut self, x: VarId, y: VarId)
where D: 'static, D::Value: PartialOrd,

Constrain x < y (for Ord-comparable values).

Source

pub fn add_greater_than(&mut self, x: VarId, y: VarId)
where D: 'static, D::Value: PartialOrd,

Constrain x > y (for Ord-comparable values).

Source

pub fn finalize(&mut self)
where D::Value: PartialEq,

Build the adjacency graph. Must be called after all variables and constraints have been added, before calling solve().

Source

pub fn propagate(&mut self) -> Result<(), Unsatisfiable>
where D::Value: PartialEq,

Propagate constraints to a fixed point (auto-select strategy).

Source

pub fn propagate_with( &mut self, strategy: PropagationStrategy, ) -> Result<(), Unsatisfiable>
where D::Value: PartialEq,

Propagate constraints with an explicit strategy.

Source

pub fn solve(&mut self, config: &SolveConfig) -> Vec<Solution<D>>
where D::Value: PartialEq,

Run backtracking (or backjumping) search with the given configuration.

Returns up to config.max_solutions solutions. When optimization_mode is MinimizeCost or MaximizeCost, uses branch-and-bound search and returns solutions sorted by cost (best first).

Source

pub fn solve_with_given( &mut self, config: &SolveConfig, given: &[(VarId, D::Value)], ) -> Vec<Solution<D>>
where D::Value: PartialEq,

Solve with initial propagation for pre-assigned (“given”) values.

Analogous to Python’s solve_with_initial_propagation. Pre-assigns the given values, propagates constraints, then searches.

Source

pub fn solve_with_cost_eval( &mut self, config: &SolveConfig, cost_eval: &dyn DomainCostEval<D>, ) -> Vec<Solution<D>>
where D::Value: PartialEq,

Run optimization search with a custom cost evaluator.

This is the most flexible entry point: you supply a DomainCostEval that defines how domain values are costed. Use solve() with OptimizationMode::MinimizeCost if you only need soft constraint penalties (zero domain cost), or solve_optimized() if your domain implements CostDomain.

Source

pub fn stats(&self) -> &SolveStats

Get solver statistics from the last run.

Source

pub fn adjacency(&self) -> Option<&Adjacency>

Get a reference to the adjacency graph (available after finalize()).

Source§

impl<D: CostDomain> Csp<D>

Source

pub fn solve_optimized(&mut self, config: &SolveConfig) -> Vec<Solution<D>>
where D::Value: PartialEq,

Run optimization search using the domain’s CostDomain implementation for value costing. This is the ergonomic entry point when your domain type implements CostDomain.

Trait Implementations§

Source§

impl<D: Domain> Default for Csp<D>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<D> Freeze for Csp<D>

§

impl<D> !RefUnwindSafe for Csp<D>

§

impl<D> !Send for Csp<D>

§

impl<D> !Sync for Csp<D>

§

impl<D> Unpin for Csp<D>
where D: Unpin, <D as Domain>::Value: Unpin,

§

impl<D> UnsafeUnpin for Csp<D>

§

impl<D> !UnwindSafe for Csp<D>

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