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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Contains the RandomOptimizer class.""" 

6 

7from typing import Optional, Tuple 

8from warnings import warn 

9 

10import pandas as pd 

11 

12from mlos_core.optimizers.optimizer import BaseOptimizer 

13 

14 

15class RandomOptimizer(BaseOptimizer): 

16 """ 

17 Optimizer class that produces random suggestions. Useful for baseline comparison 

18 against Bayesian optimizers. 

19 

20 Parameters 

21 ---------- 

22 parameter_space : ConfigSpace.ConfigurationSpace 

23 The parameter space to optimize. 

24 """ 

25 

26 def _register( 

27 self, 

28 *, 

29 configs: pd.DataFrame, 

30 scores: pd.DataFrame, 

31 context: Optional[pd.DataFrame] = None, 

32 metadata: Optional[pd.DataFrame] = None, 

33 ) -> None: 

34 """ 

35 Registers the given configs and scores. 

36 

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

38 

39 Parameters 

40 ---------- 

41 configs : pd.DataFrame 

42 Dataframe of configs / parameters. The columns are parameter names and 

43 the rows are the configs. 

44 

45 scores : pd.DataFrame 

46 Scores from running the configs. The index is the same as the index of the configs. 

47 

48 context : None 

49 Not Yet Implemented. 

50 

51 metadata : None 

52 Not Yet Implemented. 

53 """ 

54 if context is not None: 

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

56 if metadata is not None: 

57 warn(f"Not Implemented: Ignoring context {list(metadata.columns)}", UserWarning) 

58 # should we pop them from self.pending_observations? 

59 

60 def _suggest( 

61 self, 

62 *, 

63 context: Optional[pd.DataFrame] = None, 

64 ) -> Tuple[pd.DataFrame, Optional[pd.DataFrame]]: 

65 """ 

66 Suggests a new configuration. 

67 

68 Sampled at random using ConfigSpace. 

69 

70 Parameters 

71 ---------- 

72 context : None 

73 Not Yet Implemented. 

74 

75 Returns 

76 ------- 

77 configuration : pd.DataFrame 

78 Pandas dataframe with a single row. Column names are the parameter names. 

79 

80 metadata : None 

81 Not implemented. 

82 """ 

83 if context is not None: 

84 # not sure how that works here? 

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

86 return ( 

87 pd.DataFrame(dict(self.optimizer_parameter_space.sample_configuration()), index=[0]), 

88 None, 

89 ) 

90 

91 def register_pending( 

92 self, 

93 *, 

94 configs: pd.DataFrame, 

95 context: Optional[pd.DataFrame] = None, 

96 metadata: Optional[pd.DataFrame] = None, 

97 ) -> None: 

98 raise NotImplementedError() 

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