Variable

Struct Variable 

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

export all public structs and enums A Variable in a Problem.

The general usage pattern is to create Variables outside of a Problem with var!(...) and then add them to the Problem with prob.add_variable(...).

#[macro_use]
extern crate rplex;

use rplex::{Env, Problem, Variable};

fn main() {
    let env = Env::new().unwrap();
    let mut prob = Problem::new(&env, "dummy").unwrap();
    prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
    prob.add_variable(var!(0.0 <= "y" <= 100.0  -> 3.0 as Integer)).unwrap();
    prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
}

Implementations§

Source§

impl Variable

Source

pub fn new<S>(ty: VariableType, obj: f64, lb: f64, ub: f64, name: S) -> Variable
where S: Into<String>,

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}

Trait Implementations§

Source§

impl Clone for Variable

Source§

fn clone(&self) -> Variable

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 Variable

Source§

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

Formats the value using the given formatter. 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.