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>
impl<D: Domain> Csp<D>
Sourcepub fn add_variable(&mut self, domain: D) -> VarId
pub fn add_variable(&mut self, domain: D) -> VarId
Add a variable with the given domain. Returns its VarId.
Sourcepub fn add_variables(&mut self, domain: &D, count: usize) -> Vec<VarId> ⓘ
pub fn add_variables(&mut self, domain: &D, count: usize) -> Vec<VarId> ⓘ
Add multiple variables sharing the same domain. Returns their VarIds.
Sourcepub fn add_constraint(&mut self, c: impl Constraint<D> + 'static)
pub fn add_constraint(&mut self, c: impl Constraint<D> + 'static)
Add a custom constraint (wrapped in the Custom enum variant).
Sourcepub fn add_constraint_enum(&mut self, c: ConstraintEnum<D>)
pub fn add_constraint_enum(&mut self, c: ConstraintEnum<D>)
Add a pre-typed constraint enum directly (avoids boxing for built-in types).
Sourcepub fn add_soft_constraint(&mut self, c: SoftLambdaConstraint<D>)
pub fn add_soft_constraint(&mut self, c: SoftLambdaConstraint<D>)
Add a soft constraint (contributes penalty cost when violated, never prunes).
Sourcepub fn add_not_equal(&mut self, x: VarId, y: VarId)
pub fn add_not_equal(&mut self, x: VarId, y: VarId)
Add a not-equal constraint (devirtualized fast path).
Sourcepub fn add_all_different(&mut self, vars: Vec<VarId>)
pub fn add_all_different(&mut self, vars: Vec<VarId>)
Add an all-different constraint (devirtualized fast path).
Sourcepub fn add_equals(&mut self, var: VarId, value: D::Value)where
D: 'static,
pub fn add_equals(&mut self, var: VarId, value: D::Value)where
D: 'static,
Fix a variable to a specific value.
Sourcepub fn add_less_than(&mut self, x: VarId, y: VarId)where
D: 'static,
D::Value: PartialOrd,
pub fn add_less_than(&mut self, x: VarId, y: VarId)where
D: 'static,
D::Value: PartialOrd,
Constrain x < y (for Ord-comparable values).
Sourcepub fn add_greater_than(&mut self, x: VarId, y: VarId)where
D: 'static,
D::Value: PartialOrd,
pub fn add_greater_than(&mut self, x: VarId, y: VarId)where
D: 'static,
D::Value: PartialOrd,
Constrain x > y (for Ord-comparable values).
Sourcepub fn finalize(&mut self)
pub fn finalize(&mut self)
Build the adjacency graph. Must be called after all variables and
constraints have been added, before calling solve().
Sourcepub fn propagate(&mut self) -> Result<(), Unsatisfiable>
pub fn propagate(&mut self) -> Result<(), Unsatisfiable>
Propagate constraints to a fixed point (auto-select strategy).
Sourcepub fn propagate_with(
&mut self,
strategy: PropagationStrategy,
) -> Result<(), Unsatisfiable>
pub fn propagate_with( &mut self, strategy: PropagationStrategy, ) -> Result<(), Unsatisfiable>
Propagate constraints with an explicit strategy.
Sourcepub fn solve(&mut self, config: &SolveConfig) -> Vec<Solution<D>> ⓘ
pub fn solve(&mut self, config: &SolveConfig) -> Vec<Solution<D>> ⓘ
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).
Sourcepub fn solve_with_given(
&mut self,
config: &SolveConfig,
given: &[(VarId, D::Value)],
) -> Vec<Solution<D>> ⓘ
pub fn solve_with_given( &mut self, config: &SolveConfig, given: &[(VarId, D::Value)], ) -> Vec<Solution<D>> ⓘ
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.
Sourcepub fn solve_with_cost_eval(
&mut self,
config: &SolveConfig,
cost_eval: &dyn DomainCostEval<D>,
) -> Vec<Solution<D>> ⓘ
pub fn solve_with_cost_eval( &mut self, config: &SolveConfig, cost_eval: &dyn DomainCostEval<D>, ) -> Vec<Solution<D>> ⓘ
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.
Sourcepub fn stats(&self) -> &SolveStats
pub fn stats(&self) -> &SolveStats
Get solver statistics from the last run.
Source§impl<D: CostDomain> Csp<D>
impl<D: CostDomain> Csp<D>
Sourcepub fn solve_optimized(&mut self, config: &SolveConfig) -> Vec<Solution<D>> ⓘ
pub fn solve_optimized(&mut self, config: &SolveConfig) -> Vec<Solution<D>> ⓘ
Run optimization search using the domain’s CostDomain implementation
for value costing. This is the ergonomic entry point when your domain
type implements CostDomain.