-
Notifications
You must be signed in to change notification settings - Fork 0
/
viprchk.cpp
1707 lines (1379 loc) · 44.5 KB
/
viprchk.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) 2016 Kevin K. H. Cheung
* 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.
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <cassert>
#include <gmpxx.h>
#include <gmp.h>
#include <cstdio>
#include <ctime>
#include <chrono>
#include <memory>
#include "CMakeConfig.hpp"
// Avoid using namespace std to avoid non-obvious complications (ambiguities)
using std::map;
using std::string;
using std::shared_ptr;
using std::vector;
using std::ifstream;
using std::make_shared;
using std::cerr;
using std::endl;
using std::cout;
// Types
typedef map<int, bool> SVectorBool;
// The type of derivation used to derive a constraint
enum DerivationType
{
ASM, // assumption
LIN, // simple implication
RND, // simple implication with integer rounding; i.e. a CG cut
UNS, // unsplit operation
SOL, // cutoff bound from primal solution
UNKNOWN
};
// The type of relation to prove
enum RelationToProveType
{
INFEAS, // infeasible
RANGE // lower bound (-inf if none) and upper bound (inf if none) to be verified
};
// Classes
// Sparse vectors of rational numbers as maps
class SVectorGMP : public map<int, mpq_class>
{
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!=(SVectorGMP &other);
bool operator==(SVectorGMP &other) { return !(*this != other);}
SVectorGMP operator-(const SVectorGMP &other)
{
SVectorGMP returnsvec(*this);
for(auto it = other.begin(); it != other.end(); ++it)
{
returnsvec[it->first] -= it->second;
}
return returnsvec;
}
private:
bool _compact = false;
};
// Constraint format
class Constraint
{
public:
Constraint() {}
Constraint( const string label, const int sense, const mpq_class rhs,
shared_ptr<SVectorGMP> coefficients, const bool isAssumptionCon,
const SVectorBool assumptionList):
_label(label), _sense(sense), _rhs(rhs), _coefficients(coefficients),
_isAssumption(isAssumptionCon), _assumptionList(assumptionList)
{
_coefficients->compactify();
_trashed = false;
_falsehood = _isFalsehood();
}
bool round();
mpq_class getRhs() const { return _rhs; }
mpq_class getCoef(const int index) { if( _coefficients->find(index) != _coefficients->end() )
return (*_coefficients)[index];
else return mpq_class(0); }
shared_ptr<SVectorGMP> coefSVec() const { return _coefficients; }
int getSense() const { return _sense; }
bool isAssumption() { return _isAssumption; }
bool isFalsehood() const { return _falsehood; }
// true iff the constraint is a contradiction like 0 >= 1
bool isTautology();
// true iff the constraint is a tautology like 0 <= 1
bool hasAsm(const int index) {
return (_assumptionList.find(index) != _assumptionList.end());
}
void setassumptionList(const SVectorBool assumptionList) { _assumptionList = assumptionList; }
SVectorBool getassumptionList() const { return _assumptionList; }
bool dominates(Constraint &other) const;
void print();
void trash() { _trashed = true; _falsehood = false; _coefficients = nullptr;
_rhs = 0; _assumptionList.clear(); }
bool isTrashed() const { return _trashed; }
string label() const { return _label; }
void setMaxRefIdx(int refIdx) { _refIdx = refIdx; }
int getMaxRefIdx() { return _refIdx; }
Constraint operator-(const Constraint& other)
{
Constraint returncons(*this);
for(auto it = other._coefficients->begin(); it != other._coefficients->end(); ++it)
{
(*returncons._coefficients)[it->first] -= it->second;
}
returncons._rhs -= other._rhs;
return returncons;
}
private:
string _label;
int _sense;
mpq_class _rhs;
shared_ptr<SVectorGMP> _coefficients;
int _refIdx = -1;
bool _isAssumption;
SVectorBool _assumptionList; // constraint index list that are assumptions
bool _falsehood;
bool _isFalsehood();
bool _trashed;
};
// Globals
const SVectorBool emptyList;
int numberOfVariables = 0; // number of variables
int numberOfConstraints = 0; // number of constraints
int numberOfBounds = 0; // number of bounds
int numberOfDerivations = 0; // number od derivations
int numberOfSolutions = 0; // number of solutions
vector<bool> isInt; // integer variable indices
vector<string> variable; // variable names
vector<Constraint> constraint; // all the constraints, including derived ones
vector<SVectorGMP> solution; // all the solutions for checking feasibility
ifstream certificateFile; // certificate file stream
RelationToProveType relationToProveType;
mpq_class bestObjectiveValue; // best objective function value of specified solutions
mpq_class lowerBound; // lower bound for optimal value to be checked
mpq_class upperBound; // upper bound for optimal value to be checked
string lowerStr, upperStr;
bool isMin; // is minimization problem
bool checkLower; // true iff need to verify lower bound
bool checkUpper; // true iff need to verify upper bound
Constraint relationToProve; // constraint to be derived in the case of bound checking
shared_ptr<SVectorGMP> objectiveCoefficients(make_shared<SVectorGMP>()); // obj coefficients
bool objectiveIntegral;
// Forward declaration
bool checkVersion(string ver);
bool processVER();
bool processVAR();
bool processINT();
bool processOBJ();
bool processCON();
bool processRTP();
bool processSOL();
bool processDER();
bool readMultipliers(int &sense, SVectorGMP &mult);
bool readConstraintCoefficients(shared_ptr<SVectorGMP> &v);
bool readConstraint( string &label, int &sense, mpq_class &rhs,
shared_ptr<SVectorGMP> &coef);
inline mpq_class floor(const mpq_class &q); // rounding down
inline mpq_class ceil(const mpq_class &q); // rounding up
bool isInteger(const mpq_class &q); // check if variable is integer
mpq_class scalarProduct(shared_ptr<SVectorGMP> u, shared_ptr<SVectorGMP> v);
bool canUnsplit( Constraint &toDer, const int con1, const int a1, const int con2,
const int a2, SVectorBool &assumptionList);
bool readLinComb( int &sense, mpq_class &rhs, shared_ptr<SVectorGMP> coef,
int currConIdx, SVectorBool &amsList);
// Main function
int main(int argc, char *argv[])
{
int returnStatement = -1;
if( argc != 2 )
{
cerr << "Usage: " << argv[0] << " <certificate filename>\n";
return returnStatement;
}
certificateFile.open(argv[1]);
if( certificateFile.fail() )
{
cerr << "Failed to open file " << argv[1] << endl;
return returnStatement;
}
double start_cpu_tm = clock();
if( processVER() )
if( processVAR() )
if( processINT() )
if( processOBJ() )
if( processCON() )
if( processRTP() )
if( processSOL() )
if( processDER() ) {
returnStatement = 0;
double cpu_dur = (clock() - start_cpu_tm)
/ (double)CLOCKS_PER_SEC;
cout << endl << "Completed in " << cpu_dur
<< " seconds (CPU)" << endl;
}
return returnStatement;
}
// Processes in order of appearance
// Version control for .vipr input file. Backward compatibility possible for minor versions
bool checkVersion(string version)
{
bool returnStatement = false;
size_t position = version.find(".");
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" )
{
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 then indexes them in order from 0 to n-1
// Produces vector of variables
// Error if nr of variables invalid or number of variables > specified variables or section missing
bool processVAR()
{
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
{
certificateFile >> numberOfVariables; // number of variables
if( certificateFile.fail() || numberOfVariables < 0 )
{
cerr << "Invalid number after VAR" << endl;
returnStatement = false;
}
// Store variables
else
{
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;
}
variable.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()
{
cout << endl << "Processing INT section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
// Check section
if( section != "INT" )
{
cerr << "INT expected. Read instead " << section << endl;
}
else
{
auto numberOfIntegers = 0;
certificateFile >> numberOfIntegers; // number of integer variables
if( certificateFile.fail() || numberOfIntegers < 0 )
{
cerr << "Invalid number after INT" << endl;
}
// Store integer variables
else
{
isInt.resize(variable.size());
for( auto it = isInt.begin(); it != isInt.end(); ++it )
{
*it = false;
}
if( numberOfIntegers > 0 ) {
for( int i = 0; i < numberOfIntegers; ++i )
{
int index;
certificateFile >> index;
if( certificateFile.fail() )
{
cerr << "Error reading integer index " << i << endl;
goto TERMINATE;
}
isInt[index] = true;
}
}
returnStatement = true;
}
}
TERMINATE:
return returnStatement;
}
// Processes the sense of the objective function and coefficients for variables
// Stores sense and runs subroutine to store coefficients
// Error if objective sense invalid (other than -1, 0, 1 for min, equality, max) or subroutine fails
bool processOBJ()
{
cout << endl << "Processing OBJ section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
// Check section
if( section != "OBJ" )
{
cerr << "OBJ expected. Read instead " << section << endl;
}
else
{
string objectiveSense;
certificateFile >> objectiveSense;
if( objectiveSense == "min" )
{
isMin = true;
}
else if( objectiveSense == "max" )
{
isMin = false;
}
else
{
cerr << "Invalid objective sense: " << objectiveSense << endl;
goto TERMINATE;
}
returnStatement = readConstraintCoefficients(objectiveCoefficients);
objectiveIntegral = true;
for( auto it = objectiveCoefficients->begin(); it != objectiveCoefficients->end(); ++it )
{
if ( !isInteger(it->second) || !isInt[it->first] )
objectiveIntegral = false;
}
if( !returnStatement )
{
cerr << "Failed to read objective coefficients" << endl;
}
}
TERMINATE:
return returnStatement;
}
// Processes constraints
// Produces vector of constraints and calls subroutine
// Error if number of constraints or bounds smaller 0
bool processCON()
{
cout << endl << "Processing CON section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
// Check section
if( section != "CON" )
{
cerr << "CON expected. Read instead " << section << endl;
}
else
{
certificateFile >> numberOfConstraints >> numberOfBounds;
// numberOfBounds not used in verification but useful for debugging
if( certificateFile.fail() || numberOfConstraints < 0 || numberOfBounds < 0 )
{
cerr << "Invalid number(s) after CON" << endl;
}
// Store constraints
else
{
string label;
int sense;
mpq_class rhs;
for( int i = 0; i < numberOfConstraints; i++ )
{
shared_ptr<SVectorGMP> coef(make_shared<SVectorGMP>());
returnStatement = readConstraint(label, sense, rhs, coef);
if( !returnStatement ) break;
constraint.push_back(Constraint(label, sense, rhs, coef, false, emptyList));
}
}
}
return returnStatement;
}
// Processes the relation to prove - either infeasibility or given range
// Stores type of relation
// Error if invalid verification type or bounds
bool processRTP()
{
cout << endl << "Processing RTP section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
// Checking section
if( section != "RTP" )
{
cerr << "RTP expected. Read instead " << section << endl;
}
else
{
string relationToProveTypeStr;
certificateFile >> relationToProveTypeStr;
// Check verification type
if( relationToProveTypeStr == "infeas" )
{
relationToProveType = RelationToProveType::INFEAS;
cout << endl << "Need to verify infeasibility. " << endl;
}
else if( relationToProveTypeStr != "range" )
{
cerr << "RTP: unrecognized verification type: " << relationToProveTypeStr << endl;
goto TERMINATE;
}
else
{
relationToProveType = RelationToProveType::RANGE;
checkLower = checkUpper = false;
certificateFile >> lowerStr >> upperStr;
if( lowerStr != "-inf" )
{
checkLower = true;
lowerBound = mpq_class(lowerStr);
}
if( upperStr != "inf" )
{
checkUpper = true;
upperBound = mpq_class(upperStr);
}
// Check bounds
if( checkLower && checkUpper && (lowerBound > upperBound) )
{
cerr << "RTP: invalid bounds. " << endl;
goto TERMINATE;
}
// Stores relation as Constraint variable
if( isMin && checkLower )
{
relationToProve = Constraint("rtp", 1, lowerBound, objectiveCoefficients, false, emptyList);
}
else if( !isMin && checkUpper )
{
relationToProve = Constraint("rtp", -1, upperBound, objectiveCoefficients, false, emptyList);
}
else
{
returnStatement = true;
goto TERMINATE;
}
cout << "Need to verify optimal value range "
<< (lowerStr == "-inf" ? "(" : "[")
<< lowerStr << ", " << upperStr
<< (upperStr == "inf" ? ")" : "]")
<< "." << endl;
}
returnStatement = true;
}
TERMINATE:
return returnStatement;
}
// Processes solutions to be verified
// Checks constraints and bounds
// Error if wrong format, type or if bounds violated by solution
bool processSOL()
{
cout << endl << "Processing SOL section..." << endl;
bool returnStatement = false;
mpq_class value;
string section, label;
certificateFile >> section;
// Check format
if( section != "SOL" )
{
cerr << "SOL expected. Read instead " << section << endl;
return returnStatement;
}
certificateFile >> numberOfSolutions;
if( certificateFile.fail() )
{
cerr << "Failed to read number after SOL" << endl;
}
else if( numberOfSolutions < 0 )
{
cerr << "Invalid number after SOL: " << numberOfSolutions << endl;
}
else
{
auto satisfies = [] (Constraint &con, shared_ptr<SVectorGMP> &x)
{
bool returnStat = false;
mpq_class prod = scalarProduct(con.coefSVec(), x);
if( con.getSense() < 0 )
{
returnStat = (prod <= con.getRhs());
}
else if( con.getSense() > 0 )
{
returnStat = (prod >= con.getRhs());
}
else
{
returnStat = (con.getRhs() == prod);
}
return returnStat;
};
shared_ptr<SVectorGMP> solutionSpecified(make_shared<SVectorGMP>());
vector<mpq_class> sol(numberOfVariables);
for( int i = 0; i < numberOfSolutions; ++i )
{
certificateFile >> label;
cout << "checking solution " << label << endl;
if( !readConstraintCoefficients(solutionSpecified) )
{
cerr << "Failed to read solution." << endl;
goto TERMINATE;
}
else
{
for( int j = 0; j < numberOfVariables; ++j )
{
sol[j] = 0;
}
// Check integrality constraints
for( auto it = solutionSpecified->begin(); it != solutionSpecified->end(); ++it )
{
if( isInt[it->first] && !isInteger(it->second) )
{
cerr << "Noninteger value for integer variable "
<< it->first << endl;
goto TERMINATE;
}
sol[it->first] = it->second;
}
for( int j = 0; j < numberOfConstraints; ++j )
{
if( !satisfies(constraint[i], solutionSpecified) )
{
cerr << "Constraint " << i << " not satisfied." << endl;
goto TERMINATE;
}
}
}
value = scalarProduct(objectiveCoefficients , solutionSpecified);
cout << " objval = " << value << endl;
// Update best value for the objective function
if( i )
{
if( isMin && value < bestObjectiveValue )
{
bestObjectiveValue = value;
}
else if( !isMin && value > bestObjectiveValue )
{
bestObjectiveValue = value;
}
}
else
{
bestObjectiveValue = value;
}
}
if( numberOfSolutions )
{
cout << "Best objval: " << bestObjectiveValue << endl;
// Check if bounds are already violated
if( isMin && checkUpper && bestObjectiveValue > upperBound )
{
cerr << "Best objective values (" << bestObjectiveValue<< ") exceeds upper bound (" << upperBound << ")." << endl;
goto TERMINATE;
}
else if( !isMin && checkLower && bestObjectiveValue < lowerBound )
{
cerr << "Best objective values (" << bestObjectiveValue<< ") exceeds lower bound (" << lowerBound << ")." << endl;
goto TERMINATE;
}
}
returnStatement = true;
}
TERMINATE:
return returnStatement;
}
// Processes derived constraints
// Checks derivation types and derived constraints
// Finally confirms or rejects Solution and/or relation to prove
// Error if wrong format, derived constraints differ from given
bool processDER()
{
cout << endl << "Processing DER section..." << endl;
bool returnStatement = false;
string section;
certificateFile >> section;
if( section != "DER" )
{
cerr << "DER expected. Read instead " << section << endl;
return false;
}
certificateFile >> numberOfDerivations;
cout << "numberOfDerivations = " << numberOfDerivations << endl;
// No lower bound to check and no deriviations -> nothing to do
if( numberOfDerivations == 0 && !checkLower )
{
cout << "Successfully checked solution for feasibility" << endl;
return true;
}
// no upper bound to check and no deriviations -> nothing to do
if( numberOfDerivations == 0 && !checkUpper )
{
cout << "Successfully checked solution for feasibility" << endl;
return true;
}
string label;
int sense;
mpq_class rhs;
for( int i = 0; i < numberOfDerivations; ++i )
{
shared_ptr<SVectorGMP> coef(make_shared<SVectorGMP>());
if( !readConstraint(label, sense, rhs, coef) )
{
return false;
}
// Obtain derivation method and info
string bracket, kind;
int refIdx;
certificateFile >> bracket >> kind;
if( bracket != "{" )
{
cerr << "Expecting { but read instead " << bracket << endl;
return false;
}
DerivationType derivationType = DerivationType::UNKNOWN;
if( kind == "asm" )
derivationType = DerivationType::ASM;
else if( kind == "sol" )
derivationType = DerivationType::SOL;
else if( kind == "lin" )
derivationType = DerivationType::LIN;
else if( kind == "rnd" )
derivationType = DerivationType::RND;
else if( kind == "uns" )
derivationType = DerivationType::UNS;
// The constraint to be derived
Constraint toDer(label, sense, rhs, coef, (derivationType == DerivationType::ASM), emptyList);
#ifdef MORE_DEBUG_OUTPUT
cout << numberOfConstraints + i << " - deriving..." << label << endl;
#endif
SVectorBool assumptionList;
int newConIdx = constraint.size();
switch( derivationType )
{
// Assumption, i.e. set of assumptions only contains index of constraint
case DerivationType::ASM:
assumptionList[ newConIdx ] = true;
certificateFile >> bracket;
if( bracket != "}" )
{
cerr << "Expecting } but read instead " << bracket << endl;
return false;
}
break;
// Linear combination or rounding
case DerivationType::LIN:
case DerivationType::RND:
{
shared_ptr<SVectorGMP> coefDer(make_shared<SVectorGMP>());
mpq_class rhsDer;
int senseDer;
if( !readLinComb(senseDer, rhsDer, coefDer, newConIdx, assumptionList) )
return false;
certificateFile >> bracket;
if( bracket != "}" )
{
cerr << "Expecting } but read instead " << bracket << endl;
return false;
}
Constraint derived("", senseDer, rhsDer, coefDer, toDer.isAssumption(),
toDer.getassumptionList());
if( derivationType == DerivationType::RND ) // round the coefficients
if( !derived.round() )
return false;
// check the from reason derived constraint against the given
if( !derived.dominates(toDer) )
{
cout << "Failed to derive constraint " << label << endl;
toDer.print();
cout << "Derived instead " << endl;
derived.print();
cout << "difference: " << endl;
(derived - toDer).print();
return false;
}
}
break;
// Unsplit
case DerivationType::UNS:
{
int con1, asm1, con2, asm2;
certificateFile >> con1 >> asm1 >> con2 >> asm2;
if( certificateFile.fail() )
{
cerr << "Error reading con1 asm1 con2 asm2" << endl;
return false;
}
if( (con1 < 0) || (con1 >= newConIdx) )
{
cerr << "con1 out of bounds: " << con1 << endl;
return false;
}
if( (con2 < 0) || (con2 >= newConIdx) )
{
cerr << "con2 out of bounds: " << con2 << endl;
return false;
}
if( !canUnsplit(toDer, con1, asm1, con2, asm2, assumptionList) )
{
cerr << label << ": unsplit failed" << endl;
return false;
}
certificateFile >> bracket;
if( bracket != "}" )
{
cerr << "Expecting } but read instead " << bracket << endl;
return false;