-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathacquisition.py
More file actions
1211 lines (1115 loc) · 55 KB
/
acquisition.py
File metadata and controls
1211 lines (1115 loc) · 55 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
"""
References
.. [Daulton2026bonsai]
S. Daulton, D. Eriksson, M. Balandat, and E. Bakshy. BONSAI: Bayesian
Optimization with Natural Simplicity and Interpretability. ArXiv, 2026.
"""
from __future__ import annotations
import operator
from collections.abc import Callable, Mapping, Sequence
from functools import partial, reduce
from itertools import product
from logging import Logger
from typing import Any
import torch
from ax.core.search_space import SearchSpaceDigest
from ax.exceptions.core import AxError, DataRequiredError, SearchSpaceExhausted
from ax.generators.torch.botorch_modular.optimizer_argparse import optimizer_argparse
from ax.generators.torch.botorch_modular.surrogate import Surrogate
from ax.generators.torch.botorch_modular.utils import (
_fix_map_key_to_target,
_objective_threshold_to_outcome_constraints,
)
from ax.generators.torch.botorch_moo_utils import infer_objective_thresholds
from ax.generators.torch.utils import (
_get_X_pending_and_observed,
get_botorch_objective_and_transform,
subset_model,
)
from ax.generators.torch_base import TorchOptConfig
from ax.generators.utils import enumerate_discrete_combinations, mk_discrete_choices
from ax.utils.common.base import Base
from ax.utils.common.constants import Keys
from ax.utils.common.logger import get_logger
from botorch.acquisition.acquisition import AcquisitionFunction
from botorch.acquisition.input_constructors import get_acqf_input_constructor
from botorch.acquisition.knowledge_gradient import qKnowledgeGradient
from botorch.acquisition.logei import qLogProbabilityOfFeasibility
from botorch.acquisition.multioutput_acquisition import (
MultiOutputAcquisitionFunction,
MultiOutputAcquisitionFunctionWrapper,
)
from botorch.acquisition.objective import MCAcquisitionObjective, PosteriorTransform
from botorch.exceptions.errors import BotorchError, InputDataError
from botorch.generation.sampling import SamplingStrategy
from botorch.models.model import Model
from botorch.optim.optimize import (
optimize_acqf,
optimize_acqf_discrete,
optimize_acqf_discrete_local_search,
optimize_acqf_mixed,
)
from botorch.optim.optimize_mixed import (
MAX_CARDINALITY_FOR_LOCAL_SEARCH,
MAX_CHOICES_ENUMERATE,
optimize_acqf_mixed_alternating,
should_use_mixed_alternating_optimizer,
)
from botorch.optim.parameter_constraints import evaluate_feasibility
from botorch.utils.constraints import get_outcome_constraint_transforms
from pyre_extensions import assert_is_instance, none_throws
from torch import Tensor
try:
from botorch.utils.multi_objective.optimize import optimize_with_nsgaii
except ImportError:
optimize_with_nsgaii = None
logger: Logger = get_logger(__name__)
def determine_optimizer(
search_space_digest: SearchSpaceDigest,
acqf: AcquisitionFunction | None = None,
discrete_choices: Mapping[int, Sequence[float]] | None = None,
) -> str:
"""Determine the optimizer to use for a given search space.
Args:
search_space_digest: A SearchSpaceDigest object containing search space
properties, e.g. ``bounds`` for optimization.
acqf: The acquisition function to be used.
discrete_choices: A dictionary mapping indices of discrete (ordinal
or categorical) parameters to their respective sets of values
provided as a list. This excludes fixed features.
Returns:
The name of the optimizer to use for the given search space.
"""
if acqf is not None and isinstance(acqf, MultiOutputAcquisitionFunction):
return "optimize_with_nsgaii"
ssd = search_space_digest
discrete_features = sorted(ssd.ordinal_features + ssd.categorical_features)
if discrete_choices is None:
discrete_choices = {}
if len(discrete_features) == 0:
optimizer = "optimize_acqf"
else:
fully_discrete = len(discrete_choices) == len(ssd.feature_names)
if fully_discrete:
# One of the three optimizers may be used depending on the number of
# discrete choices and the cardinality of individual parameters.
# If there are less than `MAX_CHOICES_ENUMERATE` choices, we will
# evaluate all of them and pick the best.
# If there are less than `MAX_CARDINALITY_FOR_LOCAL_SEARCH` choices
# for all parameters, we will use local search. Otherwise, we will use
# the mixed alternating optimizer, which may use continuous relaxation
# for the high cardinality parameters, while using local search for
# the remaining parameters.
cardinalities = [len(c) for c in discrete_choices.values()]
max_cardinality = max(cardinalities)
total_discrete_choices = reduce(operator.mul, cardinalities)
if total_discrete_choices > MAX_CHOICES_ENUMERATE:
if max_cardinality <= MAX_CARDINALITY_FOR_LOCAL_SEARCH:
optimizer = "optimize_acqf_discrete_local_search"
else:
optimizer = "optimize_acqf_mixed_alternating"
else:
optimizer = "optimize_acqf_discrete"
else:
# For mixed (not fully discrete) search spaces, use the shared utility
# from BoTorch to determine whether to use mixed alternating optimizer.
if should_use_mixed_alternating_optimizer(discrete_dims=discrete_choices):
optimizer = "optimize_acqf_mixed_alternating"
else:
optimizer = "optimize_acqf_mixed"
return optimizer
class Acquisition(Base):
"""
**All classes in 'botorch_modular' directory are under
construction, incomplete, and should be treated as alpha
versions only.**
Ax wrapper for BoTorch `AcquisitionFunction`, subcomponent
of `BoTorchGenerator` and is not meant to be used outside of it.
Args:
surrogate: The Surrogate model, with which this acquisition
function will be used.
search_space_digest: A SearchSpaceDigest object containing metadata
about the search space (e.g. bounds, parameter types).
torch_opt_config: A TorchOptConfig object containing optimization
arguments (e.g., objective weights, constraints).
botorch_acqf_class: Type of BoTorch `AcquisitionFunction` that
should be used.
botorch_acqf_options: Optional mapping of kwargs to the underlying
`AcquisitionFunction` in BoTorch.
botorch_acqf_classes_with_options: A list of tuples of botorch
`AcquisitionFunction` classes and dicts of kwargs, passed to
the botorch `AcquisitionFunction`. This is used to specify
multiple acquisition functions to be used with MultiAcquisition.
n: The number of candidates that will be generated by this acquisition.
options: Optional mapping of kwargs to the underlying `Acquisition
Function` in BoTorch.
"""
surrogate: Surrogate
acqf: AcquisitionFunction
_model: Model
_objective_weights: Tensor
_objective_thresholds: Tensor | None
_outcome_constraints: tuple[Tensor, Tensor] | None
_learned_objective_preference_model: Model | None
_subset_idcs: Tensor | None
_pruning_target_point: Tensor | None
num_pruned_dims: list[int] | None
def __init__(
self,
surrogate: Surrogate,
search_space_digest: SearchSpaceDigest,
torch_opt_config: TorchOptConfig,
botorch_acqf_class: type[AcquisitionFunction] | None,
botorch_acqf_options: dict[str, Any] | None = None,
botorch_acqf_classes_with_options: list[
tuple[type[AcquisitionFunction], dict[str, Any]]
]
| None = None,
n: int | None = None,
options: dict[str, Any] | None = None,
) -> None:
self.surrogate = surrogate
self.options: dict[str, Any] = options or {}
botorch_acqf_options = botorch_acqf_options or {}
self.search_space_digest = search_space_digest
self.n = n
self._should_subset_model: bool = self.options.get(Keys.SUBSET_MODEL, True)
self._learned_objective_preference_model = None
self._subset_idcs = None
self._pruning_target_point = torch_opt_config.pruning_target_point
self.num_pruned_dims = None
# Extract pending and observed points.
# We fix MAP_KEY to the fixed value (if given) to avoid points getting
# discarded due to metrics being observed at different progressions.
Xs = _fix_map_key_to_target(
Xs=surrogate.Xs,
feature_names=search_space_digest.feature_names,
fixed_features=torch_opt_config.fixed_features,
)
X_pending, X_observed = _get_X_pending_and_observed(
Xs=Xs,
objective_weights=torch_opt_config.objective_weights,
bounds=search_space_digest.bounds,
pending_observations=torch_opt_config.pending_observations,
outcome_constraints=torch_opt_config.outcome_constraints,
linear_constraints=torch_opt_config.linear_constraints,
fixed_features=torch_opt_config.fixed_features,
)
self.X_pending: Tensor | None = X_pending
self.X_observed: Tensor | None = X_observed
# Store objective thresholds for all outcomes (including non-objectives).
self._full_objective_thresholds: Tensor | None = (
torch_opt_config.objective_thresholds
)
self._full_objective_weights: Tensor = torch_opt_config.objective_weights
(
self._model,
self._objective_weights,
self._outcome_constraints,
self._objective_thresholds,
) = self._subset_model(
model=surrogate.model,
objective_weights=torch_opt_config.objective_weights,
outcome_constraints=torch_opt_config.outcome_constraints,
objective_thresholds=torch_opt_config.objective_thresholds,
)
self._update_objective_thresholds(torch_opt_config=torch_opt_config)
self._set_preference_model(torch_opt_config=torch_opt_config)
if not botorch_acqf_classes_with_options:
if botorch_acqf_class is None:
raise AxError(
"One of botorch_acqf_class or botorch_acqf_classes_with_options"
" is required."
)
botorch_acqf_classes_with_options = [
(botorch_acqf_class, botorch_acqf_options)
]
self.botorch_acqf_classes_with_options: list[
tuple[type[AcquisitionFunction], dict[str, Any]]
] = none_throws(botorch_acqf_classes_with_options)
self._instantiate_acquisition()
def _subset_model(
self,
model: Model,
objective_weights: Tensor,
outcome_constraints: tuple[Tensor, Tensor] | None = None,
objective_thresholds: Tensor | None = None,
) -> tuple[Model, Tensor, tuple[Tensor, Tensor] | None, Tensor | None]:
if not self._should_subset_model:
return model, objective_weights, outcome_constraints, objective_thresholds
# Otherwise, subset
subset_model_results = subset_model(
model=model,
objective_weights=objective_weights,
outcome_constraints=outcome_constraints,
objective_thresholds=objective_thresholds,
)
if self._subset_idcs is None:
self._subset_idcs = subset_model_results.indices
elif not torch.equal(subset_model_results.indices, self._subset_idcs):
raise ValueError(
"Subsequent model subsetting inconsistent with earlier."
) # pragma: no cover
return (
subset_model_results.model,
subset_model_results.objective_weights,
subset_model_results.outcome_constraints,
subset_model_results.objective_thresholds,
)
def _update_objective_thresholds(self, torch_opt_config: TorchOptConfig) -> None:
"""If MOO and some objective thresholds are not specified, infer them using
the model that has already been subset to avoid re-subsetting it within
`infer_objective_thresholds`.
If risk measures are used, objective thresholds must be provided. If not,
this will error out.
If `infer_objective_thresholds` errors out, e.g., due to no feasible point,
this will log an error and let the optimization continue. Not all acquisition
functions require objective thresholds, so this is not necessarily a problem.
"""
if not (
torch_opt_config.is_moo
and (
self._full_objective_thresholds is None
or self._full_objective_thresholds[torch_opt_config.outcome_mask]
.isnan()
.any()
)
and self.X_observed is not None
):
return
try:
self._full_objective_thresholds = infer_objective_thresholds(
model=self._model,
objective_weights=self._full_objective_weights,
X_observed=self.X_observed,
outcome_constraints=torch_opt_config.outcome_constraints,
subset_idcs=self._subset_idcs,
objective_thresholds=self._full_objective_thresholds,
)
self._objective_thresholds = (
none_throws(self._full_objective_thresholds)[self._subset_idcs]
if self._subset_idcs is not None
else self._full_objective_thresholds
)
except (AxError, BotorchError) as e:
logger.warning(
"Failed to infer objective thresholds. Resuming optimization "
"without objective thresholds, which may or may not work depending "
f"on the acquisition function. Original error: {e}."
)
def _set_preference_model(self, torch_opt_config: TorchOptConfig) -> None:
if torch_opt_config.use_learned_objective:
if (Keys.PAIRWISE_PREFERENCE_QUERY.value,) not in self.surrogate._submodels:
raise DataRequiredError(
"PreferenceOptimizationConfig is used but missing "
"preference objective model. Double check if the preference "
"exploration auxiliary experiment has data."
)
self._learned_objective_preference_model = self.surrogate._submodels[
(Keys.PAIRWISE_PREFERENCE_QUERY.value,)
]
def _instantiate_acquisition(self) -> None:
"""Constructs the acquisition function based on the provided
botorch_acqf_classes_with_options.
"""
if len(self.botorch_acqf_classes_with_options) != 1:
raise ValueError("Only one botorch_acqf_class is supported.")
botorch_acqf_class, botorch_acqf_options = (
self.botorch_acqf_classes_with_options[0]
)
self.acqf = self._construct_botorch_acquisition(
botorch_acqf_class=botorch_acqf_class,
botorch_acqf_options=botorch_acqf_options,
model=self._model,
)
self.models_used = [self.surrogate.model_name_by_metric]
self.acq_function_sequence = None
def _construct_botorch_acquisition(
self,
botorch_acqf_class: type[AcquisitionFunction],
botorch_acqf_options: dict[str, Any],
model: Model,
) -> AcquisitionFunction:
objective, posterior_transform = self.get_botorch_objective_and_transform(
botorch_acqf_class=botorch_acqf_class,
model=model,
objective_weights=self._objective_weights,
outcome_constraints=self._outcome_constraints,
X_observed=self.X_observed,
learned_objective_preference_model=self._learned_objective_preference_model,
)
# Build constraint transforms, combining outcome constraints with
# objective threshold-derived constraints when using
# qLogProbabilityOfFeasibility for MOO.
outcome_constraints = self._outcome_constraints
constraint_transforms = get_outcome_constraint_transforms(
outcome_constraints=outcome_constraints
)
if (
issubclass(botorch_acqf_class, qLogProbabilityOfFeasibility)
and self._objective_thresholds is not None
):
threshold_constraints = _objective_threshold_to_outcome_constraints(
objective_weights=self._objective_weights,
objective_thresholds=self._objective_thresholds,
)
threshold_transforms = get_outcome_constraint_transforms(
outcome_constraints=threshold_constraints
)
if constraint_transforms is not None and threshold_transforms is not None:
constraint_transforms = constraint_transforms + threshold_transforms
elif threshold_transforms is not None:
constraint_transforms = threshold_transforms
input_constructor_kwargs = {
"model": model,
"X_baseline": self.X_observed,
"X_pending": self.X_pending,
"objective_thresholds": self._objective_thresholds,
"constraints": constraint_transforms,
"constraints_tuple": self._outcome_constraints,
"objective": objective,
"posterior_transform": posterior_transform,
**botorch_acqf_options,
}
target_fidelities = {
k: v
for k, v in self.search_space_digest.target_values.items()
if k in self.search_space_digest.fidelity_features
}
if len(target_fidelities) > 0:
input_constructor_kwargs["target_fidelities"] = target_fidelities
input_constructor = get_acqf_input_constructor(botorch_acqf_class)
# Extract the training data from the surrogate.
# If there is a single dataset, this will be the dataset itself.
# If there are multiple datasets, this will be a dict mapping the outcome names
# to the corresponding datasets.
training_data = self.surrogate.training_data
if len(training_data) == 1:
training_data = training_data[0]
else:
training_data = dict(
zip(none_throws(self.surrogate._outcomes), training_data)
)
acqf_inputs = input_constructor(
training_data=training_data,
bounds=self.search_space_digest.bounds,
**{k: v for k, v in input_constructor_kwargs.items() if v is not None},
)
return botorch_acqf_class(**acqf_inputs) # pyre-ignore [45]
@property
def botorch_acqf_class(self) -> type[AcquisitionFunction]:
"""BoTorch ``AcquisitionFunction`` class underlying this ``Acquisition``."""
return self.acqf.__class__
@property
def dtype(self) -> torch.dtype | None:
"""Torch data type of the tensors in the training data used in the model,
of which this ``Acquisition`` is a subcomponent.
"""
return self.surrogate.dtype
@property
def device(self) -> torch.device | None:
"""Torch device type of the tensors in the training data used in the model,
of which this ``Acquisition`` is a subcomponent.
"""
return self.surrogate.device
@property
def objective_thresholds(self) -> Tensor | None:
"""The objective thresholds for all outcomes.
For non-objective outcomes, the objective thresholds are nans.
"""
return self._full_objective_thresholds
@property
def objective_weights(self) -> Tensor | None:
"""The objective weights for all outcomes."""
return self._full_objective_weights
def select_from_candidate_set(
self,
n: int,
candidate_set: Tensor,
sampling_strategy: SamplingStrategy | None = None,
) -> tuple[Tensor, Tensor, Tensor]:
"""Select n candidates from a discrete set with optional weight allocation.
This method selects candidates from ``candidate_set`` using either a
``SamplingStrategy`` (e.g., Thompson Sampling with win-counting for weight
allocation) or greedy acquisition function optimization.
``candidate_set`` is the stable interface for any candidate generation
method. Any method that produces candidates (in-sample training data,
pathwise TS optimization, user-provided sets, etc.) feeds into this
parameter. The selection/weight-allocation logic is agnostic to how
candidates were generated.
Args:
n: The number of candidates to select.
candidate_set: A ``(num_choices, d)`` tensor of discrete candidate
points to select from.
sampling_strategy: An optional BoTorch ``SamplingStrategy`` instance
(e.g., ``MaxPosteriorSampling`` for Thompson Sampling, or
``BoltzmannSampling`` for acquisition-weighted sampling). When
provided, candidates are selected by sampling from ``candidate_set``
according to the strategy. When ``num_samples > n``, win-counting
mode is used: many posterior samples are drawn, wins are counted
per candidate, and the top-n candidates are returned with weights
proportional to their win probability (normalized to sum to 1).
If not provided, greedy acquisition function selection is used via
``optimize_acqf_discrete``.
Returns:
A three-element tuple containing an ``n x d``-dim tensor of selected
candidates, a tensor with the associated acquisition values, and a
tensor with the weight for each candidate (normalized to sum to 1
for win-counting mode, or uniform for direct/greedy selection).
Raises:
ValueError: If ``candidate_set`` is empty or has fewer points than
``n``.
"""
if candidate_set.shape[0] == 0:
raise ValueError(
"`candidate_set` is empty. Provide a non-empty set of candidates."
)
if candidate_set.shape[0] < n:
raise ValueError(
f"`candidate_set` has {candidate_set.shape[0]} candidates, "
f"but {n} were requested. Provide at least {n} candidates."
)
if sampling_strategy is not None:
# Check if this is a win-counting strategy (e.g., Thompson Sampling)
# or a direct selection strategy (e.g., Boltzmann Sampling).
# If num_samples is explicitly set and > n, use win-counting mode.
# Otherwise, use direct selection mode.
num_samples_attr = getattr(sampling_strategy, "num_samples", None)
num_samples: int | None = (
int(num_samples_attr) if num_samples_attr is not None else None
)
if num_samples is not None and num_samples > n:
# Win-counting mode: sample many times, count wins, return top-n
# with weights proportional to win counts (normalized to sum to 1).
sampled_candidates = sampling_strategy(
candidate_set.unsqueeze(0), num_samples=num_samples
).squeeze(0) # (num_samples, d)
# Count wins for each unique candidate
unique_candidates, inverse_indices = torch.unique(
sampled_candidates, dim=0, return_inverse=True
)
counts = torch.bincount(
inverse_indices, minlength=unique_candidates.shape[0]
)
# Select top-n candidates by win count.
# When num_unique < n (fewer unique winners than requested),
# we return all unique winners. The caller should handle
# candidates.shape[0] <= n, consistent with
# optimize_acqf_discrete which may also return fewer than n.
num_unique = unique_candidates.shape[0]
top_n = min(n, num_unique)
top_counts, top_indices = torch.topk(counts, top_n)
candidates = unique_candidates[top_indices]
arm_weights = top_counts.to(dtype=self.dtype, device=self.device)
arm_weights = arm_weights / arm_weights.sum()
else:
# Direct selection mode: sample exactly n candidates with equal
# weights. Used for strategies like BoltzmannSampling where
# weighting is built into the selection process.
sampled_candidates = sampling_strategy(
candidate_set.unsqueeze(0), num_samples=n
).squeeze(0) # (n, d)
candidates = sampled_candidates
arm_weights = torch.ones(n, dtype=self.dtype, device=self.device)
acqf_values = self.evaluate(candidates.unsqueeze(1)).view(-1)
return candidates, acqf_values, arm_weights
# Greedy selection from provided discrete candidate set via acqf.
# optimize_acqf_discrete may return fewer than n candidates when
# there are fewer feasible choices; arm_weights matches actual count.
candidates, acqf_values = optimize_acqf_discrete(
acq_function=self.acqf,
q=n,
choices=candidate_set,
unique=True,
)
arm_weights = torch.ones(
candidates.shape[0], dtype=self.dtype, device=self.device
)
return candidates, acqf_values, arm_weights
def optimize(
self,
n: int,
search_space_digest: SearchSpaceDigest,
inequality_constraints: list[tuple[Tensor, Tensor, float]] | None = None,
fixed_features: dict[int, float] | None = None,
rounding_func: Callable[[Tensor], Tensor] | None = None,
optimizer_options: dict[str, Any] | None = None,
candidate_set: Tensor | None = None,
sampling_strategy: SamplingStrategy | None = None,
) -> tuple[Tensor, Tensor, Tensor]:
"""Generate a set of candidates via multi-start optimization. Obtains
candidates and their associated acquisition function values.
Args:
n: The number of candidates to generate.
search_space_digest: A ``SearchSpaceDigest`` object containing search space
properties, e.g. ``bounds`` for optimization.
inequality_constraints: A list of tuples (indices, coefficients, rhs),
with each tuple encoding an inequality constraint of the form
``sum_i (X[indices[i]] * coefficients[i]) >= rhs``.
fixed_features: A map `{feature_index: value}` for features that
should be fixed to a particular value during generation.
rounding_func: A function that post-processes an optimization
result appropriately. This is typically passed down from
`Adapter` to ensure compatibility of the candidates with
with Ax transforms. For additional post processing, use
`post_processing_func` option in `optimizer_options`.
optimizer_options: Options for the optimizer function, e.g. ``sequential``
or ``raw_samples``. This can also include a `post_processing_func`
which is applied to the candidates before the `rounding_func`.
`post_processing_func` can be used to support more customized options
that typically only exist in MBM, such as BoTorch transforms.
See the docstring of `TorchOptConfig` for more information on passing
down these options while constructing a generation strategy.
candidate_set: An optional tensor of shape `(num_choices, d)` containing
discrete candidate points to select from instead of optimizing over
the search space. When provided, selection is delegated to
``select_from_candidate_set``. This enables in-sample candidate
generation when set to the training data (X_observed).
sampling_strategy: An optional BoTorch ``SamplingStrategy`` instance
(e.g., ``MaxPosteriorSampling`` for Thompson Sampling, or
``BoltzmannSampling`` for acquisition-weighted sampling).
Passed to ``select_from_candidate_set`` when ``candidate_set``
is provided. Requires ``candidate_set`` to be provided.
Returns:
A three-element tuple containing an `n x d`-dim tensor of generated
candidates, a tensor with the associated acquisition values, and a tensor
with the weight for each candidate.
"""
# Dispatch to candidate set selection if candidate_set or
# sampling_strategy is provided.
if sampling_strategy is not None or candidate_set is not None:
if candidate_set is None:
raise ValueError(
"`candidate_set` is required when using `sampling_strategy`. "
"Provide the discrete set of candidates to sample from."
)
return self.select_from_candidate_set(
n=n,
candidate_set=candidate_set,
sampling_strategy=sampling_strategy,
)
# Options that would need to be passed in the transformed space are
# disallowed, since this would be very difficult for an end user to do
# directly, and someone who uses BoTorch at this level of detail would
# probably be better off using BoTorch directly.
# `return_best_only` and `return_full_tree` are disallowed because
# Ax expects `optimize_acqf` to return tensors of a certain shape.
if optimizer_options is not None:
forbidden_optimizer_options = [
"equality_constraints",
"inequality_constraints", # These should be constructed by Ax
"batch_initial_conditions",
"return_best_only",
"return_full_tree",
]
for kw in optimizer_options:
if kw in forbidden_optimizer_options:
raise ValueError(
f"Argument {kw} is not allowed in `optimizer_options`."
)
_tensorize = partial(torch.tensor, dtype=self.dtype, device=self.device)
ssd = search_space_digest
bounds = _tensorize(ssd.bounds).t()
discrete_choices = mk_discrete_choices(ssd=ssd, fixed_features=fixed_features)
optimizer = determine_optimizer(
search_space_digest=ssd,
discrete_choices=discrete_choices,
acqf=self.acqf,
)
# `raw_samples` and `num_restarts` are not supported by
# `optimize_acqf_discrete`.
if (
optimizer in ("optimize_acqf_discrete", "optimize_with_nsgaii")
and optimizer_options is not None
):
optimizer_options.pop("raw_samples", None)
optimizer_options.pop("num_restarts", None)
# Prepare arguments for optimizer
optimizer_options_with_defaults = optimizer_argparse(
self.acqf,
optimizer_options=optimizer_options,
optimizer=optimizer,
)
if fixed_features is not None:
for i in fixed_features:
if not 0 <= i < len(ssd.feature_names):
raise ValueError(f"Invalid fixed_feature index: {i}")
# Return a weight of 1 for each arm by default. This can be
# customized in subclasses if necessary.
arm_weights = torch.ones(n, dtype=self.dtype)
# 1. Handle the fully continuous search space.
if optimizer == "optimize_acqf":
candidates, acqf_values = optimize_acqf(
acq_function=self.acqf,
bounds=bounds,
q=n,
inequality_constraints=inequality_constraints,
fixed_features=fixed_features,
post_processing_func=rounding_func,
acq_function_sequence=self.acq_function_sequence,
**optimizer_options_with_defaults,
)
# 2. Handle fully discrete search spaces.
elif optimizer in (
"optimize_acqf_discrete",
"optimize_acqf_discrete_local_search",
):
X_observed = self.X_observed
if self.X_pending is not None:
if X_observed is None:
X_observed = self.X_pending
else:
X_observed = torch.cat([X_observed, self.X_pending], dim=0)
# Special handling for search spaces with a large number of choices
if optimizer == "optimize_acqf_discrete_local_search":
discrete_choices = [
torch.tensor(c, device=self.device, dtype=self.dtype)
for c in discrete_choices.values()
]
candidates, acqf_values = optimize_acqf_discrete_local_search(
acq_function=self.acqf,
q=n,
discrete_choices=discrete_choices,
inequality_constraints=inequality_constraints,
X_avoid=X_observed,
**optimizer_options_with_defaults,
)
n_candidates = candidates.shape[0]
return (
candidates,
acqf_values,
arm_weights[:n_candidates] * n_candidates / n,
)
# Else, optimizer is `optimize_acqf_discrete`
# Enumerate all possible choices
all_choices = (discrete_choices[i] for i in range(len(discrete_choices)))
all_choices = _tensorize(tuple(product(*all_choices)))
try:
max_batch_size = optimizer_options_with_defaults.pop(
"max_batch_size", 2048
)
try:
# Adapt max batch size for batched models to reduce peak memory.
max_batch_size = (
max_batch_size // self.surrogate.model.batch_shape.numel()
)
except Exception: # pragma: no cover
pass # Do not error out if the model does not have a batch shape.
candidates, acqf_values = optimize_acqf_discrete(
acq_function=self.acqf,
q=n,
choices=all_choices,
max_batch_size=max_batch_size,
X_avoid=X_observed,
inequality_constraints=inequality_constraints,
**optimizer_options_with_defaults,
)
except InputDataError:
raise SearchSpaceExhausted(
"No more feasible choices in a fully discrete search space."
)
# 3. Handle mixed search spaces that have discrete and continuous features.
elif optimizer == "optimize_acqf_mixed":
candidates, acqf_values = optimize_acqf_mixed(
acq_function=self.acqf,
bounds=bounds,
q=n,
fixed_features_list=enumerate_discrete_combinations(
discrete_choices=discrete_choices
),
inequality_constraints=inequality_constraints,
post_processing_func=rounding_func,
**optimizer_options_with_defaults,
)
elif optimizer == "optimize_acqf_mixed_alternating":
# NOTE: We intentially use `ssd.discrete_choices` as opposed to
# `discrete_choices`. This optimizer checks whether `discrete_dims` and
# `cat_dims` match with the bounds. However, `discrete_choices` has
# overridden some values in `ssd.discrete_choices` using `fixed_features`,
# which would fail the check. This optimizer is able to handle fixed
# features itself. No need for overriding.
candidates, acqf_values = optimize_acqf_mixed_alternating(
acq_function=self.acqf,
bounds=bounds,
discrete_dims={
k: list(v)
for k, v in ssd.discrete_choices.items()
if k in ssd.ordinal_features
},
cat_dims={
k: list(v)
for k, v in ssd.discrete_choices.items()
if k in ssd.categorical_features
},
q=n,
post_processing_func=rounding_func,
fixed_features=fixed_features,
inequality_constraints=inequality_constraints,
**optimizer_options_with_defaults,
)
elif optimizer == "optimize_with_nsgaii":
if optimize_with_nsgaii is not None:
acqf = assert_is_instance(
self.acqf, MultiOutputAcquisitionFunctionWrapper
)
candidates, acqf_values = optimize_with_nsgaii(
acq_function=self.acqf,
bounds=bounds,
q=n,
fixed_features=fixed_features,
inequality_constraints=inequality_constraints,
num_objectives=len(acqf.acqfs),
discrete_choices=discrete_choices if discrete_choices else None,
post_processing_func=rounding_func,
**optimizer_options_with_defaults,
)
else:
raise AxError(
"optimize_with_nsgaii requires botorch to be installed with "
"the pymoo."
)
else:
raise AxError( # pragma: no cover
f"Unknown optimizer: {optimizer}. This code should be unreachable."
)
# prune irrelevant parameters post-hoc
if self.options.get("prune_irrelevant_parameters", False) and not isinstance(
self.acqf, qLogProbabilityOfFeasibility
):
if self._pruning_target_point is None:
logger.info(
"Must specify pruning_target_point to prune irrelevant "
"parameters. Skipping pruning irrelevant parameters."
)
else:
candidates, acqf_values = self._prune_irrelevant_parameters(
candidates=candidates,
search_space_digest=search_space_digest,
inequality_constraints=inequality_constraints,
fixed_features=fixed_features,
)
n_candidates = candidates.shape[0]
return candidates, acqf_values, arm_weights[:n_candidates] * n_candidates / n
def evaluate(self, X: Tensor) -> Tensor:
"""Evaluate the acquisition function on the candidate set `X`.
Args:
X: A `batch_shape x q x d`-dim Tensor of t-batches with `q` `d`-dim design
points each.
Returns:
A `batch_shape'`-dim Tensor of acquisition values at the given
design points `X`, where `batch_shape'` is the broadcasted batch shape of
model and input `X`.
"""
if isinstance(self.acqf, qKnowledgeGradient):
return self.acqf.evaluate(X=X)
else:
# NOTE: `AcquisitionFunction.__call__` calls `forward`,
# so below is equivalent to `self.acqf.forward(X=X)`.
return self.acqf(X=X)
def get_botorch_objective_and_transform(
self,
botorch_acqf_class: type[AcquisitionFunction],
model: Model,
objective_weights: Tensor,
outcome_constraints: tuple[Tensor, Tensor] | None = None,
X_observed: Tensor | None = None,
learned_objective_preference_model: Model | None = None,
) -> tuple[MCAcquisitionObjective | None, PosteriorTransform | None]:
return get_botorch_objective_and_transform(
botorch_acqf_class=botorch_acqf_class,
model=model,
objective_weights=objective_weights,
outcome_constraints=outcome_constraints,
X_observed=X_observed,
learned_objective_preference_model=learned_objective_preference_model,
)
def _condition_on_prev_candidates(
self, prev_candidates: Tensor, initial_X_pending: Tensor | None
) -> None:
"""Condition the acquisition function on the candidates.
Args:
prev_candidates: A `q' x d`-dim Tensor of candidates.
initial_X_pending: A `q'' x d`-dim Tensor of pending points. These are
points that are the initial pending evaluations and should be added
to the `X_pending` of the acquisition function.
"""
if prev_candidates.shape[0] > 0:
if initial_X_pending is not None and initial_X_pending.shape[0]:
self.X_pending = torch.cat([initial_X_pending, prev_candidates], dim=0)
else:
self.X_pending = prev_candidates
self._instantiate_acquisition()
def _compute_baseline_acqf_value(
self, last_candidate: Tensor, fixed_features: dict[int, float] | None = None
) -> float:
r"""Compute the baseline acquisition function value.
If this is the first point, the baseline AF value is the max AF value
the previously evaluated points. The otherwise the baseline value is the
acquisition value of the last point after conditioning on the previously
selected points including the last candidate. For incremental AFs, this is
will be near zero. For non-incremental AFs, this will be the AF value of the
the batch consisting of the previously selected points (since they are
pending).
Args:
last_candidate: A `1 x d`-dim Tensor containing the last candidate.
fixed_features: A map `{feature_index: value}` for features that
should be fixed to a particular value during generation.
Returns:
The baseline acquisition function value.
"""
if last_candidate.shape[0] > 0:
X = last_candidate
elif self.X_observed is not None:
# if this is the first candidate, compute the acquisition value for
# the previously evaluated set of points. Take the max and compute
# the incremental value with respect to that baseline value.
X = self.X_observed.unsqueeze(1)
if fixed_features is not None:
for idx, v in fixed_features.items():
X[..., idx] = v
else:
return 0.0
with torch.no_grad():
base_af_val = self.evaluate(X=X)
base_af_val = base_af_val.exp() if self.acqf._log else base_af_val
return base_af_val.max().item()
def _prune_irrelevant_parameters(
self,
candidates: Tensor,
search_space_digest: SearchSpaceDigest,
inequality_constraints: list[tuple[Tensor, Tensor, float]] | None = None,
fixed_features: dict[int, float] | None = None,
) -> tuple[Tensor, Tensor]:
r"""Prune irrelevant parameters from the candidates using BONSAI.
See [Daulton2026bonsai]_ for details.
The method involves first optimizing the AF without any notion of irrelevance.
Then, the irrelevant parameters are pruned via a sequential greedy algorithm
that sets each dimension to the target point value, and recomputes the AF value.
The dimension that corresponds to the smallest reduction in AF value compared to
the AF value of the dense original candidate is then set to the target value,
and the algorithm proceeds until removing any dimension causes the reduction in
the AF value of the proposed point to be less than some tolerance, relative to
the original dense point.
For `q > 1` cases, this is repeated for each point in the batch, after adding
the previously pruned points as pending points. The incremental AF value is
used in the stopping rule.
Args:
candidates: A `q x d`-dim Tensor of candidates.
search_space_digest: The SearchSpaceDigest.
inequality_constraints: A list of tuples (indices, coefficients, rhs),
with each tuple encoding an inequality constraint of the form
`\sum_i (X[indices[i]] * coefficients[i]) >= rhs`. `indices` and
`coefficients` should be torch tensors. See the docstring of
`make_scipy_linear_constraints` for an example. When q=1, or when
applying the same constraint to each candidate in the batch
(intra-point constraint), `indices` should be a 1-d tensor.
For inter-point constraints, in which the constraint is applied to the
whole batch of candidates, `indices` must be a 2-d tensor, where
in each row `indices[i] =(k_i, l_i)` the first index `k_i` corresponds
to the `k_i`-th element of the `q`-batch and the second index `l_i`
corresponds to the `l_i`-th feature of that element.
fixed_features: A map `{feature_index: value}` for features that
should be fixed to a particular value during generation.
Returns:
A two-element tuple containing an `q x d`-dim tensor of generated