-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
279 lines (206 loc) · 7.39 KB
/
test.py
File metadata and controls
279 lines (206 loc) · 7.39 KB
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
from pettingzoo.mpe import simple_spread_v3
import random
import torch
import argparse
import numpy as np
from scipy.spatial.distance import cdist
from train import DQN
# Actions
NOTHING = 0
LEFT = 1
RIGHT = 2
DOWN = 3
UP = 4
class simplePolicy:
def __init__(self, agent):
self.agent = agent
self.epsilon = 0.1
def distance(self, a, b):
return np.linalg.norm(np.array(a) - np.array(b))
def choose_target(self, observation):
self.position = observation[2:4]
landmark_rel_positions = observation[4:10]
landmarks = np.array(
[
landmark_rel_positions[0:2],
landmark_rel_positions[2:4],
landmark_rel_positions[4:6],
]
)
distances = []
for landmark in landmarks:
distances.append(self.distance(landmark, self.position))
self.target_pos = landmarks[distances.index(min(distances))]
def choose_action(self, observations):
observation = observations[self.agent]
self.choose_target(observation)
if (
abs(self.target_pos[0]) < self.epsilon
and abs(self.target_pos[1]) < self.epsilon
):
return NOTHING
if abs(self.target_pos[0]) > abs(self.target_pos[1]):
if self.target_pos[0] > self.epsilon:
return RIGHT
else:
return LEFT
else:
if self.target_pos[1] > self.epsilon:
return UP
else:
return DOWN
class complexPolicy:
def __init__(self, agent):
self.agent = agent
self.epsilon = 0.2
def choose_action(self, observations):
agents_targets = self.choose_targets(observations)
target = agents_targets[self.agent]
observation = observations[self.agent]
position = observation[2:4]
direction = target - position
if abs(direction[0]) < self.epsilon and abs(direction[1]) < self.epsilon:
return NOTHING
if abs(direction[0]) > abs(direction[1]):
if direction[0] > 0:
return RIGHT
else:
return LEFT
else:
if direction[1] > 0:
return UP
else:
return DOWN
def choose_targets(self, observations):
agents_targets = {}
obs = observations["agent_0"]
self_pos = obs[2:4]
agent_1_position = obs[10:12] + self_pos
agent_2_position = obs[12:14] + self_pos
landmark_rel_positions = obs[4:10]
landmarks = np.array(
[
landmark_rel_positions[0:2] + self_pos,
landmark_rel_positions[2:4] + self_pos,
landmark_rel_positions[4:6] + self_pos,
]
)
agents = np.array([self_pos, agent_1_position, agent_2_position])
landmarks = np.array([landmarks[i] for i in range(3)])
distances = cdist(agents, landmarks)
pairs = []
used_agents = np.zeros(len(agents), dtype=bool)
for landmark_index in range(len(landmarks)):
min_dist = np.inf
min_agent_index = None
for agent_index in range(len(agents)):
if used_agents[agent_index]:
continue
if distances[agent_index, landmark_index] < min_dist:
min_dist = distances[agent_index, landmark_index]
min_agent_index = agent_index
if min_agent_index is not None:
pairs.append((min_agent_index, landmark_index))
used_agents[min_agent_index] = True
for agent_index, landmark_index in pairs:
agent = "agent_" + str(agent_index)
agents_targets[agent] = landmarks[landmark_index]
return agents_targets
class DQLPolicy:
def __init__(self, agent):
self.agent = agent
self.load_models()
def load_models(self):
input_shape = 18
output_actions = 5
self.model = DQN(input_shape, output_actions)
self.model.load_state_dict(torch.load("models/" + self.agent + ".pt"))
def choose_action(self, observations):
return self.model(torch.from_numpy(observations[self.agent])).argmax().item()
def init_policies(policies):
agents = ["agent_0", "agent_1", "agent_2"]
agent_policies = {}
for i, agent in enumerate(agents):
if policies[i] == "rl":
agent_policies[agent] = DQLPolicy(agent)
elif policies[i] == "sp":
agent_policies[agent] = simplePolicy(agent)
elif policies[i] == "cp":
agent_policies[agent] = complexPolicy(agent)
return agent_policies
def choose_seed(seeds, run_num):
if seeds:
return seeds[run_num]
else:
return random.randint(0, 1000)
def print_rewards(chosen_seed, average_rewards, rewards):
print(
"AVERAGE REWARDS [",
chosen_seed,
"]: agent_0:",
round(average_rewards["agent_0"], 2),
", agent_1:",
round(average_rewards["agent_1"], 2),
", agent_2:",
round(average_rewards["agent_2"], 2),
)
print(
"FINAL REWARDS [",
chosen_seed,
"]: agent_0:",
round(rewards["agent_0"], 2),
", agent_1:",
round(rewards["agent_1"], 2),
", agent_2:",
round(rewards["agent_2"], 2),
)
print()
# seeds
# 73, 69! 45 40 24 85 10!
def test(policies, num_of_runs, seeds=None, DEBUG=False):
if DEBUG:
print(f"Policies: {policies}")
print(f"Number of runs: {num_of_runs}")
if seeds:
print(f"Seeds: {seeds}")
else:
print("No seeds provided")
NUM_CYCLES = 25
LOCAL_RATIO = 0.5
env = simple_spread_v3.parallel_env(
render_mode="human", local_ratio=LOCAL_RATIO, max_cycles=NUM_CYCLES
)
agent_policies = init_policies(policies)
for run_num in range(num_of_runs):
chosen_seed = choose_seed(seeds, run_num)
observations, _ = env.reset(seed=chosen_seed)
average_rewards = {"agent_0": 0, "agent_1": 0, "agent_2": 0}
actions = {}
while env.agents:
for agent in env.agents:
actions[agent] = agent_policies[agent].choose_action(observations)
observations, rewards, _, _, _ = env.step(actions)
for agent in env.agents:
average_rewards[agent] += rewards[agent] / NUM_CYCLES
print_rewards(chosen_seed, average_rewards, rewards)
env.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Test different policies for agents for certain number of runs with choosing the seed values."
)
parser.add_argument(
"policies",
nargs=3,
choices=["rl", "sp", "cp"],
help="Policies for agent_0, agent_1, agent_2. Possible options are: 'rl' - reinforement learning, 'sp' - simple policy, 'cp' - complex policy.",
)
parser.add_argument(
"num_of_runs", type=int, help="Number of runs to test policies."
)
parser.add_argument(
"seeds", type=int, nargs="*", help="Optional seed values for each of the runs."
)
args = parser.parse_args()
if args.seeds and len(args.seeds) != args.num_of_runs:
parser.error("The number of seeds provided must match number of runs.")
test(args.policies, args.num_of_runs, args.seeds)