Skip to content

Single Worker

Basic single worker setup#

A basic setup for optimizing can be done as follows. Please note, that this is example should solely show a simple setup of dehb. More in-depth examples can be found in the examples folder. First we need to setup a ConfigurationSpace, from which Configurations will be sampled:

Configuration Space
from ConfigSpace import ConfigurationSpace, Configuration

cs = ConfigurationSpace({"x0": (3.0, 10.0), "x1": ["red", "green"]})
print(cs)
Configuration space object:
  Hyperparameters:
    x0, Type: UniformFloat, Range: [3.0, 10.0], Default: 6.5
    x1, Type: Categorical, Choices: {red, green}, Default: red

Next, we need an object_function, which we are aiming to optimize:

Configuration Space
import numpy as np

def objective_function(x: Configuration, fidelity: float, **kwargs):
    # Replace this with your actual objective value (y) and cost.
    cost = (10 if x["x1"] == "red" else 100) + fidelity
    y = x["x0"] + np.random.uniform()
    return {"fitness": y, "cost": x["x0"]}

sample_config = cs.sample_configuration()
print(sample_config)

result = objective_function(sample_config, fidelity=10)
print(result)
Configuration(values={
  'x0': 7.0586409196304905,
  'x1': 'red',
})
{'fitness': 7.408409178438662, 'cost': 7.0586409196304905}

Finally, we can setup our optimizer and run DEHB:

Configuration Space
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/markdown_exec/formatters/python.py", line 59, in _run_python
    exec(compiled, exec_globals)  # noqa: S102
  File "<code block: session someid; n3; title Configuration Space>", line 17, in <module>
    config, fitness, runtime, fidelity, _ = history[0]
ValueError: too many values to unpack (expected 5)