-
Notifications
You must be signed in to change notification settings - Fork 0
/
viprcomp.cpp
1805 lines (1546 loc) · 53.3 KB
/
viprcomp.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
/*
*
* Copyright (c) 2024 Zuse Institute Berlin
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
// Includes
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <limits>
#include "soplex.h"
#include <memory>
#include <soplex/dsvector.h>
#include <soplex/lprow.h>
#include <boost/bimap.hpp>
#include "CMakeConfig.hpp"
// Timing
#include <sys/time.h>
// Parallelization
#include <tbb/tbb.h>
// Namespaces
using namespace std;
using namespace soplex;
// sparse boolean vectors
typedef map<int, bool> SVectorBool;
typedef shared_ptr<DSVectorRational> DSVectorPointer;
class SVectorRat : public map<int, Rational>
{
public:
void compactify() { if( !_compact )
{
auto it = this->begin();
while( it != this->end() )
{
if( it->second == 0 ) this->erase(it++);
else ++it;
}
_compact = true;
}
}
bool operator!=(SVectorRat &other);
bool operator==(SVectorRat &other) { return !(*this != other);}
private:
bool _compact = false;
};
// Input and output files
ifstream certificateFile;
ofstream completedFile;
// Settings
bool debugmode = false;
bool usesoplex = true;
// Global variables
DSVectorRational dummycol(0); // SoPlex placeholder column
vector<string> variableNames; // variable names
DSVectorPointer ObjCoeff(make_shared<DSVectorRational>()); // sparse vector of objective coefficients
size_t numberOfVariables;
unsigned int nthreads = thread::hardware_concurrency();
struct Constraint {DSVectorPointer vec; Rational side; int sense; string line;}; // @todo: take care of deep copies here!
vector<Constraint> constraints;
typedef boost::bimap<int, long> bimap; // maps rows of the LP to corresponding indices in the certificate used for updating the local LPs
map<int, long> origConsCertIndex;
vector<tuple<Rational,Rational,long>> lowerBounds; // rational boundval, multiplier, long certindex
vector<tuple<Rational,Rational,long>> upperBounds; // rational boundval, multiplier, long certindex
struct passData { SoPlex passLp; bimap LProwCertificateMap;};
struct parallelData { passData* bufData; stringstream linestream; bool needscompletion; std::vector<size_t> conidx; string line;};
// circular buffer (also known as ring buffer, circular queue, cyclic queue), references:
// https://oneapi-src.github.io/oneTBB/main/tbb_userguide/Using_Circular_Buffers.html
// https://stackoverflow.com/a/15167828/15777342
class circBuf
{
int tail, head, size;
std::vector<passData*> arr;
public:
circBuf( int maxtokens )
{
head = tail = -1;
size = maxtokens;
arr.resize(maxtokens);
}
/// assignment operator
void resize( int maxtokens )
{
head = tail = -1;
size = maxtokens;
arr.resize(maxtokens);
}
void enqueue(passData* warmStartData);
passData* dequeue();
bool isEmpty() { return head == -1; }
};
void circBuf::enqueue( passData* warmStartData )
{
if( head == -1 )
{
head = tail = 0;
arr[tail] = warmStartData;
}
else if( (tail == size-1) && (head != 0) )
{
tail = 0;
arr[tail] = warmStartData;
}
else
{
tail++;
arr[tail] = warmStartData;
}
}
passData* circBuf::dequeue()
{
passData* data = arr[head];
if( head == tail )
{
head = -1;
tail = -1;
}
else if( head == size-1 )
{
head = 0;
}
else
{
head++;
}
return data;
}
// Forward declaration
void modifyFileName( string &path, const string &newExtension );
bool checkversion( string ver );
bool processVER();
bool processVAR( SoPlex &workinglp );
bool processINT();
bool processOBJ( SoPlex &workinglp );
bool processCON( SoPlex &workinglp );
bool processRTP();
bool processSOL();
bool processDER( SoPlex workinglp );
bool getConstraints( SoPlex &workinglp, string &consense, Rational &rhs, int &activeConstraint, size_t currentDerivation);
std::string completelin( SoPlex &workinglp, bimap& LProwCertificateMap, Constraint &constraint);
static bool completeWeakDomination(DSVectorRational &row, int consense, Rational &rhs, stringstream& completedLine,
stringstream &initialLine);
static bool readLinComb( int &sense, Rational &rhs, SVectorRat& coefficients, SVectorRat& mult,
int currentConstraintIndex, SVectorBool &assumptionList, stringstream &baseLine);
static bool readMultipliers( int &sense, SVectorRat &mult, stringstream &initialLine );
bool completeIncomplete( SoPlex &workinglp, bimap& LProwCertificateMap, vector<long> &activeDerivations, string label, stringstream& completedLine,
stringstream &baseLine );
bool printReasoningToLine(DVectorRational &dualmultipliers, DVectorRational &reducedcosts, stringstream& completedLine, bimap& LProwCertificateMap);
// Global bound changes for completing weak
static void processGlobalBoundChange(Rational rhs, Rational boundmult, int varindex,
long boundindex, int sense)
{
char restline[256];
string tmpstring;
string lastword;
if( certificateFile.peek() != '\n' )
{
certificateFile.getline(restline, 256);
tmpstring = restline;
istringstream tmpstream(tmpstring);
while(tmpstream >> lastword)
{
if( lastword == "global" )
{
if( sense <= 0 ) // eq or ub
upperBounds[varindex] = make_tuple(rhs, boundmult, boundindex);
if( sense >= 0 ) // eq or lb
lowerBounds[varindex] = make_tuple(rhs, boundmult, boundindex);
}
}
}
}
static double getTimeSecs(timeval start, timeval end)
{
auto seconds = end.tv_sec - start.tv_sec;
auto microseconds = end.tv_usec - start.tv_usec;
auto clock_dur = seconds + microseconds*1e-6;
return clock_dur;
}
// Set usage options
static void printUsage(const char* const argv[], int idx)
{
const char* usage =
"general options:\n"
" --soplex=on/off use soplex to complete derivations? needs to be on if incomplete derivations in certificate file;\
\n turn off to boost performance if only weak derivations are present.\n"
" --debugmode=on/off enable extra debug output from viprcomp\n"
" --verbosity=<level> set verbosity level inside SoPlex\n"
" --threads=<number> maximal number of threads to use \n"
"\n";
if(idx <= 0)
cerr << "missing input file\n\n";
else
cerr << "invalid option \"" << argv[idx] << "\"\n\n";
cerr << "usage: " << argv[0] << " " << "[options] <certificateFile>\n"
<< " <certificateFile> .vipr file to be completed\n\n"
<< usage;
}
// Main function
int main(int argc, char *argv[])
{
int returnStatement = -1;
int optidx;
const char* certificateFileName;
int verbosity = 0;
string path = "";
if( argc == 0 )
{
printUsage(argv, -1);
return 1;
}
// read arguments from command line
for(optidx = 1; optidx < argc; optidx++)
{
char* option = argv[optidx];
// we reached <certificateFile>
if(option[0] != '-')
{
certificateFileName = argv[optidx];
continue;
}
// option string must start with '-', must contain at least two characters, and exactly two characters if and
// only if it is -x, -y, -q, or -c
if(option[0] != '-' || option[1] == '\0'
|| ((option[2] == '\0') != (option[1] == 'x' || option[1] == 'X' || option[1] == 'y'
|| option[1] == 'Y' || option[1] == 'q' || option[1] == 'c')))
{
printUsage(argv, optidx);
return 1;
}
switch(option[1])
{
case '-' :
option = &option[2];
// set verbosity of SoPlex
if(strncmp(option, "verbosity=", 10) == 0)
{
char* str = &option[10];
if( isdigit(option[10]))
{
verbosity = atoi(str);
if( verbosity < 0 || verbosity > 5 )
{
cerr << "Verbosity level outside range 0 to 5. Read " << verbosity << " instead." << endl;
printUsage(argv, optidx);
return 1;
}
}
}
// enable additional debug output
else if(strncmp(option, "debugmode=", 10) == 0)
{
char* str = &option[10];
// Set Debugmode
if( string(str) == "on")
{
debugmode = true;
cout << "Debugmode turned on." << endl;
}
else if( string(str) == "off")
{
debugmode = false;
cout << "Debugmode turned off." << endl;
}
else
{
cout << "Unknown input for debug settings (on/off expected). Read "
<< string(str) << " instead" << endl;
cout << "Continue with default setings (debugmode off)" << endl;
}
}
// enable/disable soplex -> faster if no incomplete, necessary if incomplete
else if(strncmp(option, "soplex=", 7) == 0)
{
char* str = &option[7];
// Set Debugmode
if( string(str) == "on")
{
usesoplex = true;
cout << "SoPlex turned on." << endl;
}
else if( string(str) == "off")
{
usesoplex = false;
cout << "SoPlex turned off." << endl;
}
else
{
cout << "Unknown input for SoPlex suopport (on/off expected). Read "
<< string(str) << " instead" << endl;
cout << "Continue with default setings (SoPlex on)" << endl;
}
}
// set maximal number of threads that should be used
else if(strncmp(option, "threads=", 8) == 0)
{
char* str = &option[8];
if( isdigit(option[8]))
{
int maxthreads = atoi(str);
if( maxthreads < 0 || maxthreads > thread::hardware_concurrency() )
{
cerr << "threads outside of valid range: specified " << maxthreads << " but maximal number is " << thread::hardware_concurrency() << endl;
printUsage(argv, optidx);
return 1;
}
else
nthreads = maxthreads;
}
}
else if(strncmp(option, "outfile=", 8) == 0)
{
path = string(&option[8]);
}
else
{
printUsage(argv, optidx);
return 1;
}
}
}
certificateFile.open(certificateFileName);
if( certificateFile.fail() )
{
cerr << "Failed to open file " << argv[1] << endl;
printUsage(argv, -1);
return returnStatement;
}
if(path == "")
{
path = certificateFileName;
modifyFileName(path, "_complete.vipr");
}
completedFile.open( path.c_str(), ios::out );
if( completedFile.fail() )
{
cerr << "Failed to open file " << path << endl;
printUsage(argv, -1);
return returnStatement;
}
// Set parameters for exact solving
SoPlex baselp;
baselp.setIntParam(SoPlex::READMODE, SoPlex::READMODE_RATIONAL);
baselp.setIntParam(SoPlex::SOLVEMODE, SoPlex::SOLVEMODE_RATIONAL);
baselp.setIntParam(SoPlex::CHECKMODE, SoPlex::CHECKMODE_RATIONAL);
baselp.setIntParam(SoPlex::SYNCMODE, SoPlex::SYNCMODE_AUTO);
baselp.setRealParam(SoPlex::FEASTOL, 0.0);
baselp.setRealParam(SoPlex::OPTTOL, 0.0);
baselp.setIntParam(SoPlex::VERBOSITY, verbosity); // verbosity var prob. obsolete for parallelization
struct timeval start, end;
gettimeofday( &start, 0);
if( processVER() )
if( processVAR(baselp) )
if( processINT() )
if( processOBJ(baselp) )
if( processCON(baselp) )
if( processRTP() )
if( processSOL() )
{
gettimeofday( &end, 0 );
cout << endl << "reading took " << getTimeSecs(start, end)
<< " seconds (Wall Clock)" << endl;
gettimeofday( &start, 0 );
if( processDER(baselp) )
{
cout << "Completion of File successful!" <<endl;
returnStatement = 0;
gettimeofday( &end, 0 );
cout << endl << "completing and printing took " << getTimeSecs(start, end)
<< " seconds (Wall Clock)" << endl;
}
}
return returnStatement;
}
// append "_complete.vipr" to filename
void modifyFileName(string &path, const string& newExtension)
{
string::size_type position = path.find_last_of('.');
if( position == string::npos )
position = path.size();
path.replace(position, newExtension.length(), newExtension);
}
// Version control for .vipr input file. Backward compatibility possible for minor versions
bool checkVersion(string version)
{
bool returnStatement = false;
size_t position = version.find(".");
completedFile << " " + version;
int major = atoi(version.substr(0, position).c_str());
int minor = atoi(version.substr(position+1, version.length()-position).c_str());
cout << "Certificate format version " << major << "." << minor << endl;
if( (major ==VIPR_VERSION_MAJOR) && (minor <=VIPR_VERSION_MINOR) )
{
returnStatement = true;
}
else
{
cerr << "Version unsupported" << endl;
}
return returnStatement;
}
// Check version and correct file format allows next process to start
// Error if the version is incompatible or not specified
bool processVER()
{
bool returnStatement = false;
string tmpStr;
for( ;; )
{
certificateFile >> tmpStr;
if( tmpStr == "VER" )
{
completedFile << tmpStr;
certificateFile >> tmpStr;
returnStatement = checkVersion(tmpStr);
break;
}
else if( tmpStr == "%" )
{
getline( certificateFile, tmpStr );
}
else
{
cerr << endl << "Comment or VER expected. Read instead "
<< tmpStr << endl;
break;
}
}
return returnStatement;
}
// Processes number of variables
// Puts placeholder variables in SoPlex LP
// Error if nr of variables invalid or number of variables > specified variables or section missing
bool processVAR(SoPlex &workinglp)
{
cout << endl << "Processing VAR section..." << endl;
auto returnStatement = true;
string section;
certificateFile >> section;
// Check section
if( section != "VAR" )
{
cerr << "VAR expected. Read instead " << section << endl;
}
else
{
completedFile << "\r\n" + section;
certificateFile >> numberOfVariables; // number of variables
if( certificateFile.fail() || numberOfVariables < 0 )
{
cerr << "Invalid number after VAR" << endl;
returnStatement = false;
}
// Store variables
else
{
completedFile << " " << numberOfVariables;
upperBounds.resize( numberOfVariables );
lowerBounds.resize( numberOfVariables );
fill(upperBounds.begin(), upperBounds.end(), make_tuple(infinity, 1, -1));
fill(lowerBounds.begin(), lowerBounds.end(), make_tuple(-infinity, 1, -1));
for( int i = 0; i < numberOfVariables; ++i )
{
string tmp;
certificateFile >> tmp;
if( certificateFile.fail() )
{
cerr << "Error reading variable for index " << i << endl;
returnStatement = false;
break;
}
completedFile <<"\r\n" + tmp;
if( usesoplex )
{
workinglp.addColRational( LPColRational( 1, dummycol, infinity, -infinity ) );
}
variableNames.push_back( tmp );
}
}
}
return returnStatement;
}
// Processes number of integer variables and specifies their indices
// Produces vector of indices of integer variables
// Error if nr of integers invalid or nr of integers > specified integers or section missing
bool processINT()
{
size_t numberOfIntegers;
cout << endl << "Processing INT section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
if( section!= "INT" )
{
cerr << "INT expected. Read instead: " << section << endl;
return returnStatement;
}
completedFile << "\r\n" + section;
certificateFile >> numberOfIntegers;
if( certificateFile.fail() )
{
cerr << "Failed to read number after INT" << endl;
return returnStatement;
}
completedFile << " " <<numberOfIntegers << "\r\n";
int index;
for( int i = 0; i < numberOfIntegers; ++i )
{
certificateFile >> index;
if( certificateFile.fail() )
{
cerr << "Error reading integer index " << i << endl;
return returnStatement;
}
completedFile << index << " ";
}
returnStatement = true;
return returnStatement;
}
// Processes the sense of the objective function and coefficients for variables
// Stores sense and coefficients for possible future use
// Error if objective sense invalid (other than -1, 0, 1 for min, equality, max) or subroutine fails
bool processOBJ(SoPlex &workinglp)
{
bool returnStatement = false;
string section, objectiveSense;
int numberOfObjCoeff, idx;
vector<Rational> values;
vector<int> indices;
Rational val;
VectorRational Objective(0); // full objective Vector
cout << endl << "Processing OBJ section..." << endl;
certificateFile >> section;
if( section != "OBJ" )
{
cerr << "OBJ expected. Read instead: " << section << endl;
return returnStatement;
}
completedFile << "\r\n" + section;
certificateFile >> objectiveSense;
completedFile << " " + objectiveSense;
if( objectiveSense == "min" )
{
workinglp.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
}
else if( objectiveSense == "max" )
{
workinglp.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MAXIMIZE);
}
else
{
cerr << "Invalid objective sense: " << objectiveSense << endl;
return returnStatement;
}
Objective.reSize(numberOfVariables);
Objective.reDim(numberOfVariables);
certificateFile >> numberOfObjCoeff;
completedFile << "\r\n" << numberOfObjCoeff << " ";
values.reserve(numberOfObjCoeff);
indices.reserve(numberOfObjCoeff);
for( int j = 0; j < numberOfObjCoeff; ++j)
{
certificateFile >> idx >> val;
completedFile << idx << " " << val << " ";
values.push_back(val);
indices.push_back(idx);
}
ObjCoeff->add(numberOfObjCoeff, indices.data(), values.data());
Objective.assign(*ObjCoeff);
if( usesoplex )
workinglp.changeObjRational(Objective);
returnStatement = true;
return returnStatement;
}
// Processes the constraint section of the file
// Stores constraints for future access
bool processCON(SoPlex &workinglp)
{
bool returnStatement = false;
string section;
string label, consense;
Rational rhs;
size_t numberOfConstraints, numberOfBoundedCons, currentDerivation;
cout << endl << "Processing CON section..." << endl;
certificateFile >> section;
if( section!= "CON" )
{
cerr << "CON expected. Read instead: " << section << endl;
return returnStatement;
}
completedFile << "\n" + section;
certificateFile >> numberOfConstraints >> numberOfBoundedCons;
completedFile << " " << numberOfConstraints << " " << numberOfBoundedCons;
currentDerivation = 0;
for( int i = 0; i < numberOfConstraints; ++i)
{
certificateFile >> label >> consense >> rhs;
completedFile << "\r\n" + label + " " + consense + " " << rhs << " ";
currentDerivation++;
returnStatement = getConstraints(workinglp, consense, rhs, i, currentDerivation);
}
return returnStatement;
}
// Processes the relation to prove in order to complete file
bool processRTP()
{
cout << endl << "Processing RTP section..." << endl;
bool returnStatement = false;
string section, relationToProveTypeStr, upperString, lowerString;
certificateFile >> section;
if( section != "RTP" )
{
cerr << "RTP expected. Read instead " << section << endl;
}
else
{
completedFile << "\r\n" + section;
certificateFile >> relationToProveTypeStr;
if( relationToProveTypeStr == "infeas")
{
completedFile << " " + relationToProveTypeStr;
returnStatement = true;
}
else if( relationToProveTypeStr != "range" )
{
cerr << "RTP: unrecognized verification type: " << relationToProveTypeStr << endl;
return returnStatement;
}
else
{
completedFile << " " + relationToProveTypeStr;
certificateFile >> lowerString >> upperString;
completedFile << " " + lowerString + " " + upperString;
returnStatement = true;
}
}
return returnStatement;
}
// Processes the solutions to check in order to complete file
bool processSOL()
{
bool returnStatement = false;
string section, label;
int numberOfVarSol = 0;
int idx;
Rational val;
size_t numberOfSolutions;
cout << endl << "Processing SOL section... " << endl;
certificateFile >> section;
if( section != "SOL" )
{
cerr << "SOL expected. Read instead " << section << endl;
return returnStatement;
}
completedFile << "\r\n" + section;
certificateFile >> numberOfSolutions;
if( certificateFile.fail() )
{
cerr << "Failed to read number after SOL" << endl;
return returnStatement;
}
else if( numberOfSolutions < 0 )
{
cerr << "Invalid number after SOL: " << numberOfSolutions << endl;
return returnStatement;
}
else
{
completedFile << " " << numberOfSolutions;
for( int i = 0; i < numberOfSolutions; ++i )
{
certificateFile >> label >> numberOfVarSol;
completedFile << "\r\n" << label << " " << numberOfVarSol;
for( int i = 0; i < numberOfVarSol; ++i )
{
certificateFile >> idx >> val;
completedFile << " " << idx << " " << val;
}
}
returnStatement = true;
}
return returnStatement;
}
static bool isEqual(DSVectorRational row1, DSVectorRational row2)
{
if( row1.size() != row2.size() )
return false;
else
{
for( int i = 0; i < row1.size(); i++ )
{
if( row1.index(i) != row2.index(i))
return false;
if( row1[i] != row2[i] )
return false;
}
}
return true;
}
static size_t pushLineToConstraints(string& line, size_t conidx)
{
stringstream linestream(line);
string label, numberOfCoefficients, consense;
int sense, intOfCoefficients = 0;
long idx;
Rational val, rhs;
DSVectorPointer row;
vector<Rational> values;
vector<int> indices;
bool isobjective, global;
linestream >> label >> consense >> rhs;
linestream >> numberOfCoefficients;
if( numberOfCoefficients == "OBJ" )
{
isobjective = true;
row = ObjCoeff;
intOfCoefficients = row->size();
}
else
{
isobjective = false;
intOfCoefficients = atoi(numberOfCoefficients.c_str());
values.reserve(intOfCoefficients);
indices.reserve(intOfCoefficients);
for( int j = 0; j < intOfCoefficients; ++j )
{
linestream >> idx >> val;
values.push_back(val);
indices.push_back(idx);
}
row = make_shared<DSVectorRational>();
(*row).add(intOfCoefficients, indices.data(), values.data());
}
switch(consense[0])
{
case 'E':
sense = 0; break;
case 'L':
sense = -1; break;
case 'G':
sense = 1; break;
default:
cerr << "wrong sense for constraints " << consense << endl;
break;
}
constraints[conidx] = {row, rhs, sense, line};
return constraints.size() - 1;
}
// read data from certificateFile and buffer it correctly in the stringstream of returnData
static void processSequentialInputFilter(parallelData& returnData, size_t& lineindex, tbb::flow_control& fc)
{
string line;
bool stop = false;
int nbufferedlines = 0;
stringstream passstream;
bool lineneedscompletion = false;
size_t filepos = certificateFile.tellg();
while( !stop && getline(certificateFile, line) )
{
lineneedscompletion = (line.find("weak") != string::npos) || (line.find("incomplete") != string::npos);
// line needs to be completed -> only one line at a time
if( lineneedscompletion )
{
if( nbufferedlines == 0 )
{
returnData.conidx.push_back(lineindex);
pushLineToConstraints(line, lineindex);
lineindex++;
nbufferedlines++;
passstream << line;
stop = true;
}
else
{
// reset to beginning of line
certificateFile.seekg(filepos);
stop = true;
lineneedscompletion = false;
}
}
else
{
returnData.conidx.push_back(lineindex);
pushLineToConstraints(line, lineindex);
lineindex++;
passstream << line << std::endl;
nbufferedlines++;
if( nbufferedlines >= 10 )
stop = true;
}
filepos = certificateFile.tellg();
}
returnData.needscompletion = lineneedscompletion;
if( nbufferedlines > 0 )
returnData.linestream << passstream.rdbuf();
else
fc.stop();
}
// read data from certificateFile and buffer it correctly in the stringstream of returnData
static void readFileIntoMemory(size_t initialLine, std::vector<size_t>& toCompleteLines)
{
string line;
size_t lineindex = initialLine;
bool lineneedscompletion = false;
while( getline(certificateFile, line) )
{
lineneedscompletion = (line.find("weak") != string::npos) || (line.find("incomplete") != string::npos);
// line needs to be completed -> only one line at a time
if( lineneedscompletion )
toCompleteLines.push_back(lineindex);
pushLineToConstraints(line, lineindex);
lineindex++;
}
}
bool processDER(SoPlex workinglp)
{
bool returnStatement = false;
string section;
size_t numberOfDerivations;
size_t numberOfConstraints = constraints.size();
size_t completedLines = 0;
size_t lineindex;
timeval start, end;
std::vector<size_t> toCompleteLines;
std::vector<string> completedlines;
cout << endl << "Processing DER section... " << endl;
certificateFile >> section;
gettimeofday( &start, 0 );
if( section!= "DER" )
{
cerr << "DER expected. Read instead " << section << endl;
return false;
}
completedFile << "\r\n" + section;
certificateFile >> numberOfDerivations;
completedFile << " " << numberOfDerivations;
constraints.resize(constraints.size() + numberOfDerivations);
cout << "Number of Derivations is " << numberOfDerivations << endl;
if( numberOfDerivations == 0 )
{
cout << "Number of derivations = 0. Nothing to complete." << endl;
return true;
}
// skip to next line
certificateFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');