-
Notifications
You must be signed in to change notification settings - Fork 6
/
NormalizedActions.py
34 lines (28 loc) · 1.15 KB
/
NormalizedActions.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
import gym
class NormalizedActions(gym.ActionWrapper):
"""
OpenAI Gym Wrapper to normalize the action.
"""
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
:param action: action normalized in [-1, 1]
:return: 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]
:param action: action in the action-space range
:return: 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