|
| 1 | +""" |
| 2 | +Synthetic Function with BOinG as optimizer |
| 3 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 4 | +
|
| 5 | +An example of applying SMAC with BO inside Grove (BOinG) to optimize a |
| 6 | +synthetic function (2d rosenbrock function). |
| 7 | +
|
| 8 | +BOinG optimizer requires a SMAC4BOING wrapper to optimize the target algorithm. It is a two stage BO algorithm. |
| 9 | +In the first stage, BOinG constructs an RF to capture the global loss landscape. Then in the second stage, it only |
| 10 | +optimizes inside a subregion near the candidate suggested by the RF model with a GP model to focus only on the most |
| 11 | +promising region. |
| 12 | +""" |
| 13 | + |
| 14 | +import logging |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +from ConfigSpace import ConfigurationSpace |
| 18 | +from ConfigSpace.hyperparameters import UniformFloatHyperparameter |
| 19 | + |
| 20 | +from smac.facade.smac_boing_facade import SMAC4BOING |
| 21 | + |
| 22 | +# Import SMAC-utilities |
| 23 | +from smac.scenario.scenario import Scenario |
| 24 | + |
| 25 | + |
| 26 | +def rosenbrock_2d(x): |
| 27 | + """The 2 dimensional Rosenbrock function as a toy model |
| 28 | + The Rosenbrock function is well know in the optimization community and |
| 29 | + often serves as a toy problem. It can be defined for arbitrary |
| 30 | + dimensions. The minimium is always at x_i = 1 with a function value of |
| 31 | + zero. All input parameters are continuous. The search domain for |
| 32 | + all x's is the interval [-5, 10]. |
| 33 | + """ |
| 34 | + x1 = x["x0"] |
| 35 | + x2 = x["x1"] |
| 36 | + |
| 37 | + val = 100.0 * (x2 - x1**2.0) ** 2.0 + (1 - x1) ** 2.0 |
| 38 | + return val |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + logging.basicConfig(level=logging.INFO) # logging.DEBUG for debug output |
| 43 | + |
| 44 | + # Build Configuration Space which defines all parameters and their ranges |
| 45 | + cs = ConfigurationSpace() |
| 46 | + x0 = UniformFloatHyperparameter("x0", -5, 10, default_value=-3) |
| 47 | + x1 = UniformFloatHyperparameter("x1", -5, 10, default_value=-4) |
| 48 | + cs.add_hyperparameters([x0, x1]) |
| 49 | + # Scenario object |
| 50 | + scenario = Scenario( |
| 51 | + { |
| 52 | + "run_obj": "quality", # we optimize quality (alternatively runtime) |
| 53 | + "runcount-limit": 20, |
| 54 | + # max. number of function evaluations; for this example set to a low number |
| 55 | + "cs": cs, # configuration space |
| 56 | + "deterministic": "true", |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + # Example call of the function |
| 61 | + # It returns: Status, Cost, Runtime, Additional Infos |
| 62 | + def_value = rosenbrock_2d(cs.get_default_configuration()) |
| 63 | + print("Default Value: %.2f" % def_value) |
| 64 | + |
| 65 | + # Optimize, using a SMAC-object |
| 66 | + print("Optimizing! Depending on your machine, this might take a few minutes.") |
| 67 | + |
| 68 | + smac = SMAC4BOING( |
| 69 | + scenario=scenario, |
| 70 | + rng=np.random.RandomState(42), |
| 71 | + tae_runner=rosenbrock_2d, |
| 72 | + ) |
| 73 | + |
| 74 | + smac.optimize() |
0 commit comments