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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 01:18 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""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. 

18 

19 Useful for baseline comparison against Bayesian optimizers. 

20 """ 

21 

22 def _register( 

23 self, 

24 *, 

25 configs: pd.DataFrame, 

26 scores: pd.DataFrame, 

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

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

29 ) -> None: 

30 """ 

31 Registers the given configs and scores. 

32 

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

34 

35 Parameters 

36 ---------- 

37 configs : pandas.DataFrame 

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

39 the rows are the configs. 

40 

41 scores : pandas.DataFrame 

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

43 

44 context : None 

45 Not Yet Implemented. 

46 

47 metadata : None 

48 Not Yet Implemented. 

49 """ 

50 if context is not None: 

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

52 if metadata is not None: 

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

54 # should we pop them from self.pending_observations? 

55 

56 def _suggest( 

57 self, 

58 *, 

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

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

61 """ 

62 Suggests a new configuration. 

63 

64 Sampled at random using ConfigSpace. 

65 

66 Parameters 

67 ---------- 

68 context : None 

69 Not Yet Implemented. 

70 

71 Returns 

72 ------- 

73 configuration : pd.DataFrame 

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

75 

76 metadata : None 

77 Not implemented. 

78 """ 

79 if context is not None: 

80 # not sure how that works here? 

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

82 return ( 

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

84 None, 

85 ) 

86 

87 def register_pending( 

88 self, 

89 *, 

90 configs: pd.DataFrame, 

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

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

93 ) -> None: 

94 raise NotImplementedError() 

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