-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
290 lines (232 loc) · 9.88 KB
/
run_tests.py
File metadata and controls
290 lines (232 loc) · 9.88 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
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
#!/usr/bin/env python3
"""
Test runner for CNF transformation and benchmarking framework
This script demonstrates how to use the framework with your own CNF files
"""
import sys
import json
from pathlib import Path
from cnf_transformer import CNFParser, CNFTransformer, CNFBenchmark, CNFInstance
from example_sat_solver import SimpleDPLLSolver, custom_dpll_transformation, SATBenchmark
def analyze_cnf_file(filepath: str):
"""Analyze a single CNF file and print detailed information"""
print(f"\n{'='*60}")
print(f"ANALYZING: {filepath}")
print(f"{'='*60}")
try:
# Parse the CNF file
cnf = CNFParser.parse_cnf_file(filepath)
# Basic information
print(f"Filename: {cnf.filename}")
print(f"Variables: {cnf.num_variables}")
print(f"Clauses: {cnf.num_clauses}")
print(f"Actual variables used: {cnf.get_variable_count()}")
# Clause analysis
clause_dist = cnf.get_clause_length_distribution()
print(f"Clause length distribution: {clause_dist}")
avg_clause_length = sum(len(clause) for clause in cnf.clauses) / len(cnf.clauses) if cnf.clauses else 0
print(f"Average clause length: {avg_clause_length:.2f}")
# Structural analysis
print(f"\nStructural Analysis:")
print(f"-" * 20)
# Adjacency graph
adjacency = CNFTransformer.to_adjacency_list(cnf)
print(f"Variable adjacency graph nodes: {len(adjacency)}")
# Implication graph
implications = CNFTransformer.to_implication_graph(cnf)
print(f"Implication graph edges: {sum(len(v) for v in implications.values())}")
# Backbone literals
backbone = CNFTransformer.extract_backbone(cnf)
print(f"Backbone literals: {len(backbone)} -> {backbone}")
# Pure literals
pure_literals = CNFTransformer.get_pure_literals(cnf)
print(f"Pure literals: {len(pure_literals)} -> {pure_literals}")
# Matrix representation
matrix, rows, cols = CNFTransformer.to_matrix_representation(cnf)
print(f"Matrix representation: {rows}x{cols}")
# Comments
if cnf.comments:
print(f"\nComments:")
for comment in cnf.comments[:5]: # Show first 5 comments
print(f" {comment}")
if len(cnf.comments) > 5:
print(f" ... and {len(cnf.comments) - 5} more comments")
return cnf
except Exception as e:
print(f"ERROR analyzing {filepath}: {e}")
return None
def test_solver_on_file(filepath: str):
"""Test the DPLL solver on a CNF file"""
print(f"\n{'='*60}")
print(f"SOLVING: {filepath}")
print(f"{'='*60}")
try:
cnf = CNFParser.parse_cnf_file(filepath)
solver = SimpleDPLLSolver()
print("Running DPLL solver...")
is_sat, assignment, stats = solver.solve(cnf)
print(f"Result: {'SATISFIABLE' if is_sat else 'UNSATISFIABLE'}")
print(f"Statistics: {stats}")
if is_sat and assignment:
print(f"Sample assignment (first 10 variables):")
for var in sorted(assignment.keys())[:10]:
print(f" x{var} = {assignment[var]}")
if len(assignment) > 10:
print(f" ... and {len(assignment) - 10} more variables")
return is_sat, stats
except Exception as e:
print(f"ERROR solving {filepath}: {e}")
return None, None
def run_comprehensive_test():
"""Run comprehensive tests on available CNF files"""
print("="*80)
print("COMPREHENSIVE CNF ANALYSIS AND BENCHMARKING")
print("="*80)
# Find available CNF files
test_files = []
search_paths = [
"benchmarks/uf_uuf/*.cnf",
"benchmarks/cbs/*.cnf",
"benchmarks/dimacs/*/*.cnf"
]
for pattern in search_paths:
test_files.extend(Path(".").glob(pattern))
if not test_files:
print("No CNF files found in benchmarks directory!")
print("Creating a simple test case...")
# Create a simple satisfiable test case
test_cnf = CNFInstance(
filename="simple_test.cnf",
num_variables=4,
num_clauses=5,
clauses=[
[1, 2], # x1 OR x2
[-1, 3], # NOT x1 OR x3
[-2, 4], # NOT x2 OR x4
[-3, -4], # NOT x3 OR NOT x4
[1, -2, 3] # x1 OR NOT x2 OR x3
],
comments=["Simple test case", "Should be satisfiable"]
)
print("\nAnalyzing simple test case...")
solver = SimpleDPLLSolver()
is_sat, assignment, stats = solver.solve(test_cnf)
print(f"Result: {'SAT' if is_sat else 'UNSAT'}")
print(f"Assignment: {assignment}")
print(f"Statistics: {stats}")
return
print(f"Found {len(test_files)} CNF files")
# Analyze first few files in detail
analysis_files = test_files[:3] # Analyze first 3 files in detail
print(f"\nDetailed analysis of {len(analysis_files)} files:")
analyzed_cnfs = []
for cnf_file in analysis_files:
cnf = analyze_cnf_file(str(cnf_file))
if cnf:
analyzed_cnfs.append((str(cnf_file), cnf))
# Test solver on small instances
small_instances = [f for f in test_files if "uf20" in f.name][:2]
if small_instances:
print(f"\nTesting solver on {len(small_instances)} small instances:")
for cnf_file in small_instances:
test_solver_on_file(str(cnf_file))
# Run benchmark
print(f"\nRunning benchmark framework...")
benchmark = CNFBenchmark()
# Test with a subset of files
test_subset = [str(f) for f in test_files[:5]] # First 5 files
print("Testing benchmark framework...")
results = []
for cnf_file in test_subset:
result = benchmark.benchmark_single_file(cnf_file, custom_dpll_transformation)
results.append(result)
if result.success:
print(f"✓ {Path(cnf_file).name}: {result.total_time*1000:.2f} ms")
else:
print(f"✗ {Path(cnf_file).name}: {result.error_message}")
# Save results
summary = benchmark.save_results(results, "sample_test_results.json")
print(f"\nBenchmark Summary:")
print(f" Total instances: {summary.get('total_instances', 0)}")
print(f" Successful: {summary.get('successful_instances', 0)}")
print(f" Failed: {summary.get('failed_instances', 0)}")
if 'average_total_time_ms' in summary:
print(f" Average time: {summary['average_total_time_ms']:.2f} ms")
print(f"\nResults saved to: results/sample_test_results.json")
def create_custom_method_template():
"""Create a template for implementing your custom method"""
template_code = '''
def your_custom_method(cnf: CNFInstance) -> Dict:
"""
Template for your custom SAT solving method
Args:
cnf: CNFInstance containing the problem
Returns:
Dict containing your method's results
"""
import time
start_time = time.perf_counter()
# 1. Extract features you need
adjacency = CNFTransformer.to_adjacency_list(cnf)
backbone = CNFTransformer.extract_backbone(cnf)
pure_literals = CNFTransformer.get_pure_literals(cnf)
# 2. Implement your custom logic here
# For example:
# - Variable ordering heuristics
# - Conflict-driven clause learning
# - Preprocessing techniques
# - Neural network inference
# - Quantum-inspired algorithms
# - etc.
# Placeholder for your method
your_result = {
'method_name': 'YourMethodName',
'satisfiable': None, # True/False/None for unknown
'assignment': None, # Variable assignment if SAT
'confidence': 0.0, # Confidence in result (0-1)
'custom_features': {
'adjacency_graph_density': len(adjacency) / cnf.num_variables if cnf.num_variables > 0 else 0,
'backbone_ratio': len(backbone) / cnf.num_variables if cnf.num_variables > 0 else 0,
'pure_literal_ratio': len(pure_literals) / cnf.num_variables if cnf.num_variables > 0 else 0,
# Add your custom features here
}
}
solve_time = time.perf_counter() - start_time
your_result['solve_time_ms'] = solve_time * 1000
return your_result
# Example usage:
# benchmark = CNFBenchmark()
# result = benchmark.benchmark_single_file("your_file.cnf", your_custom_method)
'''
template_file = Path("your_custom_method_template.py")
with open(template_file, 'w') as f:
f.write(template_code)
print(f"Custom method template created: {template_file}")
print("Edit this file to implement your specific method!")
def main():
"""Main function to run tests"""
print("CNF Transformation and Benchmarking Framework")
print("="*50)
if len(sys.argv) > 1:
# Test specific file
cnf_file = sys.argv[1]
if Path(cnf_file).exists():
analyze_cnf_file(cnf_file)
test_solver_on_file(cnf_file)
else:
print(f"File not found: {cnf_file}")
else:
# Run comprehensive tests
run_comprehensive_test()
# Create template for custom method
create_custom_method_template()
print("\n" + "="*80)
print("NEXT STEPS:")
print("="*80)
print("1. Edit 'your_custom_method_template.py' to implement your method")
print("2. Run: python run_tests.py <your_cnf_file.cnf> to test specific files")
print("3. Use the benchmark framework to compare methods")
print("4. Check the 'results' directory for detailed benchmark results")
print("5. Modify the transformation functions in cnf_transformer.py as needed")
if __name__ == "__main__":
main()