Diff of /ADDPG/ou_noise.py [000000] .. [687a25]

Switch to unified view

a b/ADDPG/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
test = np.ones(18)*0.05
12
test[2] = 0.5
13
test[3] = 0.5
14
test[9] = 0.5
15
test[12] = 0.5
16
test[13] = 0.5
17
18
class OUNoise:
19
    """docstring for OUNoise"""
20
    def __init__(self,action_dimension,mu=0.0, theta=0.1, sigma=0.2):
21
        self.action_dimension = action_dimension
22
        self.mu = test
23
        self.theta = theta
24
        self.sigma = sigma
25
        self.state = self.mu
26
        self.reset(None)
27
28
    def reset(self,settings):
29
        if isinstance(settings,(list,np.ndarray)):
30
            self.mu = settings[0]
31
            self.theta = settings[1]
32
            self.state = np.ones(self.action_dimension) * self.mu
33
        else:
34
            #self.state = np.ones(self.action_dimension) * self.mu
35
            self.state = test
36
37
    def noise(self):
38
        x = self.state
39
        dx = self.theta * (self.mu - x) + self.sigma * nr.randn(len(x))
40
        self.state = x + dx
41
        return self.state
42
43
if __name__ == '__main__':
44
    ou = OUNoise(5)
45
    states = []
46
    for i in range(200):
47
        states.append([i for i in ou.noise()])
48
    import matplotlib.pyplot as plt
49
50
    plt.plot(states)
51
    plt.show()