-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeProbe.py
executable file
·586 lines (485 loc) · 17.9 KB
/
CubeProbe.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
from __future__ import print_function
import sys
import os
import math
import random
import argparse
import tempfile
import copy
import time
import threading
from numpy.random import exponential as _exp
SAMPLER_QUICKSAMPLER = 1
SAMPLER_STS = 2
SAMPLER_CMS = 3
def parseWeights(inputFile, indVarList):
f = open(inputFile, "r")
lines = f.readlines()
f.close()
weight_map = {}
for line in lines:
if line.startswith("w"):
variable, weight = line[2:].strip().split()
variable = int(variable)
weight = float(weight)
if (0.0 < weight < 1.0):
if (variable in indVarList):
weight_map[variable] = weight
else:
print("Error: Weights should only be in (0,1) ")
exit(-1)
return weight_map
def parseIndSupport(indSupportFile): # returns List of Independent Variables
f = open(indSupportFile, "r")
lines = f.readlines()
f.close()
indList = []
numVars = 0
for line in lines:
if line.startswith("p cnf"):
fields = line.split()
numVars = int(fields[2])
if line.startswith("c ind"):
indList.extend(
line.strip()
.replace("c ind", "")
.replace(" 0", "")
.strip()
.replace("v ", "")
.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 getSolutionFromCMSsampler(inputFile, numSolutions, indVarList, newSeed):
tempOutputFile = inputFile + ".txt"
cmd = "./samplers/cmsgen --samples " + str(numSolutions) + " -s " + str(newSeed)
cmd += " --samplefile " + tempOutputFile + " " + inputFile + " > /dev/null 2>&1"
os.system(cmd)
with open(tempOutputFile, '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.append(y)
if "-" + str(y) in lits:
sol.append(-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(tempOutputFile)
return solreturnList
def getSolutionFromSampler(inputFile, numSolutions, samplerType, indVarList, seed, thread, outfp):
if samplerType == SAMPLER_QUICKSAMPLER:
return getSolutionFromQuickSampler(inputFile, numSolutions, indVarList, seed, thread, outfp)
if samplerType == SAMPLER_STS:
return getSolutionFromSTS(seed, inputFile, numSolutions, indVarList)
if samplerType == SAMPLER_CMS:
return getSolutionFromCMSsampler(inputFile, numSolutions, indVarList, seed)
else:
print("Error")
return None
def getSolutionFromSTS(seed, inputFile, numSolutions, indVarList):
kValue = 50
samplingRounds = int(numSolutions/kValue) + 1
inputFileSuffix = inputFile.split('/')[-1][:-4]
outputFile = tempfile.gettempdir()+'/'+inputFileSuffix+"sts.out"
solList = []
while True:
cmd = './samplers/STSnew -k='+str(kValue)+' -nsamples='+str(samplingRounds)+' -rnd-seed=' + str(seed) +' '+str(inputFile)
cmd += ' > '+str(outputFile)
os.system(cmd)
with open(outputFile, 'r') as f:
lines = f.readlines()
shouldStart = False
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):
i = 0
sol = []
line = lines[j].strip().split(',')
for x in indVarList:
if (line[x-1] == '0'):
sol.append(-1*x)
else:
sol.append(x)
i += 1
solList.append(sol)
solreturnList = solList
if len(solList) > numSolutions:
solreturnList = random.sample(solList, numSolutions)
break
os.unlink(outputFile)
return solreturnList
def getSolutionFromQuickSampler(inputFile, numSolutions, indVarList, seed, thread, outfp):
solreturnList = []
while True:
cmd = (
"./samplers/quicksampler" +" -n "
+ str(numSolutions * 5)
+ " "
+ str(inputFile)
+ " > /dev/null 2>&1"
)
os.system(cmd)
cmd = "./samplers/z3"+" " + str(inputFile) + " > /dev/null 2>&1"
os.system(cmd)
i = 0
if numSolutions > 1:
i = 0
f = open(inputFile + ".samples", "r")
lines = f.readlines()
f.close()
f = open(inputFile + ".samples.valid", "r")
validLines = f.readlines()
f.close()
solList = []
for j in range(len(lines)):
if validLines[j].strip() == "0":
continue
fields = lines[j].strip().split(":")
sol = []
i = 0
for x in list(fields[1].strip()):
if x == "0":
sol.append(-1*indVarList[i])
else:
sol.append(indVarList[i])
i += 1
solList.append(sol)
solreturnList += solList
if len(solreturnList) > numSolutions:
solreturnList = random.sample(solreturnList, numSolutions)
break
os.unlink(inputFile+'.samples')
os.unlink(inputFile+'.samples.valid')
return solreturnList
def constructNewFile(tempFile, var, indVarList):
f = open(tempFile, "r")
lines = f.readlines()
f.close()
#---------------------------Old formula--------------------------#
oldClauseStr = ""
for line in lines:
if line.strip().startswith("p cnf"):
numVar = int(line.strip().split()[2])
numClause = int(line.strip().split()[3]) + 1
else:
if line.strip().startswith("w"):
oldClauseStr += line.strip()+"\n"
elif not (line.strip().startswith("c")):
oldClauseStr += line.strip()+"\n"
#----------------Adding constraints-------------------#
solClause = ""
solClause += str(var) + ' 0\n'
#----------------------construct formula-----------------------------#
indStr = "c ind "
indIter = 1
for i in indVarList:
if indIter % 10 == 0:
indStr += " 0\nc ind "
indStr += str(i) + " "
indIter += 1
indStr += " 0\n"
headStr = "p cnf " + str(numVar) + " " + str(numClause) + "\n"
writeStr = headStr + indStr
writeStr += solClause
writeStr += oldClauseStr
f = open(tempFile, "w")
f.write(writeStr)
f.close()
return indVarList
def addClique(inputFile, indVarList, newFile):
f = open(inputFile, "r")
lines = f.readlines()
f.close()
#---------------------Old Clause---------------------#
oldClauseStr = ""
for line in lines:
if line.strip().startswith("p cnf"):
numVar = int(line.strip().split()[2])
numClause = int(line.strip().split()[3])
else:
if line.strip().startswith("w"):
oldClauseStr += line.strip()+"\n"
elif not (line.strip().startswith("c")):
oldClauseStr += line.strip()+"\n"
indStr = "c ind "
indIter = 1
for i in indVarList:
if indIter % 10 == 0:
indStr += " 0\nc ind "
indStr += str(i) + " "
indIter += 1
indStr += " 0\n"
numVarOrig = numVar
newClause = ""
varCount = 1
newVar = []
fmt = open("empty.cnf", "r")
mtlines = fmt.readlines()
fmt.close()
for line in mtlines:
if line.strip().startswith("p cnf"):
newnumVar = int(line.strip().split()[2])
newnumClause = int(line.strip().split()[3])
else:
if line.strip().startswith("w"):
newClause += line.strip()+"\n"
elif not (line.strip().startswith("c")):
newVarbuf = [int(_var) for _var in line.strip().split()]
for _var in newVarbuf:
if _var > 0:
newClause += str(abs(_var) + numVar) + ' '
elif _var < 0:
newClause += str( -1 * (abs(_var) + numVar)) + ' '
newVar.append(abs(_var) + numVar)
newClause += '0\n'
#------------------------------------------------------------------------------#
newVar = set(newVar)
newVar.remove(numVar)
newVar = sorted(list(newVar))
indStr += "c ind "
indIter = 1
for i in newVar:
if indIter % 10 == 0:
indStr += " 0\nc ind "
indStr += str(i) + " "
indIter += 1
indStr += " 0\n"
headStr = "p cnf " + str(numVar + newnumVar) + " " + str(numClause + newnumClause) + "\n"
writeStr = headStr + indStr
writeStr += oldClauseStr
writeStr += newClause
f = open(newFile, "w")
f.write(writeStr)
f.close()
return indVarList + newVar
def gbas(x, ith, indVarList, tempfile, samplerType, seed, k, outfp, thread):
s, r = 0, 0
k_ = int(k)
k = k_
cut_thresh = 40
nloops = 0
totsamples = 0
while s < k:
nloops += 1
sampSet = getSolutionFromSampler(tempfile, k_, samplerType, indVarList, seed, thread, outfp)
totsamples += k_
for samp in sampSet:
assert abs(samp[ith]) == abs(x[ith])
if not (samp[ith] > 0) ^ (x[ith] > 0):
s += 1
r += _exp(1)
seed += 1
k_ = k - s
if nloops > cut_thresh:
outfp.write("\texiting with current heads : " + str(s) + " out of " + str(k)+ " | exp rv: " + str(r) + "\n")
outfp.flush()
break
return (k - 1) / r, totsamples
def estimate(x, dim, indVarList, tempFile, samplerType, seed, k, outfp, threadid):
n = dim
est = 1
totsamples = 0
start_time = time.time()
out, nsamples = gbas(x, 0, indVarList, tempFile, samplerType, seed, k, outfp, threadid); out = min(1, out)
est *= out
totsamples += nsamples
end_time = time.time()
outfp.write("\n Thread " + str(threadid) + " estimated dimension " + str(0) + " of " + str(n) + " : " + str(est) + " | time taken : " + str(end_time - start_time))
outfp.flush()
indVarList = constructNewFile(tempFile, x[0], indVarList)
for i in range(1, n):
start_time = time.time()
out, nsamples= gbas(x, i, indVarList, tempFile, samplerType, seed, k, outfp, threadid); out = min(1, out)
est *= out
totsamples += nsamples
end_time = time.time()
outfp.write("\n Thread " + str(threadid) + " estimated dimension " + str(i) + " of " + str(n) + " : " + str(out) + " | time taken : " + str(end_time - start_time))
outfp.flush()
indVarList = constructNewFile(tempFile, x[i], indVarList)
outfp.write("\n Thread " + str(threadid) + " nsolns: " + str(totsamples))
outfp.flush()
return est, totsamples
def inthread(sampleSet, dim, indVarList, inputFile, samplerType, seed, k, out, est, nsamp, threadid):
print("starting Thread-", threadid)
tempFile_th= "thread_" + str(threadid) + "_" + inputFile
cmd = "cp " + inputFile + " ./" + tempFile_th
for x in sampleSet:
os.system(cmd)
estimates, nsamples = estimate(x, dim, indVarList, tempFile_th, samplerType, seed, k, out, threadid)
est.append(estimates)
nsamp.append(nsamples)
try:
os.unlink(tempFile_th)
except FileNotFoundError:
pass
def CubeProbe():
start_time = time.time()
parser = argparse.ArgumentParser()
parser.add_argument(
"--zeta", type=float, help="default = 0.3", default=0.3, dest="zeta"
)
parser.add_argument(
"--eta", type=float, help="default = 0.605", default=0.605, dest="eta"
)
parser.add_argument(
"--epsilon", type=float, help="default = 0.005", default=0.005, dest="epsilon"
)
parser.add_argument(
"--delta", type=float, help="default = 0.2", default=0.2, dest="delta"
)
parser.add_argument(
"--sampler",
type=int,
help=str(SAMPLER_QUICKSAMPLER)
+ " for QuickSampler;\n"
+ str(SAMPLER_STS)
+ " for STS;\n"
+ str(SAMPLER_CMS)
+ " for CMSGen;\n",
default=SAMPLER_QUICKSAMPLER,
dest="samplertype",
)
parser.add_argument("--seed", type=int, dest="seed", default=420)
parser.add_argument("--thread", type=int, help="default use thread", default=20, dest="thread")
parser.add_argument("--mode", help="select 'test' for tester and 'est' for estimator", default='est', dest="mode")
parser.add_argument("input", help="input file")
parser.add_argument("output", help="output file")
args = parser.parse_args()
if args.mode == 'test':
print("Running in tester mode ...")
elif args.mode == 'est':
print("Running in estimator mode ...")
samplerType = args.samplertype
UserInputFile = args.input
UserIndVarList = parseIndSupport(UserInputFile)
# Current Working File
inputFilePrefix = "sampler_" + str(samplerType) + "_" + UserInputFile.split("/")[-1][:-4]
inputFile = inputFilePrefix + ".cnf"
indVarList = addClique(UserInputFile, UserIndVarList, inputFile)
# get the model count
mcFile = inputFilePrefix + ".mc"
cmd = "./samplers/approxmc " + UserInputFile + " > " + mcFile
os.system(cmd)
with open(mcFile) as fp:
lines = fp.readlines()
mc = 0
for line in lines:
if line.strip().startswith('s mc') :
mc = line.strip().split(' ')[-1]
mc = int(mc)
os.unlink(mcFile)
# set up the parameters
isthread = args.thread
zeta = args.zeta
eta = args.eta
epsilon = args.epsilon
delta = args.delta
outputFile = args.output
seed = args.seed
random.seed(seed)
if args.mode == 'test':
zeta = (eta - epsilon) / 2
tempFile = inputFile.split(".")[0] + "_t.cnf"
samplerString = ""
if samplerType == SAMPLER_QUICKSAMPLER:
samplerString = "QuickSampler"
if samplerType == SAMPLER_STS:
samplerString = "STS"
if samplerType == SAMPLER_CMS:
samplerString = "CMSGen"
#----------------------------------------- parameter calculation----------------------------------------------
n = len(UserIndVarList)
numSolutions = math.ceil(2 / (zeta ** 2) * math.log(4 / delta))
delta_m = delta / (2 * numSolutions)
K = (epsilon + eta) / 2
gamma = zeta / (1 + zeta)
epsilon_ = gamma / 1.107
k = math.ceil(2 * n / epsilon_**2 * (1 /( 1 - 4 / 3 * epsilon_ / math.sqrt(n))) * math.log(2 * n / delta_m))
# print(k, numSolutions, n, epsilon_)
#--------------------------------------------------------------------------------------------------------------
f = open(outputFile, "w")
f.write("numSolutions: "+ str(numSolutions)+ " k : " + str(k) + "\n" + str(n) + " " + str(numSolutions) + " " + str(epsilon / math.sqrt(n)) + " " + str(delta_m / n) + "\n")
f.flush()
f.close()
#---------------------------------------------main begins---------------------------------------
# cmd = "approxmc " + UserInputFile
# os.system(cmd)
sampleSet = []
sampleSet = getSolutionFromSampler(inputFile, numSolutions, samplerType, indVarList, seed, 1, f)
dim = len(UserIndVarList)
val = 0; nsamp = 0
out = open(outputFile, "a")
# print(UserIndVarList, indVarList)
if isthread > 0:
t = []
ncores = isthread
eachthread = [numSolutions // ncores for i in range(ncores)]
rem = numSolutions % ncores
for i in range(ncores):
eachthread[i] += 1
rem -= 1
if rem == 0 : break
for i in range(1, ncores):
eachthread[i] += eachthread[i-1]
massarray = []; nsamplesarray = []
eachthread = [0] + eachthread
for i in range(1, ncores+1):
t.append(threading.Thread(target=inthread, args=(sampleSet[eachthread[i-1]: eachthread[i]], dim, indVarList, inputFile, samplerType, seed, k, out, massarray, nsamplesarray, i)))
for i in range(ncores):
t[i].start()
for i in range(ncores):
t[i].join()
out.write("\n estimated weights : " + str(massarray) + "\n")
out.flush()
for est in massarray:
try:
val += max(0, 1 - 1 /(est * mc))
except ZeroDivisionError:
pass
for ns in nsamplesarray:
nsamp += ns
else:
val = 0
count = 0
for x in sampleSet:
out.write(str(count) + " of " + str(len(sampleSet)) + "\t")
out.flush()
est, ns = estimate(x, dim, indVarList, inputFile, samplerType, seed, k, out, 1)
val = val + max(0, 1 - 1 / (est * mc) )
nsamp += ns
count += 1
out.write("\n dTV estimated : " + str(val / numSolutions))
out.write("\n nsamples : " + str(nsamp))
os.unlink(inputFile)
if args.mode == 'test':
if val / numSolutions > K:
out.write("\n REJECTED")
out.close()
return 'REJECT'
else:
out.write("\n ACCEPTED")
out.close()
return 'ACCEPT'
elif args.mode == 'est':
return str(val / numSolutions)
if __name__ == "__main__":
output = CubeProbe()
print("output:", output)