forked from thomas-young-2013/open-box
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ConditionedSpace to support complex conditions between hyperparam…
…eters (#37)
- Loading branch information
1 parent
9976707
commit be455c2
Showing
6 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import sys | ||
sys.path.insert(0, '.') | ||
|
||
import traceback | ||
from openbox import sp | ||
from openbox.utils.config_space import get_one_exchange_neighbourhood | ||
from ConfigSpace import Configuration | ||
import numpy as np | ||
import pickle as pkl | ||
|
||
|
||
verbose = True | ||
|
||
|
||
def sample_condition(config): | ||
if verbose: | ||
print('- sample_condition called') | ||
# We want x1 >= x2 >= x3 | ||
if config['x1'] < config['x2']: | ||
return False # violate condition | ||
if config['x2'] < config['x3']: | ||
return False # violate condition | ||
return True # valid | ||
|
||
|
||
if __name__ == '__main__': | ||
cs = sp.ConditionedSpace() | ||
for i in range(1, 4): | ||
cs.add_hyperparameter(sp.Int('x%d' % i, 0, 100, default_value=0)) | ||
print(cs) | ||
cs.set_sample_condition(sample_condition) | ||
cs.seed(1234) | ||
print('space initialized.') | ||
|
||
print('\nsample 10 configs:') | ||
configs = cs.sample_configuration(10) | ||
print('\nvalidate configs:') | ||
for config in configs: | ||
config.is_valid_configuration() | ||
is_valid = sample_condition(config) | ||
print(config, is_valid) | ||
if not is_valid: | ||
raise ValueError('sampled invalid config') | ||
|
||
print('\ntest invalid values (dict)') | ||
try: | ||
config = Configuration(cs, values=dict(x1=1, x2=2, x3=3)) | ||
except ValueError as e: | ||
traceback.print_exc() | ||
print('invalid values caught') | ||
print('\ntest invalid vector (np.array)') | ||
config = Configuration(cs, vector=np.array([0.1, 0.2, 0.3])) # will not check in initialization with vector | ||
try: | ||
config.is_valid_configuration() | ||
except ValueError as e: | ||
traceback.print_exc() | ||
print('invalid vector caught') | ||
print('\ntest invalid neighbors') | ||
config = Configuration(cs, values=dict(x1=100, x2=50, x3=10)) | ||
neighbors = list(get_one_exchange_neighbourhood(config, seed=2)) | ||
print(len(neighbors), 'neighbors') | ||
assert all([sample_condition(config) for config in neighbors]) | ||
|
||
verbose = False | ||
print('\nsample 10000 configs') | ||
configs = cs.sample_configuration(10000) | ||
assert all([sample_condition(config) for config in configs]) | ||
|
||
print('\ntest pickle') | ||
bcs = pkl.dumps(cs) | ||
cs = pkl.loads(bcs) | ||
cs.sample_configuration(10) | ||
|
||
print('\nAll tests passed!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
sys.path.insert(0, '.') | ||
|
||
from openbox import sp, Optimizer | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def sample_condition(config): | ||
# We want x1 >= x2 | ||
if config['x1'] < config['x2']: | ||
return False # violate condition | ||
return True # valid | ||
|
||
|
||
# Define Search Space | ||
space = sp.ConditionedSpace() | ||
x1 = sp.Real("x1", -5, 10, default_value=0) | ||
x2 = sp.Real("x2", 0, 15, default_value=0) | ||
space.add_variables([x1, x2]) | ||
space.set_sample_condition(sample_condition) | ||
|
||
|
||
# Define Objective Function | ||
def branin(config): | ||
x1, x2 = config['x1'], config['x2'] | ||
y = (x2 - 5.1 / (4 * np.pi ** 2) * x1 ** 2 + 5 / np.pi * x1 - 6) ** 2 \ | ||
+ 10 * (1 - 1 / (8 * np.pi)) * np.cos(x1) + 10 | ||
return {'objs': (y,)} | ||
|
||
|
||
# Run | ||
if __name__ == "__main__": | ||
opt = Optimizer( | ||
branin, | ||
space, | ||
max_runs=50, | ||
surrogate_type='gp', | ||
acq_optimizer_type='random_scipy', | ||
time_limit_per_trial=30, | ||
task_id='test_cs', | ||
random_state=1, | ||
) | ||
history = opt.run() | ||
|
||
print(history) | ||
|
||
history.plot_convergence(true_minimum=0.397887) | ||
plt.show() | ||
|
File renamed without changes.