-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
482 lines (420 loc) · 14.7 KB
/
Copy pathsolver.py
File metadata and controls
482 lines (420 loc) · 14.7 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
"""
Simulated Bifurcation Solver
Encapsulates the run function from core.py, allowing storage of parameters and results.
Provides convenient plotting methods for visualization.
"""
from typing import Any, Callable, Self
import numpy as np
import torch
from matplotlib.axes import Axes
import visualize
from core import MethodType, StateArray, run
from visualize import KeyType, ColorType
class Solver:
"""
Simulated Bifurcation Solver class
Encapsulates the run function from core.py, allowing storage of parameters and results.
Provides convenient plotting methods for trajectory and time histogram visualization.
"""
def __init__(self):
self.params: dict[str, Any] = {}
self._result: StateArray | None = None
self._J: np.ndarray | None = None
def solve(
self,
J: torch.Tensor | np.ndarray,
method: MethodType = "bSB",
*,
beta: float = 2**-11,
xi: float | None = None,
eta: float = 2**-3,
max_steps: int | None = None,
seed: int = 42,
progress_bar: bool = False,
device: str | None = None,
) -> StateArray:
"""
Run the Simulated Bifurcation algorithm to solve the Ising model.
Parameters
----------
J : torch.Tensor or np.ndarray
Input Ising matrix (negative of the coupling matrix).
method : str
The method to use. Options are "aSB", "bSB", "dSB", "sSB", "sSB_clip".
beta : float
Growth rate of p(t).
xi : float
Coupling strength. Defaults to 1 / (2 * sqrt(N)) if None.
eta : float
Time step size.
max_steps : int
Maximum number of steps. Defaults to int(2 / beta / eta) if None.
seed : int
Random seed.
progress_bar : bool
Whether to display a progress bar.
device : str, optional
Device to run the computation on. If None, uses "cuda" if available, otherwise "cpu".
Returns
-------
SBHistoryArray
The result of the solve.
"""
# Store original J matrix for plotting
if device is None:
if J.shape[0] > 20 and torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
if isinstance(J, np.ndarray):
self._J = J.copy()
J_torch = torch.tensor(J, dtype=torch.float32, device=device)
else:
self._J = J.cpu().numpy()
J_torch = J.to(dtype=torch.float32, device=device)
# Apply defaults for None values (same as core.py)
if xi is None:
xi = 1 / 2 / J_torch.shape[0] ** 0.5
if max_steps is None:
max_steps = int(2 / beta / eta)
# Store parameters with actual values (not None)
self.params = {
"method": method,
"beta": beta,
"xi": xi,
"eta": eta,
"max_steps": max_steps,
"seed": seed,
"progress_bar": progress_bar,
}
result = run(
J_torch,
method=method,
beta=beta,
xi=xi,
eta=eta,
max_steps=max_steps,
seed=seed,
progress_bar=progress_bar,
).numpy()
self._result = result
return result
@property
def result(self) -> StateArray:
"""
Get the result of the last solve.
Returns
-------
SBHistoryArray
The result of the last solve, or None if no result exists.
"""
if self._result is None:
raise ValueError("No result exists. Please run the solver first.")
return self._result
def get_best_solution(self) -> np.ndarray:
"""
Get the current best solution.
Returns
-------
np.ndarray
The best solution's sign values (+1 or -1), or None if no result exists.
"""
return self.result.best_x
def get_best_cut(self) -> int:
"""
Get the current best cut value.
Returns
-------
int
The best cut value.
"""
if self.result.cut is None:
raise ValueError("cut is None")
return self.result.cut.max()
def _get_energy_fn(self, dim0: int, dim1: int) -> Callable[[np.ndarray, np.ndarray], np.ndarray]:
"""
Create 2D subspace energy function for the specific method and dimensions.
Parameters
----------
dim0, dim1 : int
The dimensions to plot
Returns
-------
Callable
Energy function that takes (x0, x1) and returns energy
"""
if self._J is None:
raise ValueError("No J matrix stored. Please run solve() first.")
J = self._J
N = J.shape[0]
x_other_dim = [i for i in range(N) if i not in [dim0, dim1]]
coeff01: float = J[dim0, dim1]
xi: float = self.params["xi"] # xi should never be None now after solve() processing
beta: float = self.params["beta"]
eta: float = self.params["eta"]
method: MethodType = self.params["method"]
def energy_fn_aSB(x0, x1):
V = 1 / 4 * (x0**4 + x1**4)
t = len(self.result.x) * eta
V += 1 / 2 * (1 - beta * t) * (x0**2 + x1**2)
coeff0, coeff1 = J[[dim0, dim1], :][:, x_other_dim] @ self.result.x[-1, x_other_dim]
V -= xi * (coeff01 * x0 * x1 + coeff0 * x0 + coeff1 * x1)
return V
def energy_fn_bSB(x0, x1):
t = len(self.result.x) * eta
V = 1 / 2 * (1 - beta * t) * (x0**2 + x1**2)
coeff0, coeff1 = J[[dim0, dim1], :][:, x_other_dim] @ self.result.x[-1, x_other_dim]
V -= xi * (coeff01 * x0 * x1 + coeff0 * x0 + coeff1 * x1)
return V
def energy_fn_dSB(x0, x1):
t = len(self.result.x) * eta
V = 1 / 2 * (1 - beta * t) * (x0**2 + x1**2)
V -= xi * (coeff01 * x0 * x1)
coeff0, coeff1 = J[[dim0, dim1], :][:, x_other_dim] @ self.result.x[-1, x_other_dim]
V -= (coeff0 * np.sign(x0) + coeff1 * np.sign(x1)) / 2
coeff0, coeff1 = J[[dim0, dim1], :][:, x_other_dim] @ np.sign(self.result.x[-1, x_other_dim])
V -= (coeff0 * x0 + coeff1 * x1) / 2
return V
match method:
case "aSB":
return energy_fn_aSB
case "bSB" | "sSB" | "sSB_sgn":
return energy_fn_bSB
case "dSB":
return energy_fn_dSB
case _:
raise ValueError(f"Unknown method: {method}. Supported methods are 'aSB', 'bSB', 'dSB', 'sSB'.")
def plot_trajectory(
self,
ax: Axes | None = None,
dim0: int = 0,
dim1: int = 1,
xlim: tuple[float, float] = (-1, 1),
ylim: tuple[float, float] = (-1, 1),
show_bound: bool = True,
n_arrows: int = 100,
show_energy: bool = True,
) -> Axes:
"""
Plot the trajectory in 2D phase space.
Parameters
----------
ax : Axes, optional
Matplotlib axes to plot on. If None, uses current axes.
dim0, dim1 : int
Dimensions to plot (default: 0, 1)
xlim : tuple[float, float], optional
x-axis limits (default: None, auto-determined)
ylim : tuple[float, float], optional
y-axis limits (default: None, auto-determined)
show_bound : bool
Whether to show the bounds of the plot (default: True)
n_arrows : int
Number of arrows to show direction (default: 100)
show_energy : bool
Whether to show energy contours (default: True)
Returns
-------
Axes
The matplotlib axes with the plot
"""
if self._result is None:
raise ValueError("No result exists. Please run solve() first.")
eta = self.params["eta"]
energy_fn = self._get_energy_fn(dim0, dim1) if show_energy else None
return visualize.plot_trajectory(
ax=ax,
eta=eta,
x_history=self.result.x,
dim0=dim0,
dim1=dim1,
xlim=xlim,
ylim=ylim,
show_bound=show_bound,
energy_fn=energy_fn,
n_arrows=n_arrows,
)
def plot_time_hist(
self,
ax: Axes | None = None,
n_slice: int = 2000,
n_bins: int = 201,
vmax: float = 0.05,
xlim: tuple[float, float | None] = (0, None),
ylim: tuple[float, float] = (-1, 1),
) -> Axes:
"""
Plot time histogram of the solution trajectory.
Parameters
----------
ax : Axes, optional
Matplotlib axes to plot on. If None, uses current axes.
n_slice : int
Number of time slices (default: 2000)
n_bins : int
Number of histogram bins (default: 80)
vmax : float
Maximum value for colormap (default: 0.05)
xlim : tuple[float, float | None], optional
x-axis limits (default: (0, None), auto-determined)
ylim : tuple[float, float], optional
y-axis limits (default: (-1, 1))
Returns
-------
Axes
The matplotlib axes with the plot
"""
if self._result is None:
raise ValueError("No result exists. Please run solve() first.")
eta = self.params["eta"]
ax = visualize.plot_time_hist(
ax=ax,
eta=eta,
history=self.result.x,
n_slice=n_slice,
n_bins=n_bins,
vmax=vmax,
xlim=xlim,
ylim=ylim,
)
return ax
def plot_history(
self,
axes: list[Axes] | None = None,
title: str | None = None,
color: ColorType = "purple",
alpha: float = 0.9,
t_range: KeyType | None = None,
dim_range: KeyType | None = None,
ylabel: bool = True,
) -> list[Axes]:
"""
Plot complete history of x, y, g, V, H over time.
Parameters
----------
axes : list[Axes], optional
List of 4 axes for plotting. If None, creates new subplots.
title : str, optional
Title for the plots. Defaults to method name.
color : str
Color for the plots (default: "purple")
alpha : float
Alpha transparency (default: 0.9)
t_range : KeyType, optional
Time range to plot
dim_range : KeyType, optional
Dimension range to plot (default: first 20 dimensions)
ylabel : bool
Whether to add y-axis labels (default: True)
Returns
-------
list[Axes]
List of matplotlib axes with the plots
"""
if self._result is None:
raise ValueError("No result exists. Please run solve() first.")
if title is None:
title = self.params["method"]
# Type assertion: title is guaranteed to be a string after the check above
assert isinstance(title, str), "title must be a string"
# Ensure H is not None
H_history = self.result.H
if H_history is None:
raise ValueError("H_history is None. This may indicate an issue with the solve process.")
return visualize.plot_history(
title=title,
eta=self.params["eta"],
x_history=self.result.x,
y_history=self.result.y,
g_history=self.result.g,
V_history=self.result.V,
H_history=H_history,
color=color,
alpha=alpha,
axes=axes,
t_range=t_range,
dim_range=dim_range,
ylabel=ylabel,
)
def get_cut_history(self) -> np.ndarray:
"""
Get the cut value history.
Returns
-------
np.ndarray
Array of cut values over time
"""
if self.result.cut is None:
raise ValueError("cut is None")
return self.result.cut
def get_convergence_time(self) -> float:
"""
Estimate convergence time based on when the solution stops changing significantly.
Returns
-------
float
Estimated convergence time
"""
return self.get_n_steps() * self.params["eta"]
def get_n_steps(self) -> int:
"""
Get the number of steps taken in the last solve.
Returns
-------
int
Number of steps
"""
return len(self.result.x)
def get_final_energy(self) -> float:
"""
Get the final energy value.
Returns
-------
float
Final energy value
"""
if self._result is None:
raise ValueError("No result exists. Please run solve() first.")
return float(self.result.V[-1])
def summary(self) -> dict:
"""
Get a summary of the solver results.
Returns
-------
dict
Dictionary containing key metrics and parameters
"""
if self._result is None:
raise ValueError("No result exists. Please run solve() first.")
return {
"method": self.params["method"],
"parameters": {k: v for k, v in self.params.items() if k != "method"},
"best_cut": self.get_best_cut(),
"final_energy": self.get_final_energy(),
"convergence_time": self.get_convergence_time(),
"total_steps": len(self.result.x),
"total_time": len(self.result.x) * self.params["eta"],
}
def compare_with(self, other_solver: Self) -> dict:
"""
Compare this solver's results with another solver.
Parameters
----------
other_solver : Solver
Another Solver instance to compare with
Returns
-------
dict
Comparison results
"""
if self._result is None or other_solver._result is None:
raise ValueError("Both solvers must have results to compare.")
self_summary = self.summary()
other_summary = other_solver.summary()
return {
"methods": (self_summary["method"], other_summary["method"]),
"cut_difference": self_summary["best_cut"] - other_summary["best_cut"],
"energy_difference": self_summary["final_energy"] - other_summary["final_energy"],
"convergence_time_difference": self_summary["convergence_time"] - other_summary["convergence_time"],
"better_solver": self_summary["method"] if self_summary["best_cut"] > other_summary["best_cut"] else other_summary["method"],
}