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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Authors of CryptoMiniSat, see AUTHORS file
#
# 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.
import sys
import subprocess
def parse_lit(x):
inv = 1
if x[0] == '~':
inv = -1
x = x[1:]
assert x[0] == 'x'
v = int(x[1:])
return v*inv
def leq_translate(c):
#if len(c.lhs) == 1 and c.lhs[0][0] == 1 and c.cutoff == 0:
#print("%d %d 0" % (-c.lhs[0][1], c.rhs))
#print("%d %d 0" % (c.lhs[0][1], -c.rhs))
#return
#if len(c.lhs) == 1 and c.lhs[0][0] == -1 and c.cutoff == 1:
#print("%d %d 0" % (-c.lhs[0][1], c.rhs))
#print("%d %d 0" % (c.lhs[0][1], -c.rhs))
#return
#if len(c.lhs) == 1 and c.rhs == None and c.cutoff == 1:
#if c.lhs[0][0] == 1:
#print("%d 0" % c.lhs[0][1])
#return
#sys.stderr.write("ERRROOOR\n")
#exit(-1)
ins = []
for l in c.lhs:
if l[0] == 1:
ins.append(l[1])
continue
else:
ins.append(-l[1])
c.cutoff+=1
pr = "b "
for l in ins:
pr += "%d " % l
pr += "0 "
pr += "%d " % c.cutoff
if c.rhs is not None:
pr += "%d " % c.rhs
print("%s" % pr)
class Constr:
def __init__(self, lhs, cutoff, rhs, sign):
self.lhs = lhs
self.cutoff = cutoff
self.rhs = rhs
self.sign = sign
def __repr__(self):
return "lhs: %s cutoff: %s sign: %s rhs: %s" % (self.lhs, self.cutoff, self.sign, self.rhs)
def simple(self):
if self.sign != "=" and self.sign != ">=":
return
for l in self.lhs:
if l[0] == 1 or l[0] == -1:
continue
else:
return False
return True
def translate(self):
print("c Doing simple:", self)
assert self.simple()
if self.sign == ">=":
leq_translate(self)
elif self.sign == "=":
leq_translate(self)
c = Constr(self.lhs, self.cutoff, self.rhs, self.sign)
c.cutoff = len(c.lhs) - c.cutoff
for i in range(len(c.lhs)):
c.lhs[i][0] = -c.lhs[i][0]
leq_translate(c)
else:
print("ERROR")
exit(-1)
def parse_constr(line):
lhs = []
cutoff = None
rhs = None
sign = ""
at_lhs = True
at_cutoff = False
at_rhs = False
done = False
line = line.split(' ')
i = 0
#print("line:", line)
while i < len(line):
#print("SO FAR -- lhs: ", lhs, "cutoff: ", cutoff, "rhs: ", rhs, "at_lhs:", at_lhs, "at_cutoff:", at_cutoff, "at_rhs:", at_rhs)
k = line[i].strip()
if k == '':
i+=1
continue
if at_lhs:
if k == ">=" or k == "=":
sign = k
at_lhs = False
at_cutoff= True
i+=1
continue
#else:
#sys.stderr.write("ERRROOOR: input has:" + k)
#exit(-1)
coeff = int(k)
assert i+1 < len(line)
i+=1
k = line[i].strip()
lit = parse_lit(k)
lhs.append([coeff, lit])
i+=1
continue
if at_cutoff:
if k == "<->":
at_cutoff= False
at_rhs = True
i+=1
continue
if k == ";":
done = True
break
cutoff = int(k)
i+=1
continue
if at_rhs:
if k == ";":
done = True
break
assert k[0] == 'x'
rhs = parse_lit(k)
i+=1
continue
assert False
assert done
#if rhs is not None:
#print("lhs: ", lhs, "cutoff: ", cutoff, "rhs: ", rhs)
#else:
#print("lhs: ", lhs, "cutoff: ", cutoff)
return Constr(lhs, cutoff, rhs, sign)
def do_pbsugar(constr, maxvar):
with open("sugarin", "w") as f:
f.write("* #variable= %d #constraint= 1\n" % maxvar)
for i in constr.lhs:
f.write("%d x%d " % (i[0],i[1]))
f.write(" >= %d ;\n" % constr.cutoff)
assert constr.rhs is None
args = ("./pbsugar", "-map", "x", "-n", "sugarin", "-sat", "sugarout")
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
#print("c pbsugar output: ", output)
# get map
varmap_end = 0
varmap = {}
with open("x", "r") as f:
first = True
for line in f:
if first:
num_max = int(line.strip())
assert num_max == maxvar # or it had to use some extra vars
first = False
continue
line=line.strip().split(' ')
assert line[0][0] == 'x'
new = int(line[1])
orig = int(line[0][1:])
varmap[new] = orig
if new > varmap_end:
varmap_end = new
towrite = ""
with open("sugarout", "r") as f:
for line in f:
if "p cnf" in line:
continue
line = line.strip().split()
for lit in line:
lit = int(lit)
sign = lit < 0
var = abs(lit)
if var == 0:
towrite+= '0'
print(towrite)
towrite = ""
continue
if var in varmap:
# part of original formula
var2 = varmap[var]
else:
# fresh variable by pbsugar
assert var > varmap_end
maxvar+=1
var2 = maxvar
if sign:
lit2 = -var2
else:
lit2 = var2
towrite+="%d " % lit2
return maxvar
if __name__ == "__main__":
if len(sys.argv) != 2:
print("ERROR: must give ONE input, the OPB file")
exit(-1)
fname = sys.argv[1]
# parse input
ind = []
constr = []
maxvar = None
with open(fname, "r") as f:
# parse ind
for line in f:
line = line.strip()
if len(line) == 0:
continue
if "#variable" in line:
maxvar = int(line.split()[2])
print("c maxvar: ", maxvar)
continue
if "* ind" in line:
line = line[5:]
for k in line.split(' '):
k = k.strip()
if k != "0" and k != '':
ind.append(int(k))
continue
if line[0] == '*':
continue
constr.append(parse_constr(line))
# deal with independent
pr = "c ind "
for i in ind:
pr += "%d " % i
print("%s 0" % pr)
# create output
for c in constr:
print("c doing constraint: ", c)
if c.simple():
c.translate()
else:
print("c Using pbsugar to deal with: ", c)
maxvar = do_pbsugar(c, maxvar)
|