-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
1077 lines (1003 loc) · 37.5 KB
/
lib.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! A Rust interface to IBM [CPLEX](https://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.0/ilog.odms.cplex.help/refcallablelibrary/homepageCrefman.html).
//!
//! During build, the default location is checked for a CPLEX installation. If yours is
//! installed outside of the default location, set the `CPLEX_LIB` environment variable to the
//! appropriate path.
//!
//! # Examples
//! Usage patterns will mirror C++ relatively closely, with the exception that in most cases
//! instead of applying copious operator overloading, instead macros are used.
//!
//! To be honest, I'm still not sure if that's a win or loss.
//!
//! ```rust
//! extern crate rplex;
//! use rplex::*;
//!
//! fn main() {
//! // create a CPLEX environment
//! let env = Env::new().unwrap();
//! // populate it with a problem
//! let mut prob = Problem::new(&env, "lpex1").unwrap();
//! // maximize the objective
//! prob.set_objective_type(ObjectiveType::Maximize).unwrap();
//! // create our variables
//! let x1 = prob.add_variable(var!(0.0 <= "x1" <= 40.0 -> 1.0)).unwrap();
//! let x2 = prob.add_variable(var!("x2" -> 2.0)).unwrap();
//! let x3 = prob.add_variable(var!("x3" -> 3.0)).unwrap();
//! println!("{} {} {}", x1, x2, x3);
//!
//! // add our constraints
//! prob.add_constraint(con!("c1": 20.0 >= (-1.0) x1 + 1.0 x2 + 1.0 x3)).unwrap();
//! prob.add_constraint(con!("c2": 30.0 >= 1.0 x1 + (-3.0) x2 + 1.0 x3)).unwrap();
//!
//! // solve the problem
//! let sol = prob.solve().unwrap();
//! println!("{:?}", sol);
//! // values taken from the output of `lpex1.c`
//! assert!(sol.objective == 202.5);
//! assert!(sol.variables == vec![VariableValue::Continuous(40.0),
//! VariableValue::Continuous(17.5),
//! VariableValue::Continuous(42.5)]);
//! }
//! ```
extern crate libc;
use libc::{c_int, c_char, c_double};
use std::ffi::CString;
/// Used by CPLEX to represent a variable that has no upper bound.
pub const INFINITY: f64 = 1.0E+20;
pub const EPINT_ID: c_int = 2010; // identifier of integrality tolerance parameter 'EpInt'
pub const EPINT: c_double = 1e-5; // default value for 'EpInt'
enum CEnv {}
enum CProblem {}
type CInt = c_int;
// #[link(name="cplex", kind="static")]
#[allow(non_snake_case)]
#[allow(dead_code)]
extern "C" {
// instantiation
fn CPXopenCPLEX(status: *mut c_int) -> *mut CEnv;
fn CPXcreateprob(env: *mut CEnv, status: *mut c_int, name: *const c_char) -> *mut CProblem;
fn CPXsetintparam(env: *mut CEnv, param: c_int, value: c_int) -> c_int;
fn CPXsetdblparam(env: *mut CEnv, param: c_int, value: c_double) -> c_int;
fn CPXgetintparam(env: *mut CEnv, param: c_int, value: *mut c_int) -> c_int;
fn CPXgetdblparam(env: *mut CEnv, param: c_int, value: *mut c_double) -> c_int;
fn CPXchgprobtype(env: *mut CEnv, lp: *mut CProblem, ptype: c_int) -> c_int;
// adding variables and constraints
fn CPXnewcols(env: *mut CEnv,
lp: *mut CProblem,
count: CInt,
obj: *const c_double,
lb: *const c_double,
ub: *const c_double,
xctype: *const c_char,
name: *const *const c_char)
-> c_int;
fn CPXaddrows(env: *mut CEnv,
lp: *mut CProblem,
col_count: CInt,
row_count: CInt,
nz_count: CInt,
rhs: *const c_double,
sense: *const c_char,
rmatbeg: *const CInt,
rmatind: *const CInt,
rmatval: *const c_double,
col_name: *const *const c_char,
row_name: *const *const c_char)
-> c_int;
fn CPXaddlazyconstraints(env: *mut CEnv,
lp: *mut CProblem,
row_count: CInt,
nz_count: CInt,
rhs: *const c_double,
sense: *const c_char,
rmatbeg: *const CInt,
rmatind: *const CInt,
rmatval: *const c_double,
row_name: *const *const c_char)
-> c_int;
// querying
fn CPXgetnumcols(env: *const CEnv, lp: *mut CProblem) -> CInt;
// setting objective
fn CPXchgobj(env: *mut CEnv,
lp: *mut CProblem,
cnt: CInt,
indices: *const CInt,
values: *const c_double)
-> c_int;
fn CPXchgobjsen(env: *mut CEnv, lp: *mut CProblem, maxormin: c_int) -> c_int;
// solving
fn CPXlpopt(env: *mut CEnv, lp: *mut CProblem) -> c_int;
fn CPXmipopt(env: *mut CEnv, lp: *mut CProblem) -> c_int;
// getting solution
fn CPXgetstat(env: *mut CEnv, lp: *mut CProblem) -> c_int;
fn CPXgetobjval(env: *mut CEnv, lp: *mut CProblem, objval: *mut c_double) -> c_int;
fn CPXgetx(env: *mut CEnv,
lp: *mut CProblem,
x: *mut c_double,
begin: CInt,
end: CInt)
-> c_int;
fn CPXsolution(env: *mut CEnv,
lp: *mut CProblem,
lpstat_p: *mut c_int,
objval_p: *mut c_double,
x: *mut c_double,
pi: *mut c_double,
slack: *mut c_double,
dj: *mut c_double)
-> c_int;
// adding initial solution
fn CPXaddmipstarts(env: *mut CEnv,
lp: *mut CProblem,
mcnt: CInt,
nzcnt: CInt,
beg: *const CInt,
varindices: *const CInt,
values: *const c_double,
effortlevel: *const CInt,
mipstartname: *const *const c_char)
-> c_int;
// debugging
fn CPXgeterrorstring(env: *mut CEnv, errcode: c_int, buff: *mut c_char) -> *mut c_char;
fn CPXwriteprob(env: *mut CEnv,
lp: *mut CProblem,
fname: *const c_char,
ftype: *const c_char)
-> c_int;
// freeing
fn CPXcloseCPLEX(env: *const *mut CEnv) -> c_int;
fn CPXfreeprob(env: *mut CEnv, lp: *const *mut CProblem) -> c_int;
}
fn errstr(env: *mut CEnv, errcode: c_int) -> Result<String, String> {
unsafe {
let mut buf = vec![0i8; 1024];
let res = CPXgeterrorstring(env, errcode, buf.as_mut_ptr());
if res == std::ptr::null_mut() {
Err(format!("No error string for {}", errcode))
} else {
Ok(String::from_utf8(buf.iter()
.take_while(|&&i| i != 0 && i != '\n' as i8)
.map(|&i| i as u8)
.collect::<Vec<u8>>())
.unwrap())
}
}
}
#[derive(Copy, Clone, Debug)]
enum ParamType {
Integer(c_int),
Double(c_double),
Boolean(c_int),
}
#[derive(Copy, Clone, Debug)]
pub enum EnvParam {
Threads(u64),
ScreenOutput(bool),
RelativeGap(f64),
/// When true, set CPX_PARALLEL_DETERMINISTIC (default). When false, set
/// CPX_PARALLEL_OPPORTUNISTIC.
ParallelDeterministic(bool),
}
impl EnvParam {
fn to_id(&self) -> c_int {
use EnvParam::*;
match self {
&Threads(_) => 1067,
&ScreenOutput(_) => 1035,
&RelativeGap(_) => 2009,
&ParallelDeterministic(_) => 1109,
}
}
fn param_type(&self) -> ParamType {
use EnvParam::*;
use ParamType::*;
match self {
&Threads(t) => Integer(t as c_int),
&ScreenOutput(b) => Boolean(b as c_int),
&RelativeGap(g) => Double(g as c_double),
&ParallelDeterministic(b) => Integer(if b { 1 } else { -1 }),
}
}
}
/// A CPLEX Environment. An `Env` is necessary to create a
/// `Problem`.
pub struct Env {
inner: *mut CEnv,
}
impl Env {
pub fn new() -> Result<Env, String> {
unsafe {
let mut status = 0;
let env = CPXopenCPLEX(&mut status);
if env == std::ptr::null_mut() {
Err(format!("CPLEX returned NULL for CPXopenCPLEX (status: {})", status))
} else {
// CPXsetintparam(env, 1035, 1); //ScreenOutput
// CPXsetintparam(env, 1056, 1); //Read_DataCheck
Ok(Env { inner: env })
}
}
}
/// Set an environment parameter. e.g.
///
/// ```
/// use rplex::{Env, EnvParam};
///
/// let mut env = Env::new().unwrap();
/// env.set_param(EnvParam::ScreenOutput(true)).unwrap();
/// env.set_param(EnvParam::RelativeGap(0.01)).unwrap();
/// ```
pub fn set_param(&mut self, p: EnvParam) -> Result<(), String> {
unsafe {
let status = match p.param_type() {
ParamType::Integer(i) => CPXsetintparam(self.inner, p.to_id(), i),
ParamType::Boolean(b) => CPXsetintparam(self.inner, p.to_id(), b),
ParamType::Double(d) => CPXsetdblparam(self.inner, p.to_id(), d),
};
if status != 0 {
return match errstr(self.inner, status) {
Ok(s) => Err(s),
Err(e) => Err(e),
};
} else {
return Ok(());
}
}
}
}
impl Drop for Env {
fn drop(&mut self) {
unsafe {
assert!(CPXcloseCPLEX(&self.inner) == 0);
}
}
}
/// A Variable in a Problem.
///
/// The general usage pattern is to create Variables outside of a
/// Problem with `var!(...)` and then add them to the Problem with
/// `prob.add_variable(...)`.
///
/// ```
/// #[macro_use]
/// extern crate rplex;
///
/// use rplex::{Env, Problem, Variable};
///
/// fn main() {
/// let env = Env::new().unwrap();
/// let mut prob = Problem::new(&env, "dummy").unwrap();
/// prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
/// prob.add_variable(var!(0.0 <= "y" <= 100.0 -> 3.0 as Integer)).unwrap();
/// prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
/// }
/// ```
#[derive(Clone, Debug)]
pub struct Variable {
index: Option<usize>,
ty: VariableType,
obj: f64,
lb: f64,
ub: f64,
name: String,
}
impl Variable {
pub fn new<S>(ty: VariableType, obj: f64, lb: f64, ub: f64, name: S) -> Variable
where S: Into<String>
{
Variable {
index: None,
ty: ty,
obj: obj,
lb: lb,
ub: ub,
name: name.into(),
}
}
}
/// Expressive creation of variables.
///
/// The general syntax is:
///
/// `var!(lower <= "name" <= upper -> objective as type)`
///
/// See `Variable` for examples.
#[macro_export]
macro_rules! var {
($lb:tt <= $name:tt <= $ub:tt -> $obj:tt as $vt:path) => {
{
use $crate::VariableType::*;
$crate::Variable::new ($vt, $obj, $lb, $ub, $name)
}
};
// continuous shorthand
($lb:tt <= $name:tt <= $ub:tt -> $obj:tt) => (var!($lb <= $name <= $ub -> $obj as Continuous));
// omit either lb or ub
($lb:tt <= $name:tt -> $obj:tt) => (var!($lb <= $name <= INFINITY -> $obj));
($name:tt <= $ub:tt -> $obj:tt) => (var!(0.0 <= $name <= $ub -> $obj));
// omit both
($name:tt -> $obj:tt) => (var!(0.0 <= $name -> $obj));
// typed version
($lb:tt <= $name:tt -> $obj:tt as $vt:path) => (var!($lb <= $name <= INFINITY -> $obj as $vt));
($name:tt <= $ub:tt -> $obj:tt as $vt:path) => (var!(0.0 <= $name <= $ub -> $obj as $vt));
($name:tt -> $obj:tt as Binary) => (var!(0.0 <= $name <= 1.0 -> $obj as Binary));
($name:tt -> $obj:tt as $vt:path) => (var!(0.0 <= $name -> $obj as $vt));
}
/// A variable with weight (row) coefficient. Used in the construction
/// of `Constraint`s.
#[derive(Clone, Debug)]
pub struct WeightedVariable {
var: usize,
weight: f64,
}
impl WeightedVariable {
/// Create a `WeightedVariable` from a `Variable`. Does not keep a
/// borrowed copy of `var`.
pub fn new_var(var: &Variable, weight: f64) -> Self {
WeightedVariable {
var: var.index.unwrap(),
weight: weight,
}
}
/// Create a `WeightedVariable` from a column index. This method
/// is used by the `con!` macro, as the return value of
/// `prob.add_variable` is a column index and thus the most common
/// value.
pub fn new_idx(idx: usize, weight: f64) -> Self {
WeightedVariable {
var: idx,
weight: weight,
}
}
}
/// A Constraint (row) object for a `Problem`.
///
/// The recommended way to build these is with the `con!` macro.
///
/// ```
/// #[macro_use]
/// extern crate rplex;
///
/// use rplex::{Env, Problem, Variable};
///
/// fn main() {
/// let env = Env::new().unwrap();
/// let mut prob = Problem::new(&env, "dummy").unwrap();
/// let x = prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
/// let y = prob.add_variable(var!(0.0 <= "y" <= 100.0 -> 3.0 as Integer)).unwrap();
/// let z = prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
/// prob.add_constraint(con!("dummy": 20.0 = 1.0 x + 2.0 y + 3.0 z)).unwrap();
/// prob.add_constraint(con!("dummy2": 1.0 <= (-1.0) x + 1.0 y)).unwrap();
/// }
/// ```
///
/// However, constraints can also be constructed manually from
/// `WeightedVariables`. This can be useful if your constraints don't
/// quite fit the grammar allowed by the `con!` macro. You can create
/// part of the constraint using `con!`, then augment it with
/// `add_wvar` to obtain the constraint you need.The following example
/// is identical to the above.
///
/// ```
/// #[macro_use]
/// extern crate rplex;
///
/// use rplex::{Env, Problem, Constraint, ConstraintType, WeightedVariable};
///
/// fn main() {
/// let env = Env::new().unwrap();
/// let mut prob = Problem::new(&env, "dummy").unwrap();
/// let x = prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
/// let y = prob.add_variable(var!(0.0 <= "y" <= 100.0 -> 3.0 as Integer)).unwrap();
/// let z = prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
/// let mut dummy = Constraint::new(ConstraintType::Eq, 20.0, "dummy");
/// dummy.add_wvar(WeightedVariable::new_idx(x, 1.0));
/// dummy.add_wvar(WeightedVariable::new_idx(y, 2.0));
/// dummy.add_wvar(WeightedVariable::new_idx(z, 3.0));
/// prob.add_constraint(dummy).unwrap();
/// let mut dummy2 = Constraint::new(ConstraintType::GreaterThanEq, 1.0, "dummy2");
/// dummy2.add_wvar(WeightedVariable::new_idx(x, -1.0));
/// dummy2.add_wvar(WeightedVariable::new_idx(y, 1.0));
/// prob.add_constraint(dummy2).unwrap();
/// }
/// ```
#[derive(Clone, Debug)]
pub struct Constraint {
index: Option<usize>,
vars: Vec<WeightedVariable>,
ty: ConstraintType,
rhs: f64,
name: String,
}
impl Constraint {
pub fn new<S, F>(ty: ConstraintType, rhs: F, name: S) -> Constraint
where S: Into<String>,
F: Into<f64>
{
Constraint {
index: None,
vars: vec![],
ty: ty,
rhs: rhs.into(),
name: name.into(),
}
}
/// Move a `WeightedVariable` into the Constraint.
pub fn add_wvar(&mut self, wvar: WeightedVariable) {
self.vars.push(wvar)
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! con_ty {
(=) => ($crate::ConstraintType::Eq);
(>=) => ($crate::ConstraintType::LessThanEq);
(<=) => ($crate::ConstraintType::GreaterThanEq);
}
/// Expressive macro for writing constraints.
///
/// # Examples
/// ## Basic Form: Explicit sum of variables
/// ```
/// # #[macro_use]
/// # extern crate rplex;
/// # fn main() {
/// let x1 = 1; let x2 = 2;
/// con!("basic": 0.0 <= 1.0 x1 + (-1.0) x2);
/// # }
/// ```
/// ## Basic Form: Unweighted sum of variables
/// ```
/// # #[macro_use]
/// # extern crate rplex;
/// # fn main() {
/// let xs = vec![1, 2];
/// con!("basic sum": 0.0 >= sum (&xs));
/// # }
/// ```
/// ## Basic Form: Weighted sum of variables
/// ```
/// # #[macro_use]
/// # extern crate rplex;
/// # fn main() {
/// let xs = vec![(1, 1.0), (2, -1.0)];
/// con!("basic weighted sum": 0.0 = wsum (&xs));
/// # }
/// ```
///
/// ## Mixed Forms
/// These can be mixed at will, separated by `+`.
///
/// ```
/// # #[macro_use]
/// # extern crate rplex;
/// # fn main() {
/// let x1 = 1; let x2 = 2;
/// let ys = vec![3, 4];
/// let zs = vec![(5, 1.0), (6, -1.0)];
/// con!("mixed sum": 0.0 <= 1.0 x1 + (-1.0) x2 + sum (&ys) + wsum (&zs));
/// # }
/// ```
#[macro_export]
macro_rules! con {
(@inner $con:ident sum $body:expr) => {
for &var in $body {
$con.add_wvar($crate::WeightedVariable::new_idx(var, 1.0));
}
};
(@inner $con:ident wsum $body:expr) => {
for &(var, weight) in $body {
$con.add_wvar($crate::WeightedVariable::new_idx(var, weight));
}
};
(@inner $con:ident $weight:tt $var:expr) => {
$con.add_wvar($crate::WeightedVariable::new_idx($var, $weight));
};
($name:tt : $rhs:tt $cmp:tt $c1:tt $x1:tt $(+ $c:tt $x:tt)*) => {
{
let mut con = $crate::Constraint::new(con_ty!($cmp), $rhs, $name);
con!(@inner con $c1 $x1);
$(
con!(@inner con $c $x);
)*
con
}
};
}
/// A CPLEX Problem.
#[allow(dead_code)]
pub struct Problem<'a> {
inner: *mut CProblem,
/// The Environment to which the Problem belongs.
env: &'a Env,
/// The name of the problem.
name: String,
variables: Vec<Variable>,
constraints: Vec<Constraint>,
lazy_constraints: Vec<Constraint>,
}
/// Solution to a CPLEX Problem.
///
/// Currently, there is no way to select which variables are extracted
/// when using `prob.solve()`. I am currently unfamiliar with the C
/// API for managing variables that remain unbound in the solution,
/// and so am unsure how to represent them.
#[derive(Clone, Debug)]
pub struct Solution {
/// The value of the objective reached by CPLEX.
pub objective: f64,
/// The values bound to each variable.
pub variables: Vec<VariableValue>,
}
#[derive(Copy, Clone, Debug)]
pub enum ObjectiveType {
Maximize,
Minimize,
}
#[derive(Copy, Clone, Debug)]
pub enum VariableType {
Continuous,
Binary,
Integer,
/// A variable bounded by `[lb, ub]` or equal to 0.
SemiContinuous,
/// An integer variable bounded by `[lb, ub]` or equal to 0.
SemiInteger,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum VariableValue {
Continuous(f64),
Binary(bool),
Integer(CInt),
SemiContinuous(f64),
SemiInteger(CInt),
}
/// Kind of (in)equality of a Constraint.
///
/// Note that the direction of the inequality is *opposite* what one
/// might expect from the `con!` macro. This is because the right- and
/// left-hand sides are flipped in the macro.
#[derive(Copy, Clone, Debug)]
pub enum ConstraintType {
LessThanEq,
Eq,
GreaterThanEq,
/// `Ranged` is currently unimplemented.
Ranged,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ProblemType {
Linear,
MixedInteger,
}
impl VariableType {
fn to_c(&self) -> c_char {
match self {
&VariableType::Continuous => 'C' as c_char,
&VariableType::Binary => 'B' as c_char,
&VariableType::Integer => 'I' as c_char,
&VariableType::SemiContinuous => 'S' as c_char,
&VariableType::SemiInteger => 'N' as c_char,
}
}
}
impl ConstraintType {
fn to_c(&self) -> c_char {
match self {
&ConstraintType::LessThanEq => 'L' as c_char,
&ConstraintType::Eq => 'E' as c_char,
&ConstraintType::GreaterThanEq => 'G' as c_char,
&ConstraintType::Ranged => unimplemented!(),
}
}
}
impl ObjectiveType {
fn to_c(&self) -> c_int {
match self {
&ObjectiveType::Minimize => 1 as c_int,
&ObjectiveType::Maximize => -1 as c_int,
}
}
}
impl<'a> Problem<'a> {
pub fn new<S>(env: &'a Env, name: S) -> Result<Self, String>
where S: Into<String>
{
unsafe {
let mut status = 0;
let name = name.into();
let prob = CPXcreateprob(env.inner,
&mut status,
CString::new(name.as_str()).unwrap().as_ptr());
if prob == std::ptr::null_mut() {
Err(format!("CPLEX returned NULL for CPXcreateprob ({} ({}))",
errstr(env.inner, status).unwrap(),
status))
} else {
Ok(Problem {
inner: prob,
env: env,
name: name,
variables: vec![],
constraints: vec![],
lazy_constraints: vec![],
})
}
}
}
/// Add a variable to the problem. The Variable is **moved** into
/// the problem. At this time, it is not possible to get a
/// reference to it back.
///
/// The column index for the Variable is returned.
pub fn add_variable(&mut self, var: Variable) -> Result<usize, String> {
unsafe {
let status = CPXnewcols(self.env.inner,
self.inner,
1,
&var.obj,
&var.lb,
&var.ub,
&var.ty.to_c(),
&CString::new(var.name.as_str()).unwrap().as_ptr());
if status != 0 {
Err(format!("Failed to add {:?} variable {} ({} ({}))",
var.ty,
var.name,
errstr(self.env.inner, status).unwrap(),
status))
} else {
let index = CPXgetnumcols(self.env.inner, self.inner) as usize - 1;
self.variables.push(Variable { index: Some(index), ..var });
Ok(index)
}
}
}
/// Add a constraint to the problem.
///
/// The row index for the constraint is returned.
pub fn add_constraint(&mut self, con: Constraint) -> Result<usize, String> {
let (ind, val): (Vec<CInt>, Vec<f64>) = con.vars
.iter()
.filter(|wv| wv.weight != 0.0)
.map(|wv| (wv.var as CInt, wv.weight))
.unzip();
let nz = val.len() as CInt;
unsafe {
let status = CPXaddrows(self.env.inner,
self.inner,
0,
1,
nz,
&con.rhs,
&con.ty.to_c(),
&0,
ind.as_ptr(),
val.as_ptr(),
std::ptr::null(),
&CString::new(con.name.as_str()).unwrap().as_ptr());
if status != 0 {
Err(format!("Failed to add {:?} constraint {} ({} ({}))",
con.ty,
con.name,
errstr(self.env.inner, status).unwrap(),
status))
} else {
let index = self.constraints.len();
self.constraints.push(Constraint { index: Some(index), ..con });
Ok(index)
}
}
}
/// Adds a lazy constraint to the problem.
///
/// Returns the index of the constraint. Unclear if this has any value whatsoever.
pub fn add_lazy_constraint(&mut self, con: Constraint) -> Result<usize, String> {
let (ind, val): (Vec<CInt>, Vec<f64>) = con.vars
.iter()
.filter(|wv| wv.weight != 0.0)
.map(|wv| (wv.var as CInt, wv.weight))
.unzip();
let nz = val.len() as CInt;
unsafe {
let status = CPXaddlazyconstraints(self.env.inner,
self.inner,
1,
nz,
&con.rhs,
&con.ty.to_c(),
&0,
ind.as_ptr(),
val.as_ptr(),
&CString::new(con.name.as_str()).unwrap().as_ptr());
if status != 0 {
Err(format!("Failed to add {:?} constraint {} ({} ({}))",
con.ty,
con.name,
errstr(self.env.inner, status).unwrap(),
status))
} else {
let index = self.lazy_constraints.len();
self.constraints.push(Constraint { index: Some(index), ..con });
Ok(index)
}
}
}
/// Set the objective coefficients. A Constraint object is used
/// because it encodes a weighted sum, although it is semantically
/// incorrect. The right-hand-side and kind of (in)equality of the
/// Constraint are ignored.
pub fn set_objective(&mut self, ty: ObjectiveType, con: Constraint) -> Result<(), String> {
let (ind, val): (Vec<CInt>, Vec<f64>) = con.vars
.iter()
.map(|wv| (wv.var as CInt, wv.weight))
.unzip();
unsafe {
let status = CPXchgobj(self.env.inner,
self.inner,
con.vars.len() as CInt,
ind.as_ptr(),
val.as_ptr());
if status != 0 {
Err(format!("Failed to set objective weights ({} ({}))",
errstr(self.env.inner, status).unwrap(),
status))
} else {
self.set_objective_type(ty)
}
}
}
/// Change the objective type. Default: `ObjectiveType::Minimize`.
///
/// It is recommended to use this in conjunction with objective
/// coefficients set by the `var!` macro rather than using
/// `set_objective`.
pub fn set_objective_type(&mut self, ty: ObjectiveType) -> Result<(), String> {
unsafe {
let status = CPXchgobjsen(self.env.inner, self.inner, ty.to_c());
if status != 0 {
Err(format!("Failed to set objective type to {:?} ({} ({}))",
ty,
errstr(self.env.inner, status).unwrap(),
status))
} else {
Ok(())
}
}
}
/// Write the problem to a file named `name`. At this time, it is
/// not possible to use a `Write` object instead, as this calls C
/// code directly.
pub fn write<S>(&self, name: S) -> Result<(), String>
where S: Into<String>
{
unsafe {
let status = CPXwriteprob(self.env.inner,
self.inner,
CString::new(name.into().as_str()).unwrap().as_ptr(),
std::ptr::null());
if status != 0 {
return match errstr(self.env.inner, status) {
Ok(s) => Err(s),
Err(e) => Err(e),
};
} else {
Ok(())
}
}
}
/// Add an initial solution to the problem.
///
/// `vars` is an array of indices (i.e. the result of `prob.add_variable`) and `values` are
/// their values.
pub fn add_initial_soln(&mut self, vars: &[usize], values: &[f64]) -> Result<(), String> {
assert!(values.len() == vars.len());
let vars = vars.into_iter().map(|&u| u as CInt).collect::<Vec<_>>();
unsafe {
let status = CPXaddmipstarts(self.env.inner,
self.inner,
1,
vars.len() as CInt,
&0,
vars.as_ptr(),
values.as_ptr(),
&0,
&std::ptr::null());
if status != 0 {
return match errstr(self.env.inner, status) {
Ok(s) => Err(s),
Err(e) => Err(e),
};
} else {
Ok(())
}
}
}
/// Solve the Problem, returning a `Solution` object with the
/// result.
pub fn solve_as(&mut self, pt: ProblemType) -> Result<Solution, String> {
// TODO: support multiple solution types...
unsafe {
if pt == ProblemType::Linear {
let status = CPXchgprobtype(self.env.inner, self.inner, 0);
if status != 0 {
return Err(format!("Failed to convert to LP problem ({} ({}))",
errstr(self.env.inner, status).unwrap(),
status));
}
}
let status = match pt {
ProblemType::MixedInteger => CPXmipopt(self.env.inner, self.inner),
ProblemType::Linear => CPXlpopt(self.env.inner, self.inner),
};
if status != 0 {
CPXwriteprob(self.env.inner,
self.inner,
CString::new("lpex1.lp").unwrap().as_ptr(),
std::ptr::null());
return Err(format!("LP Optimization failed ({} ({}))",
errstr(self.env.inner, status).unwrap(),
status));
}
let mut objval: f64 = 0.0;
let status = CPXgetobjval(self.env.inner, self.inner, &mut objval);
if status != 0 {
CPXwriteprob(self.env.inner,
self.inner,
CString::new("lpex1.lp").unwrap().as_ptr(),
std::ptr::null());
return Err(format!("Failed to retrieve objective value ({} ({}))",
errstr(self.env.inner, status).unwrap(),
status));
}
let mut xs = vec![0f64; self.variables.len()];
let status = CPXgetx(self.env.inner,
self.inner,
xs.as_mut_ptr(),
0,
self.variables.len() as CInt - 1);
if status != 0 {
return Err(format!("Failed to retrieve values for variables ({} ({}))",
errstr(self.env.inner, status).unwrap(),
status));
}
let mut eps= EPINT;
CPXgetdblparam(self.env.inner, EPINT_ID, &mut eps);
return Ok(Solution {
objective: objval,
variables: xs.iter()
.zip(self.variables.iter())
.map(|(&x, v)| match v.ty {
VariableType::Binary => VariableValue::Binary(x <= 1.0 + eps && x >= 1.0 - eps),
VariableType::Continuous => VariableValue::Continuous(x),
VariableType::Integer => VariableValue::Integer(x as CInt),
VariableType::SemiContinuous => VariableValue::SemiContinuous(x),
VariableType::SemiInteger => VariableValue::SemiInteger(x as CInt),
})
.collect::<Vec<VariableValue>>(),
});
}
}
/// Solve the problem as a Mixed Integer Program
pub fn solve(&mut self) -> Result<Solution, String> {
self.solve_as(ProblemType::MixedInteger)
}
}
impl<'a> Drop for Problem<'a> {
fn drop(&mut self) {
unsafe {
assert!(CPXfreeprob(self.env.inner, &self.inner) == 0);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construct() {
let env = Env::new().unwrap();
let _prob = Problem::new(&env, "test").unwrap();
}
#[test]
fn add_variable() {
let env = Env::new().unwrap();
let mut prob = Problem::new(&env, "test").unwrap();
prob.add_variable(Variable::new(VariableType::Binary, 1.0, 0.0, 40.0, "x1")).unwrap();
}
#[test]
fn add_constraint() {
let env = Env::new().unwrap();
let mut prob = Problem::new(&env, "test").unwrap();
let var_idx = prob.add_variable(Variable::new(VariableType::Binary, 1.0, 0.0, 40.0, "x1"))
.unwrap();
let mut con = Constraint::new(ConstraintType::LessThanEq, 20.0, "c1");
con.add_wvar(WeightedVariable::new_idx(var_idx, -1.0));
prob.add_constraint(con).unwrap();
}
#[test]
fn lpex1() {
let env = Env::new().unwrap();
let mut prob = Problem::new(&env, "lpex1").unwrap();
prob.set_objective_type(ObjectiveType::Maximize).unwrap();
let x1 = prob.add_variable(var!(0.0 <= "x1" <= 40.0 -> 1.0)).unwrap();
let x2 = prob.add_variable(var!("x2" -> 2.0)).unwrap();
let x3 = prob.add_variable(var!("x3" -> 3.0)).unwrap();
println!("{} {} {}", x1, x2, x3);
prob.add_constraint(con!("c1": 20.0 >= (-1.0) x1 + 1.0 x2 + 1.0 x3)).unwrap();
prob.add_constraint(con!("c2": 30.0 >= 1.0 x1 + (-3.0) x2 + 1.0 x3)).unwrap();
prob.write("lpex1_test.lp").unwrap();
let sol = prob.solve().unwrap();
println!("{:?}", sol);
assert!(sol.objective == 202.5);
assert!(sol.variables == vec![VariableValue::Continuous(40.0),
VariableValue::Continuous(17.5),
VariableValue::Continuous(42.5)]);