pub struct CbcSolver { /* private fields */ }Implementations§
Source§impl CbcSolver
impl CbcSolver
Sourcepub fn new() -> CbcSolver
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}pub fn command_name(&self, command_name: String) -> CbcSolver
pub fn with_temp_solution_file(&self, temp_solution_file: String) -> CbcSolver
Trait Implementations§
Source§impl SolverTrait for CbcSolver
impl SolverTrait for CbcSolver
Source§impl WithMaxSeconds<CbcSolver> for CbcSolver
impl WithMaxSeconds<CbcSolver> for CbcSolver
fn max_seconds(&self) -> Option<u32>
fn with_max_seconds(&self, seconds: u32) -> CbcSolver
Source§impl WithNbThreads<CbcSolver> for CbcSolver
impl WithNbThreads<CbcSolver> for CbcSolver
fn nb_threads(&self) -> Option<u32>
fn with_nb_threads(&self, threads: u32) -> CbcSolver
Auto Trait Implementations§
impl Freeze for CbcSolver
impl RefUnwindSafe for CbcSolver
impl Send for CbcSolver
impl Sync for CbcSolver
impl Unpin for CbcSolver
impl UnwindSafe for CbcSolver
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