Model

Struct Model 

Source
pub struct Model {
    pub props: Propagators,
    pub float_precision_digits: i32,
    /* private fields */
}

Fields§

§props: Propagators§float_precision_digits: i32

Precision for float variables (decimal places)

Implementations§

Source§

impl Model

Source

pub fn with_float_precision(precision_digits: i32) -> Self

Create a new model with custom float precision

use cspsolver::prelude::*;
let mut m = Model::with_float_precision(4); // 4 decimal places
let var = m.float(0.0, 1.0);
Source

pub fn float_precision_digits(&self) -> i32

Get the current float precision setting

Source

pub fn float_step_size(&self) -> f64

Get the step size for the current float precision

Source

pub fn get_constraint_registry(&self) -> &ConstraintRegistry

Get access to constraint registry for debugging/analysis

Source

pub fn new_var(&mut self, min: Val, max: Val) -> VarId

Create a new decision variable, with the provided domain bounds.

Both lower and upper bounds are included in the domain. In case max < min the bounds will be swapped. We don’t want to deal with “unwrap” every time

use cspsolver::prelude::*;
let mut m = Model::default();
let var = m.new_var(Val::int(1), Val::int(10));
Source

pub fn new_vars( &mut self, n: usize, min: Val, max: Val, ) -> impl Iterator<Item = VarId> + '_

Create new decision variables, with the provided domain bounds.

All created variables will have the same starting domain bounds. Both lower and upper bounds are included in the domain. In case max < min the bounds will be swapped.

use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.new_vars(3, Val::int(0), Val::int(5)).collect();
Source

pub fn int_vars( &mut self, n: usize, min: i32, max: i32, ) -> impl Iterator<Item = VarId> + '_

Create new integer decision variables, with the provided domain bounds.

Both lower and upper bounds are included in the domain. In case max < min the bounds will be swapped.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.int_vars(5, 0, 9).collect();
Source

pub fn ints(&mut self, values: Vec<i32>) -> VarId

Create a new integer decision variable from a vector of specific values. This is useful for creating variables with non-contiguous domains.

§Arguments
  • values - Vector of integer values that the variable can take
§Returns

A new VarId for the created variable

use cspsolver::prelude::*;
let mut m = Model::default();
let var = m.ints(vec![2, 4, 6, 8]); // Even numbers only
Source

pub fn float_vars( &mut self, n: usize, min: f64, max: f64, ) -> impl Iterator<Item = VarId> + '_

Create new float decision variables, with the provided domain bounds.

Both lower and upper bounds are included in the domain. In case max < min the bounds will be swapped.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.float_vars(3, 0.0, 1.0).collect();
Source

pub fn bool(&mut self) -> VarId

Create a new boolean decision variable (0 or 1).

This is a convenience method equivalent to new_var_int(0, 1). Boolean variables can be used with boolean logic constraints.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let a = m.bool();
let b = m.bool();
let c = m.bool();
 
// Boolean operations using model methods:
let and_result = m.bool_and(&[a, b]);  // AND
let or_result = m.bool_or(&[a, c]);    // OR  
let not_result = m.bool_not(a);        // NOT
 
// Basic constraints:
post!(m, a != b);
post!(m, and_result != c);
Source

pub fn new_var_binary(&mut self) -> VarIdBin

Create a new binary decision variable.

use cspsolver::prelude::*;
let mut m = Model::default();
let var = m.new_var_binary();
Source

pub fn new_vars_binary( &mut self, n: usize, ) -> impl Iterator<Item = VarIdBin> + '_

Create new binary decision variables.

use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.new_vars_binary(4).collect();
Source

pub fn int(&mut self, min: i32, max: i32) -> VarId

Short method to create an integer variable. Alias for new_var_int() - more concise and readable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(0, 10);    // Instead of m.int(0, 10)
let y = m.int(-5, 5);    // Clean and concise
Source

pub fn float(&mut self, min: f64, max: f64) -> VarId

Short method to create a float variable. Alias for new_var_float() - more concise and readable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.float(0.0, 10.0);    // Instead of m.float(0.0, 10.0)
let y = m.float(-1.5, 3.14);   // Clean and concise
Source

pub fn binary(&mut self) -> VarIdBin

Short method to create a binary variable. Alias for new_var_binary() - more concise and readable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.binary();    // Instead of m.new_var_binary()
Source

pub fn add(&mut self, x: impl View, y: impl View) -> VarId

Create an expression of two views added together.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 5);
let y = m.int(2, 8);
let sum = m.add(x, y);
Source

pub fn sub(&mut self, x: impl View, y: impl View) -> VarId

Create an expression representing the difference of two views: x - y.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(5, 10);
let y = m.int(2, 4);
let diff = m.sub(x, y);
Source

