Problem

Struct Problem 

Source
pub struct Problem<'a> { /* private fields */ }
Expand description

export all public structs and enums A CPLEX Problem.

Implementations§

Source§

impl<'a> Problem<'a>

Source

pub fn new<S>(env: &'a Env, name: S) -> Result<Self, String>
where S: Into<String>,

Examples found in repository?
examples/qcp.rs (line 15)
7fn main() {
8    let num_n = 6_usize;
9    let dim = 3_usize;
10    let env = Env::new().unwrap();
11
12    // starting moment
13    let t1 = std::time::Instant::now();
14    // populate it with a problem
15    let mut prob = Problem::new(&env, "miqpex1").unwrap();
16
17    for i in 0..dim * num_n * 4 {
18        let name = format!("x{:02}", i);
19        let _x = prob
20            .add_variable(Variable::new(
21                VariableType::Continuous,
22                0.0,
23                -100.0,
24                100.0,
25                name,
26            ))
27            .unwrap();
28    }
29    let p_s = [1.0, 0.0, -1.0];
30    let v_s = [0.0, 0.0, 0.0];
31    let a_s = [0.0, 0.0, 0.0];
32    let p_g = [8.0, 18.0, 5.0];
33    let v_g = [0.0, 0.0, 5.0];
34    let a_g = [0.0, 0.0, 0.0];
35
36    let dt: f64 = 1.0;
37    let mut num_con = 0;
38
39    // init constraint
40    for k in 0_usize..3 {
41        // position
42        let mut dummy = Constraint::new(ConstraintType::Eq, p_s[k], format!("dummy{}", num_con));
43        dummy.add_wvar(WeightedVariable::new_idx(9 + k, 1.0));
44        prob.add_constraint(dummy).unwrap();
45        num_con += 1;
46
47        // velocity
48        let mut dummy = Constraint::new(ConstraintType::Eq, v_s[k], format!("dummy{}", num_con));
49        dummy.add_wvar(WeightedVariable::new_idx(6 + k, 1.0));
50        prob.add_constraint(dummy).unwrap();
51        num_con += 1;
52
53        //accelerate
54        let mut dummy = Constraint::new(ConstraintType::Eq, a_s[k], format!("dummy{}", num_con));
55        dummy.add_wvar(WeightedVariable::new_idx(3 + k, 2.0));
56        prob.add_constraint(dummy).unwrap();
57        num_con += 1;
58    }
59
60    // final constraint
61    let base = (num_n - 1) * 12;
62    for k in 0_usize..3 {
63        // position
64        let mut dummy = Constraint::new(ConstraintType::Eq, p_g[k], format!("dummy{}", num_con));
65        dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
66        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
67        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
68        dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
69        prob.add_constraint(dummy).unwrap();
70        num_con += 1;
71
72        // velocity
73        let mut dummy = Constraint::new(ConstraintType::Eq, v_g[k], format!("dummy{}", num_con));
74        dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
75        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
76        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
77        prob.add_constraint(dummy).unwrap();
78        num_con += 1;
79
80        //accelerate
81        let mut dummy = Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
82        dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
83        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
84        prob.add_constraint(dummy).unwrap();
85        num_con += 1;
86    }
87
88    // continuity
89
90    for i in 0..num_n - 1 {
91        let base = i * 12;
92        for k in 0_usize..dim {
93            // position
94            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
95            dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
96            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
97            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
98            dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
99            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 9 + k, -1.0));
100            prob.add_constraint(dummy).unwrap();
101            num_con += 1;
102
103            // velocity
104            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
105            dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
106            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
107            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
108            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 6 + k, -1.0));
109            prob.add_constraint(dummy).unwrap();
110            num_con += 1;
111
112            //accelerate
113            let mut dummy =
114                Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
115            dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
116            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
117            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 3 + k, -2.0));
118            prob.add_constraint(dummy).unwrap();
119            num_con += 1;
120        }
121    }
122
123    // maximize the objective
124    let mut qp_vars: Vec<usize> = Vec::new();
125    let dummy = Constraint::new(ConstraintType::Eq, 0.0, "obj");
126    for i in 0..num_n {
127        let base = i * 12;
128        for k in 0..dim {
129            qp_vars.push(base + k);
130            // dummy.add_wvar(WeightedVariable::new_idx(base + k, 0.0));
131        }
132    }
133    prob.set_qp_objective(ObjectiveType::Minimize, dummy, qp_vars.clone(), dt / 36.0)
134        .unwrap();
135
136    // solve the problem
137    let sol = prob.solve(ProblemType::MixedInteger).unwrap();
138
139    let t2 = std::time::Instant::now();
140
141    println!("time cost {:?}", t2 - t1);
142
143    println!("{:.4?}", sol);
144    let sol_vars = sol.variables;
145    let mut f = 0.0_f64;
146    for idx in qp_vars.into_iter() {
147        let value = sol_vars[idx];
148        if let VariableValue::Continuous(v) = value {
149            f += v.powi(2);
150        }
151    }
152    f = f * dt / 36.0 / 2.0;
153    println!("f = {}", f);
154}
Source

