-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanotherStatus.py
199 lines (189 loc) · 8.04 KB
/
anotherStatus.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from HandStat import *
import numpy as np
import UnbiasedNet
import holdem
pair_features = ['pair ' + str(i) for i in range(2, 15)]
my_preflop_features = pair_features + ['1-high', '2-high',
'2-connected', '2-suited']
my_postflop_features = pair_features + ['4-connected', '5-connected',
'4-suited', '5-suited']
my_river_features = pair_features + ['5-connected', '5-suited']
flop_features = ['3-suited', '3-connected', '1-higher', '2-higher', 'pair']
turn_features = flop_features + ['4-suited', '4-connected']
river_features = ['4-suited', '4-connected', '1-higher', '2-higher', 'pair']
class AnotherStatus(StatStatus):
def __init__(self, dealer=0):
self.vec_cards = {'my preflop': np.zeros(len(my_preflop_features)),
'flop': np.zeros(len(flop_features)),
'my flop': np.zeros(len(my_postflop_features)),
'turn': np.zeros(len(turn_features)),
'my turn': np.zeros(len(my_postflop_features)),
'river': np.zeros(len(river_features)),
'my river': np.zeros(len(my_river_features))}
self.dealer=dealer
self.vec_act=np.zeros((4,3))
self.stage=0;
self.cards = None
def copy(self):
new_one= AnotherStatus()
new_one.vec_cards={key:self.vec_cards[key] for key in self.vec_cards}
new_one.dealer=1*self.dealer
new_one.vec_act=1*self.vec_act
new_one.stage=1*self.stage
new_one.cards = self.cards
return new_one
def update_preflop(self, cards):
self.cards = list(cards) # needed later
hand = HandStat( cards=cards )
self.vec_cards['my preflop'] = hand.stat( features=my_preflop_features )
def update_flop(self, table):
#table is a list of Cards
board = HandStat(table)
self.vec_cards['flop'] = board.stat( features=flop_features,
hole_cards=self.cards )
my = HandStat( self.cards + table )
self.vec_cards['my flop'] = my.stat( features=my_postflop_features )
self.stage=1
def update_turn(self, table):
board = HandStat(table)
self.vec_cards['turn'] = board.stat( features=turn_features,
hole_cards=self.cards )
my = HandStat( self.cards + table )
self.vec_cards['my turn'] = my.stat( features=my_postflop_features )
self.stage=2
def update_river(self, table):
board = HandStat(table)
self.vec_cards['river'] = board.stat( features=river_features,
hole_cards=self.cards )
my = HandStat( self.cards + table )
self.vec_cards['my river'] = my.stat( features=my_river_features )
self.stage=3
class AnotherAutoPlayer(MyAutoPlayer):
def __init__(self, neural_net, name= "anonymous", frenzy=False,
check_prob=0.25, call_prob=0.25, raise_prob=0.25,
checkfold_prob=0.25):
MyAutoPlayer.__init__(self, neural_net, name=name,
frenzy=frenzy, check_prob=check_prob,
call_prob=call_prob, raise_prob=raise_prob,
checkfold_prob=checkfold_prob)
self.status=AnotherStatus()
def reset_status(self):
self.status=AnotherStatus()
def sim_one_hand(self, player2, game, dealer=0, debug=0):
stat_seq=[]
output=0
#clear up possible leftover status from last game
self.reset_status()
player2.reset_status()
self.status.dealer=dealer
player2.status.dealer=1-dealer
#initialize the game and deal the pocket cards.
#game= holdem.Holdem(2, 4, 4, debug);
game.setName(player2.name, self.name)
#post the blind
self.post_blinds(player2, dealer)
#deal the hands
self.status.update_preflop(game.players[0].cards)
player2.status.update_preflop(game.players[1].cards)
stat_seq.append(self.status.longvec())
if debug:
print "blinds:"
print self.status.vec_act[0][0], self.status.vec_act[0][1]
#pre-flop action
self.action(player2, dealer, debug, game)
stat_seq.append(self.status.longvec())
if debug:
print "preflop:", self.status.vec_act[0]
print "preflop:", player2.status.vec_act[0]
if (self.status.vec_act[0][0] < player2.status.vec_act[0][0]):
return [stat_seq, -self.cum_bet()]
elif (self.status.vec_act[0][0] > player2.status.vec_act[0][0]):
return (stat_seq, player2.cum_bet())
#deal the flop
#game._endStage_();
self.status.update_flop(game.table)
player2.status.update_flop(game.table)
stat_seq.append(self.status.longvec())
#flop action
self.action(player2, dealer, debug, game)
stat_seq.append(self.status.longvec())
if debug:
print "on the flop:"
print self.status.vec_act[1][0], self.status.vec_act[1][1]
if (self.status.vec_act[1][0]< player2.status.vec_act[1][0]):
return [stat_seq, -self.cum_bet()]
elif (self.status.vec_act[1][0] > player2.status.vec_act[1][0]):
return (stat_seq, player2.cum_bet())
#deal the turn
#game._endStage_();
self.status.update_turn(game.table)
player2.status.update_turn(game.table)
stat_seq.append(self.status.longvec())
#turn action
self.action(player2, dealer, debug, game)
stat_seq.append(self.status.longvec())
if debug:
print "on the turn:"
print self.status.vec_act[2][0], self.status.vec_act[2][1]
if (self.status.vec_act[2][0]< player2.status.vec_act[2][0]):
return [stat_seq, -self.cum_bet()]
elif (self.status.vec_act[2][0] > player2.status.vec_act[2][0]):
return (stat_seq, player2.cum_bet())
#deal the river
#game._endStage_()
self.status.update_river(game.table)
player2.status.update_river(game.table)
stat_seq.append(self.status.longvec())
#river action
self.action(player2, dealer, debug, game)
stat_seq.append(self.status.longvec())
if debug:
print "on the river:"
print self.status.vec_act[3][0], self.status.vec_act[3][1]
if (self.status.vec_act[3][0]< player2.status.vec_act[3][0]):
return [stat_seq, -self.cum_bet()]
elif (self.status.vec_act[3][0] > player2.status.vec_act[3][0]):
return (stat_seq, player2.cum_bet())
#show down
#game.stage=4
res= game.checkWinner()
#game.endRound()
if (res[0]>res[1]):
return (stat_seq, self.cum_bet())
elif (res[0]< res[1]):
return (stat_seq, -self.cum_bet())
else:
if debug:
print "it's a tie"
return (stat_seq, 0)
def learn_one(self, stat_seq, output):
#update all the weights
#no matter which way we take to encode infomation,
#this function should be virtually identical
self.net.learnTD( stat_seq, output)
return
def train(self,num_of_train, opponent, debug=0, frenzy=0,
recover_rate=0):
#recover_rate=1 means recovers from frenzy immediately
#recover_rate=0 means it never recovers
rate=1-recover_rate
frenzy_degree=frenzy
self.frenzy= frenzy
game= holdem.Holdem(2, 4, 4, debug);
for i in range(num_of_train):
if np.random.rand() < frenzy_degree:
self.frenzy=frenzy
else:
self.frenzy=0
frenzy_degree *=rate
result=self.sim_one_hand(opponent, game, dealer=i%2, debug=debug)
game.endRound()
# print result[1]
self.learn_one(result[0], result[1])
self.status = AnotherStatus()
opponent.status= AnotherStatus()
self.frenzy= 0
stat_rep = AnotherStatus()
n_in = len(stat_rep.longvec())
n_hidden = 50
n_out = 1