-
Notifications
You must be signed in to change notification settings - Fork 6
/
experiments.py
145 lines (124 loc) · 4.33 KB
/
experiments.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import itertools
import random
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Generator, Optional
from docker import DockerClient
from docker.models.containers import Container
from loguru import logger
sampling_rates_hz = (10, 20, 40, 60)
delays_ms = (0, 25, 50, 100)
start_run = 1
n_runs = 10
tick_rates_hz = (120,)
emu_duration = '5m'
base_output_dir = Path('/opt/cleave')
cleave_img = 'molguin/cleave:cleave'
@contextmanager
def docker_client(*args, **kwargs) -> Generator[DockerClient, None, None]:
client: DockerClient = DockerClient(*args, **kwargs)
yield client
client.close()
def run_experiment(
srate: int,
delay_ms: int,
run_idx: int,
trate: int = 120
) -> None:
# first thing first, create the output folder
logger.warning(f'Running: sampling_rate={srate}, '
f'delay_ms={delay_ms}, '
f'tick_rate_hz={trate}, '
f'run_idx={run_idx}')
exp_name = f'cleave_local_s{srate:03d}Hz' \
f'_t{trate:03d}Hz' \
f'_d{delay_ms:03d}ms'
output_dir = (base_output_dir / exp_name).resolve()
output_dir.mkdir(parents=True, exist_ok=True)
control_port = 50000
with docker_client() as client:
# start the controller
client: DockerClient = client
controller: Container = client.containers.run(
detach=True,
remove=True,
image=cleave_img,
command=['run-controller',
'examples/inverted_pendulum/controller/config.py'],
network='bridge',
environment={
'PORT': control_port,
'NAME': f'controller.run_{run_idx:02d}',
},
volumes=[f'{output_dir}:/opt/controller_metrics/']
)
# get IP of controller inside Docker network
ctrl_addr = None
while ctrl_addr is None or len(ctrl_addr) == 0:
controller.reload()
ctrl_addr = controller.attrs['NetworkSettings']['IPAddress']
time.sleep(0.1)
ctrl_addr = str(ctrl_addr)
logger.warning(f'Controller address: {ctrl_addr}')
# if delay, start the proxy
if delay_ms > 0:
proxy: Optional[Container] = client.containers.run(
detach=True,
remove=True,
image='expeca/awsproxy:latest',
network='bridge',
environment={
'SERVERIP': ctrl_addr,
'DELAY' : delay_ms // 2
},
cap_add='NET_ADMIN'
)
# get the address of the proxy
proxy_addr = None
while proxy_addr is None or len(ctrl_addr) == 0:
proxy.reload()
proxy_addr = proxy.attrs['NetworkSettings']['IPAddress']
time.sleep(0.1)
logger.warning(f'Proxy address: {proxy_addr}')
ctrl_addr = proxy_addr
else:
proxy = None
# run the plant
try:
plant = client.containers.run(
detach=False,
remove=True,
image=cleave_img,
command=['run-plant',
'examples/inverted_pendulum/plant/config.py'],
network='bridge',
environment={
'NAME' : f'plant.run_{run_idx:02d}',
'CONTROLLER_ADDRESS': ctrl_addr,
'CONTROLLER_PORT' : control_port,
'TICK_RATE' : f'{trate:d}',
'EMU_DURATION' : emu_duration,
'FAIL_ANGLE_RAD' : -1,
'SAMPLE_RATE' : f'{srate:d}'
},
volumes=[f'{output_dir}:/opt/plant_metrics/']
)
finally:
# plant done, tear down controller
controller.stop()
if proxy is not None:
proxy.stop()
if __name__ == '__main__':
# create combinations of parameters
combs = list(itertools.product(
sampling_rates_hz,
delays_ms,
range(start_run, start_run + n_runs),
tick_rates_hz
))
# shuffle combinations
random.shuffle(combs)
# run combinations
for params in combs:
run_experiment(*params)