pub fn add_variable(&mut self, var: Variable) -> Result<usize, String>

Add a variable to the problem. The Variable is moved into the problem. At this time, it is not possible to get a reference to it back.

The column index for the Variable is returned.

Examples found in repository?
examples/qcp.rs (lines 20-26)
7fn main() {
8    let num_n = 6_usize;
9    let dim = 3_usize;
10    let env = Env::new().unwrap();
11
12    // starting moment
13    let t1 = std::time::Instant::now();
14    // populate it with a problem
15    let mut prob = Problem::new(&env, "miqpex1").unwrap();
16
17    for i in 0..dim * num_n * 4 {
18        let name = format!("x{:02}", i);
19        let _x = prob
20            .add_variable(Variable::new(
21                VariableType::Continuous,
22                0.0,
23                -100.0,
24                100.0,
25                name,
26            ))
27            .unwrap();
28    }
29    let p_s = [1.0, 0.0, -1.0];
30    let v_s = [0.0, 0.0, 0.0];
31    let a_s = [0.0, 0.0, 0.0];
32    let p_g = [8.0, 18.0, 5.0];
33    let v_g = [0.0, 0.0, 5.0];
34    let a_g = [0.0, 0.0, 0.0];
35
36    let dt: f64 = 1.0;
37    let mut num_con = 0;
38
39    // init constraint
40    for k in 0_usize..3 {
41        // position
42        let mut dummy = Constraint::new(ConstraintType::Eq, p_s[k], format!("dummy{}", num_con));
43        dummy.add_wvar(WeightedVariable::new_idx(9 + k, 1.0));
44        prob.add_constraint(dummy).unwrap();
45        num_con += 1;
46
47        // velocity
48        let mut dummy = Constraint::new(ConstraintType::Eq, v_s[k], format!("dummy{}", num_con));
49        dummy.add_wvar(WeightedVariable::new_idx(6 + k, 1.0));
50        prob.add_constraint(dummy).unwrap();
51        num_con += 1;
52
53        //accelerate
54        let mut dummy = Constraint::new(ConstraintType::Eq, a_s[k], format!("dummy{}", num_con));
55        dummy.add_wvar(WeightedVariable::new_idx(3 + k, 2.0));
56        prob.add_constraint(dummy).unwrap();
57        num_con += 1;
58    }
59
60    // final constraint
61    let base = (num_n - 1) * 12;
62    for k in 0_usize..3 {
63        // position
64        let mut dummy = Constraint::new(ConstraintType::Eq, p_g[k], format!("dummy{}", num_con));
65        dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
66        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
67        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
68        dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
69        prob.add_constraint(dummy).unwrap();
70        num_con += 1;
71
72        // velocity
73        let mut dummy = Constraint::new(ConstraintType::Eq, v_g[k], format!("dummy{}", num_con));
74        dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
75        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
76        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
77        prob.add_constraint(dummy).unwrap();
78        num_con += 1;
79
80        //accelerate
81        let mut dummy = Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
82        dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
83        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
84        prob.add_constraint(dummy).unwrap();
85        num_con += 1;
86    }
87
88    // continuity
89
90    for i in 0..num_n - 1 {
91        let base = i * 12;
92        for k in 0_usize..dim {
93            // position
94            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
95            dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
96            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
97            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
98            dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
99            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 9 + k, -1.0));
100            prob.add_constraint(dummy).unwrap();
101            num_con += 1;
102
103            // velocity
104            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
105            dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
106            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
107            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
108            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 6 + k, -1.0));
109            prob.add_constraint(dummy).unwrap();
110            num_con += 1;
111
112            //accelerate
113            let mut dummy =
114                Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
115            dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
116            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
117            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 3 + k, -2.0));
118            prob.add_constraint(dummy).unwrap();
119            num_con += 1;
120        }
121    }
122
123    // maximize the objective
124    let mut qp_vars: Vec<usize> = Vec::new();
125    let dummy = Constraint::new(ConstraintType::Eq, 0.0, "obj");
126    for i in 0..num_n {
127        let base = i * 12;
128        for k in 0..dim {
129            qp_vars.push(base + k);
130            // dummy.add_wvar(WeightedVariable::new_idx(base + k, 0.0));
131        }
132    }
133    prob.set_qp_objective(ObjectiveType::Minimize, dummy, qp_vars.clone(), dt / 36.0)
134        .unwrap();
135
136    // solve the problem
137    let sol = prob.solve(ProblemType::MixedInteger).unwrap();
138
139    let t2 = std::time::Instant::now();
140
141    println!("time cost {:?}", t2 - t1);
142
143    println!("{:.4?}", sol);
144    let sol_vars = sol.variables;
145    let mut f = 0.0_f64;
146    for idx in qp_vars.into_iter() {
147        let value = sol_vars[idx];
148        if let VariableValue::Continuous(v) = value {
149            f += v.powi(2);
150        }
151    }
152    f = f * dt / 36.0 / 2.0;
153    println!("f = {}", f);
154}
Source

