-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_eval_ksat.py
executable file
·161 lines (144 loc) · 4 KB
/
generate_eval_ksat.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
#!/usr/bin/env python
import argparse
from pathlib import Path
from typing import TYPE_CHECKING
import evaluation
from rich_argparse import RichHelpFormatter
from solvers.pysat import PySAT
if TYPE_CHECKING:
from solvers.base import Solver
class Args(argparse.Namespace):
output: Path
num_vars: list[int]
alphas: list[float]
runs: int
solvers: list[str]
num_cpus: int
seed: int
force: bool
def parse_args() -> Args:
parser = argparse.ArgumentParser(
description=(
"Evaluates the instances generated by Random k-SAT. The output is similar "
"to that of the 'generate_eval.py' script, but using Random k-SAT "
"instances instead of a trained model's output"
),
formatter_class=lambda *args, **kwargs: RichHelpFormatter(
*args, **kwargs, max_help_position=28, width=90
),
add_help=False,
)
parser.add_argument(
"-n",
"--num_vars",
type=int,
action="append",
default=[],
metavar="INT",
help=(
"number of variables in the evaluation instances "
"\\[repeatable, default: 100]"
),
)
parser.add_argument(
"-a",
"--alpha",
type=float,
action="append",
dest="alphas",
default=[],
metavar="FLOAT",
help=(
"ratio of clauses to variables in the evaluation instances "
"\\[repeatable, default: 4.2]"
),
)
parser.add_argument(
"--runs",
type=int,
default=100,
metavar="INT",
help=(
"how many instances to generate and evaluate; if any of the "
"repeatable parameters are specified, this is the number of "
"instances generated for each combination of the repeatable "
"parameters \\[default: 100]"
),
)
parser.add_argument(
"--solver",
type=str,
action="append",
dest="solvers",
metavar="SOLVER",
help=(
"which solver to use; can be any solver accepted by PySAT "
"(see `pysat.solvers.SolverNames` for a full list of accepted "
"solver names) \\[repeatable, default: minisat22]"
),
)
parser.add_argument(
"-o",
"--output",
type=Path,
default="ksat_eval.parquet",
metavar="PATH",
help="output file (parquet or csv) \\[default: ksat_eval.parquet]",
)
parser.add_argument(
"--num_cpus",
type=int,
default=1,
metavar="INT",
help="number of evaluation 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(
"-f",
"--force",
action="store_true",
help="overwrite output files",
)
parser.add_argument(
"-h",
"--help",
action="help",
help="show this help message and exit",
)
args = parser.parse_args(namespace=Args())
if not args.num_vars:
args.num_vars = [100]
if not args.alphas:
args.alphas = [4.2]
return args
def main():
args = parse_args()
if args.output.exists() and not args.force:
print(f'Output file "{args.output}" exists, use --force to run anyway')
return
args.output.parent.mkdir(parents=True, exist_ok=True)
solvers: list[Solver]
if args.solvers:
solvers = [PySAT(name) for name in args.solvers]
else:
solvers = [PySAT("m22")]
results = evaluation.generate_and_eval_ksat_par(
solvers,
args.num_vars,
args.alphas,
args.runs,
args.num_cpus,
args.seed,
)
if args.output.suffix.lower() == ".parquet":
results.to_parquet(args.output)
else:
results.to_csv(args.output, index=False)
if __name__ == "__main__":
main()