-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomDemo.py
263 lines (216 loc) · 8.86 KB
/
RandomDemo.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
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
import random
import sys
import time
from colorama import Fore, Style
from BaseVisualizer import BaseVisualizer
from SATSolver import Formula, RandomSATSolver
from Utilities import RandomFormulaGenerator
class RandomWalkVisualizer(RandomSATSolver, BaseVisualizer):
"""Random Walk Solver with interactive visualization"""
def __init__(self):
RandomSATSolver.__init__(self, max_flips=100, max_tries=10, debug=True)
BaseVisualizer.__init__(self)
def solve(self, formula: Formula) -> dict:
"""Override solve method to reset step counter"""
self.step_count = 0
self.total_flips = 0
return super().solve(formula)
def wait_for_user(self, message: str = "\nPress Enter to continue..."):
"""Wait for user to press Enter"""
self.color_print(message, Fore.CYAN, end="")
input()
def _random_walk(self, formula: Formula, assignment: dict) -> dict:
"""Modified Random Walk implementation with visualization"""
self.step_count += 1
current_assignment = assignment.copy()
prev_unsat_count = float("inf")
# Clear screen
self.clear_screen()
# Print current state
self.color_print("\n" + "=" * 50, Fore.CYAN)
self.color_print(
f"Try {self.step_count} - Starting new random walk", Fore.CYAN, Style.BRIGHT
)
self.color_print("=" * 50, Fore.CYAN)
# Print initial assignment
self.color_print("\nInitial random assignment:", Fore.YELLOW, Style.BRIGHT)
for var, val in sorted(current_assignment.items()):
color = Fore.GREEN if val else Fore.RED
self.color_print(f" x{var} = {str(val)}", color)
self.wait_for_user()
# Random walk loop
for flip in range(self.max_flips):
self.total_flips += 1
self.stats.increment("total_flips")
# Clear screen and update state
self.clear_screen()
# Print current state
self.color_print("\n" + "=" * 50, Fore.CYAN)
self.color_print(
f"Try {self.step_count} - Flip {flip + 1}/{self.max_flips}",
Fore.CYAN,
Style.BRIGHT,
)
self.color_print("=" * 50, Fore.CYAN)
# Check if solution found
if self.verify_solution(formula, current_assignment):
self.color_print(
"\n✨ Success! Found satisfying assignment.",
Fore.GREEN,
Style.BRIGHT,
)
self.wait_for_user()
return current_assignment
# Find unsatisfied clauses
unsatisfied = [
clause
for clause in formula.clauses
if not self._verify_clause(clause, current_assignment)
]
unsat_count = len(unsatisfied)
# Print unsatisfied clauses
self.color_print("\nUnsatisfied clauses:", Fore.YELLOW, Style.BRIGHT)
for clause in unsatisfied:
self.color_print(f" {clause}", Fore.RED)
self.color_print(
f"\nTotal unsatisfied clauses: {unsat_count}", Fore.YELLOW, Style.BRIGHT
)
# Track improvement
if unsat_count < prev_unsat_count:
self.stats.increment("successful_flips")
self.color_print("\n↗ Improvement found!", Fore.GREEN)
elif unsat_count > prev_unsat_count:
self.color_print("\n↘ Solution quality decreased", Fore.RED)
self.stats.increment("local_minima")
else:
self.color_print("\n→ No change in solution quality", Fore.BLUE)
prev_unsat_count = unsat_count
# Choose random unsatisfied clause
chosen_clause = random.choice(unsatisfied)
chosen_literal = random.choice(chosen_clause.literals)
self.color_print(
f"\nChosen unsatisfied clause: {chosen_clause}",
Fore.MAGENTA,
Style.BRIGHT,
)
self.color_print(
f"Will flip variable: x{chosen_literal.variable}",
Fore.MAGENTA,
Style.BRIGHT,
)
# Show current assignment
self.color_print("\nCurrent assignment:", Fore.YELLOW)
for var, val in sorted(current_assignment.items()):
if var == chosen_literal.variable:
self.color_print(
f" x{var} = {str(val)} → {str(not val)}",
Fore.MAGENTA,
Style.BRIGHT,
)
else:
color = Fore.GREEN if val else Fore.RED
self.color_print(f" x{var} = {str(val)}", color)
self.wait_for_user()
# Flip the chosen variable
current_assignment[chosen_literal.variable] = not current_assignment[
chosen_literal.variable
]
self.color_print(
"\n❌ Maximum flips reached without finding solution.",
Fore.RED,
Style.BRIGHT,
)
self.wait_for_user()
return None
def main():
visualizer = RandomWalkVisualizer()
# Print welcome and explanation
visualizer.print_welcome("Welcome to the Random Walk SAT Algorithm Visualizer")
# Print algorithm description
visualizer.color_print("\nAlgorithm Steps:", Fore.WHITE, Style.BRIGHT)
visualizer.print_rule_box(
"1. Random Assignment", "Start with random values for all variables"
)
visualizer.print_rule_box(
"2. Evaluate Solution", "Count number of unsatisfied clauses"
)
visualizer.print_rule_box(
"3. Random Selection", "Choose random unsatisfied clause and variable"
)
visualizer.print_rule_box(
"4. Flip Variable", "Change chosen variable's value and repeat"
)
# Print color legend
visualizer.print_legend()
# Get parameters
n, ratio = visualizer.get_parameters(3, 4.0)
# Generate formula with animation
visualizer.color_print("\nGenerating random 3SAT formula...", Fore.BLUE)
for i in range(5):
visualizer.print_progress_bar(i, 4)
time.sleep(0.1)
visualizer.print_progress_bar(4, 4)
print()
# Generate and solve
generator = RandomFormulaGenerator()
formula = generator.generate(n, int(n * ratio))
# Split the formula string into lines if it's too long
formula_lines = []
current_line = "Formula with {} variables:".format(formula.num_variables)
formula_lines.append(current_line)
# Handle each clause separately
clauses = [str(clause) for clause in formula.clauses]
current_line = clauses[0]
for clause in clauses[1:]:
if len(current_line + " ∧ " + clause) > (visualizer.term_width - 20):
formula_lines.append(current_line)
current_line = clause
else:
current_line += " ∧ " + clause
formula_lines.append(current_line)
# Create and display the box with the formatted formula
formula_box = visualizer.create_box("\n".join(formula_lines))
for line in formula_box:
visualizer.color_print(visualizer.center_text(line), Fore.YELLOW)
visualizer.color_print(
"\nPress Enter to start Random Walk solution process...", Fore.CYAN, end=""
)
input()
# Solve formula
solution = visualizer.solve(formula)
# Print final result
result_box = visualizer.create_box("Final Result")
for line in result_box:
visualizer.color_print(visualizer.center_text(line), Fore.CYAN, Style.BRIGHT)
if solution:
success_box = visualizer.create_box("✅ Formula is SATISFIABLE")
for line in success_box:
visualizer.color_print(
visualizer.center_text(line), Fore.GREEN, Style.BRIGHT
)
visualizer.color_print("\nSatisfying assignment:", Fore.GREEN)
for var, val in sorted(solution.items()):
color = Fore.GREEN if val else Fore.RED
visualizer.color_print(
visualizer.center_text(f"x{var} = {str(val)}"), color
)
else:
failure_box = visualizer.create_box(
"❌ Maximum attempts reached - No solution found"
)
for line in failure_box:
visualizer.color_print(visualizer.center_text(line), Fore.RED, Style.BRIGHT)
# Print statistics
stats = [
("Total tries", visualizer.step_count),
("Total flips", visualizer.total_flips),
("Successful flips", visualizer.stats.stats["successful_flips"].value),
("Local minima", visualizer.stats.stats["local_minima"].value),
]
visualizer.print_statistics(stats)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nVisualization interrupted by user")
sys.exit(0)