-
Notifications
You must be signed in to change notification settings - Fork 1
/
hatrpo_vis.py
172 lines (147 loc) · 6.16 KB
/
hatrpo_vis.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
import argparse
import os
import random
import time
from distutils.util import strtobool
import sys
sys.path.append('highway_envs')
import highway_env
highway_env.register_highway_envs()
from highway_env.envs.common.agents import FollowingVictimVulnerable
import gymnasium as gym
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.distributions.categorical import Categorical
from torch.utils.tensorboard import SummaryWriter
from gym_wrapper import MultiAgentAutoResetWrapper
from util import *
from conjugate_gradient import cg
from cost import cost_function
n_attackers = 4
def config_env():
env = gym.make(args.env_name, render_mode="rgb_array")
env.configure({"observation": {
"type": "MultiAgentObservation",
"observation_config": {
"type": "AttackerKinematics",
"see_behind": True,
"vehicles_count": n_attackers+1
}
},
"attacker_num": n_attackers,
"controlled_vehicles": n_attackers,
"time_penalty": -0.1/n_attackers,
"time_penalty": 0.0,
"attacker_collide_each_other_reward": -2.5,
"vicitm_collision_reward": 10.0/n_attackers,
"randomize_starting_position": False,
"constraint_env": True,
"vis": False,
"testing": True})
env.reset()
victim_agent = FollowingVictimVulnerable(env)
env.load_agents(n_attackers, victim_agent)
env = MultiAgentAutoResetWrapper(env)
return env
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
class AttackerAgent(nn.Module):
def __init__(self, envs, num_agents, hidden_dim):
super(AttackerAgent, self).__init__()
# print(envs.action_space)
self.critic = nn.Sequential(
layer_init(nn.Linear((n_attackers+1)*5, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, 1), std=1.0),
)
self.cost_critic = nn.Sequential(
layer_init(nn.Linear((n_attackers+1)*5, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, 1), std=1.0),
)
self.actor = nn.Sequential(
layer_init(nn.Linear((n_attackers+1)*5, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, hidden_dim)),
nn.Tanh(),
layer_init(nn.Linear(hidden_dim, 5), std=0.01),
)
def get_value(self, x):
x = x.flatten(start_dim=-2)
return self.critic(x)
def get_cost_value(self, x):
x = x.flatten(start_dim=-2)
return self.cost_critic(x)
def get_action_and_value(self, x, centralized_x, action=None):
x = x.flatten(start_dim=-2)
centralized_x = centralized_x.flatten(start_dim=-2)
logits = self.actor(x)
dist = Categorical(logits=logits)
if action is None:
action = dist.sample()
return action, dist.log_prob(action), dist.entropy(), self.critic(centralized_x), logits, self.cost_critic(x)
def get_action_info(self, x, action=None):
x = x.flatten(start_dim=-2)
logits = self.actor(x)
dist = Categorical(logits=logits)
if action is None:
action = dist.sample()
return action, dist.log_prob(action), dist.entropy()
def get_kl(self, x, b_logits):
x = x.flatten(start_dim=-2)
logits = self.actor(x)
probs = torch.nn.functional.softmax(logits, dim=-1)
b_probs = torch.nn.functional.softmax(b_logits, dim=-1)
kl = categorical_kl(probs, b_probs).mean()
return kl
if __name__ == "__main__":
seed = 5
PATHS = []
# folder_path = "saved_models\\macpo_perfect_victim_middle_seed_" + str(seed) + "_" + str(n_attackers)
# folder_path = "saved_models\\macpo_perfect_victim_middle_seed_" + str(seed) + "_" + str(n_attackers) + "_100k"
# folder_path = "saved_models\\macpo_perfect_victim_middle_seed_" + str(seed) + "_" + str(n_attackers) + "_time"
# folder_path = "thesis_models_new\\matrpo_perfect_victim_surround_penalty_middle_cost20_seed_" + str(seed) + "_" + str(n_attackers)
# folder_path = "thesis_models_diff_start\\matrpo_perfect_victim_back3_start1_rand_noinvalid_cost15_seed_" + str(seed) + "_" + str(n_attackers)
folder_path = "thesis_models_diff_start2\\matrpo_perfect_victim_vulnerable_start1_rand_cost5_seed_" + str(seed) + "_" + str(n_attackers)
for i in range(n_attackers):
PATHS.append(folder_path +"\\agent" + str(i) + ".pt")
# TRY NOT TO MODIFY: seeding
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
device = torch.device("cuda")
envs = config_env()
agents = []
for i in range(n_attackers):
agents.append(torch.load(PATHS[i], map_location=device).to(device))
agents[i].eval()
num_of_eps = 0
while True:
num_of_eps += 1
terminated = [False for _ in range(n_attackers)]
truncated = False
obs, info = envs.reset()
obs = torch.Tensor(obs).to(device)
while not all(terminated) or truncated:
actions_to_take = [0 for _ in range(n_attackers)]
for i in range(n_attackers):
action, *_ = agents[i].get_action_info(obs[i])
actions_to_take[i] = action.cpu().item()
obs, reward, terminated, truncated, info = envs.step(tuple(actions_to_take))
# print(tuple(action.cpu().numpy()))
obs = torch.Tensor(obs).to(device)
# envs.render()
# time.sleep(0.1)
print("number of episodes: ", num_of_eps)
if num_of_eps == 2000:
break