-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoise.py
106 lines (85 loc) · 3.27 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
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
import numpy as np
#np.random.seed(0)
class AbstractNoise:
def __init__(self):
pass
def step_end(self):
pass
def episode_end(self):
pass
class GaussianNoise(AbstractNoise):
def __init__(self, mu, sigma, clip=None):
self.mu = mu
self.sigma = sigma
self.clip = clip
def __call__(self):
delta = self.sigma*np.random.normal(size=self.mu.shape)
if self.clip is not None:
delta = delta.clip(-self.clip,+self.clip)
return self.mu + delta
class OrnsteinUhlenbeckNoise(AbstractNoise):
def __init__(self, mu, theta = 0.15, sigma = 0.2, dt=0.02):
# 5.0, 0.02, 1.0 # 1.0, 0.02, 0.25 # 7.5, 0.02, 1.4 # 5.0, 0.02, 0.7
self.mu = mu
self.theta = theta
self.sigma = sigma
self.dt = dt
self.x_prev = np.zeros_like(self.mu)
def __call__(self):
x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + \
self.sigma * np.sqrt(self.dt) * np.random.normal(size=self.mu.shape)
self.x_prev = x
return x
class DecayingGaussianNoise(AbstractNoise):
def __init__(self, mu, sigma = 0.12, clip=None, sigma_decay = 0.999):
# 5.0, 0.02, 1.0 # 1.0, 0.02, 0.25 # 7.5, 0.02, 1.4 # 5.0, 0.02, 0.7
self.mu = mu
self.clip = clip
self.sigma = end_sigma
self.sigma_decay = sigma_decay
def episode_end(self):
self.sigma *= self.sigma_decay
def __call__(self):
delta = self.sigma*np.random.normal(size=self.mu.shape)
if self.clip is not None:
delta = delta.clip(-self.clip,+self.clip)
return x
class DecayingOrnsteinUhlenbeckNoise(AbstractNoise):
def __init__(self, mu, theta = 0.15, sigma = 0.2, dt=0.02, sigma_decay = 0.999):
# 5.0, 0.02, 1.0 # 1.0, 0.02, 0.25 # 7.5, 0.02, 1.4 # 5.0, 0.02, 0.7
self.mu = mu
self.theta = theta
self.sigma = sigma
self.dt = dt
self.x_prev = np.zeros_like(self.mu)
self.sigma_decay = sigma_decay
def __call__(self):
x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + \
self.sigma * np.sqrt(self.dt) * np.random.normal(size=self.mu.shape)
self.x_prev = x
return x
def episode_end(self):
self.sigma *= self.sigma_decay
class RandomNoise(AbstractNoise):
def __init__(self, mu, minval, maxval, prob=0.5):
self.mu = mu
self.minval = minval
self.maxval = maxval
self.prob = prob
def __call__(self):
rand_or_not = np.random.rand(*self.mu.shape) < self.prob
delta = self.minval + (self.maxval-self.minval)*np.random.rand(*self.mu.shape)
return self.mu + delta * rand_or_not
class DecayingRandomNoise(AbstractNoise):
def __init__(self, mu, minval, maxval, prob=0.5, decay = 0.999):
self.mu = mu
self.minval = minval
self.maxval = maxval
self.prob = prob
self.decay = decay
def __call__(self):
rand_or_not = np.random.rand(*self.mu.shape) < self.prob
delta = self.minval + (self.maxval-self.minval)*np.random.rand(*self.mu.shape)
return self.mu + delta * rand_or_not
def episode_end(self):
self.prob *= self.decay