1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//!
//!  Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//!
//!  File : response.rs
//!
//!  Purpose :   This example demonstrates proper response handling
//!              for problems solved with the interior-point optimizers.
//!
extern crate mosek;

use mosek::{Task,Streamtype,Solsta,Soltype};
use std::env;

const CQO1_PTF : &str = "Task 'CQO1 EXAMPLE'
Objective obj
    Minimize + x4 + x5 + x6
Constraints
    c1 [1] + x1 + x2 + 2 x3
Variables
    k1 [QUAD(3)]
        x4
        x1 [0;+inf]
        x2 [0;+inf]
    k2 [RQUAD(3)]
        x5
        x6
        x3 [0;+inf]
";


fn main() -> Result<(),String> {
    let args: Vec<String> = env::args().collect();

    let mut task = Task::new().unwrap().with_callbacks();
    if args.len() < 2 {
        task.read_ptf_string(CQO1_PTF)?;
    }
    else {
        task.read_data(args[1].as_str())?;
    }

    // Perform optimization.
    let trm = task.optimize()?;
    task.solution_summary(Streamtype::LOG)?;

    // Handle solution status. We expect Optimal
    let solsta = task.get_sol_sta(Soltype::ITR)?;

    match solsta {
        Solsta::OPTIMAL => {
            // Fetch and print the solution
            println!("An optimal interior point solution is located.");
            let numvar = task.get_num_var()?;
            let mut xx = vec![0.0; numvar as usize];
            task.get_xx(Soltype::ITR, xx.as_mut_slice())?;
            println!("xx = {:?}",xx)
        },
        Solsta::DUAL_INFEAS_CER =>
          println!("Dual infeasibility certificate found."),
        Solsta::PRIM_INFEAS_CER =>
          println!("Primal infeasibility certificate found."),
        Solsta::UNKNOWN => {
          // The solutions status is unknown. The termination code
          // indicates why the optimizer terminated prematurely.
          println!("The solution status is unknown.");
          let (symname,desc) = mosek::get_code_desc(trm)?;
          println!("   Termination code: {} {}\n", symname, desc)
        },
        _ =>
          println!("Unexpected solution status {}\n",solsta)
    }
    Ok(())
}


#[cfg(test)]
mod tests {
    #[test]
    fn test() {
        super::main().unwrap();
    }
}