-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.py
227 lines (178 loc) · 7.18 KB
/
actor.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
import collections
import zmq
import gym
import numpy as np
import statistics
import glob
import random
import tensorflow as tf
import cv2
import argparse
import os
from typing import Any, List, Sequence, Tuple
from absl import flags
import deepmind_lab
parser = argparse.ArgumentParser(description='CTF IMPALA Actor')
parser.add_argument('--env_id', type=int, default=0, help='ID of environment')
arguments = parser.parse_args()
env_id = arguments.env_id
if env_id == 0:
writer = tf.summary.create_file_writer("tensorboard_actor")
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
context = zmq.Context()
# Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:" + str(6555 + arguments.env_id))
# Create a new environment object.
width = 640
height = 640
fps = 15
action_repeat = int(60 / fps)
env = deepmind_lab.Lab("ctf_simple", ['RGB_INTERLEAVED', 'DEBUG.GADGET_AMOUNT', 'DEBUG.GADGET', 'DEBUG.HAS_RED_FLAG'],
{'fps': str(fps), 'width': str(width), 'height': str(height)})
#env = deepmind_lab.Lab("contributed/dmlab30/explore_goal_locations_small", ['RGB_INTERLEAVED'],
# {'fps': str(fps), 'width': str(width), 'height': str(height)})
def _action(*entries):
return np.array(entries, dtype=np.intc)
ACTION_LIST = []
for look_horizontal in [-10, -60, 0, 10, 60]:
for look_vertical in [-5, 0, 5]:
for strafe_horizontal in [-1, 0, 1]:
for strafe_vertical in [-1, 0, 1]:
for fire in [0, 1]:
for jump in [0, 1]:
ACTION_LIST.append(_action(look_horizontal, look_vertical, strafe_horizontal, strafe_vertical, fire, jump, 0))
exp_name = 'kill'
if exp_name == 'kill':
REWARDS = {
'PICKUP_REWARD': 0,
'PICKUP_GOAL': 0,
'TARGET_SCORE': 0,
'TAG_SELF': 0,
'TAG_PLAYER': 1,
'CTF_FLAG_BONUS': 0,
'CTF_CAPTURE_BONUS': 0,
'CTF_TEAM_BONUS': 0,
'CTF_FRAG_CARRIER_BONUS': 0,
'CTF_RECOVERY_BONUS': 0,
'CTF_CARRIER_DANGER_PROTECT_BONUS': 0,
'CTF_FLAG_DEFENSE_BONUS': 0,
'CTF_CARRIER_PROTECT_BONUS': 0,
'CTF_RETURN_FLAG_ASSIST_BONUS': 0,
'CTF_FRAG_CARRIER_ASSIST_BONUS': 0
}
elif exp_name == 'flag':
REWARDS = {
'PICKUP_REWARD': 0,
'PICKUP_GOAL': 0,
'TARGET_SCORE': 0,
'TAG_SELF': 0,
'TAG_PLAYER': 0,
'CTF_FLAG_BONUS': 0,
'CTF_CAPTURE_BONUS': 1,
'CTF_TEAM_BONUS': 0,
'CTF_FRAG_CARRIER_BONUS': 0,
'CTF_RECOVERY_BONUS': 0,
'CTF_CARRIER_DANGER_PROTECT_BONUS': 0,
'CTF_FLAG_DEFENSE_BONUS': 0,
'CTF_CARRIER_PROTECT_BONUS': 0,
'CTF_RETURN_FLAG_ASSIST_BONUS': 0,
'CTF_FRAG_CARRIER_ASSIST_BONUS': 0
}
#ACTION_LIST = list(ACTIONS)
#print("ACTION_LIST: ", ACTION_LIST)
num_actions = len(ACTION_LIST)
screen_size = (128, 128, 3)
def render(obs, name):
cv2.imshow('name', obs)
cv2.waitKey(1)
def one_hot(a, num_classes):
return np.squeeze(np.eye(num_classes)[a])
scores = []
episodes = []
average = []
for episode_step in range(0, 2000000):
env.reset(seed=arguments.env_id)
obs = env.observations()
obs_screen = obs['RGB_INTERLEAVED']
obs_screen = cv2.cvtColor(obs_screen, cv2.COLOR_BGR2RGB)
obs_screen = cv2.resize(obs_screen, dsize=(128, 128), interpolation=cv2.INTER_AREA)
obs_weapon_amount = obs['DEBUG.GADGET_AMOUNT']
obs_weapon = obs['DEBUG.GADGET']
obs_has_red_flag = obs['DEBUG.HAS_RED_FLAG']
state_screen = obs_screen / 255.0
has_red_flag_onehot = one_hot(int(obs_has_red_flag), 2)
obs_inv = np.concatenate([obs_weapon_amount / 100.0, has_red_flag_onehot])
state_inv = obs_inv
done = False
reward = 0.0
reward_sum = 0
step = 0
while True:
try:
print("step: ", step)
state_screen_reshaped = np.reshape(state_screen, (1,*screen_size))
state_inv_reshaped = np.reshape(state_inv, (1, 3))
#state_inv_reshaped = np.zeros((1, 3))
env_output = {"env_id": np.array([arguments.env_id]),
"reward": reward, "done": done,
"obs_screen": state_screen_reshaped,
"obs_inv": state_inv_reshaped}
socket.send_pyobj(env_output)
action_index = int(socket.recv_pyobj()['action'])
reward_game = env.step(ACTION_LIST[action_index], num_steps=action_repeat)
#reward = reward_game
events = env.events()
if len(events) != 0:
for event in events:
if event[0] == 'reward':
event_info = event[1]
reason = event_info[0]
team = event_info[1]
score = event_info[2]
player_id = event_info[3]
location = event_info[4]
other_player_id = event_info[5]
if team == 'blue':
reward = float(REWARDS[reason])
#print("reason: {0}, team: {1}, score: {2}, reward: {3}".format(reason, team, score, reward))
done = not env.is_running()
if done or step == 500:
if env_id == 0:
scores.append(reward_sum)
episodes.append(episode_step)
average.append(sum(scores[-50:]) / len(scores[-50:]))
with writer.as_default():
tf.summary.scalar("average_reward", average[-1], step=episode_step)
writer.flush()
#print("average_reward: " + str(average[-1]))
else:
#print("reward_sum: " + str(reward_sum))
pass
break
obs1 = env.observations()
obs1_screen = obs1['RGB_INTERLEAVED']
obs1_screen = cv2.cvtColor(obs1_screen, cv2.COLOR_BGR2RGB)
#if env_id == 0:
# render(obs1_screen, "obs1_screen")
obs1_screen = cv2.resize(obs1_screen, dsize=(128,128), interpolation=cv2.INTER_AREA)
obs1_weapon_amount = obs1['DEBUG.GADGET_AMOUNT']
obs1_weapon = obs1['DEBUG.GADGET']
obs1_has_red_flag = obs1['DEBUG.HAS_RED_FLAG']
#print("obs1_weapon_amount: ", obs1_weapon_amount)
#print("obs1_weapon: ", obs1_weapon)
#print("obs1_has_red_flag: ", obs1_has_red_flag)
#print("")
next_state_screen = obs1_screen / 255.0
has_red_flag_onehot1 = one_hot(int(obs1_has_red_flag), 2)
obs1_inv = np.concatenate([obs1_weapon_amount / 100.0, has_red_flag_onehot1])
next_state_inv = obs1_inv
reward_sum += reward
state_screen = next_state_screen
state_inv = next_state_inv
step += 1
except (tf.errors.UnavailableError, tf.errors.CancelledError):
print('Inference call failed. This is normal at the end of training.')
#logging.info('Inference call failed. This is normal at the end of training.')
env.close()