Coverage for mlos_core/mlos_core/optimizers/random_optimizer.py: 94%
16 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""RandomOptimizer class."""
7from typing import Optional
8from warnings import warn
10import pandas as pd
12from mlos_core.data_classes import Observations, Suggestion
13from mlos_core.optimizers.optimizer import BaseOptimizer
16class RandomOptimizer(BaseOptimizer):
17 """
18 Optimizer class that produces random suggestions.
20 Useful for baseline comparison against Bayesian optimizers.
21 """
23 def _register(
24 self,
25 observations: Observations,
26 ) -> None:
27 """
28 Registers the given config/score pairs.
30 Notes
31 -----
32 Doesn't do anything on the RandomOptimizer except storing configs for logging.
34 Parameters
35 ----------
36 observations : Observations
37 The observations to register.
38 """
39 if observations.contexts is not None:
40 warn(
41 f"Not Implemented: Ignoring context {list(observations.contexts.index)}",
42 UserWarning,
43 )
44 if observations.metadata is not None:
45 warn(
46 f"Not Implemented: Ignoring context {list(observations.metadata.index)}",
47 UserWarning,
48 )
49 # should we pop them from self.pending_observations?
51 def _suggest(
52 self,
53 *,
54 context: Optional[pd.Series] = None,
55 ) -> Suggestion:
56 """
57 Suggests a new configuration.
59 Sampled at random using ConfigSpace.
61 Parameters
62 ----------
63 context : None
64 Not Yet Implemented.
66 Returns
67 -------
68 suggestion: Suggestion
69 The suggestion to evaluate.
70 """
71 if context is not None:
72 # not sure how that works here?
73 warn(f"Not Implemented: Ignoring context {list(context.index)}", UserWarning)
74 return Suggestion(
75 config=pd.Series(self.optimizer_parameter_space.sample_configuration(), dtype=object),
76 context=context,
77 metadata=None,
78 )
80 def register_pending(self, pending: Suggestion) -> None:
81 raise NotImplementedError()
82 # self._pending_observations.append((configs, context))