-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoise.py
46 lines (42 loc) · 1.49 KB
/
noise.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
import numpy as np
class OrnsteinUhlenbeckActionNoise(object):
"""
An Ornstein Uhlenbeck action noise, this is designed to approximate Brownian motion with friction.
Based on http://math.stackexchange.com/questions/1287634/implementing-ornstein-uhlenbeck-in-matlab
:param mean: the mean of the noise
:param sigma: the scale of the noise
:param theta: the rate of mean reversion
:param dt: the timestep for the noise
:param initial_noise: the initial value for the noise output, (if None: 0)
"""
def __init__(
self,
mean,
sigma,
size,
theta: float = 0.15,
dt: float = 1e-2,
initial_noise = None,
):
self._theta = theta
self._mu = mean
self._sigma = sigma
self._dt = dt
self._size = size
self.initial_noise = initial_noise
self.noise_prev = np.zeros(self._size)
self.reset()
super(OrnsteinUhlenbeckActionNoise, self).__init__()
def __call__(self) -> np.ndarray:
noise = (
self.noise_prev
+ self._theta * (self._mu - self.noise_prev) * self._dt
+ self._sigma * np.sqrt(self._dt) * np.random.normal(size=self._size)
)
self.noise_prev = noise
return noise
def reset(self) -> None:
"""
reset the Ornstein Uhlenbeck noise, to the initial position
"""
self.noise_prev = self.initial_noise if self.initial_noise is not None else np.zeros_like(self._size)