-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolicy_utils.py
211 lines (196 loc) · 7.15 KB
/
policy_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import torch
import tianshou as ts
from tianshou.utils.net.common import Net, ActorCritic, MLP
from tianshou.utils.net.discrete import Actor, Critic, IntrinsicCuriosityModule
import numpy as np
from net.basic import BasicCriticNet
from net.norm_net import NormalizedNet
from utils.hint_utils import get_novel_action_indices
from policies import BiasedDQN
from config import POLICIES, POLICY_PROPS
def create_policy_for_matrix(
rl_algo,
state_space,
action_space,
all_actions,
novel_actions=[],
hidden_sizes=[256, 128, 64],
buffer=None,
lr=None,
device="cpu",
checkpoint=None
):
if lr is not None:
lr = float(lr)
if hidden_sizes is None:
hidden_sizes = [256, 128, 64]
if rl_algo == "ppo":
policy = ts.policy.PPOPolicy(
# TODO
)
def create_policy(
rl_algo,
state_shape,
action_shape,
all_actions,
novel_actions=[],
hidden_sizes=[256, 64],
buffer=None,
lr=None,
device="cpu",
checkpoint=None
):
if lr is not None:
lr = float(lr)
if hidden_sizes is None:
hidden_sizes = [256, 64]
PolicyModule = POLICIES[rl_algo]
policy_props = POLICY_PROPS.get(rl_algo) or {}
net = Net(state_shape, action_shape, hidden_sizes=[128, 64], softmax=True, device=device)
optim = torch.optim.Adam(net.parameters(), lr=lr or 1e-4)
# prepare policy
if "ppo_shared_net" in rl_algo:
net = Net(state_shape, hidden_sizes=hidden_sizes, device=device)
actor = Actor(net, action_shape, device=device)
critic = Critic(net, device=device)
actor_critic = ActorCritic(net, critic)
optim = torch.optim.Adam(actor_critic.parameters(), lr=lr or 5e-5)
ppo_policy = ts.policy.PPOPolicy(
actor=actor,
critic=critic,
optim=optim,
dist_fn=torch.distributions.Categorical
).to(device)
elif "ppo" in rl_algo:
# non-shared net ppo
critic = BasicCriticNet(state_shape, 1, device=device)
# net = Net(state_shape, hidden_sizes[0], device=device)
# actor = Actor(net, action_shape, hidden_sizes=hidden_sizes, softmax_output=True, device=device)
# critic = Critic(net, hidden_sizes=hidden_sizes, last_size=1, device=device)
actor_critic = ActorCritic(net, critic).to(device)
optim = torch.optim.Adam(actor_critic.parameters(), lr=lr or 1e-5)
ppo_policy = ts.policy.PPOPolicy(
actor=net,
critic=critic,
optim=optim,
dist_fn=torch.distributions.Categorical,
).to(device)
if rl_algo == "dqn":
policy = PolicyModule(
model=net,
optim=optim,
discount_factor=0.99,
estimation_step=3,
)
elif rl_algo == "novel_boost":
policy = PolicyModule(
model=net,
optim=optim,
discount_factor=0.99,
estimation_step=3,
novel_action_indices=get_novel_action_indices(all_actions, novel_actions),
num_actions=action_shape,
**policy_props
)
elif rl_algo == "ppo":
policy = ppo_policy
elif rl_algo == "ppo_shared_net":
policy = ppo_policy
elif rl_algo == "icm_ppo" or rl_algo == "icm_ppo_shared_net":
feature_dim = 16
lr_scale = 1.
reward_scale = 0.01
forward_loss_weight = 0.2
feature_net = MLP(
np.prod(state_shape),
output_dim=feature_dim,
hidden_sizes=hidden_sizes[:-1],
device=device
)
icm_module = IntrinsicCuriosityModule(
feature_net=feature_net,
feature_dim=feature_dim,
action_dim=np.prod(action_shape),
hidden_sizes=hidden_sizes[-1:],
device=device
).to(device)
icm_optim = torch.optim.Adam(icm_module.parameters(), lr=lr or 1e-4)
policy = ts.policy.ICMPolicy(
ppo_policy, icm_module, icm_optim, lr_scale, reward_scale,
forward_loss_weight
)
# elif rl_algo == "dsac":
# net_c1 = Net(state_shape, action_shape, hidden_sizes=[256, 128, 64])
# net_c2 = Net(state_shape, action_shape, hidden_sizes=[256, 128, 64])
# critic1 = Critic(net_c1, last_size=action_shape)
# critic2 = Critic(net_c2, last_size=action_shape)
# critic1_optim = torch.optim.Adam(critic1.parameters(), lr=lr or 1e-4)
# critic2_optim = torch.optim.Adam(critic2.parameters(), lr=lr or 1e-4)
# policy = ts.policy.DiscreteSACPolicy(
# actor=net,
# critic1=critic1,
# critic2=critic2,
# actor_optim=optim,
# critic1_optim=critic1_optim,
# critic2_optim=critic2_optim,
# )
## imitation learning
elif rl_algo == "crr":
net = Net(state_shape, hidden_sizes[0], device=device)
actor = NormalizedNet(
hidden_sizes[0],
action_shape,
preprocess_net=net,
hidden_sizes=hidden_sizes[1:],
device=device
)
critic = NormalizedNet(
hidden_sizes[0],
action_shape,
preprocess_net=net,
hidden_sizes=hidden_sizes[1:],
device=device,
output_state=False
)
actor_critic = ActorCritic(actor, critic)
optim = torch.optim.Adam(actor_critic.parameters(), lr=lr or 1e-6)
lr_scheduler = ts.utils.MultipleLRSchedulers()
policy = ts.policy.DiscreteCRRPolicy(
actor=actor,
critic=critic,
optim=optim,
discount_factor=0.99,
target_update_freq=320,
policy_improvement_mode="all"
).to(device)
elif rl_algo == "crr_separate_net":
actor = Net(state_shape, action_shape, hidden_sizes=hidden_sizes, device=device)
critic = BasicCriticNet(state_shape, action_shape).to(device)
actor_critic = ActorCritic(actor, critic)
optim = torch.optim.Adam(actor_critic.parameters(), lr=lr or 1e-6)
policy = ts.policy.DiscreteCRRPolicy(
actor=actor,
critic=critic,
optim=optim,
discount_factor=0.99,
target_update_freq=320,
policy_improvement_mode="all"
).to(device)
# elif rl_algo == "gail":
# critic = BasicCriticNet(state_shape, 1)
# disc_net = Net(state_shape + action_shape, 1, hidden_sizes=[256, 128, 64])
# disc_optim = torch.optim.Adam(disc_net.parameters(), lr=lr or 1e-4)
# policy = ts.policy.GAILPolicy(
# actor=net,
# critic=critic,
# optim=optim,
# dist_fn=torch.distributions.Categorical,
# expert_buffer=buffer,
# disc_net=disc_net,
# disc_optim=disc_optim
# )
if checkpoint is not None:
checkpoint = torch.load(checkpoint, map_location=device)
policy.load_state_dict(checkpoint["model"])
policy.optim.load_state_dict(checkpoint["optim"])
return policy