-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay_against_wbridge5.py
More file actions
164 lines (140 loc) · 5.47 KB
/
Copy pathplay_against_wbridge5.py
File metadata and controls
164 lines (140 loc) · 5.47 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
import socket
from typing import List, Optional, Union
import numpy as np
import torch
from bluechip_bot import BlueChipBridgeBot
from common_utils import assert_eq
from common_utils import tensor_dict_to_device
from wbridge5_client import WBridge5Client, Controller
from create_bridge import create_params, create_bridge_game
from set_path import append_sys_path
from agent import BridgeAgent, DEFAULT_POLICY_CONF, DEFAULT_VALUE_CONF, SimpleAgent
append_sys_path()
import bridge
import bridgelearn
def make_obs_tensor_dict(state: bridge.BridgeState):
parent_game = state.parent_game()
observation = bridge.BridgeObservation(state, state.current_player())
encoder = bridge.CanonicalEncoder(state.parent_game())
s = encoder.encode(observation)
legal_move_mask = torch.zeros(bridge.NUM_CALLS, dtype=torch.float32)
legal_moves = state.legal_moves()
for move in legal_moves:
uid = parent_game.get_move_uid(move)
legal_move_mask[uid - bridge.NUM_CARDS] = 1
obs = {
"s": torch.tensor(s, dtype=torch.float32),
"legal_move": legal_move_mask
}
return obs
def _bid_and_play(state: bridge.BridgeState, bots: List[BlueChipBridgeBot], agent: SimpleAgent,
play_bot: bridgelearn.PlayBot, agent_seats: List[int], device="cuda"):
while state.current_phase() == bridge.Phase.AUCTION:
if state.current_player() in agent_seats:
obs = make_obs_tensor_dict(state)
obs = tensor_dict_to_device(obs, device)
action = agent.act(obs)
state.apply_move(state.parent_game().get_move(int(action.item())))
else:
result = bots[state.current_player()].step(state)
state.apply_move(result)
while state.current_phase() == bridge.Phase.PLAY:
# print(state)
# play phase
if state.current_player() in agent_seats:
# print(state.legal_moves())
move = play_bot.step(state)
# print(move)
# print(search_result.moves, search_result.scores)
state.apply_move(move)
else:
result = bots[state.current_player()].step(state)
state.apply_move(result)
return state
def _run_once(state: bridge.BridgeState, bots: List[BlueChipBridgeBot], agent: SimpleAgent,
pimc_bot: bridgelearn.PlayBot, deal: Optional[Union[List[int], np.ndarray]] = None):
for bot in bots:
bot.restart()
state_0 = state.clone()
state_1 = state.clone()
game = state_0.parent_game()
if deal is not None:
assert_eq(len(deal), bridge.NUM_CARDS)
else:
deal = np.random.permutation(bridge.NUM_CARDS)
for i in range(bridge.NUM_CARDS):
move = game.get_chance_outcome(deal[i])
state_0.apply_move(move)
state_1.apply_move(move)
state_0 = _bid_and_play(state_0, bots, agent, pimc_bot, [bridge.Seat.NORTH, bridge.Seat.SOUTH])
state_1 = _bid_and_play(state_1, bots, agent, pimc_bot, [bridge.Seat.EAST, bridge.Seat.WEST])
return state_0, state_1
def play():
params = create_params(seed=23)
game = create_bridge_game(params)
bots = [
BlueChipBridgeBot(
game=game,
player_id=bridge.Seat.NORTH,
controller_factory=controller_factory
),
BlueChipBridgeBot(
game=game,
player_id=bridge.Seat.EAST,
controller_factory=controller_factory
),
BlueChipBridgeBot(
game=game,
player_id=bridge.Seat.SOUTH,
controller_factory=controller_factory
),
BlueChipBridgeBot(
game=game,
player_id=bridge.Seat.WEST,
controller_factory=controller_factory
),
]
agent = SimpleAgent(DEFAULT_POLICY_CONF)
agent.policy_net.load_state_dict(torch.load("sl/exp6/model8.pth")["state_dict"])
agent.to("cuda")
resampler = bridgelearn.UniformResampler(23)
pimc_bot = bridgelearn.PIMCBot(resampler, 200)
cheat_bot = bridgelearn.CheatBot()
results = []
same_contract_results = []
i_deal = 0
while i_deal < num_deals:
try:
state_0, state_1 = _run_once(bridge.BridgeState(game),
bots,
agent,
cheat_bot)
imp = bridge.get_imp(state_0.scores()[0], state_1.scores()[0])
print(f"Deal #{i_deal}; final state:\n{state_0}\n{state_1}\nimp: {imp}")
results.append(imp)
if state_0.get_contract().index() == state_1.get_contract().index():
same_contract_results.append(imp)
i_deal += 1
except socket.timeout:
continue
except ValueError:
continue
stats = np.array(results)
print(stats, len(stats))
mean = np.mean(stats, axis=0)
stderr = np.std(stats, axis=0, ddof=1) / np.sqrt(num_deals)
print(u"Imp: {:+.1f}\u00b1{:.1f}".format(mean, stderr))
stats = np.array(same_contract_results)
print(stats, len(stats))
mean = np.mean(stats, axis=0)
stderr = np.std(stats, axis=0, ddof=1) / np.sqrt(num_deals)
print(u"Same contract imp: {:+.1f}\u00b1{:.1f}".format(mean, stderr))
if __name__ == '__main__':
bot_cmd = "D:/wbridge5/Wbridge5.exe Autoconnect {port}"
timeout_secs = 120
num_deals = 500
def controller_factory() -> Controller:
client = WBridge5Client(bot_cmd, timeout_secs)
client.start()
return client
play()