-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpricer.rs
More file actions
313 lines (274 loc) · 8.5 KB
/
pricer.rs
File metadata and controls
313 lines (274 loc) · 8.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::{Model, Solving, ffi};
use scip_sys::SCIP_Result;
/// A trait for SCIP pricers.
pub trait Pricer {
/// Generates negative reduced cost columns.
///
/// # Arguments
/// * `model`: the current model of the SCIP instance in `Solving` stage.
/// * `pricer`: the internal pricer object.
/// * `farkas`: If true, the pricer should generate columns to repair feasibility of LP.
fn generate_columns(
&mut self,
model: Model<Solving>,
pricer: SCIPPricer,
farkas: bool,
) -> PricerResult;
}
/// An enum representing the possible states of a `PricerResult`.
#[derive(Debug, PartialEq)]
pub enum PricerResultState {
/// The pricer did not run.
DidNotRun,
/// The pricer added new columns with negative reduced cost.
FoundColumns,
/// The pricer did not find any columns with negative reduced cost (i.e. current LP solution is optimal).
NoColumns,
/// The pricer wants to perform early branching.
StopEarly,
}
/// A struct representing the result of a pricer.
pub struct PricerResult {
/// The state of the pricer result.
pub state: PricerResultState,
/// A calculated lower bound on the objective value of the current node.
pub lower_bound: Option<f64>,
}
impl From<PricerResultState> for SCIP_Result {
/// Converts a `PricerResultState` enum variant to an `SCIP_Result` value.
fn from(val: PricerResultState) -> Self {
match val {
PricerResultState::DidNotRun => ffi::SCIP_Result_SCIP_DIDNOTRUN,
PricerResultState::FoundColumns
| PricerResultState::StopEarly
| PricerResultState::NoColumns => ffi::SCIP_Result_SCIP_SUCCESS,
}
}
}
/// A wrapper around a SCIP pricer object.
pub struct SCIPPricer {
pub(crate) raw: *mut ffi::SCIP_PRICER,
}
impl SCIPPricer {
/// Returns the internal raw pointer of the pricer.
pub fn inner(&self) -> *mut ffi::SCIP_PRICER {
self.raw
}
/// Returns the name of the pricer.
pub fn name(&self) -> String {
unsafe {
let name = ffi::SCIPpricerGetName(self.raw);
std::ffi::CStr::from_ptr(name)
.to_string_lossy()
.into_owned()
}
}
/// Returns the description of the pricer.
pub fn desc(&self) -> String {
unsafe {
let desc = ffi::SCIPpricerGetDesc(self.raw);
std::ffi::CStr::from_ptr(desc)
.to_string_lossy()
.into_owned()
}
}
/// Returns the priority of the pricer.
pub fn priority(&self) -> i32 {
unsafe { ffi::SCIPpricerGetPriority(self.raw) }
}
/// Returns the delay of the pricer.
pub fn is_delayed(&self) -> bool {
unsafe { ffi::SCIPpricerIsDelayed(self.raw) != 0 }
}
/// Returns whether the pricer is active.
pub fn is_active(&self) -> bool {
unsafe { ffi::SCIPpricerIsActive(self.raw) != 0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::pricer;
use crate::{
Model, ProblemOrSolving, Solving, model::ModelWithProblem, status::Status,
variable::VarType,
};
struct LyingPricer;
impl Pricer for LyingPricer {
fn generate_columns(
&mut self,
_model: Model<Solving>,
_pricer: SCIPPricer,
_farkas: bool,
) -> PricerResult {
PricerResult {
state: PricerResultState::FoundColumns,
lower_bound: None,
}
}
}
#[test]
#[should_panic]
fn nothing_pricer() {
let pr = LyingPricer {};
let mut model = crate::model::Model::new()
.hide_output()
.include_default_plugins()
.read_prob("data/test/simple.lp")
.unwrap();
model.add(pricer(pr));
model.solve();
}
struct EarlyStoppingPricer;
impl Pricer for EarlyStoppingPricer {
fn generate_columns(
&mut self,
_model: Model<Solving>,
_pricer: SCIPPricer,
_farkas: bool,
) -> PricerResult {
PricerResult {
state: PricerResultState::StopEarly,
lower_bound: None,
}
}
}
#[test]
#[should_panic]
/// Stops pricing early then throws an error that no branching can be performed
fn early_stopping_pricer() {
let pr = EarlyStoppingPricer {};
let mut model = crate::model::Model::new()
.hide_output()
.include_default_plugins()
.read_prob("data/test/simple.lp")
.unwrap();
model.add(pricer(pr));
model.solve();
}
struct OptimalPricer;
impl Pricer for OptimalPricer {
fn generate_columns(
&mut self,
_model: Model<Solving>,
_pricer: SCIPPricer,
_farkas: bool,
) -> PricerResult {
PricerResult {
state: PricerResultState::NoColumns,
lower_bound: None,
}
}
}
#[test]
fn optimal_pricer() {
let pr = OptimalPricer {};
let mut model = crate::model::Model::new()
.hide_output()
.include_default_plugins()
.read_prob("data/test/simple.lp")
.unwrap();
model.add(pricer(pr));
let solved = model.solve();
assert_eq!(solved.status(), Status::Optimal);
}
#[derive(Debug, PartialEq, Clone)]
struct ComplexData {
a: Vec<usize>,
b: f64,
c: Option<isize>,
}
struct AddSameColumnPricer {
added: bool,
data: ComplexData,
}
impl Pricer for AddSameColumnPricer {
fn generate_columns(
&mut self,
mut model: Model<Solving>,
_pricer: SCIPPricer,
_farkas: bool,
) -> PricerResult {
assert_eq!(self.data.a, (0..1000).collect::<Vec<usize>>());
if self.added {
PricerResult {
state: PricerResultState::NoColumns,
lower_bound: None,
}
} else {
self.added = true;
let nvars_before = model.n_vars();
let var = model.add_priced_var(0.0, 1.0, 1.0, "x", VarType::Binary);
let conss = model.conss();
for cons in conss {
model.add_cons_coef(&cons, &var, 1.0);
}
let nvars_after = model.n_vars();
assert_eq!(nvars_before + 1, nvars_after);
PricerResult {
state: PricerResultState::FoundColumns,
lower_bound: None,
}
}
}
}
#[test]
fn add_same_column_pricer() {
let mut model = crate::model::Model::new()
.hide_output()
.include_default_plugins()
.read_prob("data/test/simple.lp")
.unwrap();
let conss = model.conss();
for c in conss {
model.set_cons_modifiable(&c, true);
}
let pr = AddSameColumnPricer {
added: false,
data: ComplexData {
a: (0..1000).collect::<Vec<usize>>(),
b: 1.0,
c: Some(1),
},
};
model.add(pricer(pr));
let solved = model.solve();
assert_eq!(solved.status(), Status::Optimal);
}
struct InternalSCIPPricerTester;
impl Pricer for InternalSCIPPricerTester {
fn generate_columns(
&mut self,
_model: Model<Solving>,
pricer: SCIPPricer,
_farkas: bool,
) -> PricerResult {
assert_eq!(pricer.name(), "internal");
assert_eq!(pricer.desc(), "internal pricer");
assert_eq!(pricer.priority(), 100);
assert!(!pricer.is_delayed());
assert!(pricer.is_active());
PricerResult {
state: PricerResultState::NoColumns,
lower_bound: None,
}
}
}
#[test]
fn internal_pricer() {
let pr = InternalSCIPPricerTester {};
let mut model = crate::model::Model::new()
.hide_output()
.include_default_plugins()
.read_prob("data/test/simple.lp")
.unwrap();
model.add(
pricer(pr)
.name("internal")
.desc("internal pricer")
.priority(100)
.delay(false),
);
model.solve();
}
}