Skip to main content

SolveInstance

Struct SolveInstance 

Source
pub struct SolveInstance {
    pub num_vars: u32,
    pub clauses: Vec<Clause>,
    pub weights: Option<Vec<f64>>,
    pub objective: Objective,
}
Expand description

A SAT or MaxSAT instance in Conjunctive Normal Form (CNF).

This struct represents the complete problem to be solved:

  • A set of propositional variables (indexed 0 to num_vars-1)
  • A conjunction of clauses (each clause is a disjunction of literals)
  • Optional weights for MaxSAT
  • An optimization objective

§CNF Semantics

The formula represented is:

clause[0] AND clause[1] AND ... AND clause[n-1]

where each clause is:

lit[0] OR lit[1] OR ... OR lit[k-1]

§Memory Layout

The instance is designed for efficient GPU transfer:

  • Clauses can be flattened to a contiguous literal array with offset indices
  • Weights align with clause indices for vectorized operations

§Example

use xlog_solve::{SolveInstance, Clause, Literal, Objective};

// (x0 OR NOT x1) AND (x1 OR x2)
let instance = SolveInstance::new(3, vec![
    Clause::new(vec![Literal::positive(0), Literal::negative(1)]),
    Clause::new(vec![Literal::positive(1), Literal::positive(2)]),
]);

assert_eq!(instance.num_vars, 3);
assert_eq!(instance.clauses.len(), 2);
assert_eq!(instance.objective, Objective::Satisfaction);

Fields§

§num_vars: u32

Number of propositional variables in this instance.

Variables are indexed from 0 to num_vars - 1.

§clauses: Vec<Clause>

The clauses forming this CNF formula.

The formula is satisfied when ALL clauses are satisfied.

§weights: Option<Vec<f64>>

Optional weights for each clause (for weighted MaxSAT).

If Some, must have the same length as clauses. If None, all clauses have implicit weight 1.0.

§objective: Objective

The optimization objective.

Implementations§

Source§

impl SolveInstance

Source

pub fn new(num_vars: u32, clauses: Vec<Clause>) -> Self

Creates a new SAT instance with the given variables and clauses.

The instance uses Objective::Satisfaction (standard SAT).

§Arguments
  • num_vars - The number of propositional variables
  • clauses - The clauses forming the CNF formula
§Example
use xlog_solve::{SolveInstance, Clause, Literal};

let instance = SolveInstance::new(3, vec![
    Clause::new(vec![Literal::positive(0), Literal::negative(1)]),
    Clause::new(vec![Literal::positive(1), Literal::positive(2)]),
]);
Source

pub fn with_weights( num_vars: u32, clauses: Vec<Clause>, weights: Vec<f64>, ) -> Self

Creates a weighted MaxSAT instance.

§Arguments
  • num_vars - The number of propositional variables
  • clauses - The clauses forming the CNF formula
  • weights - Weights for each clause (must match clause count)
§Panics

Panics if weights.len() != clauses.len().

§Example
use xlog_solve::{SolveInstance, Clause, Literal, Objective};

let instance = SolveInstance::with_weights(
    2,
    vec![
        Clause::new(vec![Literal::positive(0)]),
        Clause::new(vec![Literal::positive(1)]),
    ],
    vec![1.0, 2.0],  // Clause 1 has twice the weight
);

assert_eq!(instance.objective, Objective::MaxSat);
Source

pub fn add_clause(&mut self, clause: Clause)

Adds a clause to this instance.

§Arguments
  • clause - The clause to add
§Note

If the instance has weights, the new clause gets weight 1.0.

Source

pub fn add_weighted_clause(&mut self, clause: Clause, weight: f64)

Adds a weighted clause to this instance.

If the instance doesn’t have weights yet, this initializes weights with 1.0 for all existing clauses.

§Arguments
  • clause - The clause to add
  • weight - The weight for this clause
Source

pub fn num_clauses(&self) -> usize

Returns the number of clauses in this instance.

Source

pub fn is_satisfied(&self, assignment: &[bool]) -> bool

Checks if all clauses are satisfied by the given assignment.

§Arguments
  • assignment - A slice of boolean values, one per variable
§Returns

true if all clauses are satisfied, false otherwise.

§Example
use xlog_solve::{SolveInstance, Clause, Literal};

let instance = SolveInstance::new(2, vec![
    Clause::new(vec![Literal::positive(0)]),
    Clause::new(vec![Literal::positive(1)]),
]);

assert!(instance.is_satisfied(&[true, true]));
assert!(!instance.is_satisfied(&[true, false]));
Source

pub fn count_satisfied(&self, assignment: &[bool]) -> usize

Counts how many clauses are satisfied by the given assignment.

§Arguments
  • assignment - A slice of boolean values, one per variable
§Returns

The number of satisfied clauses.

Source

pub fn weighted_satisfaction(&self, assignment: &[bool]) -> f64

Computes the weighted satisfaction score for the given assignment.

If weights are specified, returns the sum of weights of satisfied clauses. If no weights are specified, each clause has implicit weight 1.0.

§Arguments
  • assignment - A slice of boolean values, one per variable
§Returns

The total weight of satisfied clauses.

Source

pub fn total_weight(&self) -> f64

Returns the total weight of all clauses.

For unweighted instances, returns the number of clauses.

Source

pub fn satisfaction_ratio(&self, assignment: &[bool]) -> f64

Returns the satisfaction ratio (satisfied weight / total weight).

§Arguments
  • assignment - A slice of boolean values, one per variable
§Returns

A value in [0.0, 1.0] representing the fraction of weight satisfied. Returns 0.0 for empty instances (no clauses).

Source

pub fn iter_clauses(&self) -> impl Iterator<Item = &Clause>

Returns an iterator over all clauses.

Source

pub fn iter_weighted(&self) -> impl Iterator<Item = (&Clause, f64)>

Returns an iterator over clauses with their weights.

If no weights are specified, uses 1.0 for each clause.

Source

pub fn validate(&self) -> bool

Validates that all variable indices in clauses are within bounds.

§Returns

true if all variable indices are less than num_vars, false otherwise.

Source

pub fn max_var(&self) -> Option<u32>

Returns the maximum variable index used in any clause.

Returns None if there are no clauses or all clauses are empty.

Source

pub fn total_literals(&self) -> usize

Returns the total number of literals across all clauses.

Trait Implementations§

Source§

impl Clone for SolveInstance

Source§

fn clone(&self) -> SolveInstance

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 Debug for SolveInstance

Source§

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

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

impl Default for SolveInstance

Source§

fn default() -> Self

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

Auto Trait Implementations§

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

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,