-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMibSModel.cpp
6881 lines (5877 loc) · 211 KB
/
MibSModel.cpp
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
/*===========================================================================*/
/* This file is part of a Mixed Integer Bilevel Solver */
/* developed using the BiCePS Linear Integer Solver (BLIS). */
/* */
/* Authors: Scott DeNegre, Lehigh University */
/* Ted Ralphs, Lehigh University */
/* */
/* Copyright (C) 2007-2015 Lehigh University, Scott DeNegre, and Ted Ralphs. */
/* All Rights Reserved. */
/* */
/* This software is licensed under the Eclipse Public License. Please see */
/* accompanying file for terms. */
/*===========================================================================*/
//#include "BlisModel.h"
#include "MibSConfig.hpp"
#include "MibSModel.hpp"
#ifdef MIBS_HAS_SYMPHONY
#include "symphony.h"
#include "SymConfig.h"
#include "CoinUtilsConfig.h"
#include "OsiSymSolverInterface.hpp"
#endif
#ifdef MIBS_HAS_CPLEX
#include "cplex.h"
#include "OsiCpxSolverInterface.hpp"
#endif
#include "MibSSolution.hpp"
#include "MibSCutGenerator.hpp"
//#include "MibSBilevel.hpp"
#include "MibSTreeNode.hpp"
#include "MibSConstants.hpp"
#include "MibSBranchStrategyMaxInf.hpp"
#include "MibSBranchStrategyPseudo.hpp"
#include "MibSBranchStrategyStrong.hpp"
//FIXME::RELIABILITY BRANCHING DOESNT WORK
//NECESSARY DATA MEMBERS ARE DESIGNATED AS PRIVATE
//IN PARENT CLASS. DIDNT WANT TO ALTER BLIS CODE
//#include "MibSBranchStrategyRel.h"
#if COIN_HAS_MPI
#include "AlpsKnowledgeBrokerMPI.h"
#else
#include "AlpsKnowledgeBrokerSerial.h"
#endif
#ifdef HAVE_CXX11
#include <random>
#endif
//#############################################################################
MibSModel::MibSModel()
{
initialize();
}
//#############################################################################
MibSModel::~MibSModel()
{
if(upperColInd_) delete [] upperColInd_;
if(lowerColInd_) delete [] lowerColInd_;
if(upperRowInd_) delete [] upperRowInd_;
if(origUpperRowInd_) delete [] origUpperRowInd_;
if(lowerRowInd_) delete [] lowerRowInd_;
if(structRowInd_) delete [] structRowInd_;
if(fixedInd_) delete [] fixedInd_;
if(interdictCost_) delete [] interdictCost_;
if(origColLb_) delete [] origColLb_;
if(origColUb_) delete [] origColUb_;
if(lColLbInLProb_) delete [] lColLbInLProb_;
if(lColUbInLProb_) delete [] lColUbInLProb_;
if(origRowLb_) delete [] origRowLb_;
if(origRowUb_) delete [] origRowUb_;
if(origRowSense_) delete [] origRowSense_;
if(lowerObjCoeffs_) delete [] lowerObjCoeffs_;
if(columnName_) delete [] columnName_;
if(rowName_) delete [] rowName_;
if(MibSPar_) delete MibSPar_;
if(lowerConstCoefMatrix_) delete lowerConstCoefMatrix_;
if(A2Matrix_) delete A2Matrix_;
if(G2Matrix_) delete G2Matrix_;
if(bS_) delete bS_;
if(stocA2Matrix_) delete stocA2Matrix_;
}
//#############################################################################
void
MibSModel::initialize()
{
llDataFile_ = "";
ulDataFile_ = "";
ulAmplModelFile_ = "";
ulAmplDataFile_ = "";
etol_ = 1e-5;
numVars_ = 0;
numOrigVars_ = 0;
numCons_ = 0;
numOrigCons_ = 0;
objSense_ = 0.0;
lowerDim_ = 0;
truncLowerDim_ = 0;
lowerObjSense_ = 0.0;
upperDim_ = 0;
leftSlope_ = 0;
rightSlope_ = 0;
lowerRowNum_ = 0;
truncLowerRowNum_ = 0;
upperRowNum_ = 0;
origUpperRowNum_ =0;
structRowNum_ = 0;
sizeFixedInd_ = 0;
counterVF_ = 0;
counterUB_ = 0;
timerVF_ = 0.0;
timerUB_ = 0.0;
countIteration_ = 0;
isInterdict_ = false;
isPureInteger_ = true;
isUpperCoeffInt_ = true;
isLowerCoeffInt_ = true;
allUpperBin_ = true;
allLowerBin_ = true;
allLinkingBin_ = true;
positiveA1_ = true;
positiveA2_ = true;
positiveG1_ = true;
positiveG2_ = true;
upperColInd_ = NULL;
lowerColInd_ = NULL;
upperRowInd_ = NULL;
origUpperRowInd_ = NULL;
lowerRowInd_ = NULL;
structRowInd_ = NULL;
fixedInd_ = NULL;
origColLb_ = NULL;
origColUb_ = NULL;
lColLbInLProb_ = NULL;
lColUbInLProb_ = NULL;
origRowLb_ = NULL;
origRowUb_ = NULL;
origRowSense_ = NULL;
lowerObjCoeffs_ = NULL;
columnName_ = NULL;
rowName_ = NULL;
interdictCost_ = NULL;
origConstCoefMatrix_ = NULL;
lowerConstCoefMatrix_ = NULL;
A2Matrix_ = NULL;
G2Matrix_ = NULL;
boundProbRoot_ = NULL;
boundProbLeafNum_ = 0;
numScenarios_ = 0;
stocA2Matrix_ = NULL;
bS_ = new MibSBilevel();
//simpleCutOnly_ = true; //FIXME: should make this a parameter
//bindingMethod_ = "BLAND"; //FIXME: should make this a parameter
//bindingMethod_ = "BASIS"; //FIXME: should make this a parameter
MibSPar_ = new MibSParams;
//maxAuxCols_ = 0; //FIXME: should make this a parameter
MibSCutGenerator *cg = new MibSCutGenerator(this);
cg->setStrategy(BlisCutStrategyPeriodic);
cg->setCutGenerationFreq(1); // Generate cuts at every node
addCutGenerator(cg);
setBlisParameters();
}
//#############################################################################
/** Read in parameters. */
void
MibSModel::readParameters(const int argnum, const char * const * arglist)
{
//std::cout << "Reading parameters ..." << std::endl;
AlpsPar_->readFromArglist(argnum, arglist);
BlisPar_->readFromArglist(argnum, arglist);
MibSPar_->readFromArglist(argnum, arglist);
}
//#############################################################################
void
MibSModel::readInstance(const char* dataFile)
{
#if 0
std::ifstream data_stream(dataFile);
if (!data_stream){
std::cout << "Error opening input data file. Aborting.\n";
abort();
}
std::string key;
std::string file;
while (data_stream >> key){
if(key == "UPPER"){
data_stream >> file;
setUpperFile(file);
}
else if(key == "LOWER"){
data_stream >> file;
setLowerFile(file);
}
else if(key == "AMPL_MODEL"){
data_stream >> file;
setUpperAmplModelFile(file);
}
else if(key == "AMPL_DATA"){
data_stream >> file;
setUpperAmplDataFile(file);
}
}
data_stream.close();
#endif
setUpperFile(dataFile);
//readAuxiliaryData(); // reads in lower-level vars, rows, obj coeffs
readProblemData(); // reads in max c^1x + d^1y s.t. (x,y) in Omega^I
}
//#############################################################################
void
MibSModel::setBlisParameters()
{
int bliscuts(MibSPar_->entry(MibSParams::blisCutStrategy));
int blisbranch(MibSPar_->entry(MibSParams::blisBranchStrategy));
/* Set Blis Parameters to keep cutting until no cut is found */
BlisPar()->setEntry(BlisParams::cutFactor, ALPS_DBL_MAX);
BlisPar()->setEntry(BlisParams::cutPass, ALPS_INT_MAX);
BlisPar()->setEntry(BlisParams::tailOff, -10000);
BlisPar()->setEntry(BlisParams::denseConFactor, ALPS_DBL_MAX);
/* Set cut generation frequency to 1 */
BlisPar()->setEntry(BlisParams::cutGenerationFrequency, 1);
/* Set Blis cut strategy using MibS parameters blisCutStrategy */
BlisPar()->setEntry(BlisParams::cutStrategy, bliscuts);
/* Set Blis branch strategy using MibS parameters blisBranchStrategy */
BlisPar()->setEntry(BlisParams::cutStrategy, blisbranch);
}
//#############################################################################
void
MibSModel::readAuxiliaryData(const CoinPackedMatrix& rowMatrix,
const double* conLB, const double* conUB,
int numCols, int numRows, double infinity,
const char *rowSense)
{
std::string stochasticityType(MibSPar_->entry
(MibSParams::stochasticityType));
int isA2Random(MibSPar_->entry(MibSParams::isA2Random));
int isSMPSFormat(MibSPar_->entry(MibSParams::isSMPSFormat));
if((stochasticityType == "stochasticWithoutSAA") && (isSMPSFormat == PARAM_OFF)){
throw CoinError("When the SAA method is not used(for the stochastic case), the input format should be SMPS",
"readAuxiliaryData",
"MibSModel");
}
if(stochasticityType == "deterministic"){
numScenarios_ = 1;
std::string fileName = getLowerFile();
if (fileName == ""){
std::string mpsFile(getUpperFile());
int length = mpsFile.length();
char *tmpArr = new char[length + 1];
fileName = mpsFile.erase(length - 3, 3);
fileName.append("aux");
if (fileCoinReadable(fileName)){
fileName.copy(tmpArr, length);
tmpArr[length]='\0';
MibSPar()->setEntry(MibSParams::auxiliaryInfoFile, tmpArr);
std::cout << "Warning: The auxiliary file was not specified. ";
std::cout << std::endl;
std::cout << "MibS used " << fileName << " automatically.";
std::cout << std::endl;
}else{
fileName = mpsFile.erase(length - 3, 3);
fileName.append("txt");
if (fileCoinReadable(fileName)){
fileName.copy(tmpArr, length);
tmpArr[length]='\0';
MibSPar()->setEntry(MibSParams::auxiliaryInfoFile, tmpArr);
std::cout << "Warning: The auxiliary file is not specified. ";
std::cout << "MibS selected " << fileName << " automatically.";
std::cout << std::endl;
}else{
std::cout << "Error: The auxiliary file is not specified. ";
std::cout << "Aborting." << std::endl;
abort();
}
}
delete [] tmpArr;
}
fileCoinReadable(fileName);
std::ifstream data_stream(fileName.c_str());
std::string inputFormat(MibSPar_->entry
(MibSParams::inputFormat));
if (!data_stream){
std::cout << "Error opening input data file. Aborting.\n";
abort();
}
std::string key;
int iValue(0);
std::string cValue;
double dValue(0.0);
int i(0), j(0), k(0), m(0), p(0), pos(0);
int lowerColNum(0), lowerRowNum(0);
while (data_stream >> key){
if(key == "N"){
data_stream >> iValue;
setLowerDim(iValue);
setTruncLowerDim(iValue);
}
else if(key == "M"){
data_stream >> iValue;
setLowerRowNum(iValue);
setTruncLowerRowNum(iValue);
}
else if(key == "LC"){
if(!getLowerColInd())
lowerColInd_ = new int[getLowerDim()];
if(inputFormat == "indexBased"){
data_stream >> iValue;
lowerColInd_[i] = iValue;
}
else{
data_stream >> cValue;
for(p = 0; p < numCols; ++p){
if(columnName_[p] == cValue){
pos = p;
break;
}
}
lowerColInd_[i] = pos;
}
i++;
}
else if(key == "LR"){
if(!getLowerRowInd())
lowerRowInd_ = new int[getLowerRowNum()];
if(inputFormat == "indexBased"){
data_stream >> iValue;
lowerRowInd_[j] = iValue;
}
else{
data_stream >> cValue;
for(p = 0; p < numRows; ++p){
if(rowName_[p] == cValue){
pos = p;
break;
}
}
lowerRowInd_[j] = pos;
}
j++;
}
else if(key == "LO"){
if(!getLowerObjCoeffs())
lowerObjCoeffs_ = new double[getLowerDim()];
data_stream >> dValue;
lowerObjCoeffs_[k] = dValue;
k++;
}
else if(key == "OS"){
data_stream >> dValue;
lowerObjSense_ = dValue; //1 min; -1 max
}
else if(key == "IC"){
if(!getInterdictCost()){
//FIXME: ALLOW MORE THAN ONE ROW
interdictCost_ = new double[getLowerDim()];
}
data_stream >> dValue;
interdictCost_[m] = dValue;
m++;
}
else if(key == "IB"){
isInterdict_ = true;
//FIXME: ALLOW MORE THAN ONE ROW
data_stream >> dValue;
interdictBudget_ = dValue;
}
else if(key == "@VARSBEGIN"){
pos = -1;
lowerColNum = getLowerDim();
if(!getLowerColInd())
lowerColInd_ = new int[lowerColNum];
if(!getLowerObjCoeffs())
lowerObjCoeffs_ = new double[lowerColNum];
for(i = 0; i < lowerColNum; i++){
data_stream >> cValue;
data_stream >> dValue;
for(p = 0; p < numCols; ++p){
if(columnName_[p] == cValue){
pos = p;
break;
}
}
if(pos < 0){
std::cout << cValue << " does not belong to the list of variables." << std::endl;
}
else{
lowerObjCoeffs_[pos] = dValue;
lowerColInd_[i] = pos;
}
pos = -1;
}
}
else if(key == "@CONSTSBEGIN"){
pos = -1;
lowerRowNum = getLowerRowNum();
if(!getLowerRowInd())
lowerRowInd_ = new int[lowerRowNum];
for(i = 0; i < lowerRowNum; i++){
data_stream >> cValue;
for(p = 0; p < numRows; ++p){
if(rowName_[p] == cValue){
pos = p;
break;
}
}
if(pos < 0){
std::cout << cValue << " does not belong to the list of constraints." << std::endl;
}
else{
lowerRowInd_[i] = pos;
}
pos = -1;
}
}
}
data_stream.close();
std::cout << "LL Data File: " << getLowerFile() << "\n";
std::cout << "Number of LL Variables: "
<< getLowerDim() << "\n\n";
}
else if(stochasticityType == "stochasticWithoutSAA"){//the format is smps necessarily
std::string timFileName = getTimFile();
fileCoinReadable(timFileName);
std::string stoFileName = getStoFile();
fileCoinReadable(stoFileName);
std::ifstream timData_stream(timFileName.c_str());
std::ifstream stoData_stream(stoFileName.c_str());
if(!timData_stream){
std::cout << "Error opening input time data file. Aborting.\n";
abort();
}
if(!stoData_stream){
std::cout << "Error opening input stochastic data file. Aborting.\n";
abort();
}
int i(0), j(0), p(0);
double etol(etol_);
std::string key, fLColName, fLRowName;
int iValue(0), posRow(-1), posCol(-1), index(0), rowNumElem(0), scCount(0), start(0);
double dValue(0.0), element(0.0);
int uColNum(0), lColNum(0), uRowNum(0), lRowNum(0), totalRowNum(0);
int truncRowNum(0), truncLColNum(0), truncLRowNum(0);
bool isFirstScenario(true);
CoinPackedVector appendedRow;
CoinPackedVector row;
int *rowInd = NULL;
double *rowElem = NULL;
CoinPackedVector col;
std::vector<double> stocLowerRhs;
//read time file
//ignoring first three lines
for(i = 0; i < 7; i++){
timData_stream >> key;
}
//name of first ll variable
timData_stream >> key;
fLColName = key;
//name of first ll row
timData_stream >> key;
fLRowName = key;
for(p = 0; p < numCols; p++){
if(columnName_[p] == fLColName){
posCol = p;
break;
}
}
if(posCol < 0){
std::cout << fLColName << " does not belong to the list of variables." << std::endl;
abort();
}
else{
uColNum = p;
truncLColNum = numCols - uColNum;
setTruncLowerDim(truncLColNum);
}
posRow = -1;
for(p = 0; p < numRows; p++){
if(rowName_[p] == fLRowName){
posRow = p;
break;
}
}
if(posRow < 0){
std::cout << fLRowName << " does not belong to the list of constraints." << std::endl;
abort();
}
else{
uRowNum = p;
truncLRowNum = numRows - uRowNum;
setTruncLowerRowNum(truncLRowNum);
}
timData_stream >> key;
//set lower objective function coeffs and sense
lowerObjCoeffs_ = new double[truncLColNum];
for(i = 0; i < truncLColNum; i++){
timData_stream >> key;
timData_stream >> dValue;
lowerObjCoeffs_[i] = dValue;
}
timData_stream >> key;
timData_stream >> iValue;
lowerObjSense_ = iValue;//1 min; -1 max
//saharSto: do we need fill lowerColInd_ and lowerRowInd_?
//set lower col and row indices
lowerColInd_ = new int[truncLColNum];
lowerRowInd_ = new int[truncLRowNum];
CoinIotaN(lowerColInd_, truncLColNum, uColNum);
CoinIotaN(lowerRowInd_, truncLRowNum, uRowNum);
timData_stream.close();
//read stochastic data
//find number of scenarios
while(stoData_stream >> key){
if(key == "SC"){
numScenarios_ ++;
}
}
stoData_stream.close();
lColNum = truncLColNum * numScenarios_;
lRowNum = truncLRowNum * numScenarios_;
totalRowNum = uRowNum + lRowNum;
truncRowNum = uRowNum + truncLRowNum;
setLowerDim(lColNum);
setLowerRowNum(lRowNum);
//saharSto3: check it
//stoData_stream(stoFileName.c_str());
stoData_stream.open(stoFileName.c_str());
origRowLb_ = new double[totalRowNum];
origRowUb_ = new double[totalRowNum];
CoinFillN(origRowLb_, totalRowNum, -1 * infinity);
CoinFillN(origRowUb_, totalRowNum, infinity);
CoinPackedMatrix *stocMatrixA2 = 0;
stocMatrixA2 = new CoinPackedMatrix(false, 0, 0);
stocMatrixA2->setDimensions(0, uColNum);
//saharSto3: check it
//double **fullMatrixA2 = NULL;
//double **fullMatrixA2Copy = NULL;
std::vector<std::vector<double> > fullMatrixA2;
std::vector<std::vector<double> > fullMatrixA2Copy;
if(isA2Random != PARAM_OFF){
/*fullMatrixA2 = new double*[truncLRowNum];
fullMatrixA2Copy = new double*[truncLRowNum];
for(i = 0; i < truncLRowNum; i++){
//saharSto3: check if they are filled with zeros
fullMatrixA2[i] = new double[uColNum]();
fullMatrixA2Copy[i] = new double[uColNum]();
}*/
fullMatrixA2.resize(truncLRowNum, std::vector<double>(uColNum, 0.0));
fullMatrixA2Copy.resize(truncLRowNum, std::vector<double>(uColNum, 0.0));
}
for(i = uRowNum; i < numRows; i++){
row = rowMatrix.getVector(i);
rowInd = row.getIndices();
rowElem = row.getElements();
rowNumElem = row.getNumElements();
for(j = 0; j < rowNumElem; j++){
index = rowInd[j];
element = rowElem[j];
if(index < uColNum){
appendedRow.insert(index, element);
if(isA2Random != PARAM_OFF){
fullMatrixA2[i - uRowNum][index] = element;
fullMatrixA2Copy[i - uRowNum][index] = element;
}
}
}
//saharSto: when A2 is not random, we just store matrix A2
//as stocMatrixA2 because we know that it is the same
//for all scenarios
if(isA2Random == PARAM_OFF){
stocMatrixA2->appendRow(appendedRow);
}
appendedRow.clear();
}
//ignore first two lines
for(i = 0; i < 4; i++){
stoData_stream >> key;
}
while(stoData_stream >> key){
if(key == "SC"){
stoData_stream >> key;
stoData_stream >> key;
stoData_stream >> dValue;
scenarioProb_.push_back(dValue);
stoData_stream >> key;
start = uRowNum + scCount * truncLRowNum;
CoinDisjointCopyN(conLB + uRowNum, truncLRowNum, origRowLb_ + start);
CoinDisjointCopyN(conUB + uRowNum, truncLRowNum, origRowUb_ + start);
if((isA2Random != PARAM_OFF) && (isFirstScenario == false)){
//start = scCount * truncLRowNum;
for(i = 0; i < truncLRowNum; i++){
for(j = 0; j < uColNum; j++){
element = fullMatrixA2Copy[i][j];
if(fabs(element) > etol){
appendedRow.insert(j,element);
}
fullMatrixA2Copy[i][j] = fullMatrixA2[i][j];
}
stocMatrixA2->appendRow(appendedRow);
appendedRow.clear();
}
}
scCount ++;
if(isFirstScenario == true){
isFirstScenario = false;
}
}
else if(key == "ENDATA"){
if(isA2Random != PARAM_OFF){
for(i = 0; i < truncLRowNum; i++){
for(j = 0; j < uColNum; j++){
element = fullMatrixA2Copy[i][j];
if(fabs(element) > etol){
appendedRow.insert(j,element);
}
}
stocMatrixA2->appendRow(appendedRow);
appendedRow.clear();
}
}
break;
}
else if(key == "RHS"){
stoData_stream >> key;
stoData_stream >> dValue;
posRow = -1;
for(p = 0; p < numRows; p++){
if(rowName_[p] == key){
posRow = p;
break;
}
}
if(posRow < 0){
std::cout << key << " does not belong to the list of constraints." << std::endl;
throw CoinError("Wrong constriant name",
"readAuxiliaryData",
"MibSModel");
}
else if(posRow < uRowNum){
throw CoinError("Upper-level RHS cannot be random",
"readAuxiliaryData",
"MibSModel");
}
else{
index = (scCount - 1) * truncLRowNum + posRow;
if(rowSense[posRow] == 'L'){
origRowUb_[index] = dValue;
}
else{
origRowLb_[index] = dValue;
}
}
}
else{
posCol = -1;
for(p = 0; p < numCols; p++){
if(columnName_[p] == key){
posCol = p;
break;
}
}
if(posCol < 0){
std::cout << key << " does not belong to the list of variables." << std::endl;
throw CoinError("Wrong variable name",
"readAuxiliaryData",
"MibSModel");
}
else if(posCol >= uColNum){
throw CoinError("Lower-level coefficients cannot be random",
"readAuxiliaryData",
"MibSModel");
}
else{
stoData_stream >> key;
stoData_stream >> dValue;
posRow = -1;
for(p = 0; p < numRows; p++){
if(rowName_[p] == key){
posRow = p;
break;
}
}
if(posRow < 0){
std::cout << key << " does not belong to the list of constraints." << std::endl;
throw CoinError("Wrong constriant name",
"readAuxiliaryData",
"MibSModel");
}
else if(posRow < uRowNum){
throw CoinError("Coefficients of lower-level constraints cannot be random",
"readAuxiliaryData",
"MibSModel");
}
else{
fullMatrixA2Copy[posRow - uRowNum][posCol] = dValue;
}
}
}
}
setStocA2Matrix(stocMatrixA2);
stoData_stream.close();
std::cout << "Time Data File: " << getTimFile() << "\n";
std::cout << "Stochastic Data File: " << getStoFile() << "\n";
std::cout << "Number of LL Variables: "
<< truncLColNum << "\n\n";
}
else{
int iValue(0), pos(-1);
double dValue(0.0);
std::string key, fLColName, fLRowName;;
int i(0), k(0), p(0);
int uColNum(0), uRowNum(0);
int truncLColNum(0), truncLRowNum(0);
if(isSMPSFormat != PARAM_ON){
std::string fileName = getLowerFile();
fileCoinReadable(fileName);
std::ifstream data_stream(fileName.c_str());
if(!data_stream){
std::cout << "Error opening input data file. Aborting.\n";
abort();
}
while(data_stream >> key){
if(key == "N"){
data_stream >> iValue;
truncLColNum = iValue;
setTruncLowerDim(iValue);
}
else if(key == "M"){
data_stream >> iValue;
truncLRowNum = iValue;
setTruncLowerRowNum(iValue);
}
else if(key == "LC"){
data_stream >> iValue;
}
else if(key == "LR"){
data_stream >> iValue;
}
else if(key == "LO"){
if(!getLowerObjCoeffs())
lowerObjCoeffs_ = new double[getTruncLowerDim()];
data_stream >> dValue;
lowerObjCoeffs_[k] = dValue;
k++;
}
else if(key == "OS"){
data_stream >> dValue;
lowerObjSense_ = dValue; //1 min; -1 max
}
}
uColNum = numCols - truncLColNum;
uRowNum = numRows - truncLRowNum;
data_stream.close();
}
else{
std::string timFileName = getTimFile();
fileCoinReadable(timFileName);
std::ifstream timData_stream(timFileName.c_str());
if(!timData_stream){
std::cout << "Error opening input time data file. Aborting.\n";
abort();
}
//read time file
//ignoring first three lines
for(i = 0; i < 7; i++){
timData_stream >> key;
}
//name of first ll variable
timData_stream >> key;
fLColName = key;
//name of first ll row
timData_stream >> key;
fLRowName = key;
for(p = 0; p < numCols; p++){
if(columnName_[p] == fLColName){
pos = p;
break;
}
}
if(pos < 0){
std::cout << fLColName << " does not belong to the list of variables." << std::endl;
abort();
}
else{
uColNum = p;
truncLColNum = numCols - uColNum;
setTruncLowerDim(truncLColNum);
}
pos = -1;
for(p = 0; p < numRows; p++){
if(rowName_[p] == fLRowName){
pos = p;
break;
}
}
if(pos < 0){
std::cout << fLRowName << " does not belong to the list of constraints." << std::endl;
abort();
}
else{
uRowNum = p;
truncLRowNum = numRows - uRowNum;
setTruncLowerRowNum(truncLRowNum);
}
timData_stream >> key;
//set lower objective function coeffs and sense
if(!getLowerObjCoeffs())
lowerObjCoeffs_ = new double[truncLColNum];
for(i = 0; i < truncLColNum; i++){
timData_stream >> key;
timData_stream >> dValue;
lowerObjCoeffs_[i] = dValue;
}
timData_stream >> key;
timData_stream >> iValue;
lowerObjSense_ = iValue;//1 min; -1 max
timData_stream.close();
}
//saharSto:in the saa format, it is assumed that
//The indices of upper-level variables are 0,...,n1-1 and
//the indices of lower-level variables are n1,...,n1+n2-1
//and the same for upper- and lower-level constraints
//saharSto: we do not need LC and LR, but I considered them in case we
//want to use the input files for deterministic problems
if(!getLowerColInd())
lowerColInd_ = new int[truncLColNum];
CoinIotaN(lowerColInd_, truncLColNum, uColNum);
if(!getLowerRowInd())
lowerRowInd_ = new int[truncLRowNum];
CoinIotaN(lowerRowInd_, truncLRowNum, uRowNum);
if(isSMPSFormat == PARAM_ON){
std::cout << "Time Data File: " << getTimFile() << "\n";
std::cout << "Stochastic Data File: " << getStoFile() << "\n";
std::cout << "Number of LL Variables: "
<< truncLColNum << "\n\n";
}
else{
std::cout << "LL Data File: " << getLowerFile() << "\n";
std::cout << "Number of LL Variables: "
<< getTruncLowerDim() << "\n\n";
}
}
}
//#############################################################################
void
MibSModel::loadAuxiliaryData(int lowerColNum, int lowerRowNum,
const int *lowerColInd,
const int *lowerRowInd,
double lowerObjSense,
const double *lowerObjCoef,
int upperColNum, int upperRowNum,
const int *upperColInd,
const int *upperRowInd,
int structRowNum,
const int *structRowInd,
double interdictBudget,
const double *interdictCost,
const double *lColLbInLProb,
const double *lColUbInLProb)
{
int *copyLowerColInd = new int[lowerColNum];
int *copyLowerRowInd = new int[lowerRowNum];
double *copyLowerObjCoef = new double[lowerColNum];
int *copyUpperColInd = NULL;
int *copyUpperRowInd = NULL;
int *copyStructRowInd = NULL;
double *copyInterdictCost = NULL;
double *copyLColLbInLProb = NULL;
double *copyLColUbInLProb = NULL;
if (upperColInd != NULL){
copyUpperColInd = new int[upperColNum];
}
if (upperRowInd != NULL){
copyUpperRowInd = new int[upperRowNum];
}
if (structRowInd != NULL){
copyStructRowInd = new int[structRowNum];
}
if (interdictCost != NULL){
copyInterdictCost = new double[lowerColNum];
}
if (lColLbInLProb != NULL){
copyLColLbInLProb = new double[lowerColNum];
}
if (lColUbInLProb != NULL){
copyLColUbInLProb = new double[lowerColNum];
}
CoinDisjointCopyN(lowerColInd, lowerColNum, copyLowerColInd);
CoinDisjointCopyN(lowerRowInd, lowerRowNum, copyLowerRowInd);
CoinDisjointCopyN(lowerObjCoef, lowerColNum, copyLowerObjCoef);
if (upperColInd != NULL){
CoinDisjointCopyN(upperColInd, upperColNum, copyUpperColInd);
}
if (upperRowInd != NULL){
CoinDisjointCopyN(upperRowInd, upperRowNum, copyUpperRowInd);
}
if (structRowInd != NULL){
CoinDisjointCopyN(structRowInd, structRowNum, copyStructRowInd);
}
if (interdictCost != NULL){
CoinDisjointCopyN(interdictCost, lowerColNum, copyInterdictCost);
}
if (lColLbInLProb != NULL){
CoinDisjointCopyN(lColLbInLProb, lowerColNum, copyLColLbInLProb);
}
if (lColUbInLProb != NULL){
CoinDisjointCopyN(lColUbInLProb, lowerColNum, copyLColUbInLProb);
}