-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_metrics.py
executable file
·148 lines (130 loc) · 3.86 KB
/
generate_metrics.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
#!/usr/bin/env python
import argparse
import multiprocessing
from pathlib import Path
import numpy as np
import pandas as pd
import utils
from pysat.formula import CNF
from rich_argparse import RichHelpFormatter
from solvers.pysat import PySAT
from tqdm.auto import tqdm, trange
class Args(argparse.Namespace):
outdir: Path
num_vars: int
num_clauses: int
num_instances: int
runs: int
num_cpus: int
seed: int
def parse_args() -> Args:
parser = argparse.ArgumentParser(
description=(
"An older alternative to `generate_eval_ksat.py` which was used "
"earlier on in the project. This script has been left here "
"because it is needed to generate the data for the notebook "
"metrics.ipynb. This script does not support repeatable options. "
"It saves the generated instances as cnf files."
),
formatter_class=lambda *args, **kwargs: RichHelpFormatter(
*args, **kwargs, max_help_position=28, width=90
),
add_help=False,
)
parser.add_argument(
"outdir",
type=Path,
help="the directory in which to store the generated instances and metrics",
)
parser.add_argument(
"--num_vars",
type=int,
default=100,
metavar="INT",
help="number of variables in the generated instances \\[default: 100]",
)
parser.add_argument(
"--num_clauses",
type=int,
default=420,
metavar="INT",
help="number of clauses in the generated instances \\[default: 420]",
)
parser.add_argument(
"--num_instances",
type=int,
default=1000,
metavar="INT",
help="number of instances to generate \\[default: 1000]",
)
parser.add_argument(
"--runs",
type=int,
default=3,
metavar="INT",
help="number of times to solve/evaluate each instance \\[default: 3]",
)
parser.add_argument(
"--num_cpus",
type=int,
default=1,
metavar="INT",
help="number of solver processes to run in parallel \\[default: 1]",
)
parser.add_argument(
"--seed",
type=int,
default=0,
metavar="INT",
help="seed for all random number generators \\[default: 0]",
)
parser.add_argument(
"-h",
"--help",
action="help",
help="show this help message and exit",
)
args = parser.parse_args(namespace=Args())
return args
def main():
args = parse_args()
args.outdir.mkdir(parents=True, exist_ok=True)
rng = np.random.default_rng(args.seed)
tasks = []
skipped = 0
for i in trange(args.num_instances, desc="generating instances"):
file = args.outdir / f"{i}.cnf"
if file.exists():
skipped += 1
else:
instance = utils.random_k_sat(
rng, n_vars=args.num_vars, n_clauses=args.num_clauses, k=3
)
instance.to_file(str(file))
for run in range(args.runs):
tasks.append((str(args.outdir), i, run))
if skipped > 0:
print(f"Skipped generation of {skipped} existing instances")
results = []
with multiprocessing.Pool(args.num_cpus) as pool:
it = tqdm(
pool.imap_unordered(solve, tasks),
total=len(tasks),
desc="solving instances",
)
for result in it:
results.append(result)
df = pd.DataFrame(results)
df.to_csv(str(args.outdir / "metrics.csv"), index=False)
def solve(data: tuple[str, int, int]):
outdir, instance, run = data
file = outdir + f"/{instance}.cnf"
cnf = CNF(from_file=file)
metrics = PySAT("minisat22").solve_instance(cnf.clauses)
return {
"instance": instance,
"run": run,
**metrics,
}
if __name__ == "__main__":
main()