1111from matplotlib import pyplot as plt
1212
1313from smac .runhistory .runhistory import RunHistory
14+ from ConfigSpace .configuration_space import ConfigurationSpace , Configuration
1415from ConfigSpace .util import impute_inactive_values
15- from ConfigSpace .hyperparameters import CategoricalHyperparameter
16+ from ConfigSpace .hyperparameters import CategoricalHyperparameter , Constant
1617
1718try :
1819 from fanova import fANOVA as fanova_pyrfr
@@ -40,6 +41,17 @@ def __init__(self, scenario, cs, model, to_evaluate: int, runhist: RunHistory, r
4041 super ().__init__ (scenario , cs , model , to_evaluate , rng , ** kwargs )
4142 self .name = 'fANOVA'
4243 self .logger = 'pimp.' + self .name
44+
45+ # Turn all Constants into Categoricals (fANOVA cannot handle Constants)
46+ self .cs_contained_constant = False
47+ if any ([isinstance (hp , Constant ) for hp in self .cs .get_hyperparameters ()]):
48+ self .logger .debug ("Replacing configspace's hyperparameter Constants by one-value Categoricals." )
49+ new_hyperparameters = [CategoricalHyperparameter (hp .name , [hp .value ]) if isinstance (hp , Constant )
50+ else hp for hp in self .cs .get_hyperparameters ()]
51+ self .cs = ConfigurationSpace ()
52+ self .cs .add_hyperparameters (new_hyperparameters )
53+ self .cs_contained_constant = True
54+
4355 # This way the instance features in X are ignored and a new forest is constructed
4456 if self .model .instance_features is None :
4557 self .logger .info ('No preprocessing necessary' )
@@ -59,7 +71,7 @@ def __init__(self, scenario, cs, model, to_evaluate: int, runhist: RunHistory, r
5971 cutoffs = (self .model .predict_marginalized_over_instances (
6072 np .array ([impute_inactive_values ( self .cs .get_default_configuration ()).get_array ()]))[0 ].flatten ()[0 ],
6173 np .inf )
62- self .evaluator = fanova_pyrfr (X = self .X , Y = self .y .flatten (), config_space = cs ,
74+ self .evaluator = fanova_pyrfr (X = self .X , Y = self .y .flatten (), config_space = self . cs ,
6375 seed = self .rng .randint (2 ** 31 - 1 ), cutoffs = cutoffs )
6476 self .n_most_imp_pairs = n_pairs
6577 self .num_single = None
@@ -77,13 +89,16 @@ def _preprocess(self, runhistory):
7789 self .logger .info ('PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING' )
7890 self .logger .info ('Marginalizing away all instances!' )
7991 configs = runhistory .get_all_configs ()
92+ if self .cs_contained_constant :
93+ configs = [Configuration (self .cs , vector = c .get_array ()) for c in configs ]
8094 X_non_hyper , X_prime = [], []
8195 for config in configs :
8296 config = impute_inactive_values (config ).get_array ()
8397 X_prime .append (config )
8498 X_non_hyper .append (config )
8599 for idx , param in enumerate (self .cs .get_hyperparameters ()):
86- if not isinstance (param , CategoricalHyperparameter ):
100+ if not (isinstance (param , CategoricalHyperparameter ) or
101+ isinstance (param , Constant )):
87102 X_non_hyper [- 1 ][idx ] = param ._transform (X_non_hyper [- 1 ][idx ])
88103 X_non_hyper = np .array (X_non_hyper )
89104 X_prime = np .array (X_prime )
0 commit comments