-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
229 lines (191 loc) · 8.49 KB
/
main.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
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
# main.py
import sys
import numpy as np
import argparse
import json
from typing import Optional, Dict
from datetime import datetime
from pathlib import Path
from PyQt6.QtWidgets import QApplication
from data import SudokuDataGenerator
from solver import SudokuSolver, format_grid
from gui import SudokuSolverGUI
from visual_recognition import VisualScanner, ScanPattern
from advanced_learning import AdvancedLearningSystem
from natural_language_explainer import NaturalLanguageExplainer
from strategy_selector import StrategySelector
class SudokuSystem:
"""Central system managing all Sudoku solving components."""
def __init__(self):
self.generator = SudokuDataGenerator()
self.visual_scanner = VisualScanner()
self.learning_system = AdvancedLearningSystem()
self.strategy_selector = StrategySelector()
self.explainer = NaturalLanguageExplainer()
# Load learning data
self.learning_system.load_learned_data()
def solve_puzzle(self, puzzle: np.ndarray, step_by_step: bool = False,
scan_pattern: Optional[ScanPattern] = None) -> Dict:
"""
Solve puzzle with full human-like reasoning.
Returns dict with solution and analysis data.
"""
solver = SudokuSolver(puzzle)
current_grid = puzzle.copy()
solving_history = []
while True:
# Visual analysis
scan_result = self.visual_scanner.scan_grid(
current_grid,
solver.get_all_candidates(),
pattern=scan_pattern
)
# Strategy selection based on visual patterns
strategy = self.strategy_selector.select_next_strategy(
current_grid,
solver.available_strategies
)
# Apply strategy
start_time = datetime.now()
step, current_grid = solver.solve_step(strategy)
time_taken = (datetime.now() - start_time).total_seconds()
if step is None:
break
# Generate natural explanation
explanation = self.explainer.explain_solving_step(step, solver.grid.cells)
# Update learning system
self.learning_system.update_learning(
current_grid,
solver.get_all_candidates(),
strategy.value,
success=True,
time_taken=time_taken
)
solving_history.append({
'step': step,
'explanation': explanation,
'patterns': scan_result.patterns,
'focus_areas': scan_result.focus_areas,
'time_taken': time_taken
})
if step_by_step:
yield {
'grid': current_grid.copy(),
'step': solving_history[-1],
'scan_result': scan_result
}
# Get learning state and analysis
learning_state = self.learning_system.get_learning_state()
return {
'success': np.all(current_grid != 0),
'solution': current_grid,
'history': solving_history,
'learning_state': learning_state
}
def analyze_puzzle(self, puzzle: np.ndarray) -> Dict:
"""Analyze puzzle without solving it."""
scan_result = self.visual_scanner.scan_grid(
puzzle,
self.solver.get_all_candidates()
)
return {
'patterns': scan_result.patterns,
'focus_areas': scan_result.focus_areas,
'difficulty_estimate': self.learning_system.estimate_difficulty(puzzle)
}
def solve_puzzle_command_line(system: SudokuSystem, puzzle: np.ndarray,
step_by_step: bool = False):
"""Enhanced command-line solving with visual analysis."""
print("\nOriginal puzzle:")
print(format_grid(puzzle))
print("\nAnalyzing puzzle...")
analysis = system.analyze_puzzle(puzzle)
print(f"Estimated difficulty: {analysis['difficulty_estimate']:.2f}")
print(f"Detected patterns: {len(analysis['patterns'])}")
if step_by_step:
print("\nSolving step by step...")
for state in system.solve_puzzle(puzzle, step_by_step=True):
print("\nVisual scan result:")
print(f"Focus areas: {len(state['scan_result'].focus_areas)}")
print("\nStrategy application:")
print(state['step']['explanation'])
print("\nCurrent grid:")
print(format_grid(state['grid']))
input("Press Enter for next step...")
else:
result = system.solve_puzzle(puzzle, step_by_step=False)
print("\nSolution found!")
print("\nKey solving steps:")
for step in result['history']:
print(f"\n{step['explanation']}")
print("\nFinal grid:")
print(format_grid(result['solution']))
print("\nLearning progress:")
learning_state = result['learning_state']
print(f"Total patterns learned: {learning_state['total_patterns_learned']}")
print("Recent discoveries:")
for discovery in learning_state['recent_discoveries']:
print(f"- {discovery['type']}: {discovery['success_rate']:.2f} success rate")
def generate_example_puzzle(system: SudokuSystem, difficulty: str) -> np.ndarray:
"""Generate puzzle with difficulty analysis."""
puzzle, _ = system.generator.generate_puzzle(difficulty)
analysis = system.analyze_puzzle(puzzle)
actual_difficulty = analysis['difficulty_estimate']
# Regenerate if difficulty doesn't match target
while abs(actual_difficulty - {'easy': 0.3, 'medium': 0.6, 'hard': 0.9}[difficulty]) > 0.2:
puzzle, _ = system.generator.generate_puzzle(difficulty)
analysis = system.analyze_puzzle(puzzle)
actual_difficulty = analysis['difficulty_estimate']
return puzzle
def main():
parser = argparse.ArgumentParser(description='Enhanced Sudoku Solver with Human Reasoning')
parser.add_argument('--cli', action='store_true', help='Run in command-line mode')
parser.add_argument('--puzzle', nargs='+', type=int,
help='Input puzzle (81 numbers, row by row, use 0 for empty cells)')
parser.add_argument('--difficulty', choices=['easy', 'medium', 'hard'],
default='medium', help='Difficulty for generated puzzle')
parser.add_argument('--step-by-step', action='store_true',
help='Show step-by-step solution in CLI mode')
parser.add_argument('--scan-pattern', choices=[p.name for p in ScanPattern],
help='Visual scanning pattern to use')
parser.add_argument('--save-learning', action='store_true',
help='Save learning progress to file')
args = parser.parse_args()
# Initialize the system
system = SudokuSystem()
if args.cli:
# Command-line mode
if args.puzzle:
if len(args.puzzle) != 81:
print("Error: Puzzle must contain exactly 81 numbers")
return
puzzle = np.array(args.puzzle).reshape(9, 9)
else:
puzzle = generate_example_puzzle(system, args.difficulty)
scan_pattern = ScanPattern[args.scan_pattern] if args.scan_pattern else None
solve_puzzle_command_line(system, puzzle, args.step_by_step)
if args.save_learning:
learning_state = system.learning_system.get_learning_state()
with open('learning_progress.json', 'w') as f:
json.dump(learning_state, f, indent=2)
else:
# GUI mode
app = QApplication(sys.argv)
window = SudokuSolverGUI(system)
window.show()
sys.exit(app.exec())
def solve_puzzle_from_file(system: SudokuSystem, filename: str,
step_by_step: bool = False) -> Optional[np.ndarray]:
"""Enhanced file-based puzzle solving."""
try:
puzzle = np.loadtxt(filename, dtype=int)
if puzzle.shape != (9, 9):
raise ValueError("Invalid puzzle dimensions")
print(f"\nSolving puzzle from {filename}")
solve_puzzle_command_line(system, puzzle, step_by_step)
return puzzle
except Exception as e:
print(f"Error reading puzzle file: {e}")
return None
if __name__ == "__main__":
main()