Skip to main content

SolveResult

Struct SolveResult 

Source
pub struct SolveResult { /* private fields */ }
Expand description

Full outcome of a solve attempt.

Implementations§

Source§

impl SolveResult

Source

pub fn new( status: SolveStatus, solution: Option<Solution>, objective_value: Option<f64>, diagnostics: SolveDiagnostics, ) -> Self

Source

pub fn status(&self) -> SolveStatus

Examples found in repository?
examples/basic_l2.rs (line 26)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Problem:
5    // minimize_x 1/2 ||M x - r||_2^2
6    //
7    // with
8    // M = [[1, 0],
9    //      [0, 1],
10    //      [1, 1]]
11    // r = [1, 2, 2.5]
12    //
13    // Least-squares intuition:
14    // x should be close to [0.8333, 1.8333]
15
16    let residual = LinearResidual::new(
17        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
18        vec![1.0, 2.0, 2.5],
19    )?;
20
21    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?;
22
23    let result = problem.solve()?;
24
25    println!("=== basic_l2 ===");
26    println!("status      : {:?}", result.status());
27    println!("objective   : {:?}", result.objective_value());
28    println!("diagnostics : {:?}", result.diagnostics());
29
30    if let Some(solution) = result.solution() {
31        println!("x           : {:?}", solution.x());
32        println!("residual    : {:?}", solution.residual());
33    } else {
34        println!("no solution returned");
35    }
36
37    Ok(())
38}
More examples
Hide additional examples
examples/clarabel_constrained_l2.rs (line 25)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let residual = LinearResidual::new(
8        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
9        vec![1.0, 2.0, 2.5],
10    )?;
11
12    let eq = LinearEqualities::new(Matrix::from_row_major(1, 2, vec![1.0, -1.0])?, vec![0.0])?;
13
14    let ineq = LinearInequalities::new(Matrix::from_row_major(1, 2, vec![1.0, 1.0])?, vec![3.0])?;
15
16    let bounds = Bounds::new(vec![Some(0.0), Some(0.0)], vec![Some(10.0), Some(10.0)])?;
17
18    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?
19        .add_equalities(eq)?
20        .add_inequalities(ineq)?
21        .with_bounds(bounds)?;
22
23    let result = problem.solve()?;
24
25    println!("status      : {:?}", result.status());
26    println!("objective   : {:?}", result.objective_value());
27    println!("diagnostics : {:?}", result.diagnostics());
28
29    if let Some(solution) = result.solution() {
30        println!("x           : {:?}", solution.x());
31        println!("residual    : {:?}", solution.residual());
32    }
33
34    Ok(())
35}
Source

pub fn solution(&self) -> Option<&Solution>

Examples found in repository?
examples/basic_l2.rs (line 30)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Problem:
5    // minimize_x 1/2 ||M x - r||_2^2
6    //
7    // with
8    // M = [[1, 0],
9    //      [0, 1],
10    //      [1, 1]]
11    // r = [1, 2, 2.5]
12    //
13    // Least-squares intuition:
14    // x should be close to [0.8333, 1.8333]
15
16    let residual = LinearResidual::new(
17        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
18        vec![1.0, 2.0, 2.5],
19    )?;
20
21    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?;
22
23    let result = problem.solve()?;
24
25    println!("=== basic_l2 ===");
26    println!("status      : {:?}", result.status());
27    println!("objective   : {:?}", result.objective_value());
28    println!("diagnostics : {:?}", result.diagnostics());
29
30    if let Some(solution) = result.solution() {
31        println!("x           : {:?}", solution.x());
32        println!("residual    : {:?}", solution.residual());
33    } else {
34        println!("no solution returned");
35    }
36
37    Ok(())
38}
More examples
Hide additional examples
examples/clarabel_constrained_l2.rs (line 29)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let residual = LinearResidual::new(
8        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
9        vec![1.0, 2.0, 2.5],
10    )?;
11
12    let eq = LinearEqualities::new(Matrix::from_row_major(1, 2, vec![1.0, -1.0])?, vec![0.0])?;
13
14    let ineq = LinearInequalities::new(Matrix::from_row_major(1, 2, vec![1.0, 1.0])?, vec![3.0])?;
15
16    let bounds = Bounds::new(vec![Some(0.0), Some(0.0)], vec![Some(10.0), Some(10.0)])?;
17
18    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?
19        .add_equalities(eq)?
20        .add_inequalities(ineq)?
21        .with_bounds(bounds)?;
22
23    let result = problem.solve()?;
24
25    println!("status      : {:?}", result.status());
26    println!("objective   : {:?}", result.objective_value());
27    println!("diagnostics : {:?}", result.diagnostics());
28
29    if let Some(solution) = result.solution() {
30        println!("x           : {:?}", solution.x());
31        println!("residual    : {:?}", solution.residual());
32    }
33
34    Ok(())
35}
Source

pub fn objective_value(&self) -> Option<f64>

