-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_PPO.py
274 lines (193 loc) · 8.32 KB
/
train_PPO.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
import time
import random
import logging
import argparse
import itertools
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from stable_baselines3 import PPO
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from stable_baselines3.common.preprocessing import get_flattened_obs_dim
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.policies import ActorCriticPolicy
import bundle
import entryfee
from utils import init_logger
from distribution import UNIF, ASYM, COMB1, COMB2, UNIFScale
""" Hyper-parameters """
class Args:
""" Env Params ------------------------------ """
env_type: str = ""
""" Environment Type (To be filled later) """
num_agents: int = 0
"""Number of agents (To be filled later) """
num_items: int = 0
"""Number of items (To be filled later) """
""" Policy Params ---------------------------- """
log_std_init: float = -2
"""std for exploration"""
num_hidden_units: int = 256
""" Number of hidden units"""
num_hidden_layers: int = 3
""" Number of hidden layers """
d_model: int = 12
""" Positional Embedding Dimensions """
""" Optimization Params ---------------------- """
learning_rate: float = 3e-4
"""Learning Rate """
batch_size: int = 1024
""" Minibatch size """
num_envs: int = 1024
""" Number of parallel environments """
gamma: float = 1.0
""" Discount Factor """
""" Miscellaneous Params --------------------- """
device: str = "cuda"
""" CUDA or CPU """
max_iteration: int = 20000
""" Max iteration """
print_iter: int = 100
""" When to log stats """
seed: int = 24
"""seed of the experiment"""
""" Positional Encoding Feature Extractor """
class CustomFeatureExtractor(BaseFeaturesExtractor):
"""
Base class that represents a features extractor.
:param observation_space:
:param features_dim: Number of features extracted.
"""
def __init__(self, observation_space, d_model, max_len):
self._observation_space = observation_space
self._features_dim = get_flattened_obs_dim(observation_space) - 1 + d_model
super().__init__(observation_space, self._features_dim)
self.flatten = nn.Flatten()
position = torch.arange(max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) * (-np.log(10000.0) / d_model))
pe = torch.zeros(max_len, d_model)
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
@property
def features_dim(self):
return self._features_dim
def forward(self, obs):
obs = self.flatten(obs)
agent_idx = (obs[:, -1]).type(torch.long)
pe_features = self.pe[agent_idx]
return torch.cat((obs[:, :-1], pe_features), dim = -1)
class ActorCriticNetworkBundle(ActorCriticPolicy):
def _get_action_dist_from_latent(self, latent_pi):
"""
Retrieve action distribution given the latent codes.
:param latent_pi: Latent code for the actor
:return: Action distribution
"""
sigmoid_offset = 0.5
mean_actions = torch.sigmoid(self.action_net(latent_pi) - sigmoid_offset)
return self.action_dist.proba_distribution(mean_actions, self.log_std)
class ActorCriticNetworkEntryFee(ActorCriticPolicy):
def _get_action_dist_from_latent(self, latent_pi):
"""
Retrieve action distribution given the latent codes.
:param latent_pi: Latent code for the actor
:return: Action distribution
"""
sigmoid_offset = 0.5
softplus_offset = 1.0
mean_actions = self.action_net(latent_pi)
posted_price = F.sigmoid(mean_actions[..., :-1] - sigmoid_offset)
entry_fee = F.softplus(mean_actions[..., -1:] - softplus_offset)
mean_actions = torch.cat([posted_price, entry_fee], dim = -1)
return self.action_dist.proba_distribution(mean_actions, self.log_std)
if __name__ == "__main__":
""" Parse arguments """
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num_agents', action='store',
dest='num_agents', required=True, type=int,
help='Num Agents')
parser.add_argument('-m', '--num_items', action='store',
dest='num_items', required=True, type=int,
help='Num Items')
parser.add_argument('-l', '--learning_rate', action='store',
dest='lr', required=True, type=float,
help='Learning Rate')
parser.add_argument('-e', '--env_type', action='store',
dest='env_type', required=True, type=str,
help='Env Type')
cmd_args = parser.parse_args()
""" Set hyper-params """
args = Args()
args.num_agents = cmd_args.num_agents
args.num_items = cmd_args.num_items
args.env_type = cmd_args.env_type
args.learning_rate = cmd_args.lr
""" Environment Type """
if args.num_items <= 10:
if args.env_type == "unif":
v_dist = UNIF(args.num_items, demand = None)
elif args.env_type == "unit":
v_dist = UNIF(args.num_items, demand = 1)
elif args.env_type == "3demand":
v_dist = UNIF(args.num_items, demand = 3)
elif args.env_type == "asym":
v_dist = ASYM(args.num_items, demand = None)
elif args.env_type == "comb1":
v_dist = COMB1(args.num_items, demand = None)
elif args.env_type == "comb2":
v_dist = COMB2(args.num_items, demand = None)
else:
print("Auction Env not supported")
exit(1)
env_class = bundle.AuctionEnv
policy_class = ActorCriticNetworkBundle
elif args.env_type == "unif":
v_dist = UNIFScale(args.num_items, demand = None)
env_class = entryfee.AuctionEnv
policy_class = ActorCriticNetworkEntryFee
else:
print("Auction Env not supported")
exit(1)
if args.num_agents <= 5:
args.max_iteration = 10000
""" Loggers """
log_fname = os.path.join("experiments", "PPO", "%s_%dx%d"%(args.env_type, args.num_agents, args.num_items))
logger = init_logger(log_fname)
""" Seed for reproducibility """
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
env_kwargs = {'num_agents': args.num_agents, 'num_items': args.num_items, 'v_dist': v_dist}
envs = make_vec_env(env_class, args.num_envs, env_kwargs = env_kwargs)
eval_env = make_vec_env(env_class, args.num_envs, env_kwargs = env_kwargs)
""" Set policy params """
policy_kwargs = dict(
log_std_init = args.log_std_init,
net_arch = dict(pi=[args.num_hidden_units] * args.num_hidden_layers, vf=[args.num_hidden_units] * args.num_hidden_layers),
features_extractor_class=CustomFeatureExtractor,
features_extractor_kwargs=dict(d_model = args.d_model, max_len = args.num_agents + 1),
)
""" Init PPO model """
model = PPO(policy_class,
envs,
learning_rate = args.learning_rate,
n_steps = args.num_agents,
batch_size = args.batch_size,
gamma = args.gamma,
policy_kwargs = policy_kwargs)
""" Training """
tic = time.time()
for iteration in range(args.max_iteration):
model.learn(args.num_envs * args.num_agents)
if iteration % args.print_iter == 0:
t = time.time() - tic
rev_eval = evaluate_policy(model, eval_env, n_eval_episodes=10240)[0]
logger.info("[Iter]: %d, [Time Elapsed]: %.4f, [Rev]: %.6f"%(iteration, t, rev_eval))
t = time.time() - tic
rev_eval = evaluate_policy(model, eval_env, n_eval_episodes=10240)[0]
logger.info("[Iter]: %d, [Time Elapsed]: %.4f, [Rev]: %.6f"%(iteration, t, rev_eval))