-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathtransition_criterion.py
More file actions
827 lines (718 loc) · 32.2 KB
/
transition_criterion.py
File metadata and controls
827 lines (718 loc) · 32.2 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
# 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
from __future__ import annotations
from abc import abstractmethod
from typing import TYPE_CHECKING
from ax.core import MultiObjectiveOptimizationConfig
from ax.core.auxiliary import AuxiliaryExperiment, AuxiliaryExperimentPurpose
from ax.core.experiment import Experiment
from ax.core.trial_status import TrialStatus
from ax.core.utils import get_trial_indices_with_required_metrics
from ax.exceptions.core import DataRequiredError, UserInputError
from ax.exceptions.generation_strategy import (
AxGenerationException,
MaxParallelismReachedException,
)
if TYPE_CHECKING:
from ax.generation_strategy.generation_node import GenerationNode
from ax.utils.common.base import SortableBase
from ax.utils.common.serialization import serialize_init_args
DATA_REQUIRED_MSG = (
"All trials for current node {node_name} have been generated, "
"but not enough data has been observed to proceed to the next "
"Generation node. Try again when more data is available."
)
# ============================================================================
# Trial Counting Utility Functions
# ============================================================================
def get_trials_by_statuses(
experiment: Experiment, statuses: list[TrialStatus]
) -> set[int]:
"""Get trial indices from the experiment with the specified statuses.
Args:
experiment: The experiment to query.
statuses: The trial statuses to filter on.
Returns:
Set of trial indices with the specified statuses.
"""
trials_with_statuses = set()
for status in statuses:
trials_with_statuses = trials_with_statuses.union(
experiment.trial_indices_by_status[status]
)
return trials_with_statuses
def filter_trials_by_status(
experiment: Experiment,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
) -> set[int]:
"""Filter trial indices by status inclusion/exclusion.
Args:
experiment: The experiment to query.
only_in_statuses: If provided, only include trials with these statuses.
not_in_statuses: If provided, exclude trials with these statuses.
Returns:
Set of trial indices matching the filter criteria.
"""
trials_to_check = set(experiment.trials.keys())
if only_in_statuses is not None:
trials_to_check = get_trials_by_statuses(
experiment=experiment, statuses=only_in_statuses
)
if not_in_statuses is not None:
trials_to_check -= get_trials_by_statuses(
experiment=experiment, statuses=not_in_statuses
)
return trials_to_check
def count_trials_toward_threshold(
experiment: Experiment,
trials_from_node: set[int],
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool = False,
count_only_trials_with_data: bool = False,
count_only_trials_without_data: bool = False,
) -> int:
"""Count trials contributing toward a threshold.
Args:
experiment: The experiment to query.
trials_from_node: Set of trial indices generated by the current node.
only_in_statuses: If provided, only count trials with these statuses.
not_in_statuses: If provided, exclude trials with these statuses.
use_all_trials_in_exp: If True, count all trials in the experiment.
Otherwise, only count trials from the current node.
count_only_trials_with_data: If True, only count trials with data.
count_only_trials_without_data: If True, only count trials that are
MISSING required metric data. Cannot be True if
count_only_trials_with_data is also True.
Returns:
The number of trials contributing to the threshold.
"""
if count_only_trials_with_data and count_only_trials_without_data:
raise UserInputError(
"Cannot set both count_only_trials_with_data and "
"count_only_trials_without_data to True."
)
all_trials_to_check = filter_trials_by_status(
experiment=experiment,
only_in_statuses=only_in_statuses,
not_in_statuses=not_in_statuses,
)
if count_only_trials_with_data:
data_trial_indices = get_trial_indices_with_required_metrics(
experiment=experiment,
df=experiment.lookup_data().df,
require_data_for_all_metrics=False,
)
all_trials_to_check = all_trials_to_check.intersection(data_trial_indices)
elif count_only_trials_without_data:
data_trial_indices = get_trial_indices_with_required_metrics(
experiment=experiment,
df=experiment.lookup_data().df,
require_data_for_all_metrics=False,
)
all_trials_to_check = all_trials_to_check - data_trial_indices
if use_all_trials_in_exp:
return len(all_trials_to_check)
return len(trials_from_node.intersection(all_trials_to_check))
# ============================================================================
# PausingCriterion - for blocking generation without transitioning
# ============================================================================
class PausingCriterion(SortableBase):
"""A criterion that pauses generation from a GenerationNode without triggering
a transition to another node.
"""
@abstractmethod
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Returns True if this criterion's condition is met."""
pass
@abstractmethod
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
"""Raises an appropriate error when generation is blocked."""
pass
@property
def criterion_class(self) -> str:
"""Name of the class of this PausingCriterion."""
return self.__class__.__name__
def __repr__(self) -> str:
return f"{self.criterion_class}({serialize_init_args(obj=self)})"
@property
def _unique_id(self) -> str:
"""Unique id for this PausingCriterion."""
return str(self)
class TrialCountPausingCriterion(PausingCriterion):
"""Abstract base class for pausing criteria based on trial count thresholds.
This class provides shared logic for pausing criteria that count trials toward
a threshold. Subclasses only need to implement `block_continued_generation_error()`
to define the specific error raised when generation is paused.
Args:
threshold: The maximum number of trials allowed before blocking generation.
only_in_statuses: A list of trial statuses to filter on when checking the
criterion threshold.
not_in_statuses: A list of trial statuses to exclude when checking the
criterion threshold.
use_all_trials_in_exp: A flag to use all trials in the experiment, instead of
only those generated by the current GenerationNode.
count_only_trials_without_data: If True, only count trials that are MISSING
required metric data. Defaults to False.
"""
def __init__(
self,
threshold: int,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool = False,
count_only_trials_without_data: bool = False,
) -> None:
self.threshold = threshold
self.only_in_statuses = only_in_statuses
self.not_in_statuses = not_in_statuses
self.use_all_trials_in_exp = use_all_trials_in_exp
self.count_only_trials_without_data = count_only_trials_without_data
def num_contributing_to_threshold(
self, experiment: Experiment, trials_from_node: set[int]
) -> int:
"""Returns the number of trials contributing to the threshold."""
return count_trials_toward_threshold(
experiment=experiment,
trials_from_node=trials_from_node,
only_in_statuses=self.only_in_statuses,
not_in_statuses=self.not_in_statuses,
use_all_trials_in_exp=self.use_all_trials_in_exp,
count_only_trials_without_data=self.count_only_trials_without_data,
)
def num_till_threshold(
self, experiment: Experiment, trials_from_node: set[int]
) -> int:
"""Returns the number of trials available before hitting the threshold."""
return self.threshold - self.num_contributing_to_threshold(
experiment=experiment, trials_from_node=trials_from_node
)
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Returns True if the trial count threshold has been reached."""
return (
self.num_contributing_to_threshold(
experiment=experiment, trials_from_node=curr_node.trials_from_node
)
>= self.threshold
)
@abstractmethod
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
"""Raises an appropriate error when generation is blocked.
Subclasses must implement this to define the specific error behavior.
"""
pass
class MaxGenerationParallelism(TrialCountPausingCriterion):
"""A PausingCriterion that pauses generation after a maximum number of trials
have been generated for the current GenerationNode and are currently running.
Args:
threshold: The maximum number of trials allowed in the specified statuses.
only_in_statuses: A list of trial statuses to filter on when checking the
criterion threshold.
not_in_statuses: A list of trial statuses to exclude when checking the
criterion threshold.
use_all_trials_in_exp: A flag to use all trials in the experiment, instead of
only those generated by the current GenerationNode.
count_only_trials_without_data: If True, only count trials that are MISSING
required metric data. Defaults to False.
"""
def __init__(
self,
threshold: int,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool = False,
count_only_trials_without_data: bool = False,
) -> None:
super().__init__(
threshold=threshold,
only_in_statuses=only_in_statuses,
not_in_statuses=not_in_statuses,
use_all_trials_in_exp=use_all_trials_in_exp,
count_only_trials_without_data=count_only_trials_without_data,
)
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
"""Raises MaxParallelismReachedException."""
raise MaxParallelismReachedException(
node_name=node_name,
num_running=self.num_contributing_to_threshold(
experiment=experiment, trials_from_node=trials_from_node
),
)
class MaxTrialsAwaitingData(TrialCountPausingCriterion):
"""A PausingCriterion that pauses generation after a maximum number of trials
have been generated, waiting for data before allowing more generation.
This criterion pauses generation from the associated GenerationNode when the
threshold is met, but does NOT trigger a transition to another node. Use this
when you want to enforce that a node generates at most a certain number of
trials before requiring data.
Args:
threshold: The maximum number of trials allowed before blocking generation.
only_in_statuses: A list of trial statuses to filter on when checking the
criterion threshold.
not_in_statuses: A list of trial statuses to exclude when checking the
criterion threshold. Defaults to [FAILED, ABANDONED].
use_all_trials_in_exp: A flag to use all trials in the experiment, instead of
only those generated by the current GenerationNode. Defaults to False.
count_only_trials_without_data: If True, only count trials that are MISSING
required metric data. Use this to block generation until running trials
have data. Defaults to False.
"""
def __init__(
self,
threshold: int,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool = False,
count_only_trials_without_data: bool = False,
) -> None:
if not_in_statuses is None:
not_in_statuses = [TrialStatus.FAILED, TrialStatus.ABANDONED]
super().__init__(
threshold=threshold,
only_in_statuses=only_in_statuses,
not_in_statuses=not_in_statuses,
use_all_trials_in_exp=use_all_trials_in_exp,
count_only_trials_without_data=count_only_trials_without_data,
)
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
"""Raises DataRequiredError when the trial threshold is reached."""
raise DataRequiredError(DATA_REQUIRED_MSG.format(node_name=node_name))
class StagedTrialsPausingCriterion(TrialCountPausingCriterion):
"""Pauses generation when staged trials exist.
Defaults to threshold=1, only_in_statuses=[TrialStatus.STAGED],
use_all_trials_in_exp=True so that any staged trial in the experiment
pauses generation.
"""
def __init__(
self,
threshold: int = 1,
use_all_trials_in_exp: bool = True,
) -> None:
super().__init__(
threshold=threshold,
only_in_statuses=[TrialStatus.STAGED],
use_all_trials_in_exp=use_all_trials_in_exp,
)
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
staged = experiment.trial_indices_by_status[TrialStatus.STAGED]
staged_str = ", ".join(str(i) for i in staged)
raise AxGenerationException(
f"You already have staged trials (indices: {staged_str}). "
"Please either allocate and run the staged trials in the associated "
"experimentation platform (QE, QRT, etc.) or abandon them to unblock "
"generation of new candidate trials."
)
class MissingOptimizationConfigPausingCriterion(PausingCriterion):
"""Pauses generation if no optimization_config exists."""
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
return experiment.optimization_config is None
def block_continued_generation_error(
self,
node_name: str,
experiment: Experiment,
trials_from_node: set[int],
) -> None:
raise AxGenerationException(
"Your experiment does not currently have an optimization config set. "
"In order to generate candidates with Bayesian Optimization, you must "
"configure + save an optimization config on your experiment. This can "
"be done via the UI or Client API."
)
# ============================================================================
# TransitionCriterion - for node transitions
# ============================================================================
class TransitionCriterion(SortableBase):
"""
Simple class to describe a condition which must be met for this GenerationNode to
take an action such as generation, transition, etc.
Args:
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to when this criterion is met.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
"""
_transition_to: str
def __init__(
self,
transition_to: str,
continue_trial_generation: bool | None = False,
) -> None:
self._transition_to = transition_to
self.continue_trial_generation = continue_trial_generation
@property
def transition_to(self) -> str:
"""The name of the next GenerationNode after this TransitionCriterion is
completed.
"""
return self._transition_to
@abstractmethod
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""If the criterion of this TransitionCriterion is met, returns True."""
pass
@property
def criterion_class(self) -> str:
"""Name of the class of this TransitionCriterion."""
return self.__class__.__name__
def __repr__(self) -> str:
return f"{self.criterion_class}({serialize_init_args(obj=self)})"
@property
def _unique_id(self) -> str:
"""Unique id for this TransitionCriterion."""
return str(self)
class AutoTransitionAfterGen(TransitionCriterion):
"""A class to designate automatic transition from one GenerationNode to another.
Args:
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to next.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
"""
def __init__(
self,
transition_to: str,
continue_trial_generation: bool | None = True,
) -> None:
super().__init__(
transition_to=transition_to,
continue_trial_generation=continue_trial_generation,
)
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Return True as soon as any GeneratorRun is generated by this
GenerationNode.
"""
# Handle edge case where the InputConstructor for a GenerationNode
# with this criterion requests no arms to be generated, therefore, indicating
# that this GenerationNode should be skipped and so we can transition to the
# next node as defined by this criterion.
if curr_node._should_skip:
return True
last_gr_from_gs = curr_node.generation_strategy.last_generator_run
return (
last_gr_from_gs._generation_node_name == curr_node.name
if last_gr_from_gs is not None
else False
)
class IsSingleObjective(TransitionCriterion):
"""A class to initiate transition based on whether the experiment is optimizing
for a single objective or multiple objectives.
Args:
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to next.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
"""
def __init__(
self,
transition_to: str,
continue_trial_generation: bool | None = False,
) -> None:
super().__init__(
transition_to=transition_to,
continue_trial_generation=continue_trial_generation,
)
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Return True if the optimization config is not of type
``MultiObjectiveOptimizationConfig``."""
return (
not isinstance(
experiment.optimization_config, MultiObjectiveOptimizationConfig
)
if experiment.optimization_config is not None
else True
)
class TrialBasedCriterion(TransitionCriterion):
"""Common class for transition criterion that are based on trial information.
Args:
threshold: The threshold as an integer for this criterion. Ex: If we want to
generate at most 3 trials, then the threshold is 3.
only_in_statuses: A list of trial statuses to filter on when checking the
criterion threshold.
not_in_statuses: A list of trial statuses to exclude when checking the
criterion threshold.
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to when this criterion is met, if it exists.
use_all_trials_in_exp: A flag to use all trials in the experiment, instead of
only those generated by the current GenerationNode.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
count_only_trials_with_data: If set to True, only trials with data for all
metrics in the opt config will be counted towards the ``threshold``.
Defaults to False.
"""
def __init__(
self,
threshold: int,
transition_to: str,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool | None = False,
continue_trial_generation: bool | None = False,
count_only_trials_with_data: bool = False,
) -> None:
self.threshold = threshold
self.only_in_statuses = only_in_statuses
self.not_in_statuses = not_in_statuses
self.use_all_trials_in_exp = use_all_trials_in_exp
self.count_only_trials_with_data = count_only_trials_with_data
super().__init__(
transition_to=transition_to,
continue_trial_generation=continue_trial_generation,
)
def experiment_trials_by_status(
self, experiment: Experiment, statuses: list[TrialStatus]
) -> set[int]:
"""Get the trial indices from the experiment with the desired statuses."""
return get_trials_by_statuses(experiment=experiment, statuses=statuses)
def all_trials_to_check(self, experiment: Experiment) -> set[int]:
"""All the trials to check that meet the provided status filters."""
return filter_trials_by_status(
experiment=experiment,
only_in_statuses=self.only_in_statuses,
not_in_statuses=self.not_in_statuses,
)
def num_contributing_to_threshold(
self, experiment: Experiment, trials_from_node: set[int]
) -> int:
"""Returns the number of trials contributing to the threshold."""
return count_trials_toward_threshold(
experiment=experiment,
trials_from_node=trials_from_node,
only_in_statuses=self.only_in_statuses,
not_in_statuses=self.not_in_statuses,
use_all_trials_in_exp=bool(self.use_all_trials_in_exp),
count_only_trials_with_data=self.count_only_trials_with_data,
)
def num_till_threshold(
self, experiment: Experiment, trials_from_node: set[int]
) -> int:
"""Returns the number of trials needed to meet the threshold.
Args:
experiment: The experiment associated with this GenerationStrategy.
trials_from_node: The set of trials generated by this GenerationNode.
"""
return self.threshold - self.num_contributing_to_threshold(
experiment=experiment, trials_from_node=trials_from_node
)
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Returns if this criterion has been met given its constraints.
Args:
experiment: The experiment associated with this GenerationStrategy.
trials_from_node: The set of trials generated by this GenerationNode.
block_continued_generation: A flag to prevent continued generation from the
associated GenerationNode if this criterion is met but other criterion
remain unmet. Ex: ``MinTrials`` has not been met yet, but
MinTrials has been reached. If this flag is set to true on MinTrials
then we will raise an error, otherwise we will continue to generate
trials until ``MinTrials`` is met (thus overriding MinTrials).
"""
return (
self.num_contributing_to_threshold(
experiment=experiment, trials_from_node=curr_node.trials_from_node
)
>= self.threshold
)
class MinTrials(TrialBasedCriterion):
"""
Simple class to enforce a minimum threshold for the number of trials with the
designated statuses being generated by a specific GenerationNode.
Args:
threshold: The threshold as an integer for this criterion. Ex: If we want to
generate at most 3 trials, then the threshold is 3.
only_in_statuses: A list of trial statuses to filter on when checking the
criterion threshold.
not_in_statuses: A list of trial statuses to exclude when checking the
criterion threshold.
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to when this criterion is met.
use_all_trials_in_exp: A flag to use all trials in the experiment, instead of
only those generated by the current GenerationNode.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
count_only_trials_with_data: If set to True, only trials with data will be
counted towards the ``threshold``. Defaults to False.
"""
def __init__(
self,
threshold: int,
transition_to: str,
only_in_statuses: list[TrialStatus] | None = None,
not_in_statuses: list[TrialStatus] | None = None,
use_all_trials_in_exp: bool | None = False,
continue_trial_generation: bool | None = False,
count_only_trials_with_data: bool = False,
) -> None:
super().__init__(
threshold=threshold,
transition_to=transition_to,
only_in_statuses=only_in_statuses,
not_in_statuses=not_in_statuses,
use_all_trials_in_exp=use_all_trials_in_exp,
continue_trial_generation=continue_trial_generation,
count_only_trials_with_data=count_only_trials_with_data,
)
class AuxiliaryExperimentCheck(TransitionCriterion):
"""A class to transition from one GenerationNode to another by checking if certain
types of Auxiliary Experiment purposes exists.
A common use case is to use auxiliary_experiment_purposes_to_include to transition
to a node and auxiliary_experiment_purposes_to_exclude to transition away from it.
Example usage: In Bayesian optimization with preference exploration (BOPE), we
check if the preference exploration (PE) auxiliary experiment exists to indicate
transition to the node that will generate candidates based on the learned
objective. Since preference exploration is usually conducted after the exploratory
batch is completed, we do not know at experiment creation time if the PE node
should be used during the GenerationStrategy.
Args:
transition_to: The name of the GenerationNode the GenerationStrategy should
transition to when this criterion is met, if it exists.
auxiliary_experiment_purposes_to_include: Optional list of auxiliary experiment
purposes we expect to have. This can be helpful when need to transition to
a node based on AuxiliaryExperimentPurpose. Criterion is met when all
inclusion and exclusion checks pass.
auxiliary_experiment_purposes_to_exclude: Optional list of auxiliary experiment
purpose we expect to not have. This can be helpful when need to transition
out of a node based on AuxiliaryExperimentPurpose. Criterion is met when
all inclusion and exclusion checks pass.
continue_trial_generation: A flag to indicate that all generation for a given
trial is not completed, and thus even after transition, the next node will
continue to generate arms for the same trial. Example usage: in
``BatchTrial``s we may enable generation of arms within a batch from
different ``GenerationNodes`` by setting this flag to True.
"""
def __init__(
self,
transition_to: str,
auxiliary_experiment_purposes_to_include: (
list[AuxiliaryExperimentPurpose] | None
) = None,
auxiliary_experiment_purposes_to_exclude: (
list[AuxiliaryExperimentPurpose] | None
) = None,
continue_trial_generation: bool | None = False,
) -> None:
super().__init__(
transition_to=transition_to,
continue_trial_generation=continue_trial_generation,
)
if (
auxiliary_experiment_purposes_to_include is None
and auxiliary_experiment_purposes_to_exclude is None
):
raise UserInputError(
f"{self.__class__} cannot have both "
"`auxiliary_experiment_purposes_to_include` and "
"`auxiliary_experiment_purposes_to_exclude` be None."
)
self.auxiliary_experiment_purposes_to_include = (
auxiliary_experiment_purposes_to_include
)
self.auxiliary_experiment_purposes_to_exclude = (
auxiliary_experiment_purposes_to_exclude
)
def check_aux_exp_purposes(
self,
aux_exp_by_purposes: dict[
AuxiliaryExperimentPurpose, list[AuxiliaryExperiment]
],
include: bool,
expected_aux_exp_purposes: list[AuxiliaryExperimentPurpose] | None = None,
) -> bool:
"""Helper method to check if all elements in expected_aux_exp_purposes
are in (or not in) aux_exp_purposes"""
if expected_aux_exp_purposes is not None:
for purpose in expected_aux_exp_purposes:
purpose_present = (
purpose in aux_exp_by_purposes
and len(aux_exp_by_purposes[purpose]) > 0
)
if purpose_present != include:
return False
return True
def is_met(
self,
experiment: Experiment,
curr_node: GenerationNode,
) -> bool:
"""Check if the experiment has auxiliary experiments for certain purpose."""
inclusion_check = self.check_aux_exp_purposes(
aux_exp_by_purposes=experiment.auxiliary_experiments_by_purpose,
include=True,
expected_aux_exp_purposes=self.auxiliary_experiment_purposes_to_include,
)
exclusion_check = self.check_aux_exp_purposes(
aux_exp_by_purposes=experiment.auxiliary_experiments_by_purpose,
include=False,
expected_aux_exp_purposes=self.auxiliary_experiment_purposes_to_exclude,
)
return inclusion_check and exclusion_check