pub fn add_constraint(&mut self, con: Constraint) -> Result<usize, String>

Add a constraint to the problem.

The row index for the constraint is returned.

Examples found in repository?
examples/qcp.rs (line 44)
7fn main() {
8    let num_n = 6_usize;
9    let dim = 3_usize;
10    let env = Env::new().unwrap();
11
12    // starting moment
13    let t1 = std::time::Instant::now();
14    // populate it with a problem
15    let mut prob = Problem::new(&env, "miqpex1").unwrap();
16
17    for i in 0..dim * num_n * 4 {
18        let name = format!("x{:02}", i);
19        let _x = prob
20            .add_variable(Variable::new(
21                VariableType::Continuous,
22                0.0,
23                -100.0,
24                100.0,
25                name,
26            ))
27            .unwrap();
28    }
29    let p_s = [1.0, 0.0, -1.0];
30    let v_s = [0.0, 0.0, 0.0];
31    let a_s = [0.0, 0.0, 0.0];
32    let p_g = [8.0, 18.0, 5.0];
33    let v_g = [0.0, 0.0, 5.0];
34    let a_g = [0.0, 0.0, 0.0];
35
36    let dt: f64 = 1.0;
37    let mut num_con = 0;
38
39    // init constraint
40    for k in 0_usize..3 {
41        // position
42        let mut dummy = Constraint::new(ConstraintType::Eq, p_s[k], format!("dummy{}", num_con));
43        dummy.add_wvar(WeightedVariable::new_idx(9 + k, 1.0));
44        prob.add_constraint(dummy).unwrap();
45        num_con += 1;
46
47        // velocity
48        let mut dummy = Constraint::new(ConstraintType::Eq, v_s[k], format!("dummy{}", num_con));
49        dummy.add_wvar(WeightedVariable::new_idx(6 + k, 1.0));
50        prob.add_constraint(dummy).unwrap();
51        num_con += 1;
52
53        //accelerate
54        let mut dummy = Constraint::new(ConstraintType::Eq, a_s[k], format!("dummy{}", num_con));
55        dummy.add_wvar(WeightedVariable::new_idx(3 + k, 2.0));
56        prob.add_constraint(dummy).unwrap();
57        num_con += 1;
58    }
59
60    // final constraint
61    let base = (num_n - 1) * 12;
62    for k in 0_usize..3 {
63        // position
64        let mut dummy = Constraint::new(ConstraintType::Eq, p_g[k], format!("dummy{}", num_con));
65        dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
66        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
67        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
68        dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
69        prob.add_constraint(dummy).unwrap();
70        num_con += 1;
71
72        // velocity
73        let mut dummy = Constraint::new(ConstraintType::Eq, v_g[k], format!("dummy{}", num_con));
74        dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
75        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
76        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
77        prob.add_constraint(dummy).unwrap();
78        num_con += 1;
79
80        //accelerate
81        let mut dummy = Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
82        dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
83        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
84        prob.add_constraint(dummy).unwrap();
85        num_con += 1;
86    }
87
88    // continuity
89
90    for i in 0..num_n - 1 {
91        let base = i * 12;
92        for k in 0_usize..dim {
93            // position
94            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
95            dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
96            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
97            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
98            dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
99            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 9 + k, -1.0));
100            prob.add_constraint(dummy).unwrap();
101            num_con += 1;
102
103            // velocity
104            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
105            dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
106            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
107            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
108            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 6 + k, -1.0));
109            prob.add_constraint(dummy).unwrap();
110            num_con += 1;
111
112            //accelerate
113            let mut dummy =
114                Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
115            dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
116            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
117            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 3 + k, -2.0));
118            prob.add_constraint(dummy).unwrap();
119            num_con += 1;
120        }
121    }
122
123    // maximize the objective
124    let mut qp_vars: Vec<usize> = Vec::new();
125    let dummy = Constraint::new(ConstraintType::Eq, 0.0, "obj");
126    for i in 0..num_n {
127        let base = i * 12;
128        for k in 0..dim {
129            qp_vars.push(base + k);
130            // dummy.add_wvar(WeightedVariable::new_idx(base + k, 0.0));
131        }
132    }
133    prob.set_qp_objective(ObjectiveType::Minimize, dummy, qp_vars.clone(), dt / 36.0)
134        .unwrap();
135
136    // solve the problem
137    let sol = prob.solve(ProblemType::MixedInteger).unwrap();
138
139    let t2 = std::time::Instant::now();
140
141    println!("time cost {:?}", t2 - t1);
142
143    println!("{:.4?}", sol);
144    let sol_vars = sol.variables;
145    let mut f = 0.0_f64;
146    for idx in qp_vars.into_iter() {
147        let value = sol_vars[idx];
148        if let VariableValue::Continuous(v) = value {
149            f += v.powi(2);
150        }
151    }
152    f = f * dt / 36.0 / 2.0;
153    println!("f = {}", f);
154}
Source

