a b/rdpg/ou_noise.py
1
# --------------------------------------
2
# Ornstein-Uhlenbeck Noise
3
# Author: Flood Sung
4
# Date: 2016.5.4
5
# Reference: https://github.com/rllab/rllab/blob/master/rllab/exploration_strategies/ou_strategy.py
6
# --------------------------------------
7
8
import numpy as np
9
import numpy.random as nr
10
11
class OUNoise:
12
    """docstring for OUNoise"""
13
    def __init__(self,action_dimension,mu=0, theta=0.15, sigma=0.2):
14
        self.action_dimension = action_dimension
15
        self.mu = mu
16
        self.theta = theta
17
        self.sigma = sigma
18
        self.state = np.ones(self.action_dimension) * self.mu
19
        self.reset()
20
21
    def reset(self):
22
        self.state = np.ones(self.action_dimension) * self.mu
23
24
    def noise(self):
25
        x = self.state
26
        dx = self.theta * (self.mu - x) + self.sigma * nr.randn(len(x))
27
        self.state = x + dx
28
        return self.state
29
30
if __name__ == '__main__':
31
    ou = OUNoise(3)
32
    states = []
33
    for i in range(1000):
34
        states.append(ou.noise())
35
    import matplotlib.pyplot as plt
36
37
    plt.plot(states)
38
    plt.show()