pub fn mul(&mut self, x: impl View, y: impl View) -> VarId

Create an expression of the multiplication of two views.

§Example
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(3, 5);
let y = m.int(2, 4);
let product = m.mul(x, y);
Source

pub fn modulo(&mut self, x: impl View, y: impl View) -> VarId

Create a new variable that holds the result of x % y (modulo operation).

For the modulo operation x % y = result:

  • If y > 0: result is in range [0, y-1]
  • If y < 0: result is in range [y+1, 0]
  • If y contains 0, the constraint may fail during solving
§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(10, 20);
let y = m.int(3, 7);
let remainder = m.modulo(x, y);
Source

pub fn abs(&mut self, x: impl View) -> VarId

Create a new variable that holds the result of |x| (absolute value).

The absolute value operation always produces a non-negative result:

  • If x >= 0: |x| = x
  • If x < 0: |x| = -x
§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(-10, 5);
let abs_x = m.abs(x);
Source

pub fn min(&mut self, vars: &[VarId]) -> VarId

Create a new variable that holds the minimum value of the given variables.

The minimum operation finds the smallest value among all input variables:

  • result = min(vars[0], vars[1], ..., vars[n])
  • At least one variable must be able to achieve the minimum value
  • All variables must be >= result
§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(5, 15);
let z = m.int(3, 8);
let minimum = m.min(&[x, y, z]);
Source

pub fn max(&mut self, vars: &[VarId]) -> VarId

Create a new variable that holds the maximum value of the given variables.

The maximum operation finds the largest value among all input variables:

  • result = max(vars[0], vars[1], ..., vars[n])
  • At least one variable must be able to achieve the maximum value
  • All variables must be <= result
§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(5, 15);
let z = m.int(3, 8);
let maximum = m.max(&[x, y, z]);
Source

pub fn div(&mut self, x: impl View, y: impl View) -> VarId

Create a new variable that holds the result of x / y (division).

For the division operation x / y = result:

  • If y contains 0, the constraint may fail during solving
  • Division by values very close to 0 is also avoided for numerical stability
  • The result may be converted to float even for integer inputs
§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(10, 20);
let y = m.int(2, 5);
let quotient = m.div(x, y);
Source

pub fn sum(&mut self, xs: &[impl View]) -> VarId

Create an expression of the sum of a slice of views.

use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.int_vars(3, 1, 10).collect();
let total = m.sum(&vars);
Source

pub fn sum_iter(&mut self, xs: impl IntoIterator<Item = impl View>) -> VarId

Create an expression of the sum of an iterator of views.

use cspsolver::prelude::*;
let mut m = Model::default();
let vars: Vec<_> = m.int_vars(3, 1, 10).collect();
let total = m.sum_iter(vars.iter().copied());
Source

pub fn bool_and(&mut self, operands: &[VarId]) -> VarId

Create a variable representing the boolean AND of multiple operands. Returns a variable that is 1 if ALL operands are non-zero, 0 otherwise.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let a = m.bool();
let b = m.bool();
let c = m.bool();
let and_result = m.bool_and(&[a, b, c]);
Source

pub fn bool_or(&mut self, operands: &[VarId]) -> VarId

Create a variable representing the boolean OR of multiple operands. Returns a variable that is 1 if ANY operand is non-zero, 0 otherwise.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let a = m.bool();
let b = m.bool();
let or_result = m.bool_or(&[a, b]);
Source

pub fn bool_not(&mut self, operand: VarId) -> VarId

Create a variable representing the boolean NOT of an operand. Returns a variable that is 1 if the operand is 0, and 0 if the operand is non-zero.

§Examples
use cspsolver::prelude::*;
let mut m = Model::default();
let a = m.bool();
let not_a = m.bool_not(a);
Source

pub fn minimize(self, objective: impl View) -> Option<Solution>

Find assignment that minimizes objective expression while satisfying all constraints.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
post!(m, x > 3);
let solution = m.minimize(x);
Source

pub fn minimize_with_callback<F>( self, objective: impl View, callback: F, ) -> Option<Solution>
where F: FnOnce(&SolveStats),

Find assignment that minimizes objective expression with callback to capture solving statistics.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let solution = m.minimize_with_callback(x, |stats| {
    println!("Propagations: {}", stats.propagation_count);
});
Source

pub fn minimize_and_iterate( self, objective: impl View, ) -> impl Iterator<Item = Solution>

Enumerate assignments that satisfy all constraints, while minimizing objective expression.

The order in which assignments are yielded is not stable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 5);
let solutions: Vec<_> = m.minimize_and_iterate(x).collect();
Source

pub fn minimize_and_iterate_with_callback<F>( self, objective: impl View, callback: F, ) -> Vec<Solution>
where F: FnOnce(&SolveStats),

