LpProblem

Struct LpProblem 

Source
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

Source

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}
Source

pub fn variables(&self) -> HashMap<String, (usize, usize)>

Trait Implementations§

Source§

impl AddAssign<LpConstraint> for LpProblem

Add constraints

Source§

fn add_assign(&mut self, _rhs: LpConstraint)

Performs the += operation. Read more
Source§

impl<T> AddAssign<T> for LpProblem
where T: Into<LpExpression>,

Add an expression as an objective function

Source§

fn add_assign(&mut self, _rhs: T)

Performs the += operation. Read more
Source§

impl Debug for LpProblem

Source§

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

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

impl LpFileFormat for LpProblem

Source§

fn to_lp_file_format(&self) -> String

Source§

fn write_lp(&self, file_model: &str) -> Result<()>

Source§

impl Problem for LpProblem

Source§

fn add_objective_expression(&mut self, expr_arena: &mut LpExpression)

Source§

fn add_constraints(&mut self, constraint_expr: &LpConstraint)

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> 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, 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.