forked from thomas-young-2013/open-box
-
Notifications
You must be signed in to change notification settings - Fork 53
/
distributed_optimization.py
73 lines (60 loc) · 2.04 KB
/
distributed_optimization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# License: MIT
"""
Distributed Optimization Example
command line:
[Master]
python distributed_optimization.py --role master --n_workers 2 --parallel_strategy async --port 13579
[Worker]
python distributed_optimization.py --role worker --master_ip 127.0.0.1 --port 13579
"""
import argparse
import numpy as np
from openbox import space as sp, DistributedOptimizer, DistributedWorker
from openbox.utils.platform import get_platform
parser = argparse.ArgumentParser()
parser.add_argument('--role', type=str, choices=['master', 'worker'])
parser.add_argument('--n_workers', type=int)
parser.add_argument('--parallel_strategy', type=str, default='async', choices=['sync', 'async'])
parser.add_argument('--master_ip', type=str, default='127.0.0.1')
parser.add_argument('--port', type=int, default=13579)
# Parse args
args = parser.parse_args()
role = args.role
master_ip = args.master_ip
port = args.port
n_workers = args.n_workers
# Define Search Space
space = sp.Space()
x1 = sp.Real("x1", -5, 10, default_value=0)
x2 = sp.Real("x2", 0, 15, default_value=0)
space.add_variables([x1, x2])
# 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 {'objectives': [y]}
if __name__ == "__main__":
if role == 'master':
ip = master_ip if get_platform() == 'Windows' else ''
opt = DistributedOptimizer(
branin,
space,
parallel_strategy='async',
batch_size=n_workers,
batch_strategy='default',
num_objectives=1,
num_constraints=0,
max_runs=50,
# surrogate_type='gp',
surrogate_type='auto',
task_id='distributed_opt',
ip=master_ip,
port=port,
authkey=b'abc',
)
history = opt.run()
print(history)
else:
worker = DistributedWorker(branin, master_ip, port, authkey=b'abc')
worker.run()