forked from adivas24/rlcard-getaway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leducholdem_rule_models.py
139 lines (120 loc) · 3.83 KB
/
leducholdem_rule_models.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
128
129
130
131
132
133
134
135
136
137
138
139
''' Leduc Hold 'em rule model
'''
import rlcard
from rlcard.models.model import Model
class LeducHoldemRuleAgentV1(object):
''' Leduc Hold 'em Rule agent version 1
'''
def __init__(self):
self.use_raw = True
@staticmethod
def step(state):
''' Predict the action when given raw state. A simple rule-based AI.
Args:
state (dict): Raw state from the game
Returns:
action (str): Predicted action
'''
legal_actions = state['raw_legal_actions']
# Aggressively play 'raise' and 'call'
if 'raise' in legal_actions:
return 'raise'
if 'call' in legal_actions:
return 'call'
if 'check' in legal_actions:
return 'check'
else:
return 'fold'
def eval_step(self, state):
''' Step for evaluation. The same to step
'''
return self.step(state), []
class LeducHoldemRuleAgentV2(object):
''' Leduc Hold 'em Rule agent version 2
'''
def __init__(self):
self.use_raw = True
@staticmethod
def step(state):
''' Predict the action when given raw state. A simple rule-based AI.
Args:
state (dict): Raw state from the game
Returns:
action (str): Predicted action
'''
legal_actions = state['raw_legal_actions']
state = state['raw_obs']
hand = state['hand']
public_card = state['public_card']
action = 'fold'
# When having only 2 hand cards at the game start, choose fold to drop terrible cards:
# Acceptable hand cards:
# Pairs
# AK, AQ, AJ, AT
# A9s, A8s, ... A2s(s means flush)
# KQ, KJ, QJ, JT
# Fold all hand types except those mentioned above to save money
if public_card:
if public_card[1] == hand[1]:
action = 'raise'
else:
action = 'fold'
else:
if hand[0] == 'K':
action = 'raise'
elif hand[0] == 'Q':
action = 'check'
else:
action = 'fold'
#return action
if action in legal_actions:
return action
else:
if action == 'raise':
return 'call'
if action == 'check':
return 'fold'
if action == 'call':
return 'raise'
else:
return action
def eval_step(self, state):
''' Step for evaluation. The same to step
'''
return self.step(state), []
class LeducHoldemRuleModelV1(Model):
''' Leduc holdem Rule Model version 1
'''
def __init__(self):
''' Load pretrained model
'''
env = rlcard.make('leduc-holdem')
rule_agent = LeducHoldemRuleAgentV1()
self.rule_agents = [rule_agent for _ in range(env.num_players)]
@property
def agents(self):
''' Get a list of agents for each position in a the game
Returns:
agents (list): A list of agents
Note: Each agent should be just like RL agent with step and eval_step
functioning well.
'''
return self.rule_agents
class LeducHoldemRuleModelV2(Model):
''' Leduc holdem Rule Model version 2
'''
def __init__(self):
''' Load pretrained model
'''
env = rlcard.make('leduc-holdem')
rule_agent = LeducHoldemRuleAgentV2()
self.rule_agents = [rule_agent for _ in range(env.num_players)]
@property
def agents(self):
''' Get a list of agents for each position in a the game
Returns:
agents (list): A list of agents
Note: Each agent should be just like RL agent with step and eval_step
functioning well.
'''
return self.rule_agents