Sampling Module¶
compute_lon(func: Callable[[np.ndarray], float], dim: int, lower_bound: float | list[float], upper_bound: float | list[float], seed: int | None = None, step_size: float = 0.01, step_mode: StepMode = 'percentage', n_runs: int = 10, n_iterations: int = 1000, opt_digits: int = -1, hash_digits: int = 4, bounded: bool = True) -> LON
¶
Compute a LON from an objective function.
This is the simplest way to construct a Local Optima Network. For more control, use BasinHoppingSampler directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[ndarray], float]
|
Objective function f(x) -> float to minimize. |
required |
dim
|
int
|
Number of dimensions. |
required |
lower_bound
|
float | list[float]
|
Lower bound (scalar or per-dimension list). |
required |
upper_bound
|
float | list[float]
|
Upper bound (scalar or per-dimension list). |
required |
seed
|
int | None
|
Random seed for reproducibility. |
None
|
step_size
|
float
|
Perturbation step size. |
0.01
|
step_mode
|
StepMode
|
"percentage" (of domain) or "fixed". |
'percentage'
|
n_runs
|
int
|
Number of independent Basin-Hopping runs. |
10
|
n_iterations
|
int
|
Iterations per run. |
1000
|
opt_digits
|
int
|
Decimal precision for optimization (-1 for double). |
-1
|
hash_digits
|
int
|
Decimal precision for solution hashing. |
4
|
bounded
|
bool
|
Whether to enforce domain bounds. |
True
|
Returns:
| Type | Description |
|---|---|
LON
|
LON instance. |
Example
import numpy as np def sphere(x): ... return np.sum(x**2) lon = compute_lon(sphere, dim=5, lower_bound=-5.0, upper_bound=5.0) print(f"Found {lon.n_vertices} local optima")
Source code in src/lonpy/sampling.py
BasinHoppingSamplerConfig
dataclass
¶
Configuration for Basin-Hopping sampling.
Attributes:
| Name | Type | Description |
|---|---|---|
n_runs |
int
|
Number of independent Basin-Hopping runs. |
n_iterations |
int
|
Number of iterations per run. |
step_mode |
StepMode
|
Perturbation mode - "percentage" (of domain range) or "fixed" (absolute step size). |
step_size |
float
|
Perturbation magnitude (interpretation depends on step_mode). |
opt_digits |
int
|
Decimal precision for optimization results. Use -1 for double precision. |
hash_digits |
int
|
Decimal precision for solution hashing. Solutions rounded to this precision are considered identical. |
bounded |
bool
|
Whether to enforce domain bounds during perturbation. |
minimizer_method |
str
|
Scipy minimizer method (default: "L-BFGS-B"). |
minimizer_options |
dict
|
Options passed to scipy.optimize.minimize. |
seed |
int | None
|
Random seed for reproducibility. |
Source code in src/lonpy/sampling.py
BasinHoppingSampler
¶
Basin-Hopping sampler for constructing Local Optima Networks.
Basin-Hopping is a global optimization algorithm that combines random perturbations with local minimization. This implementation records transitions between local optima for LON construction.
Example
config = BasinHoppingSamplerConfig(n_runs=10, n_iterations=1000) sampler = BasinHoppingSampler(config) lon = sampler.sample_to_lon(objective_func, domain)
Source code in src/lonpy/sampling.py
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 | |
sample(func: Callable[[np.ndarray], float], domain: list[tuple[float, float]], progress_callback: Callable[[int, int], None] | None = None) -> tuple[pd.DataFrame, list[dict]]
¶
Run Basin-Hopping sampling to generate LON data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[ndarray], float]
|
Objective function to minimize (f: R^n -> R). |
required |
domain
|
list[tuple[float, float]]
|
List of (lower, upper) bounds per dimension. |
required |
progress_callback
|
Callable[[int, int], None] | None
|
Optional callback(run, total_runs) for progress. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, list[dict]]
|
Tuple of (trace_df, raw_records): - trace_df: DataFrame with columns [run, fit1, node1, fit2, node2] ready for LON construction. - raw_records: List of dicts with detailed iteration data. |
Source code in src/lonpy/sampling.py
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 | |
sample_to_lon(func: Callable[[np.ndarray], float], domain: list[tuple[float, float]], progress_callback: Callable[[int, int], None] | None = None) -> LON
¶
Source code in src/lonpy/sampling.py
hash_solution(x: np.ndarray, fitness: float = 0.0) -> str
¶
Create hash string for a solution.
Creates a unique identifier for a local optimum based on rounded coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Solution coordinates. |
required |
fitness
|
float
|
Fitness value (unused, kept for API compatibility). |
0.0
|
Returns:
| Type | Description |
|---|---|
str
|
Hash string identifying the local optimum. |
Source code in src/lonpy/sampling.py
fitness_to_int(fitness: float) -> int
¶
Convert fitness to integer representation for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fitness
|
float
|
Floating-point fitness value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Scaled integer fitness value. |