pub struct LpProblem {
pub name: &'static str,
pub unique_name: String,
pub objective_type: LpObjective,
pub obj_expr_arena: Option<LpExpression>,
pub constraints: Vec<LpConstraint>,
}Expand description
Structure used for creating the model and solving a linear problem.
§Examples:
use lp_modeler::dsl::*;
use lp_modeler::solvers::{SolverTrait, CbcSolver, Solution};
let ref a = LpInteger::new("a");
let ref b = LpInteger::new("b");
let ref c = LpInteger::new("c");
let mut problem = LpProblem::new("One Problem", LpObjective::Maximize);
problem += 10.0 * a + 20.0 * b;
problem += (500*a + 1200*b + 1500*c).le(10000);
problem += (a + b*2 + c).le(10);
problem += (a).le(b);
let solver = CbcSolver::new();
match solver.run(&problem) {
Ok( solution ) => {
println!("Status {:?}", solution.status);
for (name, value) in solution.results.iter() {
println!("value of {} = {}", name, value);
}
},
Err(msg) => println!("{}", msg),
}Fields§
§name: &'static str§unique_name: String§objective_type: LpObjective§obj_expr_arena: Option<LpExpression>§constraints: Vec<LpConstraint>Implementations§
Source§impl LpProblem
impl LpProblem
Sourcepub fn new(name: &'static str, objective: LpObjective) -> LpProblem
pub fn new(name: &'static str, objective: LpObjective) -> LpProblem
Create a new problem
Examples found in repository?
examples/assignment.rs (line 25)
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 variables(&self) -> HashMap<String, (usize, usize)>
Trait Implementations§
Source§impl AddAssign<LpConstraint> for LpProblem
Add constraints
impl AddAssign<LpConstraint> for LpProblem
Add constraints
Source§fn add_assign(&mut self, _rhs: LpConstraint)
fn add_assign(&mut self, _rhs: LpConstraint)
Performs the
+= operation. Read moreSource§impl<T> AddAssign<T> for LpProblemwhere
T: Into<LpExpression>,
Add an expression as an objective function
impl<T> AddAssign<T> for LpProblemwhere
T: Into<LpExpression>,
Add an expression as an objective function
Source§fn add_assign(&mut self, _rhs: T)
fn add_assign(&mut self, _rhs: T)
Performs the
+= operation. Read moreSource§impl LpFileFormat for LpProblem
impl LpFileFormat for LpProblem
Source§impl Problem for LpProblem
impl Problem for LpProblem
fn add_objective_expression(&mut self, expr_arena: &mut LpExpression)
fn add_constraints(&mut self, constraint_expr: &LpConstraint)
Auto Trait Implementations§
impl Freeze for LpProblem
impl RefUnwindSafe for LpProblem
impl Send for LpProblem
impl Sync for LpProblem
impl Unpin for LpProblem
impl UnwindSafe for LpProblem
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