1818
1919import smac
2020import numpy as np
21- from smac .callback .sawei_callback import get_sawei_kwargs
21+ import pandas as pd
22+ import seaborn as sns
23+ from smac .callback .sawei_callback import get_sawei_kwargs , WEITracker , UpperBoundRegretCallback
2224
2325
2426__copyright__ = "Copyright 2021, AutoML.org Freiburg-Hannover"
2830class QuadraticFunction :
2931 @property
3032 def configspace (self ) -> ConfigurationSpace :
33+ """Configuration/search space of the problem
34+
35+ Returns:
36+ ConfigurationSpace: Configuration space.
37+ """
3138 cs = ConfigurationSpace (seed = 0 )
3239 x = Float ("x" , (- 5 , 5 ), default = - 5 )
3340 cs .add_hyperparameters ([x ])
@@ -41,6 +48,14 @@ def train(self, config: Configuration, seed: int = 0) -> float:
4148
4249
4350def plot (runhistory : RunHistory , incumbent : Configuration ) -> None :
51+ """Plot the objective function, all trials and the incumbent
52+
53+ Args:
54+ runhistory (RunHistory): Runhistory object after optimization
55+ incumbent (Configuration): Best configuration found by SMAC
56+ """
57+ sns .set_style ("whitegrid" )
58+ sns .set_palette ("colorblind" )
4459 plt .figure ()
4560
4661 # Plot ground truth
@@ -60,34 +75,72 @@ def plot(runhistory: RunHistory, incumbent: Configuration) -> None:
6075
6176 plt .show ()
6277
78+ def plot_sawei (facade : smac .facade .abstract_facade .AbstractFacade ) -> None :
79+ """Plot the interpolation weight alpha of SAWEI and the UBR (Upper Bound Regret)
80+
81+ Args:
82+ facade (smac.facade.abstract_facade.AbstractFacade): SMAC facade object after optimization
83+ """
84+
85+ sns .set_style ("whitegrid" )
86+ sns .set_palette ("colorblind" )
87+
88+ weitracker = None
89+ ubrtracker = None
90+ for callback in facade ._callbacks :
91+ if isinstance (callback , WEITracker ):
92+ weitracker = callback
93+ if isinstance (callback , UpperBoundRegretCallback ):
94+ ubrtracker = callback
95+
96+ # The data also lies in the output folder as a csv file
97+ df_alpha = pd .DataFrame (weitracker .history )
98+ df_ubr = pd .DataFrame (ubrtracker .history )
99+ df = pd .merge (df_alpha , df_ubr , on = "n_evaluated" )
100+
101+ fig , ax = plt .subplots ()
102+ ax2 = ax .twinx ()
103+ ax = sns .lineplot (data = df , x = "n_evaluated" , y = "alpha" , ax = ax , label = "alpha" , color = sns .color_palette ()[0 ])
104+ ax2 = sns .lineplot (data = df , x = "n_evaluated" , y = "ubr" , ax = ax2 , label = "ubr" , color = sns .color_palette ()[1 ])
105+ ax .legend (loc = "upper left" )
106+ ax2 .legend (loc = "upper right" )
107+ ax .set_ylabel ("alpha" )
108+ ax2 .set_ylabel ("ubr" )
109+ ax .set_xlabel ("n_evaluated" )
110+ plt .show ()
111+
112+
63113
64114if __name__ == "__main__" :
65115 model = QuadraticFunction ()
66116
67117 # Scenario object specifying the optimization "environment"
68- scenario = Scenario (model .configspace , deterministic = True , n_trials = 100 , seed = np .random .randint (low = 0 ,high = 10000 ))
118+ scenario = Scenario (model .configspace , deterministic = True , n_trials = 50 , seed = np .random .randint (low = 0 ,high = 10000 ))
69119
70120 # Get the kwargs necessary to use SAWEI
71121 # SAWEI is implemented as a chain of callbacks
72122 sawei_kwargs = get_sawei_kwargs ()
73123
74124 # Now we use SMAC to find the best hyperparameters
75- smac = BlackBoxFacade (
125+ optimizer = BlackBoxFacade (
76126 scenario ,
77127 model .train , # We pass the target function here
78128 overwrite = True , # Overrides any previous results that are found that are inconsistent with the meta-data
79129 ** sawei_kwargs # Add SAWEI
80130 )
81131
82- incumbent = smac .optimize ()
132+ incumbent = optimizer .optimize ()
83133
84134 # Get cost of default configuration
85- default_cost = smac .validate (model .configspace .get_default_configuration ())
135+ default_cost = optimizer .validate (model .configspace .get_default_configuration ())
86136 print (f"Default cost: { default_cost } " )
87137
88138 # Let's calculate the cost of the incumbent
89- incumbent_cost = smac .validate (incumbent )
139+ incumbent_cost = optimizer .validate (incumbent )
90140 print (f"Incumbent cost: { incumbent_cost } " )
91141
92142 # Let's plot it too
93- plot (smac .runhistory , incumbent )
143+ plot (optimizer .runhistory , incumbent )
144+
145+ # Let's plot the interpolation weight alpha of SAWEI and the UBR (Upper Bound Regret)
146+ plot_sawei (optimizer )
0 commit comments