-
Notifications
You must be signed in to change notification settings - Fork 9
/
start_server.py
127 lines (116 loc) · 3.16 KB
/
start_server.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
"""
Start a server for remote agent evaluation.
"""
import argparse
import logging
from hearts_gym import HeartsEnv, utils
from hearts_gym.server.hearts_server import (
HeartsServer,
HeartsRequestHandler,
SERVER_ADDRESS,
PORT,
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--num_players',
default=4,
type=int,
help='Number of players in the game.',
)
parser.add_argument(
'--deck_size',
default=52,
type=int,
help='Number of cards in the deck.',
)
parser.add_argument(
'--mask_actions',
default=HeartsEnv.MASK_ACTIONS_DEFAULT,
type=utils.parse_bool,
help='Whether to apply action masking.',
)
parser.add_argument(
'--seed',
default=None,
type=int,
help='Random number generator base seed.',
)
parser.add_argument(
'--num_parallel_games',
default=1024,
type=int,
help=(
'Number of games to play in parallel. '
'Approximately also the batch size times four.'
),
)
parser.add_argument(
'--num_procs',
default=utils.get_num_cpus() - 1,
type=int,
help='How many processes to use for playing the parallel games.',
)
parser.add_argument(
'--max_num_games',
default=None,
type=int,
help=(
'Number of games to play in total before disconnecting clients. '
'By default, play with the same clients forever.'
),
)
parser.add_argument(
'--accept_repeating_client_addresses',
default=True,
type=utils.parse_bool,
help=(
'Whether clients can connect from the same address more than once.'
),
)
parser.add_argument(
'--wait_duration_sec',
default=None,
type=int,
help='How long to wait until filling with randomly acting agents.',
)
parser.add_argument(
'--server_address',
type=str,
default=SERVER_ADDRESS,
help='Server address to connect to.',
)
parser.add_argument(
'--port',
type=int,
default=PORT,
help='Server port to connect to.',
)
return parser.parse_args()
def main() -> None:
"""Start a server for remote agent evaluation."""
args = parse_args()
logging.basicConfig()
with HeartsServer(
(args.server_address, args.port),
HeartsRequestHandler,
num_players=args.num_players,
deck_size=args.deck_size,
mask_actions=args.mask_actions,
seed=args.seed,
num_parallel_games=args.num_parallel_games,
num_procs=args.num_procs,
max_num_games=args.max_num_games,
accept_repeating_client_addresses=(
args.accept_repeating_client_addresses
),
wait_duration_sec=args.wait_duration_sec,
) as server:
try:
server.serve_forever()
except Exception:
pass
finally:
server.envs.terminate_pool()
if __name__ == '__main__':
main()