pub fn add_lazy_constraint(&mut self, con: Constraint) -> Result<usize, String>

Adds a lazy constraint to the problem.

Returns the index of the constraint. Unclear if this has any value whatsoever.

Source

pub fn set_objective( &mut self, ty: ObjectiveType, con: Constraint, ) -> Result<(), String>

Set the objective coefficients. A Constraint object is used because it encodes a weighted sum, although it is semantically incorrect. The right-hand-side and kind of (in)equality of the Constraint are ignored.

Source

pub fn set_qp_objective( &mut self, ty: ObjectiveType, con: Constraint, qp_var: Vec<usize>, beta: f64, ) -> Result<(), String>

Examples found in repository?
examples/qcp.rs (line 133)
7fn main() {
8    let num_n = 6_usize;
9    let dim = 3_usize;
10    let env = Env::new().unwrap();
11
12    // starting moment
13    let t1 = std::time::Instant::now();
14    // populate it with a problem
15    let mut prob = Problem::new(&env, "miqpex1").unwrap();
16
17    for i in 0..dim * num_n * 4 {
18        let name = format!("x{:02}", i);
19        let _x = prob
20            .add_variable(Variable::new(
21                VariableType::Continuous,
22                0.0,
23                -100.0,
24                100.0,
25                name,
26            ))
27            .unwrap();
28    }
29    let p_s = [1.0, 0.0, -1.0];
30    let v_s = [0.0, 0.0, 0.0];
31    let a_s = [0.0, 0.0, 0.0];
32    let p_g = [8.0, 18.0, 5.0];
33    let v_g = [0.0, 0.0, 5.0];
34    let a_g = [0.0, 0.0, 0.0];
35
36    let dt: f64 = 1.0;
37    let mut num_con = 0;
38
39    // init constraint
40    for k in 0_usize..3 {
41        // position
42        let mut dummy = Constraint::new(ConstraintType::Eq, p_s[k], format!("dummy{}", num_con));
43        dummy.add_wvar(WeightedVariable::new_idx(9 + k, 1.0));
44        prob.add_constraint(dummy).unwrap();
45        num_con += 1;
46
47        // velocity
48        let mut dummy = Constraint::new(ConstraintType::Eq, v_s[k], format!("dummy{}", num_con));
49        dummy.add_wvar(WeightedVariable::new_idx(6 + k, 1.0));
50        prob.add_constraint(dummy).unwrap();
51        num_con += 1;
52
53        //accelerate
54        let mut dummy = Constraint::new(ConstraintType::Eq, a_s[k], format!("dummy{}", num_con));
55        dummy.add_wvar(WeightedVariable::new_idx(3 + k, 2.0));
56        prob.add_constraint(dummy).unwrap();
57        num_con += 1;
58    }
59
60    // final constraint
61    let base = (num_n - 1) * 12;
62    for k in 0_usize..3 {
63        // position
64        let mut dummy = Constraint::new(ConstraintType::Eq, p_g[k], format!("dummy{}", num_con));
65        dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
66        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
67        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
68        dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
69        prob.add_constraint(dummy).unwrap();
70        num_con += 1;
71
72        // velocity
73        let mut dummy = Constraint::new(ConstraintType::Eq, v_g[k], format!("dummy{}", num_con));
74        dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
75        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
76        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
77        prob.add_constraint(dummy).unwrap();
78        num_con += 1;
79
80        //accelerate
81        let mut dummy = Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
82        dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
83        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
84        prob.add_constraint(dummy).unwrap();
85        num_con += 1;
86    }
87
88    // continuity
89
90    for i in 0..num_n - 1 {
91        let base = i * 12;
92        for k in 0_usize..dim {
93            // position
94            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
95            dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
96            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
97            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
98            dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
99            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 9 + k, -1.0));
100            prob.add_constraint(dummy).unwrap();
101            num_con += 1;
102
103            // velocity
104            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
105            dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
106            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
107            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
108            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 6 + k, -1.0));
109            prob.add_constraint(dummy).unwrap();
110            num_con += 1;
111
112            //accelerate
113            let mut dummy =
114                Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
115            dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
116            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
117            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 3 + k, -2.0));
118            prob.add_constraint(dummy).unwrap();
119            num_con += 1;
120        }
121    }
122
123    // maximize the objective
124    let mut qp_vars: Vec<usize> = Vec::new();
125    let dummy = Constraint::new(ConstraintType::Eq, 0.0, "obj");
126    for i in 0..num_n {
127        let base = i * 12;
128        for k in 0..dim {
129            qp_vars.push(base + k);
130            // dummy.add_wvar(WeightedVariable::new_idx(base + k, 0.0));
131        }
132    }
133    prob.set_qp_objective(ObjectiveType::Minimize, dummy, qp_vars.clone(), dt / 36.0)
134        .unwrap();
135
136    // solve the problem
137    let sol = prob.solve(ProblemType::MixedInteger).unwrap();
138
139    let t2 = std::time::Instant::now();
140
141    println!("time cost {:?}", t2 - t1);
142
143    println!("{:.4?}", sol);
144    let sol_vars = sol.variables;
145    let mut f = 0.0_f64;
146    for idx in qp_vars.into_iter() {
147        let value = sol_vars[idx];
148        if let VariableValue::Continuous(v) = value {
149            f += v.powi(2);
150        }
151    }
152    f = f * dt / 36.0 / 2.0;
153    println!("f = {}", f);
154}
Source

