-
Notifications
You must be signed in to change notification settings - Fork 29
/
problems.rs
225 lines (188 loc) · 6.71 KB
/
problems.rs
1
2
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
extern crate lp_modeler;
use std::collections::HashMap;
use lp_modeler::solvers::{CbcSolver, SolverTrait, Solution};
#[cfg(feature = "native_coin_cbc")]
use lp_modeler::solvers::NativeCbcSolver;
use lp_modeler::dsl::*;
use lp_modeler::format::lp_format::LpFileFormat;
#[test]
fn test_readme_example_1() {
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).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),
}
let output1 = "\\ One Problem
Maximize
obj: 10 a + 20 b
Subject To
c1: 500 a + 1200 b + 1500 c <= 10000
c2: a - b <= 0
"
.to_string();
let output2 = problem.to_lp_file_format();
let output2 = output2.split("Generals").collect::<Vec<&str>>();
let output2 = output2[0];
assert_eq!(output1, output2);
}
#[test]
fn test_full_example() {
let ref a = LpInteger::new("a").lower_bound(1.0);
let ref b = LpInteger::new("b").upper_bound(10.0);
let ref c = LpInteger::new("c").lower_bound(2.0).upper_bound(8.5);
let ref d = LpBinary::new("d");
let ref e = LpContinuous::new("e");
let mut problem = LpProblem::new("One Problem", LpObjective::Maximize);
problem += a + b + c + d + e;
problem += (a + b + c + d + e).le(100.0);
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),
}
let output1 = problem.to_lp_file_format();
for expr in vec!("e free", "1 <= a", "2 <= c <= 8.5", "b <= 10") {
assert!(output1.contains(expr), format!("{} is not present",expr));
}
}
#[test]
fn test_readme_example_2() {
// Problem Data
let men = vec!["A", "B", "C"];
let women = vec!["D", "E", "F"];
let compat_scores : HashMap<(&str,&str),f32> = vec![
(("A", "D"), 50.0),
(("A", "E"), 75.0),
(("A", "F"), 75.0),
(("B", "D"), 60.0),
(("B", "E"), 95.0),
(("B", "F"), 80.0),
(("C", "D"), 60.0),
(("C", "E"), 70.0),
(("C", "F"), 80.0),
].into_iter().collect();
// Define Problem
let mut problem = LpProblem::new("Matchmaking", LpObjective::Maximize);
// Define Variables
let mut vars = HashMap::new();
for m in &men {
for w in &women {
vars.insert((m, w), LpBinary::new(&format!("{}_{}", m, w)));
}
}
// Define Objective Function
let mut obj_vec: Vec<LpExpression> = Vec::new();
for (&(&m, &w), var) in &vars {
let obj_coef = compat_scores.get(&(m, w)).unwrap();
obj_vec.push(*obj_coef * var);
}
problem += lp_sum(&obj_vec);
// Define Constraints
// Constraint 1: Each man must be assigned to exactly one woman
for m in &men {
let mut constr_vec = Vec::new();
for w in &women {
constr_vec.push(1.0 * vars.get(&(m, w)).unwrap());
}
problem += lp_sum(&constr_vec).equal(1);
}
// Constraint 2: Each woman must be assigned to exactly one man
for w in &women {
let mut constr_vec = Vec::new();
for m in &men {
constr_vec.push(1.0 * vars.get(&(m, w)).unwrap());
}
problem += lp_sum(&constr_vec).equal(1);
}
// Optionally write to file
// let result = problem.write_lp("problem.lp");
// match result{
// Ok(_) => println!("Written to file"),
// Err(msg) => println!("{}", msg)
// }
// Run Solver
let solver = CbcSolver::new();
let result = solver.run(&problem);
// Terminate if error, or assign status & variable values
assert!(result.is_ok(), result.unwrap_err());
let Solution { status: solver_status, results: var_values, related_problem: _ } = result.unwrap();
// Compute final objective function value
let mut obj_value = 0f32;
for (&(&m, &w), var) in &vars {
let obj_coef = compat_scores.get(&(m, w)).unwrap();
let var_value = var_values.get(&var.name).unwrap();
obj_value += obj_coef * var_value;
}
// Print output
println!("Status: {:?}", solver_status);
println!("Objective Value: {}", obj_value);
// println!("{:?}", var_values);
for (var_name, var_value) in &var_values {
let int_var_value = *var_value as u32;
if int_var_value == 1 {
println!("{} = {}", var_name, int_var_value);
}
}
assert_eq!(solver_status, lp_modeler::solvers::Status::Optimal);
assert_eq!(obj_value, 230f32);
assert_eq!(*var_values.get("A_F").unwrap(), 1f32);
assert_eq!(*var_values.get("B_E").unwrap(), 1f32);
assert_eq!(*var_values.get("C_D").unwrap(), 1f32);
}
#[cfg(feature = "native_coin_cbc")]
#[test]
// as in https://github.com/KardinalAI/coin_cbc/blob/master/examples/knapsack.rs
//
// Maximize 5a + 3b + 2c + 7d - 4e
// s.t. 2a - 8b + 4c + 2d + 5e <= 10
fn cbc_native_optimal() {
let mut problem = LpProblem::new("Knapsack", LpObjective::Maximize);
let objective: HashMap<&str, f32> =
vec![("a", 5.0), ("b", 3.0), ("c", 2.0), ("d", 7.0), ("e", 4.0)]
.into_iter()
.collect();
let x: HashMap<&str, LpBinary> = objective
.iter()
.map(|(name, _)| (*name, LpBinary::new(name)))
.collect();
problem +=
(2.0 * &x["a"] - 8.0 * &x["b"] + 4.0 * &x["c"] + 2. * &x["d"] + 5. * &x["e"]).le(10.);
problem += 5.0 * &x["a"] + 3.0 * &x["b"] + 2.0 * &x["c"] + 7. * &x["d"] + -4. * &x["e"];
let solver = NativeCbcSolver::new();
match solver.run(&problem) {
Ok(sol) => {
println!("Status {:?}", sol.status);
println!("{:?}", sol.results);
assert_eq!(
17f32,
x.iter()
.map(|(name, var)| match sol.results.get(&var.name) {
Some(s) => {
println!("{:?}*{}", s, objective.get(name).unwrap());
s * objective.get(name).unwrap()
}
_ => 0.,
})
.sum()
);
}
Err(msg) => panic!("Native Cbc Solver panicked at run: {}", msg),
}
}