1515import threading
1616import grpc
1717import logging
18+ import itertools
1819
1920from pkg .apis .manager .v1beta1 .python import api_pb2
2021from pkg .apis .manager .v1beta1 .python import api_pb2_grpc
21- from pkg .suggestion .v1beta1 .internal .constant import INTEGER , DOUBLE
2222from pkg .suggestion .v1beta1 .internal .search_space import HyperParameterSearchSpace
2323from pkg .suggestion .v1beta1 .internal .trial import Trial , Assignment
2424from pkg .suggestion .v1beta1 .optuna .base_service import BaseOptunaService
@@ -55,7 +55,7 @@ def GetSuggestions(self, request, context):
5555
5656 def ValidateAlgorithmSettings (self , request , context ):
5757 is_valid , message = OptimizerConfiguration .validate_algorithm_spec (
58- request .experiment . spec . algorithm )
58+ request .experiment )
5959 if not is_valid :
6060 context .set_code (grpc .StatusCode .INVALID_ARGUMENT )
6161 context .set_details (message )
@@ -86,6 +86,9 @@ class OptimizerConfiguration(object):
8686 "random" : {
8787 "seed" : lambda x : int (x ),
8888 },
89+ "grid" : {
90+ "seed" : lambda x : int (x ),
91+ }
8992 }
9093
9194 @classmethod
@@ -110,7 +113,8 @@ def convert_algorithm_spec(cls, algorithm_spec):
110113 return algorithm_spec .algorithm_name , config
111114
112115 @classmethod
113- def validate_algorithm_spec (cls , algorithm_spec ):
116+ def validate_algorithm_spec (cls , experiment ):
117+ algorithm_spec = experiment .spec .algorithm
114118 algorithm_name = algorithm_spec .algorithm_name
115119 algorithm_settings = algorithm_spec .algorithm_settings
116120
@@ -120,6 +124,10 @@ def validate_algorithm_spec(cls, algorithm_spec):
120124 return cls ._validate_cmaes_setting (algorithm_settings )
121125 elif algorithm_name == "random" :
122126 return cls ._validate_random_setting (algorithm_settings )
127+ elif algorithm_name == "grid" :
128+ return cls ._validate_grid_setting (experiment )
129+ else :
130+ return False , "unknown algorithm name {}" .format (algorithm_name )
123131
124132 @classmethod
125133 def _validate_tpe_setting (cls , algorithm_spec ):
@@ -178,3 +186,34 @@ def _validate_random_setting(cls, algorithm_settings):
178186 exception = e )
179187
180188 return True , ""
189+
190+ @classmethod
191+ def _validate_grid_setting (cls , experiment ):
192+ algorithm_settings = experiment .spec .algorithm .algorithm_settings
193+ search_space = HyperParameterSearchSpace .convert (experiment )
194+
195+ for s in algorithm_settings :
196+ try :
197+ if s .name == "random_state" :
198+ if not int (s .value ) >= 0 :
199+ return False , ""
200+ else :
201+ return False , "unknown setting {} for algorithm grid" .format (s .name )
202+
203+ except Exception as e :
204+ return False , "failed to validate {name}({value}): {exception}" .format (name = s .name , value = s .value ,
205+ exception = e )
206+
207+ try :
208+ combinations = HyperParameterSearchSpace .convert_to_combinations (search_space )
209+ num_combinations = len (list (itertools .product (* combinations .values ())))
210+ max_trial_count = experiment .spec .max_trial_count
211+ if max_trial_count > num_combinations :
212+ return False , "Max Trial Count: {max_trial} > all possible search combinations: {combinations}" .\
213+ format (max_trial = max_trial_count , combinations = num_combinations )
214+
215+ except Exception as e :
216+ return False , "failed to validate parameters({parameters}): {exception}" .\
217+ format (parameters = search_space .params , exception = e )
218+
219+ return True , ""
0 commit comments