-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrandom_agent.py
More file actions
61 lines (51 loc) · 2.38 KB
/
Copy pathrandom_agent.py
File metadata and controls
61 lines (51 loc) · 2.38 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
import random
import sys
import acpc_python_client as acpc
class RandomAgent(acpc.Agent):
def __init__(self):
super().__init__()
self.actions = [acpc.ActionType.FOLD, acpc.ActionType.CALL, acpc.ActionType.RAISE]
self.action_probabilities = [0] * 3
self.action_probabilities[0] = 0.3 # fold probability
self.action_probabilities[1] = (1 - self.action_probabilities[0]) * 0.5 # call probability
self.action_probabilities[2] = (1 - self.action_probabilities[0]) * 0.5 # raise probability
def on_game_start(self, game):
pass
def on_next_turn(self, game, match_state, is_acting_player):
if is_acting_player:
# Create current action probabilities, leave out invalid actions
current_probabilities = [0] * 3
if self.is_fold_valid():
current_probabilities[0] = self.action_probabilities[0]
# call is always valid action
current_probabilities[1] = self.action_probabilities[1]
if self.is_raise_valid():
current_probabilities[2] = self.action_probabilities[2]
# Normalize the probabilities
probabilities_sum = sum(current_probabilities)
current_probabilities = [p / probabilities_sum for p in current_probabilities]
# Randomly select one action
action_index = -1
r = random.random()
for i in range(3):
if r <= current_probabilities[i]:
action_index = i
else:
r -= current_probabilities[i]
action_type = self.actions[action_index]
if action_type == acpc.ActionType.RAISE \
and game.get_betting_type() == acpc.BettingType.NO_LIMIT:
raise_min = self.get_raise_min()
raise_max = self.get_raise_max()
raise_size = raise_min + (raise_max - raise_min) * random.random()
self.set_next_action(action_type, int(round(raise_size)))
else:
self.set_next_action(action_type)
def on_game_finished(self, game, match_state):
pass
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage {game_file_path} {dealer_hostname} {dealer_port}")
sys.exit(1)
client = acpc.Client(sys.argv[1], sys.argv[2], sys.argv[3])
client.play(RandomAgent())