-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
152 lines (121 loc) · 4.77 KB
/
eval.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
from magent2.environments import battle_v4
from model.networks import Pretrained_QNets, Final_QNets
from agent.base_agent import PretrainedAgent
import torch
import numpy as np
import argparse
try:
from tqdm import tqdm
except ImportError:
tqdm = lambda x, *args, **kwargs: x # Fallback: tqdm becomes a no-op
def eval(my_model_path):
max_cycles = 300
env = battle_v4.env(map_size=45, max_cycles=max_cycles)
device = "cuda" if torch.cuda.is_available() else "cpu"
def random_policy(env, agent, obs):
return env.action_space(agent).sample()
q_network = Pretrained_QNets(
env.observation_space("red_0").shape, env.action_space("red_0").n
)
q_network.load_state_dict(
torch.load("model/state_dict/red.pt", weights_only=True, map_location="cpu")
)
q_network.to(device)
final_q_network = Final_QNets(
env.observation_space("red_0").shape, env.action_space("red_0").n
)
final_q_network.load_state_dict(
torch.load("model/state_dict/red_final.pt", weights_only=True, map_location="cpu")
)
final_q_network.to(device)
def my_policy(env, agent, obs):
my_agent = PretrainedAgent(env.observation_space("red_0").shape, env.action_space("red_0").n, model_path= my_model_path)
return my_agent.get_action(obs)
def pretrain_policy(env, agent, obs):
observation = (
torch.Tensor(obs).float().permute([2, 0, 1]).unsqueeze(0).to(device)
)
with torch.no_grad():
q_values = q_network(observation)
return torch.argmax(q_values, dim=1).cpu().numpy()[0]
def final_pretrain_policy(env, agent, obs):
observation = (
torch.Tensor(obs).float().permute([2, 0, 1]).unsqueeze(0).to(device)
)
with torch.no_grad():
q_values = final_q_network(observation)
return torch.argmax(q_values, dim=1).cpu().numpy()[0]
def run_eval(env, red_policy, blue_policy, n_episode: int = 100):
red_win, blue_win = [], []
red_tot_rw, blue_tot_rw = [], []
n_agent_each_team = len(env.env.action_spaces) // 2
blue_agents = []
red_agents = []
for _ in tqdm(range(n_episode)):
env.reset()
n_kill = {"red": 0, "blue": 0}
red_reward, blue_reward = 0, 0
for agent in env.agent_iter():
observation, reward, termination, truncation, info = env.last()
agent_team = agent.split("_")[0]
n_kill[agent_team] += (
reward > 4.5
) # This assumes default reward settups
if agent_team == "red":
red_reward += reward
else:
blue_reward += reward
if termination or truncation:
action = None # this agent has died
else:
if agent_team == "red":
action = red_policy(env, agent, observation)
else:
action = blue_policy(env, agent, observation)
env.step(action)
who_wins = "red" if n_kill["red"] >= n_kill["blue"] + 5 else "draw"
who_wins = "blue" if n_kill["red"] + 5 <= n_kill["blue"] else who_wins
red_win.append(who_wins == "red")
blue_win.append(who_wins == "blue")
blue_agents.append(n_kill["blue"])
red_agents.append(n_kill["red"])
red_tot_rw.append(red_reward / n_agent_each_team)
blue_tot_rw.append(blue_reward / n_agent_each_team)
return {
"winrate_red": np.mean(red_win),
"winrate_blue": np.mean(blue_win),
"average_rewards_red": np.mean(red_tot_rw),
"average_rewards_blue": np.mean(blue_tot_rw),
"red_kill": np.mean(red_agents) / n_agent_each_team,
"blue_kill": np.mean(blue_agents) / n_agent_each_team,
}
print("=" * 20)
print("Eval with random policy")
print(
run_eval(
env=env, red_policy=random_policy, blue_policy=my_policy, n_episode=30
)
)
print("=" * 20)
print("Eval with trained policy")
print(
run_eval(
env=env, red_policy=pretrain_policy, blue_policy=my_policy, n_episode=30
)
)
print("=" * 20)
print("Eval with final trained policy")
print(
run_eval(
env=env,
red_policy=final_pretrain_policy,
blue_policy=my_policy,
n_episode=30,
)
)
print("=" * 20)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Eval my Agent")
parser.add_argument("-model_path", type=str, required=True, help="Path to model")
args = parser.parse_args()
eval(args.model_path)