class TestTrial:
@staticmethod
def suggest_float(name: str, low: float, high: float, **kwargs) -> float:
return np.random.uniform(low, high)
Constrained Search Space
Generate constrained search space. Alert if constraints are not satisfiable.
Trial
Trial (*args, **kwargs)
Protocol for a trial object
ConstrainedSearchSpace
ConstrainedSearchSpace (bounds:dict[str,tuple[float,float]], constraint:tuple[float,float])
A class that generates a search space with constraints
ConstrainedSearchSpace.__call__
ConstrainedSearchSpace.__call__ (trial:__main__.Trial)
Sample from constrained search space
Type | Details | |
---|---|---|
trial | Trial | trial object |
Returns | dict | selected budget |
= np.random.default_rng(44)
RNG = np.exp(RNG.normal(7, 2, 5)) # generate some random spends
actual_spends = {f"dim_{dim}": (np.round(.8*spend, 2), np.round(1.2*spend, 2)) for dim, spend in enumerate(actual_spends)} # generate bounds for each spend
bounds = sum(actual_spends) # calculate the total spend
total_spend = (total_spend, total_spend) # set the constraint to be the total spend
constraint = TestTrial() # creates a mock trial
trial = ConstrainedSearchSpace(bounds=bounds, constraint=constraint) # create the search space
search_space = search_space(trial) # get the first sample
selected_budget for name, sample in selected_budget.items(): # iterate over the sample
print(f"Sample {name}: {sample:.2f}, Bound {name}: {bounds[name]}") # print the sample and the bound
print(f"Sample Total: {sum(selected_budget.values()):.2f}, Total: {total_spend:.2f}") # print the total of the sample and the total spend
Sample dim_1: 1297.19, Bound dim_1: (1076.5, 1614.74)
Sample dim_2: 2059.89, Bound dim_2: (1686.42, 2529.63)
Sample dim_4: 5580.39, Bound dim_4: (4561.22, 6841.82)
Sample dim_3: 12345.41, Bound dim_3: (8516.67, 12775.01)
Sample dim_0: 18288.21, Bound dim_0: (15816.07, 23724.11)
Sample Total: 39571.09, Total: 39571.09