pub fn set_objective_type(&mut self, ty: ObjectiveType) -> Result<(), String>

Change the objective type. Default: ObjectiveType::Minimize.

It is recommended to use this in conjunction with objective coefficients set by the var! macro rather than using set_objective.

Source

pub fn write<S>(&self, name: S) -> Result<(), String>
where S: Into<String>,

Write the problem to a file named name. At this time, it is not possible to use a Write object instead, as this calls C code directly.

Source

pub fn add_initial_soln( &mut self, vars: &[usize], values: &[f64], ) -> Result<(), String>

Add an initial solution to the problem.

vars is an array of indices (i.e. the result of prob.add_variable) and values are their values.

Source

pub fn solve_as(&mut self, pt: ProblemType) -> Result<Solution, String>

Solve the Problem, returning a Solution object with the result.

Source

pub fn solve(&mut self, prob_type: ProblemType) -> Result<Solution, String>

Solve the problem as a Mixed Integer Program

Examples found in repository?
examples/qcp.rs (line 137)
7fn main() {
8    let num_n = 6_usize;
9    let dim = 3_usize;
10    let env = Env::new().unwrap();
11
12    // starting moment
13    let t1 = std::time::Instant::now();
14    // populate it with a problem
15    let mut prob = Problem::new(&env, "miqpex1").unwrap();
16
17    for i in 0..dim * num_n * 4 {
18        let name = format!("x{:02}", i);
19        let _x = prob
20            .add_variable(Variable::new(
21                VariableType::Continuous,
22                0.0,
23                -100.0,
24                100.0,
25                name,
26            ))
27            .unwrap();
28    }
29    let p_s = [1.0, 0.0, -1.0];
30    let v_s = [0.0, 0.0, 0.0];
31    let a_s = [0.0, 0.0, 0.0];
32    let p_g = [8.0, 18.0, 5.0];
33    let v_g = [0.0, 0.0, 5.0];
34    let a_g = [0.0, 0.0, 0.0];
35
36    let dt: f64 = 1.0;
37    let mut num_con = 0;
38
39    // init constraint
40    for k in 0_usize..3 {
41        // position
42        let mut dummy = Constraint::new(ConstraintType::Eq, p_s[k], format!("dummy{}", num_con));
43        dummy.add_wvar(WeightedVariable::new_idx(9 + k, 1.0));
44        prob.add_constraint(dummy).unwrap();
45        num_con += 1;
46
47        // velocity
48        let mut dummy = Constraint::new(ConstraintType::Eq, v_s[k], format!("dummy{}", num_con));
49        dummy.add_wvar(WeightedVariable::new_idx(6 + k, 1.0));
50        prob.add_constraint(dummy).unwrap();
51        num_con += 1;
52
53        //accelerate
54        let mut dummy = Constraint::new(ConstraintType::Eq, a_s[k], format!("dummy{}", num_con));
55        dummy.add_wvar(WeightedVariable::new_idx(3 + k, 2.0));
56        prob.add_constraint(dummy).unwrap();
57        num_con += 1;
58    }
59
60    // final constraint
61    let base = (num_n - 1) * 12;
62    for k in 0_usize..3 {
63        // position
64        let mut dummy = Constraint::new(ConstraintType::Eq, p_g[k], format!("dummy{}", num_con));
65        dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
66        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
67        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
68        dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
69        prob.add_constraint(dummy).unwrap();
70        num_con += 1;
71
72        // velocity
73        let mut dummy = Constraint::new(ConstraintType::Eq, v_g[k], format!("dummy{}", num_con));
74        dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
75        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
76        dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
77        prob.add_constraint(dummy).unwrap();
78        num_con += 1;
79
80        //accelerate
81        let mut dummy = Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
82        dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
83        dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
84        prob.add_constraint(dummy).unwrap();
85        num_con += 1;
86    }
87
88    // continuity
89
90    for i in 0..num_n - 1 {
91        let base = i * 12;
92        for k in 0_usize..dim {
93            // position
94            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
95            dummy.add_wvar(WeightedVariable::new_idx(base + k, dt.powi(3)));
96            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, dt.powi(2)));
97            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, dt));
98            dummy.add_wvar(WeightedVariable::new_idx(base + 9 + k, 1.0));
99            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 9 + k, -1.0));
100            prob.add_constraint(dummy).unwrap();
101            num_con += 1;
102
103            // velocity
104            let mut dummy = Constraint::new(ConstraintType::Eq, 0.0, format!("dummy{}", num_con));
105            dummy.add_wvar(WeightedVariable::new_idx(base + k, 3.0 * dt.powi(2)));
106            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0 * dt));
107            dummy.add_wvar(WeightedVariable::new_idx(base + 6 + k, 1.0));
108            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 6 + k, -1.0));
109            prob.add_constraint(dummy).unwrap();
110            num_con += 1;
111
112            //accelerate
113            let mut dummy =
114                Constraint::new(ConstraintType::Eq, a_g[k], format!("dummy{}", num_con));
115            dummy.add_wvar(WeightedVariable::new_idx(base + k, 6.0 * dt));
116            dummy.add_wvar(WeightedVariable::new_idx(base + 3 + k, 2.0));
117            dummy.add_wvar(WeightedVariable::new_idx(base + 12 + 3 + k, -2.0));
118            prob.add_constraint(dummy).unwrap();
119            num_con += 1;
120        }
121    }
122
123    // maximize the objective
124    let mut qp_vars: Vec<usize> = Vec::new();
125    let dummy = Constraint::new(ConstraintType::Eq, 0.0, "obj");
126    for i in 0..num_n {
127        let base = i * 12;
128        for k in 0..dim {
129            qp_vars.push(base + k);
130            // dummy.add_wvar(WeightedVariable::new_idx(base + k, 0.0));
131        }
132    }
133    prob.set_qp_objective(ObjectiveType::Minimize, dummy, qp_vars.clone(), dt / 36.0)
134        .unwrap();
135
136    // solve the problem
137    let sol = prob.solve(ProblemType::MixedInteger).unwrap();
138
139    let t2 = std::time::Instant::now();
140
141    println!("time cost {:?}", t2 - t1);
142
143    println!("{:.4?}", sol);
144    let sol_vars = sol.variables;
145    let mut f = 0.0_f64;
146    for idx in qp_vars.into_iter() {
147        let value = sol_vars[idx];
148        if let VariableValue::Continuous(v) = value {
149            f += v.powi(2);
150        }
151    }
152    f = f * dt / 36.0 / 2.0;
153    println!("f = {}", f);
154}

Trait Implementations§

Source§

impl<'a> Drop for Problem<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Problem<'a>

§

impl<'a> RefUnwindSafe for Problem<'a>

§

impl<'a> !Send for Problem<'a>

§

impl<'a> !Sync for Problem<'a>

§

impl<'a> Unpin for Problem<'a>

§

impl<'a> UnwindSafe for Problem<'a>

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.