Repository navigation
Expand file tree
/
Copy pathvalidate_models.py
More file actions
executable file
·170 lines (146 loc) · 5.49 KB
/
Copy pathvalidate_models.py
File metadata and controls
executable file
·170 lines (146 loc) · 5.49 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
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
#!/usr/bin/env python3
"""Validate generated formulations on known and randomized small instances.
The pure-Python test suite exhaustively audits each encoding component. This
script adds implementation diversity by asking an installed CaDiCaL binary to
solve complete small CNFs. It also compares the column-type constraints with
direct matrix semantics on a fixed random sample.
"""
from __future__ import annotations
import argparse
import itertools
import json
import random
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from finite_zarankiewicz_closures.encodings import ( # noqa: E402
build_cell_cnf,
cell_variable,
column_constraints_hold,
column_counts,
dimacs_text,
)
SEED = 20260704
def direct_k33_free(matrix: list[list[int]]) -> bool:
"""Return the definition directly, without using project verifiers."""
return not any(
all(matrix[row][column] for row in rows for column in columns)
for rows in itertools.combinations(range(len(matrix)), 3)
for columns in itertools.combinations(range(len(matrix[0])), 3)
)
def cadical_solve(executable: str, path: Path) -> bool:
"""Run CaDiCaL and translate its conventional 10/20 exit status."""
completed = subprocess.run(
[executable, "--quiet", str(path)],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if completed.returncode == 10:
return True
if completed.returncode == 20:
return False
raise RuntimeError(
f"CaDiCaL returned {completed.returncode}: {completed.stdout[-1000:]}"
)
def solve_instance(
executable: str,
directory: Path,
matrix: list[list[int]] | None,
*,
rows: int,
columns: int,
target: int,
) -> bool:
"""Generate a small CNF, optionally fix every cell, and solve it."""
cnf, _ = build_cell_cnf(rows, columns, target)
if matrix is not None:
for row in range(rows):
for column in range(columns):
variable = cell_variable(row, column, columns)
cnf.add_clause(variable if matrix[row][column] else -variable)
path = directory / f"instance_{rows}_{columns}_{target}_{len(list(directory.iterdir()))}.cnf"
path.write_text(dimacs_text(cnf), encoding="ascii")
return cadical_solve(executable, path)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, help="optional JSON report path")
parser.add_argument(
"--random-cases", type=int, default=30, help="number of seeded fixed matrices"
)
args = parser.parse_args()
cadical = shutil.which("cadical")
if cadical is None:
print("CaDiCaL is required for this implementation-diverse validation.", file=sys.stderr)
return 2
version = subprocess.run(
[cadical, "--version"], check=False, capture_output=True, text=True
).stdout.strip()
rng = random.Random(SEED)
random_reports: list[dict[str, object]] = []
with tempfile.TemporaryDirectory(prefix="z9-23-validation-") as raw_directory:
directory = Path(raw_directory)
known = []
for target, expected in ((10, True), (11, False)):
observed = solve_instance(
cadical, directory, None, rows=3, columns=4, target=target
)
row = {
"problem": "Z(3,4,3,3)",
"target": target,
"expected_satisfiable": expected,
"observed_satisfiable": observed,
}
if observed != expected:
raise AssertionError(row)
known.append(row)
for index in range(args.random_cases):
matrix = [[rng.randrange(2) for _ in range(6)] for _ in range(5)]
weight = sum(map(sum, matrix))
expected = direct_k33_free(matrix)
cell_observed = solve_instance(
cadical,
directory,
matrix,
rows=5,
columns=6,
target=weight,
)
column_observed = column_constraints_hold(
column_counts(matrix), rows=5, columns=6, target_ones=weight
)
report = {
"index": index,
"ones": weight,
"direct_k33_free": expected,
"cell_cnf_satisfiable_when_fixed": cell_observed,
"column_constraints_hold": column_observed,
}
if cell_observed != expected or column_observed != expected:
raise AssertionError(report)
random_reports.append(report)
result = {
"status": "VERIFIED",
"solver": {"name": "CaDiCaL", "version": version},
"seed": SEED,
"known_cases": known,
"random_fixed_matrices": random_reports,
"scope": (
"Small-instance semantic validation only. Eight excluded-target impossibilities "
"are established by separate case-specific proofs."
),
}
rendered = json.dumps(result, indent=2, sort_keys=True) + "\n"
if args.output:
destination = args.output if args.output.is_absolute() else ROOT / args.output
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(rendered, encoding="utf-8")
print(rendered, end="")
return 0
if __name__ == "__main__":
raise SystemExit(main())