-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_client.py
95 lines (70 loc) · 2.76 KB
/
run_client.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
from __future__ import annotations
import argparse
import uuid
import hockey.hockey_env as h_env
import numpy as np
from comprl.client import Agent, launch_client
class RandomAgent(Agent):
"""A hockey agent that simply uses random actions."""
def get_step(self, observation: list[float]) -> list[float]:
return np.random.uniform(-1, 1, 4).tolist()
def on_start_game(self, game_id) -> None:
print("game started")
def on_end_game(self, result: bool, stats: list[float]) -> None:
text_result = "won" if result else "lost"
print(
f"game ended: {text_result} with my score: "
f"{stats[0]} against the opponent with score: {stats[1]}"
)
class HockeyAgent(Agent):
"""A hockey agent that can be weak or strong."""
def __init__(self, weak: bool) -> None:
super().__init__()
self.hockey_agent = h_env.BasicOpponent(weak=weak)
def get_step(self, observation: list[float]) -> list[float]:
# NOTE: If your agent is using discrete actions (0-7), you can use
# HockeyEnv.discrete_to_continous_action to convert the action:
#
# from hockey.hockey_env import HockeyEnv
# env = HockeyEnv()
# continuous_action = env.discrete_to_continous_action(discrete_action)
action = self.hockey_agent.act(observation).tolist()
return action
def on_start_game(self, game_id) -> None:
game_id = uuid.UUID(int=int.from_bytes(game_id))
print(f"Game started (id: {game_id})")
def on_end_game(self, result: bool, stats: list[float]) -> None:
text_result = "won" if result else "lost"
print(
f"Game ended: {text_result} with my score: "
f"{stats[0]} against the opponent with score: {stats[1]}"
)
# Function to initialize the agent. This function is used with `launch_client` below,
# to lauch the client and connect to the server.
def initialize_agent(agent_args: list[str]) -> Agent:
# Use argparse to parse the arguments given in `agent_args`.
parser = argparse.ArgumentParser()
parser.add_argument(
"--agent",
type=str,
choices=["weak", "strong", "random"],
default="weak",
help="Which agent to use.",
)
args = parser.parse_args(agent_args)
# Initialize the agent based on the arguments.
agent: Agent
if args.agent == "weak":
agent = HockeyAgent(weak=True)
elif args.agent == "strong":
agent = HockeyAgent(weak=False)
elif args.agent == "random":
agent = RandomAgent()
else:
raise ValueError(f"Unknown agent: {args.agent}")
# And finally return the agent.
return agent
def main() -> None:
launch_client(initialize_agent)
if __name__ == "__main__":
main()