-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
44 lines (36 loc) · 1.17 KB
/
utils.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
import contextlib
import numpy as np
@contextlib.contextmanager
def temp_seed(seed):
state = np.random.get_state()
np.random.seed(seed)
try:
yield
finally:
np.random.set_state(state)
def EulerIntegrate(controller, f, B, xstar, ustar, xinit, t_max = 10, dt = 0.05, with_tracking = False, sigma = 0., noise_bound = None):
t = np.arange(0, t_max, dt)
trace = []
u = []
xcurr = xinit
trace.append(xcurr)
for i in range(len(t)):
if with_tracking:
xe = xcurr - xstar[i]
ui = controller(xcurr, xe, ustar[i]) if with_tracking else ustar[i]
if with_tracking:
# print(xcurr.reshape(-1), xstar[i].reshape(-1), ui.reshape(-1))
pass
if not noise_bound:
noise_bound = 3 * sigma
noise = np.random.randn(*xcurr.shape) * sigma
noise[noise>noise_bound] = noise_bound
noise[noise<-noise_bound] = -noise_bound
dx = f(xcurr) + B(xcurr).dot(ui) + noise
xnext = xcurr + dx*dt
# xnext[xnext>100] = 100
# xnext[xnext<-100] = -100
trace.append(xnext)
u.append(ui)
xcurr = xnext
return trace, u