Skip to main content

pow1/
pow1.rs

1//!
2//!  Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
3//!
4//!  File : pow1.rs
5//!
6//!  Purpose: Demonstrates how to solve the problem
7//!
8//!    maximize x^0.2*y^0.8 + z^0.4 - x
9//!          st x + y + 0.5z = 2
10//!             x,y,z >= 0
11//!
12
13extern crate mosek;
14use mosek::*;
15
16const INF : f64 = 0.0;
17
18fn main()  -> Result<(),String> {
19    let numcon : i32 = 1;
20    let numvar : i32 = 5;
21
22    // Since the value infinity is never used, we define
23    // 'infinity' symbolic purposes only
24
25    let cval = vec![ 1.0, 1.0, -1.0 ];
26    let csub = vec![ 3,   4,    0 ];
27
28    let aval = vec![ 1.0, 1.0, 0.5 ];
29    let asub = vec![ 0, 1, 2 ];
30
31    /* Create the optimization task. */
32    let mut task = match Task::new() {
33        Some(e) => e,
34        None => return Err("Failed to create task".to_string()),
35    }.with_callbacks();
36    // Directs the log task stream to the user specified
37    // method msgclass.streamCB
38    task.put_stream_callback(Streamtype::LOG, |msg| print!("{}",msg))?;
39
40    /* Append 'numcon' empty constraints.
41    The constraints will initially have no bounds. */
42    task.append_cons(numcon)?;
43
44    /* Append 'numvar' variables.
45    The variables will initially be fixed at zero (x=0). */
46    task.append_vars(numvar)?;
47
48    /* Set up the linear part of the problem */
49    task.put_c_list(&csub, &cval)?;
50    task.put_a_row(0, &asub, &aval)?;
51    task.put_con_bound(0, Boundkey::FX, 2.0, 2.0)?;
52
53
54    task.put_var_bound_slice_const(0, numvar, Boundkey::FR, -INF, INF)?;
55
56    /* Add a conic constraint */
57    let pc1 = task.append_primal_power_cone_domain(3, &[0.2, 0.8])?;
58    let pc2 = task.append_primal_power_cone_domain(3, &[4.0, 6.0])?;
59
60    // Create data structures F,g so that
61    //
62    //   F * x + g = (x(0), x(1), x(3), x(2), 1.0, x(4))
63    //
64    task.append_afes(6)?;
65    task.put_afe_f_entry_list(&[0, 1, 2, 3, 5],         // Rows
66                              &[0, 1, 3, 2, 4],         // Columns
67                              &[1.0, 1.0, 1.0, 1.0, 1.0])?;
68    task.put_afe_g(4, 1.0)?;
69
70    // Append the two conic constraints
71    task.append_acc(pc1,                  // Domain
72                    &[0, 1, 2],           // Rows from F
73                    &[0.0,0.0,0.0])?;     // Unused
74    task.append_acc(pc2,                  // Domain
75                    &[3, 4, 5],           // Rows from F
76                    &[0.0,0.0,0.0])?;     // Unused
77
78    task.put_obj_sense(Objsense::MAXIMIZE)?;
79    task.optimize()?;
80
81    task.write_data("pow1.ptf")?;
82    // Print a summary containing information
83    // about the solution for debugging purposes
84    task.solution_summary(Streamtype::LOG)?;
85    /* Get status information about the solution */
86    let solsta = task.get_sol_sta(Soltype::ITR)?;
87
88    assert!(solsta == Solsta::OPTIMAL);
89
90    let mut xx = vec![0.0; numvar as usize];
91    task.get_xx(Soltype::ITR,
92                xx.as_mut_slice())?;
93
94    println!("Optimal primal solution");
95    for (j,&xj) in xx[0..3].iter().enumerate() {
96        println!("x[{}]: {}",j+1,xj);
97    }
98
99    Ok(())
100}
101
102#[cfg(test)]
103mod tests {
104    #[test]
105    fn test() {
106        super::main().unwrap();
107    }
108}