Note
Go to the end to download the full example code. or to run this example in your browser via Binder
Call Target Function From Script¶
This simple example shows how to call a script with the following content:
#!/bin/bash
# Set arguments first
for argument in "$@"
do
key=$(echo $argument | cut -f1 -d=)
value=$(echo $argument | cut -f2 -d=)
if [[ $key == *"--"* ]]; then
v="${key/--/}"
declare $v="${value}"
fi
done
# We simply set the cost to our parameter
cost=$x0
# Return everything
echo "cost=$cost"
[INFO][abstract_initial_design.py:95] Reducing the number of initial configurations from 8 to 7 (max_ratio == 0.25).
[INFO][abstract_initial_design.py:147] Using 7 initial design configurations and 0 additional configurations.
[INFO][abstract_intensifier.py:306] Using only one seed for deterministic scenario.
[INFO][abstract_intensifier.py:516] Added config 568877 as new incumbent because there are no incumbents yet.
[INFO][abstract_intensifier.py:595] Added config 79965a and rejected config 568877 as incumbent because it is not better than the incumbents on 1 instances:
[INFO][abstract_intensifier.py:595] Added config 98169b and rejected config 79965a as incumbent because it is not better than the incumbents on 1 instances:
[INFO][abstract_intensifier.py:595] Added config 5beafa and rejected config 98169b as incumbent because it is not better than the incumbents on 1 instances:
[INFO][smbo.py:328] Configuration budget is exhausted:
[INFO][smbo.py:329] --- Remaining wallclock time: inf
[INFO][smbo.py:330] --- Remaining cpu time: inf
[INFO][smbo.py:331] --- Remaining trials: 0
[INFO][abstract_intensifier.py:306] Using only one seed for deterministic scenario.
Default cost: 500.0
Incumbent cost: 0.0
from ConfigSpace import ConfigurationSpace
from smac import BlackBoxFacade, Scenario
__copyright__ = "Copyright 2021, AutoML.org Freiburg-Hannover"
__license__ = "3-clause BSD"
if __name__ == "__main__":
cs = ConfigurationSpace({"x0": (0, 1000)}, seed=0)
# Scenario object specifying the optimization "environment"
scenario = Scenario(cs, deterministic=True, n_trials=30)
# Now we use SMAC to find the best hyperparameters
smac = BlackBoxFacade(
scenario,
"./script.sh", # We pass the filename of our script here
overwrite=True, # Overrides any previous results that are found that are inconsistent with the meta-data
)
incumbent = smac.optimize()
# Get cost of default configuration
default_cost = smac.validate(cs.get_default_configuration())
print(f"Default cost: {default_cost}")
# Let's calculate the cost of the incumbent
incumbent_cost = smac.validate(incumbent)
print(f"Incumbent cost: {incumbent_cost}")
Total running time of the script: (0 minutes 20.003 seconds)