-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpolicies.py
executable file
·50 lines (36 loc) · 1.38 KB
/
policies.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
import numpy as np
def setup_policy(env, theta):
if env.action_space.type == "discrete":
return DiscretePolicy(env, theta)
elif env.action_space.type == "continuous":
return ContinuousPolicy(env, theta)
else:
raise ValueError
class ContinuousPolicy:
def __init__(self, env, theta):
self.env = env
obs_shape = env.observation_space.shape[0]
act_shape = env.action_space.shape[0]
assert len(theta) == (obs_shape + 1) * act_shape
self.parameter_dim = obs_shape * act_shape
self.b = theta[self.parameter_dim :]
self.W = theta[: self.parameter_dim].reshape(obs_shape, act_shape)
def act(self, observation):
return np.clip(
observation.dot(self.W) + self.b,
self.env.action_space.low,
self.env.action_space.high,
)
class DiscretePolicy:
def __init__(self, env, theta):
self.env = env
obs_shape = env.observation_space.shape[0]
num_actions = env.action_space.n
assert len(theta) == (obs_shape + 1) * num_actions
self.parameter_dim = obs_shape * num_actions
self.W = theta[: self.parameter_dim].reshape(obs_shape, num_actions)
self.b = theta[self.parameter_dim :]
def act(self, observation):
y = observation.dot(self.W) + self.b
action = np.argmax(y)
return action