pub struct Solution { /* private fields */ }
Expand description
A solution to a MILP problem.
This is a thin wrapper over a raw::Model
with accessors using
the typed identifiers.
Implementations§
source§impl Solution
impl Solution
sourcepub fn raw(&self) -> &Model
pub fn raw(&self) -> &Model
Gets a shared reference to the internal raw::Model
.
Examples found in repository?
examples/knapsack.rs (line 50)
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]));
}
sourcepub fn col(&self, col: Col) -> f64
pub fn col(&self, col: Col) -> f64
Gets the value of the given column in the solution.
Examples found in repository?
examples/knapsack.rs (line 54)
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]));
}
sourcepub fn is_basic(&self, col: Col) -> bool
pub fn is_basic(&self, col: Col) -> bool
Returns whether the given variable is basic (equal to zero in the solution)
sourcepub fn row_activity(&self, row: Row) -> f64
Available on crate feature cbc-310
only.
pub fn row_activity(&self, row: Row) -> f64
cbc-310
only.Primal row solution : gets the value of the linear expression in the given constraint
sourcepub fn reduced_cost(&self, col: Col) -> f64
Available on crate feature cbc-310
only.
pub fn reduced_cost(&self, col: Col) -> f64
cbc-310
only.For a minimization problem, the reduced cost of a nonbasic variable (a variable that is null in the solution) is the amount by which the value of the objective will decrease if we increase the value of the variable by 1
Auto Trait Implementations§
impl RefUnwindSafe for Solution
impl !Send for Solution
impl !Sync for Solution
impl Unpin for Solution
impl UnwindSafe for Solution
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more