-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
543 lines (465 loc) · 20 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import argparse
import grpc
import numpy as np
import os
import random
import time
import torch
import torch.nn as nn
from concurrent import futures
from google.protobuf import json_format
from grpc import RpcError
from sklearn.preprocessing import StandardScaler
from torch.distributions import Categorical
from internal.handler.coms import game_pb2
from internal.handler.coms import game_pb2_grpc as game_grpc
timeout_to_response = 1 # 1 second
SAVED_MODEL = True # Set to True to use a saved model that you trained previously
STATE_MAPS = True # Set to True to use the state format of maps and architecture CNN and set to False for vector format and architecture MLP
MODEL_PATH = './models' # Path where the model has been saved
MODEL_FILENAME = 'ppo_cnn_test.pth' # Filename of the saved model
ACTIONS = ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1), "attack", "connect", "pass")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class BotGameTurn:
def __init__(self, turn, action):
self.turn = turn
self.action = action
def layer_init(layer, std=np.sqrt(2), bias_const=0.0):
torch.nn.init.orthogonal_(layer.weight, std)
torch.nn.init.constant_(layer.bias, bias_const)
return layer
# Define the neural network for the policy
class AgentMLP(nn.Module):
def __init__(self, s_size, a_size,):
super(AgentMLP, self).__init__()
self.critic = nn.Sequential(
layer_init(nn.Linear(np.array(s_size).prod(), 64)),
nn.ReLU(),
layer_init(nn.Linear(64, 64)),
nn.ReLU(),
layer_init(nn.Linear(64, 1), std=1.0),
)
self.actor = nn.Sequential(
layer_init(nn.Linear(np.array(s_size).prod(), 64)),
nn.ReLU(),
layer_init(nn.Linear(64, 64)),
nn.ReLU(),
layer_init(nn.Linear(64, a_size), std=0.01),
)
# self.critic = nn.Sequential(
# layer_init(nn.Linear(np.array(s_size).prod(), 64)),
# nn.Tanh(),
# layer_init(nn.Linear(64, 64)),
# nn.Tanh(),
# layer_init(nn.Linear(64, 1), std=1.0),
# )
# self.actor = nn.Sequential(
# layer_init(nn.Linear(np.array(s_size).prod(), 64)),
# nn.Tanh(),
# layer_init(nn.Linear(64, 64)),
# nn.Tanh(),
# layer_init(nn.Linear(64, a_size), std=0.01),
# )
def get_value(self, x):
return self.critic(x)
def get_action_and_value(self, x, action=None):
logits = self.actor(x)
probs = Categorical(logits=logits)
if action is None:
action = probs.sample()
return action, probs.log_prob(action), probs.entropy(), self.critic(x)
class AgentCNN(nn.Module):
def __init__(self, num_maps, a_size: list):
super(AgentCNN, self).__init__()
self.critic = nn.Sequential(
layer_init(nn.Conv2d(in_channels=num_maps, out_channels=16, kernel_size=5, stride=2)),
nn.ReLU(),
layer_init(nn.Conv2d(16, 32, kernel_size=3)),
nn.ReLU(),
nn.Flatten(),
layer_init(nn.Linear(32*18*8, 256)),
nn.ReLU(),
layer_init(nn.Linear(256, 1), std=1)
)
self.actor = nn.Sequential(
layer_init(nn.Conv2d(in_channels=num_maps, out_channels=16, kernel_size=5, stride=2)),
nn.ReLU(),
layer_init(nn.Conv2d(16, 32, kernel_size=3)),
nn.ReLU(),
nn.Flatten(),
layer_init(nn.Linear(32*18*8, 256)),
nn.ReLU(),
layer_init(nn.Linear(256, a_size), std=0.01)
)
# self.critic = nn.Sequential(
# layer_init(nn.Conv2d(in_channels=num_maps, out_channels=16, kernel_size=7)),
# nn.ReLU(),
# layer_init(nn.Conv2d(16, 32, 5)),
# nn.ReLU(),
# nn.Flatten(),
# layer_init(nn.Linear(32*33*13, 256)),
# nn.Tanh(),
# layer_init(nn.Linear(256, 1), std=1)
# )
# self.actor = nn.Sequential(
# layer_init(nn.Conv2d(in_channels=num_maps, out_channels=16, kernel_size=7)),
# nn.ReLU(),
# layer_init(nn.Conv2d(16, 32, 5)),
# nn.ReLU(),
# nn.Flatten(),
# layer_init(nn.Linear(32*13*13, 256)),
# nn.Tanh(),
# layer_init(nn.Linear(256, a_size), std=0.01)
# )
def get_value(self, x):
return self.critic(x)
def get_action_and_value(self, x, action=None):
logits = self.actor(x)
probs = Categorical(logits=logits)
if action is None:
action = probs.sample()
return action, probs.log_prob(action), probs.entropy(), self.critic(x)
class BotGame:
def __init__(self, player_num=None):
self.player_num = player_num
self.initial_state = None
self.turn_states = []
self.countT = 1
self.model_path = MODEL_PATH
self.model_filename = MODEL_FILENAME
self.use_saved_model = SAVED_MODEL
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.torch_deterministic = True
self.num_envs = 1 # this should be 1 for inference/ playing the game
self.a_size = len(ACTIONS)
self.state_maps = STATE_MAPS
def load_saved_model(self):
"""
Load a saved model.
"""
if os.path.isfile(os.path.join(self.model_path, self.model_filename)):
self.policy.load_state_dict(torch.load(os.path.join(self.model_path, self.model_filename)))
print(f"Loaded saved model: {self.model_filename}")
else:
print("No saved model")
def initialize_game(self, turn):
"""
Initialize the agent and load the saved model.
"""
self.saved_log_probs = []
if self.state_maps:
print("Using maps for state: PolicyCNN")
state = self.convert_state_cnn(turn)
self.num_maps = state.shape[2]
state = np.expand_dims(state, axis=0)
state = np.transpose(state, (0,3,1,2))
self.agent = AgentCNN(self.num_maps, self.a_size).to(self.device)
else:
print("Using array for state: PolicyMLP")
state = self.convert_state_mlp(turn)
self.s_size = len(state)
self.agent = AgentMLP(self.s_size, self.a_size).to(self.device)
if self.use_saved_model:
self.load_saved_model()
def convert_state_mlp(self, turn):
"""
Convert the state from the game engine into a state that can be used by the neural network (MLP).
The information in
"""
view = []
for i in range(len(turn.View)):
view = view + list(turn.View[i].Row)
cx = turn.Position.X
cy = turn.Position.Y
cx_min, cx_max = cx-3, cx+3
cy_min, cy_max = cy-3, cy+3
lighthouses = np.zeros((7,7), dtype=int)
lighthouses_dict = dict((tuple((lh.Position.X, lh.Position.Y)), lh.Energy) for lh in turn.Lighthouses)
for key in lighthouses_dict.keys():
if cx_min <= key[0] <= cx_max and cy_min <= key[1] <= cy_max:
lighthouses[key[0]+3-cx, key[1]+3-cy] = lighthouses_dict[key] + 1
lighthouses_info = []
# Create array for lighthouses data (within 3 steps of the bot)
for i in range(len(lighthouses)):
lighthouses_info = lighthouses_info + list(lighthouses[i])
new_state = np.array([turn.Position.X, turn.Position.Y, turn.Energy, len(turn.Lighthouses)] + view + lighthouses_info)
sc = StandardScaler()
new_state = sc.fit_transform(new_state.reshape(-1, 1))
new_state = new_state.squeeze()
return new_state
def z_score_scaling(self, arr):
arr_mean = np.mean(arr)
arr_std = np.std(arr)
scaled_arr = (arr - arr_mean) / arr_std
return scaled_arr
def convert_state_cnn(self, turn):
# Create base layer that will serve as the base for all layers of the state
# This layer has zeros in all cells except the border cells in which the value is -1
map = []
for i in range(len(self.initial_state.Map)):
map.append(self.initial_state.Map[i].Row)
base_layer = np.array(map.copy())
# Create player layer which has the value of the energy of the player + 1 where the player is located
# 1 is added to the energy to cover the case that the energy of the player is 0
player_layer = base_layer.copy()
x, y = turn.Position.X, turn.Position.Y
player_layer[x,y] = 1 + turn.Energy
player_layer = self.z_score_scaling(player_layer)
# Create view layer with energy level near the player
view_layer = base_layer.copy()
view = []
for i in range(len(turn.View)):
view.append(turn.View[i].Row)
view = np.array(view)
start_row, start_col = x-3, y-3
if y+3 > view_layer.shape[1]-1:
adjust = view_layer.shape[1]-1 - (y+3)
view = view[:,:adjust]
if x+3 > view_layer.shape[0]-1:
adjust = view_layer.shape[0]-1 - (x+3)
view = view[:adjust,:]
if y-3 < 0:
adjust = 3-y
view = view[:,adjust:]
start_col = 0
if x-3 < 0:
adjust = 3-x
view = view[adjust:,:]
start_row = 0
view_layer[start_row:start_row+view.shape[0], start_col:start_col+view.shape[1]] = view
view_layer = self.z_score_scaling(view_layer)
# Create layer that has the energy of the lighthouse + 1 where the lighthouse is located
# 1 is added to the lighthouse energy to cover the case that the energy of the lighthouse is 0
lh_energy_layer = base_layer.copy()
lh = turn.Lighthouses
for i in range(len(lh)):
x, y = lh[i].Position.X, lh[i].Position.Y
lh_energy_layer[x,y] = 1 - lh[i].Energy
lh_energy_layer = self.z_score_scaling(lh_energy_layer)
# Create layer that has the number of the player that controls each lighthouse
# If no player controls the lighthouse, then a value of -1 is assigned
lh_control_layer = base_layer.copy()
lh = turn.Lighthouses
for i in range(len(lh)):
x, y = lh[i].Position.X, lh[i].Position.Y
lh_control_layer[x,y] = lh[i].Owner
lh_control_layer = self.z_score_scaling(lh_control_layer)
# Create layer that indicates the lighthouses that are connected
# If the lighthouse is not connected, then a value of -1 is assigned, if it is connected then it is
# assigned the number of connections that it has
lh_connections_layer = base_layer.copy()
for i in range(len(lh)):
x, y = lh[i].Position.X, lh[i].Position.Y
if lh[i].Connections:
lh_connections_layer[x,y] = len(lh[i].Connections)
else:
lh_connections_layer[x,y] = -1
lh_connections_layer = self.z_score_scaling(lh_connections_layer)
# Create layer that indicates if the player has the key to the light house
# Assign value of 1 if has key and -1 if does not have key
lh_key_layer = base_layer.copy()
for i in range(len(lh)):
x, y = lh[i].Position.X, lh[i].Position.Y
if lh[i].HaveKey:
lh_key_layer[x,y] = 1
else:
lh_key_layer[x,y] = -1
lh_key_layer = self.z_score_scaling(lh_key_layer)
# Concatenate the maps into one state
player_layer = np.expand_dims(player_layer, axis=2)
view_layer = np.expand_dims(view_layer, axis=2)
lh_energy_layer = np.expand_dims(lh_energy_layer, axis=2)
lh_control_layer = np.expand_dims(lh_control_layer, axis=2)
lh_connections_layer = np.expand_dims(lh_connections_layer, axis=2)
lh_key_layer = np.expand_dims(lh_key_layer, axis=2)
new_state = np.concatenate((player_layer, view_layer, lh_energy_layer, lh_connections_layer, lh_control_layer, lh_key_layer), axis=2)
return new_state
def valid_lighthouse_connections(self, turn):
cx = turn.Position.X
cy = turn.Position.Y
lighthouses = dict((tuple((lh.Position.X, lh.Position.Y)), lh) for lh in turn.Lighthouses)
possible_connections = []
if (cx, cy) in lighthouses:
if lighthouses[(cx, cy)].Owner == self.player_num:
for dest in lighthouses:
if (dest != (cx, cy) and
lighthouses[dest].HaveKey and
[cx, cy] not in lighthouses[dest].Connections and
lighthouses[dest].Owner == self.player_num):
possible_connections.append(dest)
return possible_connections
def new_turn_action(self, turn: game_pb2.NewTurn, step=None) -> game_pb2.NewAction:
if self.countT == 1:
self.initialize_game(turn)
if self.state_maps:
new_state = self.convert_state_cnn(turn)
new_state = np.expand_dims(new_state, axis=0)
new_state = np.transpose(new_state, (0,3,1,2))
else:
new_state = self.convert_state_mlp(turn)
new_state = torch.from_numpy(np.array(new_state)).float().to(device)
with torch.no_grad():
action, log_prob, _, value = self.agent.get_action_and_value(new_state)
if ACTIONS[action] != "attack" and ACTIONS[action] != "connect" and ACTIONS[action] != "pass":
move = ACTIONS[action]
action = game_pb2.NewAction(
Action=game_pb2.MOVE,
Destination=game_pb2.Position(
X=turn.Position.X + move[0],
Y=turn.Position.Y + move[1]
)
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
self.countT += 1
return action
elif ACTIONS[action] == "pass":
action = game_pb2.NewAction(
Action=game_pb2.MOVE,
Destination=game_pb2.Position(
X=turn.Position.X,
Y=turn.Position.Y
)
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
self.countT += 1
return action
elif ACTIONS[action] == "attack":
energy = turn.Energy
action = game_pb2.NewAction(
Action=game_pb2.ATTACK,
Energy=energy,
Destination=game_pb2.Position(
X=turn.Position.X,
Y=turn.Position.Y
)
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
self.countT += 1
return action
elif ACTIONS[action] == "connect":
possible_connections = self.valid_lighthouse_connections(turn)
if not possible_connections:
action = game_pb2.NewAction(
Action=game_pb2.PASS,
Destination=game_pb2.Position(
X=turn.Position.X,
Y=turn.Position.Y
)
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
self.countT += 1
return action
else:
possible_connection = random.choice(possible_connections)
action = game_pb2.NewAction(
Action=game_pb2.CONNECT,
Destination=game_pb2.Position(X=possible_connection[0], Y=possible_connection[1])
)
bgt = BotGameTurn(turn, action)
self.turn_states.append(bgt)
self.countT += 1
return action
def load_saved_model(self):
if self.model_filename and os.path.isfile(os.path.join(self.model_path, self.model_filename)):
self.agent.load_state_dict(torch.load(os.path.join(self.model_path, self.model_filename)))
print(f"Loaded saved model: {self.model_filename}")
else:
print("No saved model")
class BotComs:
def __init__(self, bot_name, my_address, game_server_address, verbose=True):
self.bot_id = None
self.bot_name = bot_name
self.my_address = my_address
self.game_server_address = game_server_address
self.verbose = verbose
def wait_to_join_game(self):
channel = grpc.insecure_channel(self.game_server_address)
client = game_grpc.GameServiceStub(channel)
player = game_pb2.NewPlayer(name=self.bot_name, serverAddress=self.my_address)
while True:
try:
player_id = client.Join(player, timeout=timeout_to_response)
self.bot_id = player_id.PlayerID
print(f"Joined game with ID {player_id.PlayerID}")
if self.verbose:
print(json_format.MessageToJson(player_id))
break
except RpcError as e:
print(f"Could not join game: {e.details()}")
time.sleep(1)
def start_listening(self):
print("Starting to listen on", self.my_address)
# configure gRPC server
grpc_server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
interceptors=(ServerInterceptor(),)
)
# registry of the service
cs = ClientServer(bot_id=self.bot_id, verbose=self.verbose)
game_grpc.add_GameServiceServicer_to_server(cs, grpc_server)
# server start
grpc_server.add_insecure_port(self.my_address)
grpc_server.start()
try:
grpc_server.wait_for_termination() # wait until server finish
except KeyboardInterrupt:
grpc_server.stop(0)
class ServerInterceptor(grpc.ServerInterceptor):
def intercept_service(self, continuation, handler_call_details):
start_time = time.time_ns()
method_name = handler_call_details.method
# Invoke the actual RPC
response = continuation(handler_call_details)
# Log after the call
duration = time.time_ns() - start_time
print(f"Unary call: {method_name}, Duration: {duration:.2f} nanoseconds")
return response
class ClientServer(game_grpc.GameServiceServicer):
def __init__(self, bot_id, verbose=False):
self.bg = BotGame(bot_id)
self.verbose = verbose
def Join(self, request, context):
return None
def InitialState(self, request, context):
print("Receiving InitialState")
if self.verbose:
print(json_format.MessageToJson(request))
self.bg.initial_state = request
print("initial state: ", self.bg.initial_state)
return game_pb2.PlayerReady(Ready=True)
def Turn(self, request, context):
print(f"Processing turn: {self.bg.countT}")
if self.verbose:
print(json_format.MessageToJson(request))
action = self.bg.new_turn_action(request)
return action
def ensure_params():
parser = argparse.ArgumentParser(description="Bot configuration")
parser.add_argument("--bn", type=str, default="random-bot", help="Bot name")
parser.add_argument("--la", type=str, default="localhost:3001", help="Listen address")
parser.add_argument("--gs", type=str, default="localhost:50051", help="Game server address")
args = parser.parse_args()
if not args.bn:
raise ValueError("Bot name is required")
if not args.la:
raise ValueError("Listen address is required")
if not args.gs:
raise ValueError("Game server address is required")
return args.bn, args.la, args.gs
def main():
verbose = False
bot_name, listen_address, game_server_address = ensure_params()
bot = BotComs(
bot_name=bot_name,
my_address=listen_address,
game_server_address=game_server_address,
verbose=verbose,
)
bot.wait_to_join_game()
bot.start_listening()
if __name__ == "__main__":
main()