-
Notifications
You must be signed in to change notification settings - Fork 6
/
normalized_actions.py
41 lines (33 loc) · 1.23 KB
/
normalized_actions.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
import gym
class NormalizedActions(gym.ActionWrapper):
def __init__(self, env):
"""
Args:
env:
"""
super().__init__(env)
def action(self, action):
"""Transform the action normalized between [-1, 1] to the correct
action-space bound of the environment. mod -> range of the actions tra
-> center of the actions
Args:
action: action normalized in [-1, 1]
Returns:
action de-normalized in the environment space
"""
mod = (self.action_space.high - self.action_space.low) / 2
tra = (self.action_space.high + self.action_space.low) / 2
action = action * mod + tra
# action = action * (self.action_space.high - self.action_space.low) + self.action_space.low
return action
def _reverse_action(self, action):
"""Normalize the action between [-1, 1]
Args:
action: action in the action-space range
Returns:
action normalized in [-1, 1]
"""
mod = (self.action_space.high - self.action_space.low) / 2
tra = (self.action_space.high + self.action_space.low) / 2
action = (action - tra) / mod
return action