mirror of
https://github.com/wassname/DeepRL.git
synced 2026-08-21 11:09:46 +08:00
32 lines
874 B
Python
32 lines
874 B
Python
import numpy as np
|
|
|
|
class RandomProcess(object):
|
|
def reset_states(self):
|
|
pass
|
|
|
|
class GaussianProcess(RandomProcess):
|
|
def __init__(self, size, std):
|
|
self.size = size
|
|
self.std = std
|
|
|
|
def sample(self):
|
|
return np.random.randn(*self.size) * self.std()
|
|
|
|
class OrnsteinUhlenbeckProcess(RandomProcess):
|
|
def __init__(self, size, std, theta=.15, dt=1e-2, x0=None):
|
|
self.theta = theta
|
|
self.mu = 0
|
|
self.std = std
|
|
self.dt = dt
|
|
self.x0 = x0
|
|
self.size = size
|
|
self.reset_states()
|
|
|
|
def sample(self):
|
|
x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + self.std() * np.sqrt(self.dt) * np.random.randn(*self.size)
|
|
self.x_prev = x
|
|
return x
|
|
|
|
def reset_states(self):
|
|
self.x_prev = self.x0 if self.x0 is not None else np.zeros(self.size)
|