bayesian-optimization 3.2.1__tar.gz → 3.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/PKG-INFO +1 -1
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/bayesian_optimization.py +37 -45
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/pyproject.toml +1 -1
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/LICENSE +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/README.md +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/__init__.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/acquisition.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/constraint.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/domain_reduction.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/exception.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/logger.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/parameter.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/py.typed +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/target_space.py +0 -0
- {bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/util.py +0 -0
{bayesian_optimization-3.2.1 → bayesian_optimization-3.3.0}/bayes_opt/bayesian_optimization.py
RENAMED
|
@@ -12,7 +12,6 @@ from collections.abc import Iterable
|
|
|
12
12
|
from os import PathLike
|
|
13
13
|
from pathlib import Path
|
|
14
14
|
from typing import TYPE_CHECKING, Any
|
|
15
|
-
from warnings import warn
|
|
16
15
|
|
|
17
16
|
import numpy as np
|
|
18
17
|
from scipy.optimize import NonlinearConstraint
|
|
@@ -27,7 +26,7 @@ from bayes_opt.target_space import TargetSpace
|
|
|
27
26
|
from bayes_opt.util import ensure_rng
|
|
28
27
|
|
|
29
28
|
if TYPE_CHECKING:
|
|
30
|
-
from collections.abc import Callable
|
|
29
|
+
from collections.abc import Callable
|
|
31
30
|
|
|
32
31
|
from numpy.random import RandomState
|
|
33
32
|
from numpy.typing import NDArray
|
|
@@ -86,7 +85,7 @@ class BayesianOptimization:
|
|
|
86
85
|
def __init__(
|
|
87
86
|
self,
|
|
88
87
|
f: Callable[..., float] | None,
|
|
89
|
-
pbounds:
|
|
88
|
+
pbounds: BoundsMapping,
|
|
90
89
|
acquisition_function: AcquisitionFunction | None = None,
|
|
91
90
|
constraint: NonlinearConstraint | None = None,
|
|
92
91
|
random_state: int | RandomState | None = None,
|
|
@@ -138,8 +137,6 @@ class BayesianOptimization:
|
|
|
138
137
|
raise TypeError(msg)
|
|
139
138
|
self._bounds_transformer.initialize(self._space)
|
|
140
139
|
|
|
141
|
-
self._sorting_warning_already_shown = False # TODO: remove in future version
|
|
142
|
-
|
|
143
140
|
# Initialize logger
|
|
144
141
|
self.logger = ScreenLogger(verbose=self._verbose, is_constrained=self.is_constrained)
|
|
145
142
|
|
|
@@ -278,17 +275,6 @@ class BayesianOptimization:
|
|
|
278
275
|
constraint_value: float or None
|
|
279
276
|
Value of the constraint function at the observation, if any.
|
|
280
277
|
"""
|
|
281
|
-
# TODO: remove in future version
|
|
282
|
-
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
|
|
283
|
-
msg = (
|
|
284
|
-
"You're attempting to register an np.ndarray. In previous versions, the optimizer internally"
|
|
285
|
-
" sorted parameters by key and expected any registered array to respect this order."
|
|
286
|
-
" In the current and any future version the order as given by the pbounds dictionary will be"
|
|
287
|
-
" used. If you wish to retain sorted parameters, please manually sort your pbounds"
|
|
288
|
-
" dictionary before constructing the optimizer."
|
|
289
|
-
)
|
|
290
|
-
warn(msg, stacklevel=1)
|
|
291
|
-
self._sorting_warning_already_shown = True
|
|
292
278
|
self._space.register(params, target, constraint_value)
|
|
293
279
|
self.logger.log_optimization_step(
|
|
294
280
|
self._space.keys, self._space.res()[-1], self._space.params_config, self.max
|
|
@@ -308,18 +294,6 @@ class BayesianOptimization:
|
|
|
308
294
|
If True, the optimizer will evaluate the points when calling
|
|
309
295
|
maximize(). Otherwise it will evaluate it at the moment.
|
|
310
296
|
"""
|
|
311
|
-
# TODO: remove in future version
|
|
312
|
-
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
|
|
313
|
-
msg = (
|
|
314
|
-
"You're attempting to register an np.ndarray. In previous versions, the optimizer internally"
|
|
315
|
-
" sorted parameters by key and expected any registered array to respect this order."
|
|
316
|
-
" In the current and any future version the order as given by the pbounds dictionary will be"
|
|
317
|
-
" used. If you wish to retain sorted parameters, please manually sort your pbounds"
|
|
318
|
-
" dictionary before constructing the optimizer."
|
|
319
|
-
)
|
|
320
|
-
warn(msg, stacklevel=1)
|
|
321
|
-
self._sorting_warning_already_shown = True
|
|
322
|
-
params = self._space.array_to_params(params)
|
|
323
297
|
if lazy:
|
|
324
298
|
self._queue.append(params)
|
|
325
299
|
else:
|
|
@@ -432,13 +406,13 @@ class BayesianOptimization:
|
|
|
432
406
|
params["kernel"] = wrap_kernel(kernel=params["kernel"], transform=self._space.kernel_transform)
|
|
433
407
|
self._gp.set_params(**params)
|
|
434
408
|
|
|
435
|
-
def
|
|
436
|
-
"""
|
|
409
|
+
def _state_to_dict(self) -> dict[str, Any]:
|
|
410
|
+
"""Convert optimizer state to a dictionary.
|
|
437
411
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
412
|
+
Returns
|
|
413
|
+
-------
|
|
414
|
+
dict
|
|
415
|
+
Dictionary containing the complete optimizer state.
|
|
442
416
|
"""
|
|
443
417
|
random_state = None
|
|
444
418
|
if self._random_state is not None:
|
|
@@ -454,7 +428,7 @@ class BayesianOptimization:
|
|
|
454
428
|
# Get constraint values if they exist
|
|
455
429
|
constraint_values = self._space._constraint_values.tolist() if self.is_constrained else None
|
|
456
430
|
acquisition_params = self._acquisition_function.get_acquisition_params()
|
|
457
|
-
|
|
431
|
+
return {
|
|
458
432
|
"pbounds": {key: self._space._bounds[i].tolist() for i, key in enumerate(self._space.keys)},
|
|
459
433
|
# Add current transformed bounds if using bounds transformer
|
|
460
434
|
"transformed_bounds": (self._space.bounds.tolist() if self._bounds_transformer else None),
|
|
@@ -474,20 +448,14 @@ class BayesianOptimization:
|
|
|
474
448
|
"acquisition_params": acquisition_params,
|
|
475
449
|
}
|
|
476
450
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
def load_state(self, path: str | PathLike[str]) -> None:
|
|
481
|
-
"""Load optimizer state from a JSON file.
|
|
451
|
+
def _load_state_dict(self, state: dict[str, Any]) -> None:
|
|
452
|
+
"""Load optimizer state from a dictionary.
|
|
482
453
|
|
|
483
454
|
Parameters
|
|
484
455
|
----------
|
|
485
|
-
|
|
486
|
-
|
|
456
|
+
state : dict
|
|
457
|
+
Dictionary containing the optimizer state.
|
|
487
458
|
"""
|
|
488
|
-
with Path(path).open("r") as file:
|
|
489
|
-
state = json.load(file)
|
|
490
|
-
|
|
491
459
|
params_array = np.asarray(state["params"], dtype=np.float64)
|
|
492
460
|
target_array = np.asarray(state["target"], dtype=np.float64)
|
|
493
461
|
constraint_array = (
|
|
@@ -530,3 +498,27 @@ class BayesianOptimization:
|
|
|
530
498
|
state["random_state"]["cached_gaussian"],
|
|
531
499
|
)
|
|
532
500
|
self._random_state.set_state(random_state_tuple)
|
|
501
|
+
|
|
502
|
+
def save_state(self, path: str | PathLike[str]) -> None:
|
|
503
|
+
"""Save complete state for reconstruction of the optimizer.
|
|
504
|
+
|
|
505
|
+
Parameters
|
|
506
|
+
----------
|
|
507
|
+
path : str or PathLike
|
|
508
|
+
Path to save the optimization state
|
|
509
|
+
"""
|
|
510
|
+
state = self._state_to_dict()
|
|
511
|
+
with Path(path).open("w") as f:
|
|
512
|
+
json.dump(state, f, indent=2)
|
|
513
|
+
|
|
514
|
+
def load_state(self, path: str | PathLike[str]) -> None:
|
|
515
|
+
"""Load optimizer state from a JSON file.
|
|
516
|
+
|
|
517
|
+
Parameters
|
|
518
|
+
----------
|
|
519
|
+
path : str or PathLike
|
|
520
|
+
Path to the JSON file containing the optimizer state.
|
|
521
|
+
"""
|
|
522
|
+
with Path(path).open("r") as file:
|
|
523
|
+
state = json.load(file)
|
|
524
|
+
self._load_state_dict(state)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|