-
Notifications
You must be signed in to change notification settings - Fork 1
/
sirius_interface_test.cc
882 lines (766 loc) · 28.5 KB
/
sirius_interface_test.cc
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
//#include "ortools/linear_solver/sirius_interface.cc"
#include "ortools/linear_solver/linear_solver.h"
#include "gtest/gtest.h"
extern "C" {
#include "srs_api.h"
}
#include <fstream>
namespace operations_research {
class SRSGetter {
public:
SRSGetter(MPSolver* solver) : solver_(solver) {}
bool isMip() {
return prob()->is_mip;
}
int getNumVariables() {
return SRSgetnbcols(prob());
}
std::string getVariableName(int n) {
return prob()->problem_mps->LabelDeLaVariable[n];
}
int getNumConstraints() { return SRSgetnbrows(prob()); }
std::string getConstraintName(int n) {
return prob()->problem_mps->LabelDeLaContrainte[n];
}
double getLb(int n) {
EXPECT_LT(n, getNumVariables());
return prob()->problem_mps->Umin[n];
}
double getUb(int n) {
EXPECT_LT(n, getNumVariables());
return prob()->problem_mps->Umax[n];
}
int getVariableType(int n) {
EXPECT_LT(n, getNumVariables());
return prob()->problem_mps->TypeDeVariable[n];
}
char getConstraintType(int n) {
EXPECT_LT(n, getNumConstraints());
return prob()->problem_mps->SensDeLaContrainte[n];
}
double getConstraintRhs(int n) {
EXPECT_LT(n, getNumConstraints());
return prob()->problem_mps->B[n];
}
double getConstraintCoef(int row, int col) {
EXPECT_LT(col, getNumVariables());
EXPECT_LT(row, getNumConstraints());
PROBLEME_MPS* problem_mps = prob()->problem_mps;
int rowBeg = problem_mps->Mdeb[row];
for (int i = 0; i < problem_mps->NbTerm[row]; ++i) {
if (problem_mps->Nuvar[rowBeg + i] == col) {
return problem_mps->A[rowBeg + i];
}
}
return 0.;
}
double getObjectiveCoef(int n) {
EXPECT_LT(n, getNumVariables());
return prob()->problem_mps->L[n];
}
bool getObjectiveSense() {
return prob()->maximize;
}
int getPresolve() {
return prob()->presolve;
}
int getScaling() {
return prob()->scaling;
}
double getRelativeMipGap() {
return prob()->relativeGap;
}
int getVarBoundType(int n) {
EXPECT_LT(n, getNumVariables());
return prob()->problem_mps->TypeDeBorneDeLaVariable[n];
}
private:
MPSolver* solver_;
SRS_PROBLEM* prob() {
return (SRS_PROBLEM*)solver_->underlying_solver();
}
};
#define UNITTEST_INIT_MIP() \
MPSolver solver("SIRIUS_MIP", MPSolver::SIRIUS_MIXED_INTEGER_PROGRAMMING);\
SRSGetter getter(&solver)
#define UNITTEST_INIT_LP() \
MPSolver solver("SIRIUS_LP", MPSolver::SIRIUS_LINEAR_PROGRAMMING);\
SRSGetter getter(&solver)
void _unittest_verify_var(SRSGetter* getter, MPVariable* x, int type, double lb, double ub) {
EXPECT_EQ(getter->getVariableType(x->index()), type);
EXPECT_EQ(getter->getLb(x->index()), lb);
EXPECT_EQ(getter->getUb(x->index()), ub);
}
void _unittest_verify_constraint(SRSGetter* getter, MPConstraint* c, char type, double lb, double ub) {
int idx = c->index();
EXPECT_EQ(getter->getConstraintType(idx), type);
switch (type) {
case SRS_LESSER_THAN:
EXPECT_EQ(getter->getConstraintRhs(idx), ub);
break;
case SRS_GREATER_THAN:
EXPECT_EQ(getter->getConstraintRhs(idx), lb);
break;
case SRS_EQUAL:
EXPECT_EQ(getter->getConstraintRhs(idx), ub);
EXPECT_EQ(getter->getConstraintRhs(idx), lb);
break;
}
}
TEST(TestSiriusInterface, isMIP) {
UNITTEST_INIT_MIP();
EXPECT_EQ(solver.IsMIP(), true);
}
TEST(TestSiriusInterface, isLP) {
UNITTEST_INIT_LP();
EXPECT_EQ(solver.IsMIP(), false);
}
TEST(TestSiriusInterface, NumVariables) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x1 = solver.MakeNumVar(-1., 5.1, "x1");
MPVariable* x2 = solver.MakeNumVar(3.14, 5.1, "x2");
std::vector<MPVariable*> xs;
solver.MakeBoolVarArray(500, "xs", &xs);
solver.Solve();
EXPECT_EQ(getter.getNumVariables(), 502);
}
TEST(TestSiriusInterface, VariablesName) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
std::string pi("Pi");
std::string secondVar("Name");
MPVariable* x1 = solver.MakeNumVar(-1., 5.1, pi);
MPVariable* x2 = solver.MakeNumVar(3.14, 5.1, secondVar);
solver.Solve();
EXPECT_EQ(getter.getVariableName(0), pi);
EXPECT_EQ(getter.getVariableName(1), secondVar);
}
TEST(TestSiriusInterface, NumConstraints) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(100.0, 100.0);
solver.MakeRowConstraint(-solver.infinity(), 13.1);
solver.MakeRowConstraint(12.1, solver.infinity());
solver.Solve();
EXPECT_EQ(getter.getNumConstraints(), 3);
}
TEST(TestSiriusInterface, ConstraintsName) {
UNITTEST_INIT_MIP();
std::string phi("Phi");
std::string otherCnt("constraintName");
solver.MakeRowConstraint(100.0, 100.0, phi);
solver.MakeRowConstraint(-solver.infinity(), 13.1, otherCnt);
solver.Solve();
EXPECT_EQ(getter.getConstraintName(0), phi);
EXPECT_EQ(getter.getConstraintName(1), otherCnt);
}
TEST(TestSiriusInterface, Reset) {
UNITTEST_INIT_MIP();
solver.MakeBoolVar("x1");
solver.MakeBoolVar("x2");
solver.MakeRowConstraint(-solver.infinity(), 100.0);
solver.Solve();
EXPECT_EQ(getter.getNumConstraints(), 1);
EXPECT_EQ(getter.getNumVariables(), 2);
solver.Reset();
EXPECT_EQ(getter.getNumConstraints(), 0);
EXPECT_EQ(getter.getNumVariables(), 0);
}
TEST(TestSiriusInterface, MakeIntVar) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
int lb = 0, ub = 10;
MPVariable* x = solver.MakeIntVar(lb, ub, "x");
solver.Solve();
_unittest_verify_var(&getter, x, SRS_INTEGER_VAR, lb, ub);
}
TEST(TestSiriusInterface, MakeNumVar) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
double lb = 1.5, ub = 158.2;
MPVariable* x = solver.MakeNumVar(lb, ub, "x");
solver.Solve();
_unittest_verify_var(&getter, x, SRS_CONTINUOUS_VAR, lb, ub);
}
TEST(TestSiriusInterface, MakeBoolVar) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x = solver.MakeBoolVar("x");
solver.Solve();
_unittest_verify_var(&getter, x, SRS_INTEGER_VAR, 0, 1);
}
TEST(TestSiriusInterface, MakeIntVarArray) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
int n1 = 25, lb1 = -7, ub1 = 18;
solver.MakeRowConstraint(-solver.infinity(), 0);
std::vector<MPVariable*> xs1;
solver.MakeIntVarArray(n1, lb1, ub1, "xs1", &xs1);
int n2 = 37, lb2 = 19, ub2 = 189;
std::vector<MPVariable*> xs2;
solver.MakeIntVarArray(n2, lb2, ub2, "xs2", &xs2);
solver.Solve();
for (int i = 0; i < n1; ++i) {
_unittest_verify_var(&getter, xs1[i], SRS_INTEGER_VAR, lb1, ub1);
}
for (int i = 0; i < n2; ++i) {
_unittest_verify_var(&getter, xs2[i], SRS_INTEGER_VAR, lb2, ub2);
}
}
TEST(TestSiriusInterface, MakeNumVarArray) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
int n1 = 1;
double lb1 = 5.1, ub1 = 8.1;
std::vector<MPVariable*> xs1;
solver.MakeNumVarArray(n1, lb1, ub1, "xs1", &xs1);
int n2 = 13;
double lb2 = -11.5, ub2 = 189.9;
std::vector<MPVariable*> xs2;
solver.MakeNumVarArray(n2, lb2, ub2, "xs2", &xs2);
solver.Solve();
for (int i = 0; i < n1; ++i) {
_unittest_verify_var(&getter, xs1[i], SRS_CONTINUOUS_VAR, lb1, ub1);
}
for (int i = 0; i < n2; ++i) {
_unittest_verify_var(&getter, xs2[i], SRS_CONTINUOUS_VAR, lb2, ub2);
}
}
TEST(TestSiriusInterface, MakeBoolVarArray) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
double n = 43;
std::vector<MPVariable*> xs;
solver.MakeBoolVarArray(n, "xs", &xs);
solver.Solve();
for (int i = 0; i < n; ++i) {
_unittest_verify_var(&getter, xs[i], SRS_INTEGER_VAR, 0, 1);
}
}
TEST(TestSiriusInterface, SetVariableBounds) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
int lb1 = 3, ub1 = 4;
MPVariable* x1 = solver.MakeIntVar(lb1, ub1, "x1");
double lb2 = 3.7, ub2 = 4;
MPVariable* x2 = solver.MakeNumVar(lb2, ub2, "x2");
solver.Solve();
_unittest_verify_var(&getter, x1, SRS_INTEGER_VAR, lb1, ub1);
_unittest_verify_var(&getter, x2, SRS_CONTINUOUS_VAR, lb2, ub2);
lb1 = 12, ub1 = 15;
x1->SetBounds(lb1, ub1);
lb2 = -1.1, ub2 = 0;
x2->SetBounds(lb2, ub2);
solver.Solve();
_unittest_verify_var(&getter, x1, SRS_INTEGER_VAR, lb1, ub1);
_unittest_verify_var(&getter, x2, SRS_CONTINUOUS_VAR, lb2, ub2);
}
TEST(TestSiriusInterface, DISABLED_SetVariableInteger) {
// Here we test a badly definied behaviour
// depending on the sirius version the sirius-workflow breaks at:
// either the call of x->SetInteger(false) like the test suggest
// or at solver.Solve() because integer variables are not supported
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
int lb = -1, ub = 7;
MPVariable* x = solver.MakeIntVar(lb, ub, "x");
solver.Solve();
_unittest_verify_var(&getter, x, SRS_INTEGER_VAR, lb, ub);
EXPECT_THROW(x->SetInteger(false), std::logic_error);
}
TEST(TestSiriusInterface, ConstraintL) {
UNITTEST_INIT_MIP();
double lb = -solver.infinity(), ub = 10.;
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_LESSER_THAN, lb, ub);
}
TEST(TestSiriusInterface, ConstraintR) {
UNITTEST_INIT_MIP();
double lb = -2, ub = -1;
solver.MakeRowConstraint(lb, ub);
EXPECT_THROW(solver.Solve(), std::logic_error);
}
TEST(TestSiriusInterface, ConstraintG) {
UNITTEST_INIT_MIP();
double lb = 8.1, ub = solver.infinity();
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_GREATER_THAN, lb, ub);
}
TEST(TestSiriusInterface, ConstraintE) {
UNITTEST_INIT_MIP();
double lb = 18947.3, ub = lb;
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_EQUAL, lb, ub);
}
TEST(TestSiriusInterface, SetConstraintBoundsL) {
UNITTEST_INIT_MIP();
double lb = 18947.3, ub = lb;
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_EQUAL, lb, ub);
lb = -solver.infinity(), ub = 16.6;
c->SetBounds(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_LESSER_THAN, lb, ub);
}
TEST(TestSiriusInterface, SetConstraintBoundsG) {
UNITTEST_INIT_MIP();
double lb = 18947.3, ub = lb;
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_EQUAL, lb, ub);
lb = 5, ub = solver.infinity();
c->SetBounds(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_GREATER_THAN, lb, ub);
}
TEST(TestSiriusInterface, SetConstraintBoundsE) {
UNITTEST_INIT_MIP();
double lb = -1, ub = solver.infinity();
MPConstraint* c = solver.MakeRowConstraint(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_GREATER_THAN, lb, ub);
lb = 128, ub = lb;
c->SetBounds(lb, ub);
solver.Solve();
_unittest_verify_constraint(&getter, c, SRS_EQUAL, lb, ub);
}
TEST(TestSiriusInterface, DISABLED_ConstraintCoef) {
UNITTEST_INIT_MIP();
MPVariable* x1 = solver.MakeBoolVar("x1");
MPVariable* x2 = solver.MakeBoolVar("x2");
MPConstraint* c1 = solver.MakeRowConstraint(4.1, solver.infinity());
MPConstraint* c2 = solver.MakeRowConstraint(-solver.infinity(), 0.1);
double c11 = -15.6, c12 = 0.4, c21 = -11, c22 = 4.5;
c1->SetCoefficient(x1, c11);
c1->SetCoefficient(x2, c12);
c2->SetCoefficient(x1, c21);
c2->SetCoefficient(x2, c22);
solver.Solve();
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x1->index()), c11);
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x2->index()), c12);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x1->index()), c21);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x2->index()), c22);
// Next part causes sirius to crash ("free(): invalid next size (fast)")
c11 = 0.11, c12 = 0.12, c21 = 0.21, c22 = 0.22;
c1->SetCoefficient(x1, c11);
c1->SetCoefficient(x2, c12);
c2->SetCoefficient(x1, c21);
c2->SetCoefficient(x2, c22);
solver.Solve();
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x1->index()), c11);
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x2->index()), c12);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x1->index()), c21);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x2->index()), c22);
}
TEST(TestSiriusInterface, DISABLED_ClearConstraint) {
UNITTEST_INIT_MIP();
MPVariable* x1 = solver.MakeBoolVar("x1");
MPVariable* x2 = solver.MakeBoolVar("x2");
MPConstraint* c1 = solver.MakeRowConstraint(4.1, solver.infinity());
MPConstraint* c2 = solver.MakeRowConstraint(-solver.infinity(), 0.1);
double c11 = -1533.6, c12 = 3.4, c21 = -11000, c22 = 0.0001;
c1->SetCoefficient(x1, c11);
c1->SetCoefficient(x2, c12);
c2->SetCoefficient(x1, c21);
c2->SetCoefficient(x2, c22);
solver.Solve();
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x1->index()), c11);
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x2->index()), c12);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x1->index()), c21);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x2->index()), c22);
c1->Clear();
c2->Clear();
// next part causes sirius to crash ("free(): invalid next size (fast)")
solver.Solve();
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x1->index()), 0);
EXPECT_EQ(getter.getConstraintCoef(c1->index(), x2->index()), 0);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x1->index()), 0);
EXPECT_EQ(getter.getConstraintCoef(c2->index(), x2->index()), 0);
}
TEST(TestSiriusInterface, ObjectiveCoef) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x = solver.MakeBoolVar("x");
MPObjective* obj = solver.MutableObjective();
double coef = 3112.4;
obj->SetCoefficient(x, coef);
solver.Solve();
EXPECT_EQ(getter.getObjectiveCoef(x->index()), coef);
coef = 0.2;
obj->SetCoefficient(x, coef);
solver.Solve();
EXPECT_EQ(getter.getObjectiveCoef(x->index()), coef);
}
TEST(TestSiriusInterface, DISABLED_ObjectiveOffset) {
// ObjectiveOffset not implemented for sirius_interface
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x = solver.MakeBoolVar("x");
MPObjective* obj = solver.MutableObjective();
double offset = 4.3;
obj->SetOffset(offset);
solver.Solve();
// EXPECT_EQ(getter.getObjectiveOffset(), offset);
offset = 3.6;
obj->SetOffset(offset);
solver.Solve();
// EXPECT_EQ(getter.getObjectiveOffset(), offset);
}
TEST(TestSiriusInterface, ObjectiveOffset) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x = solver.MakeBoolVar("x");
MPObjective* obj = solver.MutableObjective();
double offset = 4.3;
EXPECT_THROW(obj->SetOffset(offset), std::logic_error);
}
TEST(TestSiriusInterface, ClearObjective) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPVariable* x = solver.MakeBoolVar("x");
MPObjective* obj = solver.MutableObjective();
double coef = -15.6;
obj->SetCoefficient(x, coef);
solver.Solve();
EXPECT_EQ(getter.getObjectiveCoef(x->index()), coef);
obj->Clear();
solver.Solve();
EXPECT_EQ(getter.getObjectiveCoef(x->index()), 0);
}
TEST(TestSiriusInterface, ObjectiveSense) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPObjective* const objective = solver.MutableObjective();
objective->SetMinimization();
solver.Solve();
EXPECT_EQ(getter.getObjectiveSense(), false);
objective->SetMaximization();
solver.Solve();
EXPECT_EQ(getter.getObjectiveSense(), true);
}
TEST(TestSiriusInterface, interations) {
UNITTEST_INIT_LP();
int nc = 100, nv = 100;
std::vector<MPConstraint*> cs(nc);
for (int ci = 0; ci < nc; ++ci) {
cs[ci] = solver.MakeRowConstraint(-solver.infinity(), ci + 1);
}
MPObjective* const objective = solver.MutableObjective();
for (int vi = 0; vi < nv; ++vi) {
MPVariable* v = solver.MakeNumVar(0, nv, "x" + std::to_string(vi));
for (int ci = 0; ci < nc; ++ci) {
cs[ci]->SetCoefficient(v, vi + ci);
}
objective->SetCoefficient(v, 1);
}
solver.Solve();
EXPECT_GT(solver.iterations(), 0);
}
TEST(TestSiriusInterface, DISABLED_nodes) {
// The problem seems to be incorrectly returned as infeasible
UNITTEST_INIT_MIP();
int nc = 100, nv = 100;
std::vector<MPConstraint*> cs(2*nc);
for (int ci = 0; ci < nc; ++ci) {
cs[2*ci ] = solver.MakeRowConstraint(-solver.infinity(), ci + 1);
cs[2*ci + 1] = solver.MakeRowConstraint(ci, solver.infinity());
}
MPObjective* const objective = solver.MutableObjective();
for (int vi = 0; vi < nv; ++vi) {
MPVariable* v = solver.MakeIntVar(0, nv, "x" + std::to_string(vi));
for (int ci = 0; ci < nc; ++ci) {
cs[2*ci ]->SetCoefficient(v, vi + ci);
cs[2*ci + 1]->SetCoefficient(v, vi + ci);
}
objective->SetCoefficient(v, 1);
}
VLOG(0) << solver.Solve();
EXPECT_GT(solver.nodes(), 0);
}
TEST(TestSiriusInterface, SolverVersion) {
UNITTEST_INIT_MIP();
EXPECT_GE(solver.SolverVersion().size(), 36);
}
TEST(TestSiriusInterface, DISABLED_Write) {
// SRSwritempsprob has different implementations on the metrix branch
UNITTEST_INIT_MIP();
MPVariable* x1 = solver.MakeIntVar(-1.2, 9.3, "x1");
MPVariable* x2 = solver.MakeNumVar(-1, 5, "x2");
MPConstraint* c1 = solver.MakeRowConstraint(-solver.infinity(), 1);
c1->SetCoefficient(x1, 3);
c1->SetCoefficient(x2, 1.5);
MPConstraint* c2 = solver.MakeRowConstraint(3, solver.infinity());
c2->SetCoefficient(x2, -1.1);
MPObjective* obj = solver.MutableObjective();
obj->SetMaximization();
obj->SetCoefficient(x1, 1);
obj->SetCoefficient(x2, 2);
std::string tmpName = std::string(std::tmpnam(nullptr)) + ".mps";
solver.Write(tmpName);
std::ifstream tmpFile(tmpName);
std::stringstream tmpBuffer;
tmpBuffer << tmpFile.rdbuf();
tmpFile.close();
std::remove(tmpName.c_str());
EXPECT_EQ(tmpBuffer.str(), R"(* Number of variables: 2
* Number of constraints: 2
NAME Pb Solve
ROWS
N OBJECTIF
L R0000000
G R0000001
COLUMNS
C0000000 OBJECTIF 1.0000000000
C0000000 R0000000 3.0000000000
C0000001 OBJECTIF 2.0000000000
C0000001 R0000000 1.5000000000
C0000001 R0000001 -1.1000000000
RHS
RHSVAL R0000000 1.000000000
RHSVAL R0000001 3.000000000
BOUNDS
LI BNDVALUE C0000000 -1
UI BNDVALUE C0000000 9
LO BNDVALUE C0000001 -1.000000000
UP BNDVALUE C0000001 5.000000000
ENDATA
)");
}
TEST(TestSiriusInterface, DISABLED_SetPrimalTolerance) {
// SetPrimalTolerance not implemented for sirius_interface
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
double tol = 1e-4;
params.SetDoubleParam(MPSolverParameters::PRIMAL_TOLERANCE, tol);
solver.Solve(params);
// EXPECT_EQ(getter.getPrimalTolerance(), tol);
}
TEST(TestSiriusInterface, SetPrimalTolerance) {
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
double tol = 1e-4;
params.SetDoubleParam(MPSolverParameters::PRIMAL_TOLERANCE, tol);
solver.Solve(params);
}
TEST(TestSiriusInterface, DISABLED_SetDualTolerance) {
// SetDualTolerance not implemented for sirius_interface
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
double tol = 1e-2;
params.SetDoubleParam(MPSolverParameters::DUAL_TOLERANCE, tol);
solver.Solve(params);
// EXPECT_EQ(getter.getDualTolerance(), tol) << "Not available";
}
TEST(TestSiriusInterface, SetDualTolerance) {
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
double tol = 1e-2;
params.SetDoubleParam(MPSolverParameters::DUAL_TOLERANCE, tol);
solver.Solve(params);
}
TEST(TestSiriusInterface, SetPresolveMode) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::PRESOLVE, MPSolverParameters::PRESOLVE_OFF);
solver.Solve(params);
EXPECT_EQ(getter.getPresolve(), 0);
params.SetIntegerParam(MPSolverParameters::PRESOLVE, MPSolverParameters::PRESOLVE_ON);
solver.Solve(params);
EXPECT_EQ(getter.getPresolve(), 1);
}
TEST(TestSiriusInterface, DISABLED_SetLpAlgorithm) {
// SetLpAlgorithm not implemented for sirius_interface
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::DUAL);
solver.Solve(params);
// EXPECT_EQ(getter.getLpAlgorithm(), 2);
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::PRIMAL);
solver.Solve(params);
// EXPECT_EQ(getter.getLpAlgorithm(), 3);
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::BARRIER);
solver.Solve(params);
// EXPECT_EQ(getter.getLpAlgorithm(), 4);
}
TEST(TestSiriusInterface, SetLpAlgorithm) {
UNITTEST_INIT_LP();
MPConstraint* c = solver.MakeRowConstraint(-solver.infinity(), 0.5);
MPVariable* x = solver.MakeNumVar(0, 1, "x");
MPObjective* obj = solver.MutableObjective();
c->SetCoefficient(x, 1);
obj->SetCoefficient(x, 1);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::DUAL);
solver.Solve(params);
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::PRIMAL);
solver.Solve(params);
params.SetIntegerParam(MPSolverParameters::LP_ALGORITHM, MPSolverParameters::BARRIER);
solver.Solve(params);
}
TEST(TestSiriusInterface, DISABLED_SetScaling) {
// SetScaling not implemented for sirius_interface
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::SCALING, MPSolverParameters::SCALING_OFF);
solver.Solve(params);
EXPECT_EQ(getter.getScaling(), 0);
params.SetIntegerParam(MPSolverParameters::SCALING, MPSolverParameters::SCALING_ON);
solver.Solve(params);
EXPECT_EQ(getter.getScaling(), 1);
}
TEST(TestSiriusInterface, SetScaling) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::SCALING, MPSolverParameters::SCALING_OFF);
solver.Solve(params);
params.SetIntegerParam(MPSolverParameters::SCALING, MPSolverParameters::SCALING_ON);
solver.Solve(params);
}
TEST(TestSiriusInterface, DISABLED_SetRelativeMipGap) {
// SetRelativeMipGap not implemented for sirius_interface
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPSolverParameters params;
double relativeMipGap = 1e-3;
params.SetDoubleParam(MPSolverParameters::RELATIVE_MIP_GAP, relativeMipGap);
solver.Solve(params);
EXPECT_EQ(getter.getRelativeMipGap(), relativeMipGap);
}
TEST(TestSiriusInterface, SetRelativeMipGap) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
MPSolverParameters params;
double relativeMipGap = 1e-3;
params.SetDoubleParam(MPSolverParameters::RELATIVE_MIP_GAP, relativeMipGap);
solver.Solve(params);
}
TEST(TestSiriusInterface, setVarBoundType) {
UNITTEST_INIT_MIP();
solver.MakeRowConstraint(-solver.infinity(), 0);
double infty = solver.infinity();
solver.MakeIntVar(2, 2, "VARIABLE_FIXE");
solver.MakeIntVar(-10, -1, "VARIABLE_BORNEE_DES_DEUX_COTES");
solver.MakeIntVar(3, infty, "VARIABLE_BORNEE_INFERIEUREMENT");
solver.MakeIntVar(-infty, -1, "VARIABLE_BORNEE_SUPERIEUREMENT");
solver.MakeIntVar(-infty, infty, "VARIABLE_NON_BORNEE");
std::array<int, 5> varBoundTypes = {
VARIABLE_FIXE,
VARIABLE_BORNEE_DES_DEUX_COTES,
VARIABLE_BORNEE_INFERIEUREMENT,
VARIABLE_BORNEE_SUPERIEUREMENT,
VARIABLE_NON_BORNEE
};
std::string xpressParamString = "VAR_BOUNDS_TYPE";
for (int boundType : varBoundTypes)
xpressParamString += " " + std::to_string(boundType);
solver.SetSolverSpecificParametersAsString(xpressParamString);
solver.Solve();
for (int i = 0; i < varBoundTypes.size(); ++i)
EXPECT_EQ(getter.getVarBoundType(i), varBoundTypes[i]);
}
TEST(TestSiriusInterface, DISABLED_SolveMIP) {
// The problem seems to incorrectly be returned as infeasible
UNITTEST_INIT_MIP();
// max x + 2y
// st. -x + y <= 1
// 2x + 3y <= 12
// 3x + 2y <= 12
// x , y >= 0
// x , y \in Z
double inf = solver.infinity();
MPVariable* x = solver.MakeIntVar(0, inf, "x");
MPVariable* y = solver.MakeIntVar(0, inf, "y");
MPObjective* obj = solver.MutableObjective();
obj->SetCoefficient(x, 1);
obj->SetCoefficient(y, 2);
obj->SetMaximization();
MPConstraint* c1 = solver.MakeRowConstraint(-inf, 1);
c1->SetCoefficient(x, -1);
c1->SetCoefficient(y, 1);
MPConstraint* c2 = solver.MakeRowConstraint(-inf, 12);
c2->SetCoefficient(x, 3);
c2->SetCoefficient(y, 2);
MPConstraint* c3 = solver.MakeRowConstraint(-inf, 12);
c3->SetCoefficient(x, 2);
c3->SetCoefficient(y, 3);
solver.Solve();
EXPECT_EQ(obj->Value(), 6);
EXPECT_EQ(obj->BestBound(), 6);
EXPECT_EQ(x->solution_value(), 2);
EXPECT_EQ(y->solution_value(), 2);
}
TEST(TestSiriusInterface, DISABLED_SolveLP) {
// Sign of dual values seems to be off
// This sign problem occurs with presolve on and presolve off
UNITTEST_INIT_LP();
// max x + 2y
// st. -x + y <= 1
// 2x + 3y <= 12
// 3x + 2y <= 12
// x , y \in R+
double inf = solver.infinity();
MPVariable* x = solver.MakeNumVar(0, inf, "x");
MPVariable* y = solver.MakeNumVar(0, inf, "y");
MPObjective* obj = solver.MutableObjective();
obj->SetCoefficient(x, 1);
obj->SetCoefficient(y, 2);
obj->SetMaximization();
MPConstraint* c1 = solver.MakeRowConstraint(-inf, 1);
c1->SetCoefficient(x, -1);
c1->SetCoefficient(y, 1);
MPConstraint* c2 = solver.MakeRowConstraint(-inf, 12);
c2->SetCoefficient(x, 3);
c2->SetCoefficient(y, 2);
MPConstraint* c3 = solver.MakeRowConstraint(-inf, 12);
c3->SetCoefficient(x, 2);
c3->SetCoefficient(y, 3);
MPSolverParameters params;
params.SetIntegerParam(MPSolverParameters::PRESOLVE, MPSolverParameters::PRESOLVE_OFF);
solver.Solve(params);
EXPECT_NEAR(obj->Value(), 7.4, 1e-8);
EXPECT_NEAR(x->solution_value(), 1.8, 1e-8);
EXPECT_NEAR(y->solution_value(), 2.8, 1e-8);
EXPECT_NEAR(x->reduced_cost(), 0, 1e-8);
EXPECT_NEAR(y->reduced_cost(), 0, 1e-8);
EXPECT_NEAR(c1->dual_value(), 0.2, 1e-8);
EXPECT_NEAR(c2->dual_value(), 0, 1e-8);
EXPECT_NEAR(c3->dual_value(), 0.6, 1e-8);
}
} // namespace operations_research
int main(int argc, char** argv) {
absl::SetFlag(&FLAGS_stderrthreshold, 0);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}