-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathhuman_vs_agent.py
73 lines (58 loc) · 2 KB
/
human_vs_agent.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
import numpy as np
import torch
from examples.selfplay.tictactoe_utils.tictactoe_render import TictactoeRender
from openrl.configs.config import create_config_parser
from openrl.envs.common import make
from openrl.envs.wrappers import FlattenObservation
from openrl.modules.common import PPONet as Net
from openrl.runners.common import PPOAgent as Agent
from openrl.selfplay.wrappers.human_opponent_wrapper import HumanOpponentWrapper
from openrl.selfplay.wrappers.random_opponent_wrapper import RandomOpponentWrapper
def get_fake_env(env_num):
env = make(
"tictactoe_v3",
env_num=env_num,
asynchronous=True,
opponent_wrappers=[RandomOpponentWrapper],
env_wrappers=[FlattenObservation],
auto_reset=False,
)
return env
def get_human_env(env_num):
env = make(
"tictactoe_v3",
env_num=env_num,
asynchronous=True,
opponent_wrappers=[TictactoeRender, HumanOpponentWrapper],
env_wrappers=[FlattenObservation],
auto_reset=False,
)
return env
def human_vs_agent():
env_num = 1
fake_env = get_fake_env(env_num)
env = get_human_env(env_num)
cfg_parser = create_config_parser()
cfg = cfg_parser.parse_args()
net = Net(fake_env, cfg=cfg, device="cuda" if torch.cuda.is_available() else "cpu")
agent = Agent(net)
agent.load("./ppo_agent/")
total_reward = 0.0
ep_num = 5
for ep_now in range(ep_num):
agent.set_env(fake_env)
obs, info = env.reset()
done = False
step = 0
while not np.any(done):
# predict next action based on the observation
action, _ = agent.act(obs, info, deterministic=True)
obs, r, done, info = env.step(action)
step += 1
if np.any(done):
total_reward += np.mean(r) > 0
print(f"{ep_now}/{ep_num}: reward: {np.mean(r)}")
print(f"win rate: {total_reward / ep_num}")
env.close()
if __name__ == "__main__":
human_vs_agent()