-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconvert_cpx_old_format.py
More file actions
executable file
·59 lines (56 loc) · 2.67 KB
/
Copy pathconvert_cpx_old_format.py
File metadata and controls
executable file
·59 lines (56 loc) · 2.67 KB
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
#!/usr/bin/env python3
#
# Copyright (c) 2025 Mate Soos
#
# 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.
import argparse
import re
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--verbose", help="Verbose debug printing", action="store_const",
const=True)
parser.add_argument("inputFile", help="input File (in Weighted CNF format)")
parser.add_argument("outputFile", help="output File (in Weighted CNF format)")
args = parser.parse_args()
with open(args.outputFile, 'w') as out:
with open(args.inputFile, 'r') as f:
for line in f:
line = line.strip()
if line.startswith("c p weight"):
line = re.sub(r'\s+', ' ', line)
if not re.fullmatch(r'c p weight \S+ \S+ \+ \S+ 0', line):
print(f"Error: Invalid line format: {line}")
print(f"Expected format: 'c p weight <var1> <real weight> <complex weight> 0'")
print(f"Instead got: {line}")
exit(1)
elems = line.split()
var = elems[3]
real_weight = elems[4]
complex_weight = elems[6]
if "/" in real_weight:
real_weight = real_weight.split("/")
real_weight = str(float(real_weight[0])/float(real_weight[1]))
if "/" in complex_weight:
complex_weight = complex_weight[:-1] # Remove the trailing 'i'
complex_weight = complex_weight.split("/")
complex_weight = str(float(complex_weight[0])/float(complex_weight[1])) + "i"
out.write(f"c p weight {var} {real_weight} {complex_weight} 0\n")
else:
out.write(line + "\n")