Coverage for mlos_core/mlos_core/optimizers/random_optimizer.py: 93%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""RandomOptimizer class.""" 

6 

7from warnings import warn 

8 

9import pandas as pd 

10 

11from mlos_core.data_classes import Observations, Suggestion 

12from mlos_core.optimizers.optimizer import BaseOptimizer 

13 

14 

15class RandomOptimizer(BaseOptimizer): 

16 """ 

17 Optimizer class that produces random suggestions. 

18 

19 Useful for baseline comparison against Bayesian optimizers. 

20 """ 

21 

22 def _register( 

23 self, 

24 observations: Observations, 

25 ) -> None: 

26 """ 

27 Registers the given config/score pairs. 

28 

29 Notes 

30 ----- 

31 Doesn't do anything on the RandomOptimizer except storing configs for logging. 

32 

33 Parameters 

34 ---------- 

35 observations : Observations 

36 The observations to register. 

37 """ 

38 if observations.contexts is not None: 

39 warn( 

40 f"Not Implemented: Ignoring context {list(observations.contexts.index)}", 

41 UserWarning, 

42 ) 

43 if observations.metadata is not None: 

44 warn( 

45 f"Not Implemented: Ignoring context {list(observations.metadata.index)}", 

46 UserWarning, 

47 ) 

48 # should we pop them from self.pending_observations? 

49 

50 def _suggest( 

51 self, 

52 *, 

53 context: pd.Series | None = None, 

54 ) -> Suggestion: 

55 """ 

56 Suggests a new configuration. 

57 

58 Sampled at random using ConfigSpace. 

59 

60 Parameters 

61 ---------- 

62 context : None 

63 Not Yet Implemented. 

64 

65 Returns 

66 ------- 

67 suggestion: Suggestion 

68 The suggestion to evaluate. 

69 """ 

70 if context is not None: 

71 # not sure how that works here? 

72 warn(f"Not Implemented: Ignoring context {list(context.index)}", UserWarning) 

73 return Suggestion( 

74 config=pd.Series(self.optimizer_parameter_space.sample_configuration(), dtype=object), 

75 context=context, 

76 metadata=None, 

77 ) 

78 

79 def register_pending(self, pending: Suggestion) -> None: 

80 raise NotImplementedError() 

81 # self._pending_observations.append((configs, context))