Enumerate assignments that satisfy all constraints, while minimizing objective expression, with callback.

The callback is called with final statistics after all solutions are found. Returns a vector of all solutions found during the search.

Source

pub fn maximize(self, objective: impl View) -> Option<Solution>

Find assignment that maximizes objective expression while satisfying all constraints.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
post!(m, x < 8);
let solution = m.maximize(x);
Source

pub fn maximize_with_callback<F>( self, objective: impl View, callback: F, ) -> Option<Solution>
where F: FnOnce(&SolveStats),

Find assignment that maximizes objective expression with callback to capture solving statistics.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let solution = m.maximize_with_callback(x, |stats| {
    println!("Nodes explored: {}", stats.node_count);
});
Source

pub fn maximize_and_iterate( self, objective: impl View, ) -> impl Iterator<Item = Solution>

Enumerate assignments that satisfy all constraints, while maximizing objective expression.

The order in which assignments are yielded is not stable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 5);
let solutions: Vec<_> = m.maximize_and_iterate(x).collect();
Source

pub fn maximize_and_iterate_with_callback<F>( self, objective: impl View, callback: F, ) -> Vec<Solution>
where F: FnOnce(&SolveStats),

Enumerate assignments that satisfy all constraints, while maximizing objective expression, with callback.

The callback is called with final statistics after all solutions are found. Returns a vector of all solutions found during the search.

Source

pub fn validate(&self) -> Result<(), String>

Validate that all integer variable domains fit within the u16 optimization range.

This method checks that all integer variables have domains that can be represented using u16 optimization (domain size ≤ 65535). Since we’ve already replaced VarI with VarSparse in the new_var_with_bounds method, this validation mainly serves as a safety check and provides clear error messages for invalid domain sizes.

§Returns

Returns Ok(()) if validation succeeds, or Err(String) with error details if validation fails.

Source

pub fn solve(self) -> Option<Solution>

Search for assignment that satisfies all constraints within bounds of decision variables.

This method automatically tries optimization algorithms for suitable problems before falling back to traditional constraint propagation search.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(1, 10);
post!(m, x != y);
let solution = m.solve();
Source

pub fn solve_with_callback<F>(self, callback: F) -> Option<Solution>
where F: FnOnce(&SolveStats),

Search for assignment with a callback to capture solving statistics.

The callback receives the solving statistics when the search completes.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 10);
let solution = m.solve_with_callback(|stats| {
    println!("Search completed with {} propagations", stats.propagation_count);
});
Source

pub fn enumerate(self) -> impl Iterator<Item = Solution>

Enumerate all assignments that satisfy all constraints.

The order in which assignments are yielded is not stable.

use cspsolver::prelude::*;
let mut m = Model::default();
let x = m.int(1, 3);
let y = m.int(1, 3);
post!(m, x != y);
let solutions: Vec<_> = m.enumerate().collect();
Source

pub fn enumerate_with_callback<F>(self, callback: F) -> Vec<Solution>
where F: FnOnce(&SolveStats),

Enumerate all assignments that satisfy all constraints with callback to capture solving statistics.

The callback is called with final statistics after all solutions are found. Returns a vector of all solutions found during the search.

Source§

impl Model

Source

pub fn eq_op(&mut self, left: VarId, right: VarId)

Create equality constraint using operator syntax

Source

pub fn ne_op(&mut self, left: VarId, right: VarId)

Create inequality constraint using operator syntax

Source

pub fn lt_op(&mut self, left: VarId, right: VarId)

Create less-than constraint using operator syntax

Source

pub fn le_op(&mut self, left: VarId, right: VarId)

Create less-than-or-equal constraint using operator syntax

Source

pub fn gt_op(&mut self, left: VarId, right: VarId)

Create greater-than constraint using operator syntax

Source

pub fn ge_op(&mut self, left: VarId, right: VarId)

Create greater-than-or-equal constraint using operator syntax

Source

pub fn and_op(&mut self, left: VarId, right: VarId)

Create boolean AND constraint using operator syntax

Source

pub fn or_op(&mut self, left: VarId, right: VarId)

Create boolean OR constraint using operator syntax

Source

pub fn not_op(&mut self, var: VarId)

Create boolean NOT constraint using operator syntax

Trait Implementations§

Source§

impl Debug for Model

Source§

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

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

impl Default for Model

Source§

fn default() -> Self

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

impl Index<VarId> for Model

Source§

type Output = Var

The returned type after indexing.
Source§

fn index(&self, index: VarId) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl Freeze for Model

§

impl !RefUnwindSafe for Model

§

impl !Send for Model

§

impl !Sync for Model

§

impl Unpin for Model

§

impl !UnwindSafe for Model

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.