CbcSolver

Struct CbcSolver 

Source
pub struct CbcSolver { /* private fields */ }

Implementations§

Source§

impl CbcSolver

Source

pub fn new() -> CbcSolver

Examples found in repository?
examples/assignment.rs (line 59)
8fn main() {
9    // Problem Data
10    let men = vec!["A", "B", "C"];
11    let women = vec!["D", "E", "F"];
12    let compatibility_score: HashMap<(&str, &str),f32> = vec![
13        (("A", "D"), 50.0),
14        (("A", "E"), 75.0),
15        (("A", "F"), 75.0),
16        (("B", "D"), 60.0),
17        (("B", "E"), 95.0),
18        (("B", "F"), 80.0),
19        (("C", "D"), 60.0),
20        (("C", "E"), 70.0),
21        (("C", "F"), 80.0),
22    ].into_iter().collect();
23
24    // Define Problem
25    let mut problem = LpProblem::new("Matchmaking", LpObjective::Maximize);
26
27    // Define Variables
28    let vars: HashMap<(&str,&str), LpBinary> =
29        men.iter()
30            .flat_map(|&m| women.iter()
31            .map(move |&w| {
32                let key = (m,w);
33                let value = LpBinary::new(&format!("{}_{}", m,w));
34                (key, value)
35            }))
36            .collect();
37
38    // Define Objective Function
39    let obj_vec: Vec<LpExpression> = {
40       vars.iter().map( |(&(m,w), bin)| {
41           let &coef = compatibility_score.get(&(m, w)).unwrap();
42           coef * bin
43       } )
44    }.collect();
45    problem += obj_vec.sum();
46
47    // Define Constraints
48    // - constraint 1: Each man must be assigned to exactly one woman
49    for &m in &men{
50        problem += sum(&women, |&w| vars.get(&(m,w)).unwrap() ).equal(1);
51    }
52
53    // - constraint 2: Each woman must be assigned to exactly one man
54    for &w in &women{
55        problem += sum(&men, |&m| vars.get(&(m,w)).unwrap() ).equal(1);
56    }
57
58    // Run Solver
59    let solver = CbcSolver::new();
60    let result = solver.run(&problem);
61
62    // Compute final objective function value
63    // (terminate if error, or assign status & variable values)
64    assert!(result.is_ok(), result.unwrap_err());
65    let solution = result.unwrap();
66    let mut obj_value = 0f32;
67    for (&(m, w), var) in &vars{
68        let obj_coef = compatibility_score.get(&(m, w)).unwrap();
69        let var_value = solution.results.get(&var.name).unwrap();
70
71        obj_value += obj_coef * var_value;
72    }
73
74    // Print output
75    println!("Status: {:?}", solution.status);
76    println!("Objective Value: {}", obj_value);
77    for (var_name, var_value) in &solution.results{
78        let int_var_value = *var_value as u32;
79        if int_var_value == 1{
80            println!("{} = {}", var_name, int_var_value);
81        }
82    }
83}
Source

pub fn command_name(&self, command_name: String) -> CbcSolver

Source

pub fn with_temp_solution_file(&self, temp_solution_file: String) -> CbcSolver

Trait Implementations§

Source§

impl Clone for CbcSolver

Source§

fn clone(&self) -> CbcSolver

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 CbcSolver

Source§

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

Formats the value using the given formatter. Read more
Source§

impl SolverTrait for CbcSolver

Source§

type P = LpProblem

Source§

fn run<'a>(&self, problem: &'a Self::P) -> Result<Solution<'a>, String>

Source§

impl SolverWithSolutionParsing for CbcSolver

Source§

fn read_specific_solution<'a>( &self, f: &File, problem: Option<&'a LpProblem>, ) -> Result<Solution<'a>, String>

Source§

fn read_solution<'a>( &self, temp_solution_file: &String, problem: Option<&'a LpProblem>, ) -> Result<Solution<'a>, String>

Source§

impl WithMaxSeconds<CbcSolver> for CbcSolver

Source§

impl WithNbThreads<CbcSolver> for CbcSolver

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.