Sampling Module¶
compute_lon(func: Callable[[np.ndarray], float], dim: int, lower_bound: float | Sequence[float], upper_bound: float | Sequence[float], seed: int | None = None, step_size: float = 0.01, step_mode: StepMode = 'fixed', n_runs: int = 100, max_perturbations_without_improvement: int = 1000, fitness_precision: int | None = None, coordinate_precision: int | None = 5, bounded: bool = True, initial_points: np.ndarray | None = None, lon_config: LONConfig | None = None) -> 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, where x is in R^n_var. |
required |
dim
|
int
|
Number of dimensions (n_var). |
required |
lower_bound
|
float | Sequence[float]
|
Lower bound (scalar or per-dimension list/array). |
required |
upper_bound
|
float | Sequence[float]
|
Upper bound (scalar or per-dimension list/array). |
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". |
'fixed'
|
n_runs
|
int
|
Number of independent Basin-Hopping runs. |
100
|
max_perturbations_without_improvement
|
int
|
Maximum number of consecutive non-improving perturbations before stopping each run. |
1000
|
fitness_precision
|
int | None
|
Decimal precision for fitness values (None for full double). Passing negative values behaves the same as passing None. |
None
|
coordinate_precision
|
int | None
|
Decimal precision for coordinate hashing (None for no rounding). Passing negative values behaves the same as passing None. |
5
|
bounded
|
bool
|
Whether to enforce domain bounds. |
True
|
initial_points
|
ndarray | None
|
Optional array of shape (n_runs, dim) with starting points for each run. If None, points are sampled uniformly at random from the domain. |
None
|
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.
Default values have been set to match the paper Jason Adair, Gabriela Ochoa, and Katherine M. Malan. 2019. Local optima networks for continuous fitness landscapes. In Proceedings of the Genetic and Evolutionary Computation Conference Companion (GECCO '19). Association for Computing Machinery, New York, NY, USA, 1407-1414. https://doi.org/10.1145/3319619.3326852
Attributes:
| Name | Type | Description |
|---|---|---|
n_runs |
int
|
Number of independent Basin-Hopping runs. |
max_perturbations_without_improvement |
int
|
Number of perturbations without improvement before stopping. |
step_mode |
StepMode
|
Perturbation mode - "percentage" (of domain range) or "fixed" (absolute step size). |
step_size |
float
|
Perturbation magnitude (interpretation depends on step_mode). |
fitness_precision |
int | None
|
Decimal precision for fitness values. Use None for full double precision. Passing negative values behaves the same as passing None. |
coordinate_precision |
int | None
|
Decimal precision for coordinate rounding and hashing. Solutions rounded to this precision are considered identical. Use None for full double precision (no rounding). Passing negative values behaves the same as passing None. |
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, max_perturbations_without_improvement=1000) sampler = BasinHoppingSampler(config) lon = sampler.sample_to_lon(objective_func, domain)
Source code in src/lonpy/sampling.py
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 | |
sample(func: Callable[[np.ndarray], float], domain: list[tuple[float, float]], initial_points: np.ndarray | None = None, progress_callback: Callable[[int, int], None] | None = None) -> tuple[pd.DataFrame, list[dict]]
¶
Run Basin-Hopping sampling and construct trace data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[ndarray], float]
|
Objective function to minimize (f: R^n_var -> R). |
required |
domain
|
list[tuple[float, float]]
|
List of (lower, upper) bounds per dimension. |
required |
initial_points
|
ndarray | None
|
Optional array of shape (n_runs, n_var) with starting points for each run. If None, points are sampled uniformly at random from the domain. |
None
|
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] - raw_records: List of dicts with detailed iteration data. |