-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbarbarik.py
1133 lines (935 loc) · 41.2 KB
/
barbarik.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Kuldeep Meel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
from __future__ import print_function
import sys
import os
import math
import random
import argparse
import copy
import tempfile
import pandas as pd
import re
SAMPLER_UNIGEN = 1
SAMPLER_QUICKSAMPLER = 2
SAMPLER_STS = 3
SAMPLER_CMS = 4
SAMPLER_UNIGEN3 = 5
SAMPLER_SPUR = 6
SAMPLER_SMARCH = 7
SAMPLER_UNIGEN2 = 8
SAMPLER_KUS = 9
SAMPLER_DISTAWARE = 10
CURR_REF_SAMPLER = 6 # default is SPUR
P_THREADS = 4
# We need a dictionary for Distribution-aware distance sampling
# which records names and not feature ids in outputted samples
features_dict = {}
def create_features_dict(inputFile):
nb_vars = 0
with open(inputFile, 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith("c") and not line.startswith("c ind"):
line = line[0:len(line) - 1]
_feature = line.split(" ", 4)
del _feature[0]
# handling non-numeric feature IDs, necessary to parse os-like models with $ in feature names...
if len(_feature) <= 2 and len(_feature) > 0: # needs to deal with literate comments, e.g., in V15 models
if (_feature[0].isdigit()):
_feature[0] = int(_feature[0])
else:
# num_filter = filter(_feature[0].isdigit(), _feature[0])
num_feature = "".join(c for c in _feature[0] if c.isdigit())
_feature[0] = int(num_feature)
# print('key ' + str(_feature[1]) + ' value ' + str(_feature[0])) -- debug
global features_dict
features_dict.update({str(_feature[1]): str(_feature[0])})
elif line.startswith('p cnf'):
_line = line.split(" ", 4)
nb_vars = int(_line[2])
print("there are : " + str(nb_vars) + " integer variables")
if (len(features_dict.keys()) == 0):
print("could not create dict from comments, faking it with integer variables in the 'p cnf' header")
for i in range(1, nb_vars + 1):
# global features_dict
features_dict.update({str(i): str(i)})
def get_sampler_string(samplerType):
if samplerType == SAMPLER_UNIGEN:
return 'UniGen'
if samplerType == SAMPLER_UNIGEN3:
return 'UniGen3'
if samplerType == SAMPLER_QUICKSAMPLER:
return 'QuickSampler'
if samplerType == SAMPLER_STS:
return 'STS'
if samplerType == SAMPLER_CMS:
return 'CustomSampler'
if samplerType == SAMPLER_SPUR:
return 'SPUR'
if samplerType == SAMPLER_SMARCH:
return 'SMARCH'
if samplerType == SAMPLER_UNIGEN2:
return 'UNIGEN2'
if samplerType == SAMPLER_KUS:
return 'KUS'
if samplerType == SAMPLER_DISTAWARE:
return 'DistanceBasedSampling'
print("ERROR: unknown sampler type")
exit(-1)
class ChainFormulaSetup:
def __init__(self, countList, newVarList, indicatorLits):
self.countList = countList
self.newVarList = newVarList
self.indicatorLits = indicatorLits
def check_cnf(fname):
with open(fname, 'r') as f:
lines = f.readlines()
given_vars = None
given_cls = None
cls = 0
max_var = 0
for line in lines:
line = line.strip()
if len(line) == 0:
print("ERROR: CNF is incorrectly formatted, empty line!")
return False
line = line.split()
line = [l.strip() for l in line]
if line[0] == "p":
assert len(line) == 4
assert line[1] == "cnf"
given_vars = int(line[2])
given_cls = int(line[3])
continue
if line[0] == "c":
continue
cls += 1
for l in line:
var = abs(int(l))
max_var = max(var, max_var)
if max_var > given_vars:
print("ERROR: Number of variables given is LESS than the number of variables ued")
print("ERROR: Vars in header: %d max var: %d" % (given_vars, max_var))
return False
if cls != given_cls:
print("ERROR: Number of clauses in header is DIFFERENT than the number of clauses in the CNF")
print("ERROR: Claues in header: %d clauses: %d" % (given_cls, cls))
return False
return True
class SolutionRetriver:
@staticmethod
def getSolutionFromSampler(inputFile, numSolutions, samplerType, indVarList, newSeed):
topass_withseed = (inputFile, numSolutions, indVarList, newSeed)
ok = check_cnf(inputFile)
if not ok:
print("ERROR: CNF is malformatted. Sampler may give wrong solutions in this case. Exiting.")
print("File is: %s" % inputFile)
exit(-1)
print("Using sampler: %s" % get_sampler_string(samplerType))
if (samplerType == SAMPLER_UNIGEN):
sols = SolutionRetriver.getSolutionFromUniGen(*topass_withseed)
elif (samplerType == SAMPLER_UNIGEN3):
sols = SolutionRetriver.getSolutionFromUniGen3(*topass_withseed)
elif (samplerType == SAMPLER_QUICKSAMPLER):
sols = SolutionRetriver.getSolutionFromQuickSampler(*topass_withseed)
elif (samplerType == SAMPLER_STS):
sols = SolutionRetriver.getSolutionFromSTS(*topass_withseed)
elif (samplerType == SAMPLER_CMS):
sols = SolutionRetriver.getSolutionFromCMSsampler(*topass_withseed)
elif (samplerType == SAMPLER_SPUR):
sols = SolutionRetriver.getSolutionFromSpur(*topass_withseed)
elif (samplerType == SAMPLER_SMARCH):
sols = SolutionRetriver.getSolutionFromSMARCH(*topass_withseed)
elif (samplerType == SAMPLER_UNIGEN2):
sols = SolutionRetriver.getSolutionFromUniGen2(*topass_withseed)
elif (samplerType == SAMPLER_KUS):
sols = SolutionRetriver.getSolutionFromKUS(*topass_withseed)
elif (samplerType == SAMPLER_DISTAWARE):
sols = SolutionRetriver.getSolutionFromDistAware(*topass_withseed)
else:
print("Error: No such sampler!")
exit(-1)
# clean up the solutions
for i in range(len(sols)):
sols[i] = sols[i].strip()
if sols[i].endswith(' 0'):
sols[i] = sols[i][:-2]
print("Number of solutions returned by sampler:", len(sols))
# if args.verbose:
# print("Solutions:", sols)
return sols
@staticmethod
def getSolutionFromUniGen(inputFile, numSolutions, indVarList, newSeed):
# must construct ./unigen --samples=500 --verbosity=0 --threads=1 CNF-FILE SAMPLESFILE
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".txt"
cmd = './samplers/unigen --samples=' + str(numSolutions)
cmd += ' ' + inputFile + ' ' + str(tempOutputFile) + ' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
line = line.strip()
if line.startswith('v'):
freq = int(line.split(':')[-1])
for i in range(freq):
solList.append(line.split(':')[0].replace('v', '').strip())
if (len(solList) == numSolutions):
break
if (len(solList) == numSolutions):
break
solreturnList = solList
if (len(solList) > numSolutions):
solreturnList = random.sample(solList, numSolutions)
os.unlink(str(tempOutputFile))
return solreturnList
@staticmethod
def getSolutionFromUniGen2(inputFile, numSolutions, indVarList, newSeed):
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".txt"
cwd = os.getcwd()
cmd = 'python3 UniGen2.py -samples=' + str(numSolutions)
cmd += ' ' + str(os.path.abspath(inputFile)) + ' ' + str(tempfile.gettempdir()) + ' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.chdir(str(os.getcwd()) + '/samplers')
os.system(cmd)
os.chdir(str(cwd))
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
line = line.strip()
if line.startswith('v'):
freq = int(line.split(':')[-1])
for i in range(freq):
solList.append(line.split(':')[0].replace('v', '').strip())
if (len(solList) == numSolutions):
break
if (len(solList) == numSolutions):
break
solreturnList = solList
if (len(solList) > numSolutions):
solreturnList = random.sample(solList, numSolutions)
os.unlink(str(tempOutputFile))
return solreturnList
@staticmethod
def getSolutionFromKUS(inputFile, numSolutions, indVarList, newSeed):
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".txt"
cwd = os.getcwd()
cmd = 'python3 KUS.py --samples=' + str(numSolutions) + ' ' + '--outputfile ' + tempOutputFile
cmd += ' ' + str(os.path.abspath(inputFile)) + ' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.chdir(str(os.getcwd()) + '/samplers')
os.system(cmd)
os.chdir(str(cwd))
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
sol = re.sub('[0-9]*,', '', line)
solList.append(sol)
os.unlink(str(tempOutputFile))
return solList
@staticmethod
def getSolutionFromUniGen3(inputFile, numSolutions, indVarList, newSeed):
# must construct: ./approxmc3 -s 1 -v2 --sampleout /dev/null --samples 500
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".txt"
cmd = './samplers/approxmc3 -s ' + str(int(newSeed)) + ' -v 0 --samples ' + str(numSolutions)
cmd += ' --sampleout ' + str(tempOutputFile)
cmd += ' ' + inputFile + ' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
line = line.strip()
freq = int(line.split(':')[0])
for i in range(freq):
solList.append(line.split(':')[1].strip())
if len(solList) == numSolutions:
break
if len(solList) == numSolutions:
break
solreturnList = solList
if len(solList) > numSolutions:
solreturnList = random.sample(solList, numSolutions)
os.unlink(str(tempOutputFile))
return solreturnList
@staticmethod
def getSolutionFromDistAware(inputFile, numSolutions, indVarList, newSeed):
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".txt"
# creating the file to configure the sampler
dbsConfigFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".a"
with open(dbsConfigFile, 'w+') as f:
f.write("log " + tempfile.gettempdir() + '/' + "output.txt" + "\n")
f.write("dimacs " + str(os.path.abspath(inputFile)) + "\n")
params = " solver z3" + "\n"
params += "hybrid distribution-aware distance-metric:manhattan distribution:uniform onlyBinary:true onlyNumeric:false"
params += " selection:SolverSelection number-weight-optimization:1"
params += " numConfigs:" + str(numSolutions)
f.write(params + "\n")
f.write("printconfigs " + tempOutputFile)
cmd = "mono ./samplers/distribution-aware/CommandLine.exe "
cmd += dbsConfigFile
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
features = re.findall("%\w+%", line)
sol = []
for feature in features:
feat = feature[1:-1]
sol.append(feat)
solution = ''
for k, v in features_dict.items():
if k in sol:
solution += ' ' + str(v)
else:
solution += ' -' + str(v)
solList.append(solution)
# cleaning temporary files
os.unlink(str(tempOutputFile))
os.unlink(dbsConfigFile)
os.unlink(str(tempfile.gettempdir()) + '/' + "output.txt")
os.unlink(str(tempfile.gettempdir()) + '/' + "output.txt_error")
return solList
@staticmethod
def getSolutionFromQuickSampler(inputFile, numSolutions, indVarList, newSeed):
cmd = "./samplers/quicksampler -n " + str(numSolutions * 5) + ' ' + str(inputFile) + ' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
cmd = "./samplers/z3-quicksampler/z3 sat.quicksampler_check=true sat.quicksampler_check.timeout=3600.0 " + str(
inputFile) + ' > /dev/null 2>&1'
# os.system(cmd)
# cmd = "./samplers/z3 "+str(inputFile)#+' > /dev/null 2>&1'
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
if (numSolutions > 1):
i = 0
with open(inputFile + '.samples', 'r') as f:
lines = f.readlines()
with open(inputFile + '.samples.valid', 'r') as f:
validLines = f.readlines()
solList = []
for j in range(len(lines)):
if (validLines[j].strip() == '0'):
continue
fields = lines[j].strip().split(':')
sol = ''
i = 0
# valutions are 0 and 1 and in the same order as c ind.
for x in list(fields[1].strip()):
if (x == '0'):
sol += ' -' + str(indVarList[i])
else:
sol += ' ' + str(indVarList[i])
i += 1
solList.append(sol)
if (len(solList) == numSolutions):
break
os.unlink(inputFile + '.samples')
os.unlink(inputFile + '.samples.valid')
if len(solList) != numSolutions:
print("Did not find required number of solutions")
sys.exit(1)
return solList
@staticmethod
def getSolutionFromSpur(inputFile, numSolutions, indVarList, newSeed):
inputFileSuffix = inputFile.split('/')[-1][:-4]
tempOutputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".out"
cmd = './samplers/spur -seed %d -q -s %d -out %s -cnf %s' % (
newSeed, numSolutions, tempOutputFile, inputFile)
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(tempOutputFile, 'r') as f:
lines = f.readlines()
solList = []
startParse = False
for line in lines:
if (line.startswith('#START_SAMPLES')):
startParse = True
continue
if (not (startParse)):
continue
if (line.startswith('#END_SAMPLES')):
startParse = False
continue
fields = line.strip().split(',')
solCount = int(fields[0])
sol = ' '
i = 1
for x in list(fields[1]):
if (x == '0'):
sol += ' -' + str(i)
else:
sol += ' ' + str(i)
i += 1
for i in range(solCount):
solList.append(sol)
os.unlink(tempOutputFile)
return solList
@staticmethod
def getSolutionFromSTS(inputFile, numSolutions, indVarList, newSeed):
kValue = 50
samplingRounds = numSolutions / kValue + 1
inputFileSuffix = inputFile.split('/')[-1][:-4]
outputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".out"
cmd = './samplers/STS -k=' + str(kValue) + ' -nsamples=' + str(samplingRounds) + ' ' + str(inputFile)
cmd += ' > ' + str(outputFile)
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(outputFile, 'r') as f:
lines = f.readlines()
solList = []
shouldStart = False
# baseList = {}
for j in range(len(lines)):
if (lines[j].strip() == 'Outputting samples:' or lines[j].strip() == 'start'):
shouldStart = True
continue
if (lines[j].strip().startswith('Log') or lines[j].strip() == 'end'):
shouldStart = False
if (shouldStart):
'''if lines[j].strip() not in baseList:
baseList[lines[j].strip()] = 1
else:
baseList[lines[j].strip()] += 1'''
sol = ''
i = 0
# valutions are 0 and 1 and in the same order as c ind.
for x in list(lines[j].strip()):
if (x == '0'):
sol += ' -' + str(indVarList[i])
else:
sol += ' ' + str(indVarList[i])
i += 1
solList.append(sol)
if len(solList) == numSolutions:
break
if len(solList) != numSolutions:
print(len(solList))
print("STS Did not find required number of solutions")
sys.exit(1)
os.unlink(outputFile)
return solList
@staticmethod
def getSolutionFromCMSsampler(inputFile, numSolutions, indVarList, newSeed):
inputFileSuffix = inputFile.split('/')[-1][:-4]
outputFile = tempfile.gettempdir() + '/' + inputFileSuffix + ".out"
cmd = "./samplers/cryptominisat5 --restart luby --maple 0 --verb 10 --nobansol"
cmd += " --scc 1 -n1 --presimp 0 --polar rnd --freq 0.9999"
cmd += " --random " + str(int(newSeed)) + " --maxsol " + str(numSolutions)
cmd += " " + inputFile
cmd += " --dumpresult " + outputFile + " > /dev/null 2>&1"
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
with open(outputFile, 'r') as f:
lines = f.readlines()
solList = []
for line in lines:
if line.strip() == 'SAT':
continue
sol = ""
lits = line.split(" ")
for y in indVarList:
if str(y) in lits:
sol += ' ' + str(y)
if "-" + str(y) in lits:
sol += ' -' + str(y)
solList.append(sol)
solreturnList = solList
if len(solList) > numSolutions:
solreturnList = random.sample(solList, numSolutions)
if len(solList) < numSolutions:
print("cryptominisat5 Did not find required number of solutions")
sys.exit(1)
os.unlink(outputFile)
return solreturnList
@staticmethod
def getSolutionFromSMARCH(inputFile, numSolutions, indVarList, newSeed):
cmd = "python3 ./samplers/smarch_mp.py -p " + str(P_THREADS) + " -o " + os.path.dirname(
inputFile) + " " + inputFile + " " + str(numSolutions) + " > /dev/null 2>&1"
# cmd = "python3 /home/gilles/ICST2019-EMSE-Ext/Kclause_Smarch-local/Smarch/smarch.py " + " -o " + os.path.dirname(inputFile) + " " + inputFile + " " + str(numSolutions)
if args.verbose:
print("cmd: ", cmd)
os.system(cmd)
# if (numSolutions > 1):
# i = 0
solList = []
tempFile = inputFile.replace('.cnf', '_' + str(numSolutions)) + '.samples'
if args.verbose:
print(tempFile)
df = pd.read_csv(tempFile, header=None)
# with open(inputFile+'.samples', 'r') as f:
# lines = f.readlines()
for x in df.values:
# tmpLst = []
lst = x.tolist()
sol = ''
for i in lst:
if not math.isnan(i):
sol += ' ' + str(int(i))
# tmpList = [str(int(i)) for i in lst if not math.isnan(i)]
# if args.verbose:
# print(sol)
# solList.append(tmpList)
# solList = [x for x in df.values]
os.unlink(tempFile)
solList.append(sol)
return solList
# returns List of Independent Variables
def parseIndSupport(indSupportFile):
with open(indSupportFile, 'r') as f:
lines = f.readlines()
indList = []
numVars = 0
for line in lines:
if line.startswith('p cnf'):
fields = line.split()
numVars = int(fields[2])
if line.startswith('c ind'):
line = line.strip().replace('c ind', '').replace(' 0', '').strip().replace('v ', '')
indList.extend(line.split())
if len(indList) == 0:
indList = [int(x) for x in range(1, numVars + 1)]
else:
indList = [int(x) for x in indList]
return indList
def chainFormulaSetup(sampleSol, unifSol, numSolutions):
# number of solutions for each: k1, k2, k3
# TODO rename to chainSolutions
countList = [5, 5, 5]
# chain formula number of variables for each
# TODO rename to chainVars
newVarList = [4, 4, 4]
##########
# clean up the solutions
##########
sampleSol = sampleSol[0].strip()
if sampleSol.endswith(' 0'):
sampleSol = sampleSol[:-2]
unifSol = unifSol[0].strip()
if unifSol.endswith(' 0'):
unifSol = unifSol[:-2]
# adding more chain formulas (at most 8 in total: 3 + 5)
# these chain formulas will have 31 solutions over 6 variables
lenSol = len(sampleSol.split())
for i in range(min(int(math.log(numSolutions, 2)) + 4, lenSol - 3, 5)):
countList.append(31)
newVarList.append(6)
assert len(countList) == len(newVarList)
# picking selector literals, i.e. k1, k2, k3, randomly
if args.verbose:
print("len count list: " + str(len(countList)))
print("#num of samples" + str(len(sampleSol.split())))
assert len(sampleSol.split()) > len(countList), "There are not enough samples to proceed, sampler failed ?"
sampleLitList = random.sample(sampleSol.split(), len(countList))
unifLitList = []
unifSolMap = unifSol.split()
# from the last version of barbarik...
if CURR_REF_SAMPLER == SAMPLER_SPUR:
for lit in sampleLitList:
unifLitList.append(unifSolMap[abs(int(lit)) - 1])
else:
# since the reference is not always spur, some adapations are required here
for lit in sampleLitList:
if lit in unifSolMap:
unifLitList.append(lit)
elif int(lit) > 0 and str('-' + lit) in unifSolMap:
unifLitList.append(str('-' + lit))
elif int(lit) < 0 and str(abs(int(lit))) in unifSolMap:
unifLitList.append(str(abs(int(lit))))
else:
print("ERROR in Sampling ! ")
# print("appending: " + unifSolMap[abs(int(lit))-1]+ " for "+ lit)
# unifLitList.append(unifSolMap[abs(int(lit))-1])
assert len(unifLitList) == len(sampleLitList)
# print(unifLitList)
# print(sampleLitList)
for a, b in zip(unifLitList, sampleLitList):
assert abs(int(a)) == abs(int(b))
indicatorLits = []
indicatorLits.append(sampleLitList)
indicatorLits.append(unifLitList)
# print("countList:", countList)
# print("newVarList:", newVarList)
# print("indicatorLits:", indicatorLits)
return ChainFormulaSetup(countList, newVarList, indicatorLits)
def pushVar(variable, cnfClauses):
cnfLen = len(cnfClauses)
for i in range(cnfLen):
cnfClauses[i].append(variable)
return cnfClauses
def getCNF(variable, binStr, sign, origTotalVars):
cnfClauses = []
binLen = len(binStr)
if sign is False:
cnfClauses.append([-(binLen + 1 + origTotalVars)])
else:
cnfClauses.append([binLen + 1 + origTotalVars])
for i in range(binLen):
newVar = int(binLen - i + origTotalVars)
if sign is False:
newVar = -1 * (binLen - i + origTotalVars)
if (binStr[binLen - i - 1] == '0'):
cnfClauses.append([newVar])
else:
cnfClauses = pushVar(newVar, cnfClauses)
pushVar(variable, cnfClauses)
return cnfClauses
def constructChainFormula(originalVar, solCount, newVar, origTotalVars, invert):
assert type(solCount) == int
binStr = str(bin(int(solCount)))[2:-1]
binLen = len(binStr)
for _ in range(newVar - binLen - 1):
binStr = '0' + binStr
firstCNFClauses = getCNF(-int(originalVar), binStr, invert, origTotalVars)
addedClauseNum = 0
writeLines = ''
for cl in firstCNFClauses:
addedClauseNum += 1
for lit in cl:
writeLines += "%d " % lit
writeLines += '0\n'
return writeLines, addedClauseNum
# returns whether new file was created and the list of TMP+OLD independent variables
def constructNewCNF(inputFile, tempFile, sampleSol, unifSol, chainFormulaConf, indVarList):
# which variables are in pos/neg value in the sample
sampleVal = {}
for i in sampleSol.strip().split():
i = int(i)
if i != 0:
if abs(i) not in indVarList:
continue
sampleVal[abs(i)] = int(i / abs(i))
# which variables are in pos/neg value in the uniform sample
unifVal = {}
diffIndex = -1
for j in unifSol.strip().split():
j = int(j)
if j != 0:
if abs(j) not in indVarList:
continue
unifVal[abs(j)] = int(j / abs(j))
if sampleVal[abs(j)] != unifVal[abs(j)]:
diffIndex = abs(j)
# the two solutions are the same
# can't do anything, let's do another experiment
if diffIndex == -1:
return False, None, None
with open(inputFile, 'r') as f:
lines = f.readlines()
# variables must be shifted by sumNewVar
sumNewVar = sum(chainFormulaConf.newVarList)
# emit the original CNF, but with shifted variables
shiftedCNFStr = ''
for line in lines:
line = line.strip()
if line.startswith('p cnf'):
numVar = int(line.split()[2])
numCls = int(line.split()[3])
continue
if line.startswith('c'):
# comment
continue
for x in line.split():
x = int(x)
if x == 0:
continue
sign = int(x / abs(x))
shiftedCNFStr += "%d " % (sign * (abs(x) + sumNewVar))
shiftedCNFStr += ' 0\n'
del i
# Fixing the solution based on splittingVar
# X = sigma1 OR X = singma2
# All variables are set except for the index where they last differ
solClause = ''
splittingVar = diffIndex + sumNewVar
for var in indVarList:
if var != diffIndex:
numCls += 2
solClause += "%d " % (-splittingVar * sampleVal[diffIndex])
solClause += "%d 0\n" % (sampleVal[var] * (var + sumNewVar))
solClause += "%d " % (-splittingVar * unifVal[diffIndex])
solClause += "%d 0\n" % (unifVal[var] * (var + sumNewVar))
##########
# We add the N number of chain formulas
# where chainFormulaConf.indicatorLits must be of size 2
# and len(chainFormulaConf.indicatorLits) == len(chainFormulaConf.newVarList)
# Adding K soluitons over Z variables, where
# Z = chainFormulaConf.newVarList[k]
# K = chainFormulaConf.countList[k]
##########
invert = True
seenLits = {}
for indicLits in chainFormulaConf.indicatorLits: # loop runs twice
currentNumVar = 0
for i in range(len(indicLits)):
newvar = chainFormulaConf.newVarList[i]
indicLit = int(indicLits[i])
addedClause = ''
addedClauseNum = 0
# not adding the chain formula twice to the same literal
if indicLit not in seenLits:
sign = int(indicLit / abs(indicLit))
addedClause, addedClauseNum = constructChainFormula(
sign * (abs(indicLit) + sumNewVar),
chainFormulaConf.countList[i], newvar, currentNumVar,
invert)
seenLits[indicLit] = True
currentNumVar += newvar
numCls += addedClauseNum
solClause += addedClause
invert = not invert
del seenLits
del invert
# create "c ind ..." lines
oldIndVarList = [x + sumNewVar for x in indVarList]
tempIndVarList = copy.copy(oldIndVarList)
indIter = 1
indStr = 'c ind '
for i in range(1, currentNumVar + 1):
if indIter % 10 == 0:
indStr += ' 0\nc ind '
indStr += "%d " % i
indIter += 1
tempIndVarList.append(i)
for i in oldIndVarList:
if indIter % 10 == 0:
indStr += ' 0\nc ind '
indStr += "%d " % i
indIter += 1
indStr += ' 0\n'
# dump new CNF
with open(tempFile, 'w') as f:
f.write('p cnf %d %d\n' % (currentNumVar + numVar, numCls))
f.write(indStr)
f.write(solClause)
# f.write("c -- old CNF below -- \n")
f.write(shiftedCNFStr)
# print("New file: ", tempFile)
# exit(0)
return True, tempIndVarList, oldIndVarList
class Experiment:
def __init__(self, inputFile, maxSamples, minSamples, samplerType, refSamplerType):
inputFileSuffix = inputFile.split('/')[-1][:-4]
self.tempFile = tempfile.gettempdir() + "/" + inputFileSuffix + "_t.cnf"
self.indVarList = parseIndSupport(inputFile)
self.inputFile = inputFile
self.samplerType = samplerType
self.maxSamples = maxSamples
self.minSamples = minSamples
self.samplerString = get_sampler_string(samplerType)
self.ref_sampler_type = refSamplerType
# Returns True if uniform and False otherwise
def testUniformity(self, solList, indVarList):
solMap = {}
baseMap = {}
for sol in solList:
solution = ''
solFields = sol.split()
for entry in solFields:
if abs(int(entry)) in indVarList:
solution += entry + ' '
if solution in solMap.keys():
solMap[solution] += 1
else:
solMap[solution] = 1
if sol not in baseMap.keys():
baseMap[sol] = 1
else:
baseMap[sol] += 1
if not bool(solMap):
print("No Solutions were given to the test")
exit(1)
key = next(iter(solMap))
print("baseMap: {:<6} numSolutions: {:<6} SolutionsCount: {:<6} loThresh: {:<6} hiThresh: {:<6}".format(
len(baseMap.keys()), self.numSolutions, solMap[key], self.loThresh, self.hiThresh))
if solMap[key] >= self.loThresh and solMap[key] <= self.hiThresh:
return True
else:
return False
def one_experiment(self, experiment, j, i, numExperiments, tj):
self.thresholdSolutions += self.numSolutions
if self.thresholdSolutions < self.minSamples:
return None, None
# generate a new seed value for every different (i,j,experiment)
newSeed = numExperiments * (i * tj + j) + experiment
# get sampler's solutions
sampleSol = SolutionRetriver.getSolutionFromSampler(
self.inputFile, 1, self.samplerType, self.indVarList, newSeed)
self.totalSolutionsGenerated += 1
# get uniform sampler's solutions
# get uniform sampler's solutions
# unifSol = SolutionRetriver.getSolutionFromSampler(
# self.inputFile, 1, SAMPLER_SPUR, self.indVarList, newSeed)
# self.totalUniformSamples += 1
# The reference sampler is a now a parameter of barbarik
unifSol = SolutionRetriver.getSolutionFromSampler(
self.inputFile, 1, self.ref_sampler_type, self.indVarList, newSeed)
self.totalUniformSamples += 1
chainFormulaConf = chainFormulaSetup(sampleSol, unifSol, self.numSolutions)
shakuniMix, tempIndVarList, oldIndVarList = constructNewCNF(
self.inputFile, self.tempFile, sampleSol[0], unifSol[0],
chainFormulaConf, self.indVarList)
# the two solutions were the same, couldn't construct CNF
if not shakuniMix:
return False, None
# seed update
newSeed = newSeed + 1
# get sampler's solutions
solList = SolutionRetriver.getSolutionFromSampler(
self.tempFile, self.numSolutions, self.samplerType, tempIndVarList, newSeed)
os.unlink(self.tempFile)
self.totalSolutionsGenerated += self.numSolutions
isUniform = self.testUniformity(solList, oldIndVarList)
print("sampler: {:<8s} i: {:<4d} isUniform: {:<4d} TotalSolutionsGenerated: {:<6d}".format(
self.samplerString, i, isUniform,
self.totalSolutionsGenerated))