Design spaces
banditry.variable_domains.design_space.DesignSpace()
Search-space definition for a contextual-bandit / optimisation problem.
A design space is built from a list of spec dicts via :meth:parse, one dict per
parameter. The built-in parameter types (see :attr:parameter_types) accept:
{"name": ..., "type": "num", "lb": float, "ub": float}-- continuous parameter.{"name": ..., "type": "int", "lb": int, "ub": int}-- integer parameter (inclusive bounds).{"name": ..., "type": "bool"}-- boolean parameter.{"name": ..., "type": "cat", "categories": [...]}-- categorical parameter.
Custom types can be added with :meth:register_parameter_type.
Note on ordering: :attr:para_names lists the numeric-ish parameters
(num/int/bool) first, followed by the categorical ones, regardless of
their order in the spec list.
Example
space = DesignSpace.parse( ... [ ... {"name": "lr", "type": "num", "lb": 1e-4, "ub": 1e-1}, ... {"name": "num_layers", "type": "int", "lb": 1, "ub": 8}, ... {"name": "use_bias", "type": "bool"}, ... {"name": "activation", "type": "cat", "categories": ["relu", "tanh", "gelu"]}, ... ] ... ) df = space.sample(5) x_num, x_cat = space.transform(df) # FloatTensor (5, 3), LongTensor (5, 1) space.inverse_transform(x_num, x_cat).columns.tolist() ['lr', 'num_layers', 'use_bias', 'activation']
Source code in src/banditry/variable_domains/design_space.py
49 50 51 52 53 | |
num_paras
property
Total number of parameters in the design space.
num_numeric
property
Number of numeric-ish parameters (num/int/bool).
num_categorical
property
Number of categorical parameters.
opt_lb
property
Per-parameter lower bounds in the transformed/optimiser domain (numeric first, then categorical).
opt_ub
property
Per-parameter upper bounds in the transformed/optimiser domain (numeric first, then categorical).
parse(rec)
classmethod
Build a :class:DesignSpace from a list of parameter spec dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rec
|
list[dict]
|
List of spec dicts, one per parameter. Each dict must have a unique
|
required |
Returns:
| Type | Description |
|---|---|
DesignSpace
|
A new :class: |
DesignSpace
|
then categorical. |
Source code in src/banditry/variable_domains/design_space.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
register_parameter_type(type_name, para_class)
staticmethod
Register a custom parameter type.
After registration, type_name can be used as the "type" value in the
spec dicts passed to :meth:parse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Identifier to use as the |
required |
para_class
|
type[Parameter]
|
A :class: |
required |
Source code in src/banditry/variable_domains/design_space.py
101 102 103 104 105 106 107 108 109 110 111 112 113 | |
transform(data)
Map a DataFrame of raw parameter values into the model/optimiser domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with one column per parameter holding raw values, as
produced by :meth: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple |
Tensor
|
of shape |
tuple[Tensor, Tensor]
|
columns, and |
tuple[Tensor, Tensor]
|
|
Source code in src/banditry/variable_domains/design_space.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
inverse_transform(x_numerical, x_categorical)
Map transformed tensors back to a DataFrame of raw parameter values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_numerical
|
Tensor
|
Tensor of shape |
required |
x_categorical
|
Tensor
|
Tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with one column per parameter holding raw values (floats, ints, |
DataFrame
|
bools and the original category labels). |
Source code in src/banditry/variable_domains/design_space.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
to_pymoo_vars()
Express the design space as a dict of pymoo decision variables.
Returns:
| Type | Description |
|---|---|
|
Dict mapping each parameter name to a pymoo variable: |
|
|
continuous parameters, |
|
|
discrete after transform ( |
|
|
category indices for categorical parameters. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a parameter is neither numeric nor categorical. |
Source code in src/banditry/variable_domains/design_space.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
sample(num_samples=1)
Draw uniform random samples of the raw parameter values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples
|
int
|
Number of rows to draw. |
1
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame of shape |
DataFrame
|
attr: |
Source code in src/banditry/variable_domains/design_space.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
banditry.variable_domains.parameter_types
Parameter(param_dict)
Bases: ABC
Abstract base class for a single design-space parameter.
A parameter converts values between two representations:
- the raw domain, as produced by :meth:
sampleand seen by the user (floats, ints, bools, category labels), and - the transformed / optimiser domain, a numeric encoding bounded by
:attr:
opt_lband :attr:opt_ubthat surrogate models and acquisition optimisers operate on.
Subclasses implement :meth:sample, :meth:transform (raw to transformed) and
:meth:inverse_transform (transformed back to raw), plus the is_numeric,
is_discrete and is_discrete_after_transform flags that determine how the
optimiser treats the parameter. is_categorical is simply not is_numeric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_dict
|
Spec dict for this parameter; must contain at least |
required |
Source code in src/banditry/variable_domains/parameter_types.py
27 28 29 30 | |
NumericParameter(param_dict)
Bases: Parameter
Continuous parameter on [lb, ub] (spec type "num").
Reads the spec keys "name", "lb" and "ub". The transform is the
identity, so the optimiser domain equals the raw domain with bounds
opt_lb = lb and opt_ub = ub. Sampling is uniform on [lb, ub).
Source code in src/banditry/variable_domains/parameter_types.py
82 83 84 85 | |
CategoricalParameter(param)
Bases: Parameter
Categorical parameter over a fixed set of choices (spec type "cat").
Reads the spec keys "name" and "categories". transform maps each
category label to its (float) index in categories; inverse_transform
rounds to the nearest index and returns the corresponding label. Optimiser
bounds are [0, len(categories) - 1] and sampling is uniform over the
categories.
Source code in src/banditry/variable_domains/parameter_types.py
128 129 130 131 132 133 134 135 136 | |
BoolParameter(param)
Bases: Parameter
Boolean parameter (spec type "bool").
Reads only the spec key "name". transform casts to float (0.0/1.0);
inverse_transform thresholds at 0.5 (x > 0.5), so any optimiser value in
[0, 1] maps back to a bool. Sampling is a fair coin flip.
Source code in src/banditry/variable_domains/parameter_types.py
185 186 187 188 | |
IntParameter(param_dict)
Bases: Parameter
Integer parameter on the inclusive range [lb, ub] (spec type "int").
Reads the spec keys "name", "lb" and "ub" (rounded to the nearest
integer). transform casts to float; inverse_transform rounds back to the
nearest integer. Sampling is uniform over the integers in [lb, ub].
Source code in src/banditry/variable_domains/parameter_types.py
229 230 231 232 | |