-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (37 loc) · 1.5 KB
/
utils.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
import numpy as np
import gym
from collections import deque
import random
class OUNoise(object):
def __init__(self, action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3,
decay_period=100000):
self.mu = mu
self.theta = theta
self.sigma = max_sigma
self.max_sigma = max_sigma
self.min_sigma = min_sigma
self.decay_period = decay_period
self.action_dim = action_space.shape[0]
self.low = action_space.low
self.high = action_space.high
self.reset()
def reset(self):
self.state = np.ones(self.action_dim)*self.mu
def evolve_state(self):
x = self.state
dx = self.theta * (self.mu - x) + self.sigma*np.random.randn(self.action_dim)
self.state = x + dx
return self.state
def get_action(self, action, t=0):
ou_state = self.evolve_state()
self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma)*min(1.0, t/self.decay_period)
return np.clip(action+ou_state, self.low, self.high)
class NormalizedEnv(gym.ActionWrapper):
def action(self, action):
act_k = (self.action_space.high - self.action_space.low)/ 2.
act_b = (self.action_space.high + self.action_space.low)/ 2.
return act_k*action + act_b
def reverse_action(self, action):
act_k_inv = 2./(self.action_space.high - self.action_space.low)
act_b = (self.action_space.high + self.action_space.low)/ 2.
return act_k_inv*(action - act_b)