-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_cig.py
executable file
·338 lines (273 loc) · 13.7 KB
/
main_cig.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import copy
import glob
import os
import time
import sys
import signal
import pickle
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data.sampler import BatchSampler, SubsetRandomSampler
from event_buffer import EventBuffer, EventBufferSQLProxy
from arguments import get_args
from envs import make_env, make_cig_env
from vec_env_cig import VecEnv
from model import CNNPolicy
from storage import RolloutStorage
from vizdoom import *
# from gif import make_gif
envs = None
args = get_args()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
def main():
print("###############################################################")
print("#################### VIZDOOM LEARNER START ####################")
print("###############################################################")
save_path = os.path.join(args.save_dir, "a2c")
num_updates = int(args.num_frames) // args.num_steps // args.num_processes
reward_name = ""
if args.roe:
reward_name = "_event"
scenario_name = args.config_path.split("/")[1].split(".")[0]
print("############### " + scenario_name + " ###############")
log_file_name = "log/vizdoom_" + scenario_name + reward_name + "_" + str(args.agent_id) + ".log"
log_event_file_name = "log/vizdoom_" + scenario_name + reward_name + "_" + str(args.agent_id) + ".eventlog"
log_event_reward_file_name = "log/vizdoom_" + scenario_name + reward_name + "_" + str(args.agent_id) + ".eventrewardlog"
start_updates = 0
start_step = 0
best_final_rewards = -1000000.0
os.environ['OMP_NUM_THREADS'] = '1'
# ---- DOOM Environments
# Host:
game = DoomGame()
# Use CIG example config or your own.
game.load_config("../../scenarios/cig.cfg")
game.set_doom_map("map01") # Limited deathmatch.
#game.set_doom_map("map02") # Full deathmatch.
# Host game with options that will be used in the competition.
game.add_game_args("-host 2 " # This machine will function as a host for a multiplayer game with this many players (including this machine). It will wait for other machines to connect using the -join parameter and then start the game when everyone is connected.
"-deathmatch " # Deathmatch rules are used for the game.
"+timelimit 10.0 " # The game (episode) will end after this many minutes have elapsed.
"+sv_forcerespawn 1 " # Players will respawn automatically after they die.
"+sv_noautoaim 1 " # Autoaim is disabled for all players.
"+sv_respawnprotect 1 " # Players will be invulnerable for two second after spawning.
"+sv_spawnfarthest 1 " # Players will be spawned as far as possible from any other players.
"+sv_nocrouch 1 " # Disables crouching.
"+viz_respawn_delay 10 " # Sets delay between respanws (in seconds).
"+viz_nocheat 1") # Disables depth and labels buffer and the ability to use commands that could interfere with multiplayer game.
# This can be used to host game without taking part in it (can be simply added as argument of vizdoom executable).
game.add_game_args("viz_spectator 1")
# During the competition, async mode will be forced for all agents.
game.set_mode(Mode.ASYNC_PLAYER)
# Start game
game.init()
# Clients:
global envs
es = [make_cig_env(i, visual=args.visual) for i in range(args.num_processes)]
envs = VecEnv([
es[i] for i in range(args.num_processes)
])
obs_shape = envs.observation_space_shape
obs_shape = (obs_shape[0] * args.num_stack, *obs_shape[1:])
if args.resume:
actor_critic = torch.load(os.path.join(save_path, log_file_name + ".pt"))
filename = glob.glob(os.path.join(args.log_dir, log_file_name))[0]
#if args.roe:
# TODO: Load event buffer
with open(filename) as file:
lines = file.readlines()
start_updates = (int)(lines[-1].strip().split(",")[0])
start_steps = (int)(lines[-1].strip().split(",")[1])
num_updates += start_updates
else:
if not args.debug:
try:
os.makedirs(args.log_dir)
except OSError:
files = glob.glob(os.path.join(args.log_dir, log_file_name))
for f in files:
os.remove(f)
with open(log_file_name, "w") as myfile:
myfile.write("")
files = glob.glob(os.path.join(args.log_dir, log_event_file_name))
for f in files:
os.remove(f)
with open(log_event_file_name, "w") as myfile:
myfile.write("")
files = glob.glob(os.path.join(args.log_dir, log_event_reward_file_name))
for f in files:
os.remove(f)
with open(log_event_reward_file_name, "w") as myfile:
myfile.write("")
actor_critic = CNNPolicy(obs_shape[0], envs.action_space_shape)
action_shape = 1
if args.cuda:
actor_critic.cuda()
optimizer = optim.RMSprop(actor_critic.parameters(), args.lr, eps=args.eps, alpha=args.alpha)
rollouts = RolloutStorage(args.num_steps, args.num_processes, obs_shape, envs.action_space_shape)
current_obs = torch.zeros(args.num_processes, *obs_shape)
def update_current_obs(obs):
shape_dim0 = envs.observation_space_shape[0]
obs = torch.from_numpy(obs).float()
if args.num_stack > 1:
current_obs[:, :-shape_dim0] = current_obs[:, shape_dim0:]
current_obs[:, -shape_dim0:] = obs
obs = envs.reset()
update_current_obs(obs)
last_game_vars = []
for i in range(args.num_processes):
last_game_vars.append(np.zeros(args.num_events))
rollouts.observations[0].copy_(current_obs)
# These variables are used to compute average rewards for all processes.
episode_rewards = torch.zeros([args.num_processes, 1])
final_rewards = torch.zeros([args.num_processes, 1])
episode_intrinsic_rewards = torch.zeros([args.num_processes, 1])
final_intrinsic_rewards = torch.zeros([args.num_processes, 1])
episode_events = torch.zeros([args.num_processes, args.num_events])
final_events = torch.zeros([args.num_processes, args.num_events])
if args.cuda:
current_obs = current_obs.cuda()
rollouts.cuda()
# Create event buffer
if args.qd:
event_buffer = EventBufferSQLProxy(args.num_events, args.capacity, args.exp_id, args.agent_id)
elif not args.resume:
event_buffer = EventBuffer(args.num_events, args.capacity)
else:
event_buffer = pickle.load(open(log_file_name + "_event_buffer_temp.p", "rb"))
event_episode_rewards = []
start = time.time()
for j in np.arange(start_updates, num_updates):
for step in range(args.num_steps):
value, action = actor_critic.act(Variable(rollouts.observations[step], volatile=True))
cpu_actions = action.data.squeeze(1).cpu().numpy()
obs, reward, done, info, events = envs.step(cpu_actions)
intrinsic_reward = []
# Fix broken rewards - upscale
for i in range(len(reward)):
if scenario_name in ["deathmatch", "my_way_home"]:
reward[i] *= 100
if scenario_name == "deadly_corridor":
reward[i] = 1 if events[i][2] >= 1 else 0
for e in events:
if args.roe:
intrinsic_reward.append(event_buffer.intrinsic_reward(e))
else:
r = reward[len(intrinsic_reward)]
intrinsic_reward.append(r)
reward = torch.from_numpy(np.expand_dims(np.stack(reward), 1)).float()
intrinsic_reward = torch.from_numpy(np.expand_dims(np.stack(intrinsic_reward), 1)).float()
#events = torch.from_numpy(np.expand_dims(np.stack(events), args.num_events)).float()
events = torch.from_numpy(events).float()
episode_rewards += reward
episode_intrinsic_rewards += intrinsic_reward
episode_events += events
# Event stats
event_rewards = []
for ei in range(0,args.num_events):
ev = np.zeros(args.num_events)
ev[ei] = 1
er = event_buffer.intrinsic_reward(ev)
event_rewards.append(er)
event_episode_rewards.append(event_rewards)
# If done then clean the history of observations.
masks = torch.FloatTensor([[0.0] if done_ else [1.0] for done_ in done])
final_rewards *= masks
final_intrinsic_rewards *= masks
final_events *= masks
final_rewards += (1 - masks) * episode_rewards
final_intrinsic_rewards += (1 - masks) * episode_intrinsic_rewards
final_events += (1 - masks) * episode_events
for i in range(args.num_processes):
if done[i]:
event_buffer.record_events(np.copy(final_events[i].numpy()), frame=j*args.num_steps)
episode_rewards *= masks
episode_intrinsic_rewards *= masks
episode_events *= masks
if args.cuda:
masks = masks.cuda()
if current_obs.dim() == 4:
current_obs *= masks.unsqueeze(2).unsqueeze(2)
else:
current_obs *= masks
update_current_obs(obs)
rollouts.insert(step, current_obs, action.data, value.data, intrinsic_reward, masks)
final_episode_reward = np.mean(event_episode_rewards, axis=0)
event_episode_rewards = []
next_value = actor_critic(Variable(rollouts.observations[-1], volatile=True))[0].data
if hasattr(actor_critic, 'obs_filter'):
actor_critic.obs_filter.update(rollouts.observations[:-1].view(-1, *obs_shape))
rollouts.compute_returns(next_value, args.use_gae, args.gamma, args.tau)
values, action_log_probs, dist_entropy = actor_critic.evaluate_actions(Variable(rollouts.observations[:-1].view(-1, *obs_shape)), Variable(rollouts.actions.view(-1, action_shape)))
values = values.view(args.num_steps, args.num_processes, 1)
action_log_probs = action_log_probs.view(args.num_steps, args.num_processes, 1)
advantages = Variable(rollouts.returns[:-1]) - values
value_loss = advantages.pow(2).mean()
action_loss = -(Variable(advantages.data) * action_log_probs).mean()
optimizer.zero_grad()
(value_loss * args.value_loss_coef + action_loss - dist_entropy * args.entropy_coef).backward()
nn.utils.clip_grad_norm(actor_critic.parameters(), args.max_grad_norm)
optimizer.step()
rollouts.observations[0].copy_(rollouts.observations[-1])
if final_rewards.mean() > best_final_rewards and not args.debug:
try:
os.makedirs(save_path)
except OSError:
pass
best_final_rewards = final_rewards.mean()
save_model = actor_critic
if args.cuda:
save_model = copy.deepcopy(actor_critic).cpu()
torch.save(save_model, os.path.join(save_path, log_file_name.split(".log")[0] + ".pt"))
if j % args.save_interval == 0 and args.save_dir != "" and not args.debug:
try:
os.makedirs(save_path)
except OSError:
pass
# A really ugly way to save a model to CPU
save_model = actor_critic
if args.cuda:
save_model = copy.deepcopy(actor_critic).cpu()
torch.save(save_model, os.path.join(save_path, log_file_name + "_temp.pt"))
if isinstance(event_buffer, EventBuffer):
pickle.dump(event_buffer, open( log_file_name + "_event_buffer_temp.p", "wb" ))
if j % args.log_interval == 0:
envs.log()
end = time.time()
total_num_steps = (j + 1) * args.num_processes * args.num_steps
log = "Updates {}, num timesteps {}, FPS {}, mean/max reward {:.5f}/{:.5f}, mean/max intrinsic reward {:.5f}/{:.5f}"\
.format(j, total_num_steps,
int(total_num_steps / (end - start)),
final_rewards.mean(),
final_rewards.max(),
final_intrinsic_rewards.mean(),
final_intrinsic_rewards.max()
)
log_to_file = "{}, {}, {:.5f}, {:.5f}, {:.5f}, {:.5f}\n" \
.format(j, total_num_steps,
final_rewards.mean(),
final_rewards.std(),
final_intrinsic_rewards.mean(),
final_intrinsic_rewards.std())
log_to_event_file = ','.join(map(str, event_buffer.get_event_mean().tolist())) + "\n"
log_to_event_reward_file = ','.join(map(str, event_buffer.get_event_rewards().tolist())) + "\n"
print(log)
print(log_to_event_file)
# Save to files
with open(log_file_name, "a") as myfile:
myfile.write(log_to_file)
with open(log_event_file_name, "a") as myfile:
myfile.write(str(total_num_steps) + "," + log_to_event_file)
with open(log_event_reward_file_name, "a") as myfile:
myfile.write(str(total_num_steps) + "," + log_to_event_reward_file)
envs.close()
game.close()
time.sleep(5)
if __name__ == "__main__":
main()