-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolicy.py
58 lines (39 loc) · 1.3 KB
/
policy.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
49
50
51
52
53
54
55
56
57
58
import abc
import numpy as np
class Policy(abc.ABC):
"""Implements a policy \pi(a | s)."""
@abc.abstractmethod
def act(self, state):
"""Returns an action given the current state.
Args:
state (object): current state.
Returns:
action (int): action to take.
"""
raise NotImplementedError()
@property
def stats(self):
"""Returns a dict of relevant statistics about the policy."""
return {}
class RandomPolicy(Policy):
"""Acts uniformly at random on discrete actions."""
def __init__(self, action_space):
"""Constructs on a discrete action space.
Args:
action_space (spaces.Discrete): action space of the environment.
"""
self._action_space = action_space
def act(self, state, hidden_state, test=False):
del state, hidden_state, test
return np.random.randint(self._action_space.n), None
def update(self, experience):
pass
class ConstantActionPolicy(Policy):
"""Always returns the same action."""
def __init__(self, action):
self._action = action
def act(self, state, hidden_state, test=False):
del state, hidden_state, test
return self._action, None
def update(self, experience):
pass