Struct coin_cbc::Model

source ·
pub struct Model { /* private fields */ }
Expand description

A MILP model.

Implementations§

source§

impl Model

source

pub fn num_rows(&self) -> u32

Gets the current number of rows of the model.

source

pub fn num_cols(&self) -> u32

Gets the current number of columns of the model.

source

pub fn remove_initial_solution(&mut self)

Removes the initial solution.

source

pub fn set_col_initial_solution(&mut self, col: Col, value: f64)

Sets the column value to the initial solution.

If the solution is not present, it will be initialized with 0 for all coefficients.

source

pub fn get_col_initial_solution(&self, col: Col) -> Option<f64>

Gets the column value to the initial solution.

source

pub fn set_initial_solution(&mut self, solution: &Solution)

Sets the initial solution from a Solution.

source

pub fn set_parameter(&mut self, key: &str, value: &str)

Sets a parameter.

For documentation, launch the cbc binary and type ?.

source

pub fn set_parameters( &mut self, iter: impl IntoIterator<Item = (impl AsRef<str>, impl AsRef<str>)> )

Sets parameters for an iterator.

source

pub fn rows(&self) -> impl Iterator<Item = Row>

Gets an iterator on the row identifiers.

source

pub fn cols(&self) -> impl Iterator<Item = Col>

Gets an iterator on the column identifiers.

source

pub fn add_col(&mut self) -> Col

Adds a column to the model. Returns the corresponding column identifier.

At creation, the bounds of the column are setted to [0, +∞].

source

pub fn add_integer(&mut self) -> Col

Adds an integer variable to the model.

Equivalent to adding a column and setting it to integer.

source

pub fn add_binary(&mut self) -> Col

Adds a binary variable to the model.

Equivalent to adding a column and setting it to binary.

Examples found in repository?
examples/knapsack.rs (line 15)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn add_row(&mut self) -> Row

Adds a row to the model. Returns the corresponding row identifier.

At creation, the bounds of the row are setted to [-∞, +∞].

Examples found in repository?
examples/knapsack.rs (line 23)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn set_weight(&mut self, row: Row, col: Col, weight: f64)

Sets the weight corresponding to the given row and column in the constraint matrix.

Examples found in repository?
examples/knapsack.rs (line 29)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn set_integer(&mut self, col: Col)

Changes the given column to integer variable.

source

pub fn set_continuous(&mut self, col: Col)

Changes the given column to continuous variable.

source

pub fn set_binary(&mut self, col: Col)

Changes the given column to binary variable.

Equivalent to setting the column as integer and restricting it to [0, 1].

source

pub fn set_col_upper(&mut self, col: Col, value: f64)

Sets the upper bound of the given column.

source

pub fn set_col_lower(&mut self, col: Col, value: f64)

Sets the lower bound of the given column.

source

pub fn set_obj_coeff(&mut self, col: Col, value: f64)

Sets the objective coefficient of the given variable.

Examples found in repository?
examples/knapsack.rs (line 36)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn set_row_upper(&mut self, row: Row, value: f64)

Sets the upper bound of the given row.

Examples found in repository?
examples/knapsack.rs (line 26)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn set_row_lower(&mut self, row: Row, value: f64)

Sets the lower bound of the given row.

source

pub fn set_row_equal(&mut self, row: Row, value: f64)

Force the given row to be equal to the given value.

Equivalent to setting the upper bound and the lower bound.

source

pub fn add_sos1<I: IntoIterator<Item = (Col, f64)>>( &mut self, columns_and_weights: I )

Add a special ordered set constraint, preventing all but one variable in a set from being non-zero at the same time. weights can be used as hints to the optimizer to improve the resolution speed. In case you don’t have any weights for your variables, you can use 1, 2, 3, … For more information about SOS weights, see: http://lpsolve.sourceforge.net/5.5/SOS.htm

source

pub fn add_sos2<I: IntoIterator<Item = (Col, f64)>>( &mut self, columns_and_weights: I )

Add a special ordered set constraint, preventing all but two adjacent variables in a set from being non-zero at the same time. Weights determine the adjacency of the variables. For more information about SOS weights, see: http://lpsolve.sourceforge.net/5.5/SOS.htm

source

pub fn set_obj_sense(&mut self, sense: Sense)

Sets the objective sense.

Examples found in repository?
examples/knapsack.rs (line 43)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}
source

pub fn to_raw(&self) -> Model

Construct a raw::Model corresponding to the current state.

source

pub fn solve(&self) -> Solution

Solves the model. Returns the solution.

Examples found in repository?
examples/knapsack.rs (line 46)
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
fn main() {
    // a simple knapsack problem
    // Maximize  5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5]
    // s.t.      2x[1] + 8x[2] + 4x[3] + 2x[4] + 5x[5] <= 10
    // All x binary

    // Create the problem.
    let mut m = Model::default();

    // The columns. m.add_binary() returns a typed indentifier for a
    // column.
    let cols = [
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
        m.add_binary(),
    ];

    // The row. m.add_row() returns a typed identifier for a row.
    let row = m.add_row();

    // Set the bound of the knapsack constraint.
    m.set_row_upper(row, 10.);

    // Set weights of the constraint.
    m.set_weight(row, cols[0], 2.);
    m.set_weight(row, cols[1], 8.);
    m.set_weight(row, cols[2], 4.);
    m.set_weight(row, cols[3], 2.);
    m.set_weight(row, cols[4], 5.);

    // Set objective coefficients
    m.set_obj_coeff(cols[0], 5.);
    m.set_obj_coeff(cols[1], 3.);
    m.set_obj_coeff(cols[2], 2.);
    m.set_obj_coeff(cols[3], 7.);
    m.set_obj_coeff(cols[4], 4.);

    // Set objective sense.
    m.set_obj_sense(Sense::Maximize);

    // Solve the problem. Returns the solution
    let sol = m.solve();

    // Check the result. sol.raw() returns a shared reference to the
    // raw bindings, allowing to use all getters.
    assert_eq!(Status::Finished, sol.raw().status());
    assert_eq!(16., sol.raw().obj_value());

    // Check for the solution.
    assert_eq!(1., sol.col(cols[0]));
    assert_eq!(0., sol.col(cols[1]));
    assert_eq!(0., sol.col(cols[2]));
    assert_eq!(1., sol.col(cols[3]));
    assert_eq!(1., sol.col(cols[4]));
}

Trait Implementations§

source§

impl Clone for Model

source§

fn clone(&self) -> Model

Returns a copy 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 Default for Model

source§

fn default() -> Model

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Model

§

impl Send for Model

§

impl Sync for Model

§

impl Unpin for Model

§

impl UnwindSafe for Model

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.