Examples found in repository?
examples/basic_l2.rs (line 27)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Problem:
5    // minimize_x 1/2 ||M x - r||_2^2
6    //
7    // with
8    // M = [[1, 0],
9    //      [0, 1],
10    //      [1, 1]]
11    // r = [1, 2, 2.5]
12    //
13    // Least-squares intuition:
14    // x should be close to [0.8333, 1.8333]
15
16    let residual = LinearResidual::new(
17        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
18        vec![1.0, 2.0, 2.5],
19    )?;
20
21    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?;
22
23    let result = problem.solve()?;
24
25    println!("=== basic_l2 ===");
26    println!("status      : {:?}", result.status());
27    println!("objective   : {:?}", result.objective_value());
28    println!("diagnostics : {:?}", result.diagnostics());
29
30    if let Some(solution) = result.solution() {
31        println!("x           : {:?}", solution.x());
32        println!("residual    : {:?}", solution.residual());
33    } else {
34        println!("no solution returned");
35    }
36
37    Ok(())
38}
More examples
Hide additional examples
examples/clarabel_constrained_l2.rs (line 26)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let residual = LinearResidual::new(
8        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
9        vec![1.0, 2.0, 2.5],
10    )?;
11
12    let eq = LinearEqualities::new(Matrix::from_row_major(1, 2, vec![1.0, -1.0])?, vec![0.0])?;
13
14    let ineq = LinearInequalities::new(Matrix::from_row_major(1, 2, vec![1.0, 1.0])?, vec![3.0])?;
15
16    let bounds = Bounds::new(vec![Some(0.0), Some(0.0)], vec![Some(10.0), Some(10.0)])?;
17
18    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?
19        .add_equalities(eq)?
20        .add_inequalities(ineq)?
21        .with_bounds(bounds)?;
22
23    let result = problem.solve()?;
24
25    println!("status      : {:?}", result.status());
26    println!("objective   : {:?}", result.objective_value());
27    println!("diagnostics : {:?}", result.diagnostics());
28
29    if let Some(solution) = result.solution() {
30        println!("x           : {:?}", solution.x());
31        println!("residual    : {:?}", solution.residual());
32    }
33
34    Ok(())
35}
Source

pub fn diagnostics(&self) -> &SolveDiagnostics

Examples found in repository?
examples/basic_l2.rs (line 28)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    // Problem:
5    // minimize_x 1/2 ||M x - r||_2^2
6    //
7    // with
8    // M = [[1, 0],
9    //      [0, 1],
10    //      [1, 1]]
11    // r = [1, 2, 2.5]
12    //
13    // Least-squares intuition:
14    // x should be close to [0.8333, 1.8333]
15
16    let residual = LinearResidual::new(
17        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
18        vec![1.0, 2.0, 2.5],
19    )?;
20
21    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?;
22
23    let result = problem.solve()?;
24
25    println!("=== basic_l2 ===");
26    println!("status      : {:?}", result.status());
27    println!("objective   : {:?}", result.objective_value());
28    println!("diagnostics : {:?}", result.diagnostics());
29
30    if let Some(solution) = result.solution() {
31        println!("x           : {:?}", solution.x());
32        println!("residual    : {:?}", solution.residual());
33    } else {
34        println!("no solution returned");
35    }
36
37    Ok(())
38}
More examples
Hide additional examples
examples/clarabel_constrained_l2.rs (line 27)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let residual = LinearResidual::new(
8        Matrix::from_row_major(3, 2, vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0])?,
9        vec![1.0, 2.0, 2.5],
10    )?;
11
12    let eq = LinearEqualities::new(Matrix::from_row_major(1, 2, vec![1.0, -1.0])?, vec![0.0])?;
13
14    let ineq = LinearInequalities::new(Matrix::from_row_major(1, 2, vec![1.0, 1.0])?, vec![3.0])?;
15
16    let bounds = Bounds::new(vec![Some(0.0), Some(0.0)], vec![Some(10.0), Some(10.0)])?;
17
18    let problem = ConstrainedResidualProblem::new(residual, Loss::L2Squared)?
19        .add_equalities(eq)?
20        .add_inequalities(ineq)?
21        .with_bounds(bounds)?;
22
23    let result = problem.solve()?;
24
25    println!("status      : {:?}", result.status());
26    println!("objective   : {:?}", result.objective_value());
27    println!("diagnostics : {:?}", result.diagnostics());
28
29    if let Some(solution) = result.solution() {
30        println!("x           : {:?}", solution.x());
31        println!("residual    : {:?}", solution.residual());
32    }
33
34    Ok(())
35}
Source

pub fn is_solved(&self) -> bool

Trait Implementations§

Source§

impl Clone for SolveResult

Source§

fn clone(&self) -> SolveResult

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 SolveResult

Source§

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

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

impl PartialEq for SolveResult

Source§

fn eq(&self, other: &SolveResult) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SolveResult

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.