-
Notifications
You must be signed in to change notification settings - Fork 0
/
super_naive_training_version2.cpp
286 lines (255 loc) · 10.7 KB
/
super_naive_training_version2.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "player.hpp"
#define LOW_THRESHOLD 15
#define NUM_STATES 60
#define VOLATILE 0.00003
int rounds_in_preflop;
bool discard_flop, discard_turn;
bool cantfold;
int preflop_cnt;
ofstream fout("training.txt");
int cnumber(string card)
{
int stuff[100];
stuff['T'] = 8;
stuff['J'] = 9;
stuff['Q'] = 10;
stuff['K'] = 11;
stuff['A'] = 12;
return ((card[0] >= '2' && card[0] <= '9') ? card[0] - '2' : stuff[card[0]]);
}
// this is terrible code
string Training::get_action(vector<string> hole, vector<string> table, vector<string> legal_actions)
{
// check if we are at preflop
if(table.size() != 0) preflop_cnt = 0;
else preflop_cnt++;
// updating allowed_to_discard
// beginning of a new game, we can discard (for now) both at the flop and at the turn, and we can also fold
if(preflop_cnt == 1) {
discard_flop = discard_turn = true;
cantfold = false;
}
vector<string> all_cards = table;
all_cards.push_back(hole[0]);
all_cards.push_back(hole[1]);
int hs = b.hand_strength(all_cards);
int u = b.usefulness(hole, table);
State cur = s[ttoi(hs, u, b.current_round)];
string bet_string = "X";
// find string that says "BET:min:max" or "RAISE:min:max"
for(int i = 0; i < legal_actions.size(); ++i) {
if(legal_actions[i][0] == 'B' || legal_actions[i][0] == 'R' /*|| legal_actions[i][0] == 'D'*/) {
bet_string = legal_actions[i];
break;
}
}
int bluffing = 0, check = 0, discard = 0;
for(int i = 0; i < legal_actions.size(); ++i) {
if(legal_actions[i] == "CHECK") check = 1;
}
string pre = ""; // will either say "BET:" or "RAISE:" or "DISCARD"
if(bet_string[0] == 'B') {
pre = bet_string.substr(0, 4);
bet_string = bet_string.substr(4);
}
else if(bet_string[0] == 'R') {
pre = bet_string.substr(0, 6);
bet_string = bet_string.substr(6);
}
else if(bet_string[0] == 'D') {
pre = bet_string;
discard = 1;
}
else if(check == 1) {
// 0 = CHECK
b.actions.push_back(fttoi(0, hs, u, b.current_round));
return "CHECK";
}
stringstream ss(bet_string);
int i1, i2;
ss >> i1;
if(ss.peek() == ':') ss.ignore();
ss >> i2;
// ==============================
int action = rand() % 100; // 100 is just any number
// assuming that when we are able to discard our only options are DISCARD/CHECK
if(discard == 1 || pre == "DISCARD") {
;
}
/*if(discard == 1) {
if(table.size() == 3) {
if(!discard_flop && check == 1) return "CHECK";
else {
if(u == 0 && check == 1) return "CHECK";
// discarding with probability 50% (very naive)
else if(u == 1 && rand() % 2 == 0) {
// one is the table with card 0, two is the table with card 1
vector<string> one = table, two = table;
one.push_back(hole[0]);
two.push_back(hole[1]);
int sone = b.hand_strength_discard(one);
int stwo = b.hand_strength_discard(two);
if(sone == stwo) {
if(cnumber(hole[0]) > cnumber(hole[1])) return "DISCARD:" + hole[1];
else if(cnumber(hole[0]) < cnumber(hole[1]) || check == 0) return "DISCARD:" + hole[0];
else return "CHECK"; // dont discard; might need to change that
}
else if(sone > stwo) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
} else if(u == 2) {
vector<string> one = table, two = table;
one.push_back(hole[0]);
two.push_back(hole[1]);
int sone = b.hand_strength_discard(one);
int stwo = b.hand_strength_discard(two);
if(sone == stwo) {
if(cnumber(hole[0]) > cnumber(hole[1])) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
// always discard at usefulness 2
}
else if(sone > stwo) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
}
}
} // if(table.size() == 3)
else {
if(!discard_turn) return "CHECK";
else {
if(u == 0) return "CHECK";
// discarding with probability 50% (very naive)
else if(u == 1 && rand() % 2 == 0) {
vector<string> one = table, two = table;
one.push_back(hole[0]);
two.push_back(hole[1]);
int sone = b.hand_strength_discard(one);
int stwo = b.hand_strength_discard(two);
if(sone == stwo) {
if(cnumber(hole[0]) > cnumber(hole[1])) return "DISCARD:" + hole[1];
else if(cnumber(hole[0]) < cnumber(hole[1])) return "DISCARD:" + hole[0];
else return "CHECK"; // dont discard; might need to change that
}
else if(sone > stwo) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
} else if(u == 2) {
// one is the table with card 0, two is the table with card 1
vector<string> one = table, two = table;
one.push_back(hole[0]);
two.push_back(hole[1]);
int sone = b.hand_strength_discard(one);
int stwo = b.hand_strength_discard(two);
if(sone == stwo) {
if(cnumber(hole[0]) > cnumber(hole[1])) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
// always discard at usefulness 2
}
else if(sone > stwo) return "DISCARD:" + hole[1];
else return "DISCARD:" + hole[0];
}
}
}
} // if(discard)
else {*/
double total_prob = cur.checkfold + cur.betlow + cur.bethigh;
double betting_prob = cur.betlow + cur.bethigh;
if(!cantfold && double(action)/100 < cur.checkfold / total_prob) {
if(check == 1) {
//cout << "checkfolding\n";
// 0 = CHECK
b.actions.push_back(fttoi(0, hs, u, b.current_round));
return "CHECK";
} else if(!cantfold) return "FOLD";
}
else if(double(action)/100 < (cur.checkfold + cur.betlow) / total_prob || (cantfold && double(action)/100 < cur.betlow / betting_prob)) {
//cout << "bet low\n";
// maybe we should change this, as a safety measure in case the opponent goes all-in
if(check == 1) {
b.actions.push_back(fttoi(0, hs, u, b.current_round));
return "CHECK";
}
if(i1 > LOW_THRESHOLD && !cantfold) {
// 0 = FOLD
b.actions.push_back(fttoi(0, hs, u, b.current_round));
return "FOLD";
} else {
int call_vs_raise = rand() % 10;
if(pre == "BET:") {
// can tweak 10 -- this just says we're betting between 2 and 20
int upper = int(double(i1) * 10);
int actual = rand() % (upper-i1+1) + i1;
stringstream ss2;
ss2 << max(i1, min(i2, min(actual, LOW_THRESHOLD)));
// 1 = BETLOW
b.actions.push_back(fttoi(1, hs, u, b.current_round));
return pre + ss2.str();
} else if(pre == "RAISE:" && (call_vs_raise >= 8 || (table.size() == 0 && i1 == 4))) {
int upper = int(double(i1) * 1.5); // can tweak 1.5
int actual = rand() % (upper-i1+1) + i1;
stringstream ss2;
ss2 << max(i1, min(i2, min(actual, LOW_THRESHOLD)));
// 1 = BETLOW
b.actions.push_back(fttoi(1, hs, u, b.current_round));
return pre + ss2.str();
} else return "CALL";
}
}
else {
//cout << "bet high\n";
cantfold = true;
if(table.empty()) discard_flop = discard_turn = false;
else discard_flop = false;
if(pre != "") {
cantfold = true; // we just bet high; folding is no longer an option
int lower = int(double(i2) * 0.3); // can tweak 0.5
int actual = rand() % (100-lower+1) + lower;
stringstream ss2;
ss2 << min(max(i1, actual), i2);
// 2 = BETHIGH
b.actions.push_back(fttoi(2, hs, u, b.current_round));
return pre + ss2.str();
} else {
return "CALL";
}
}
//}
return "CALL";
}
// HAVE NOT INCORPORATED VOLATILITY
void Training::train(int payoff)
{
fout << "payoff == " << payoff << endl;
// every action in b.actions was some degree of "correctness"
for(int i = 0; i < b.actions.size(); ++i) {
int three_tuple = b.actions[i] % NUM_STATES;
int action = b.actions[i] / NUM_STATES;
// might need a separate function to deal with this as it gets more complicated
// or a new design -- maybe keep checkfold/betlow/bethigh in an array
// i know i was sloppy :(((
if(action == 0) {
fout << three_tuple << ", 0: " << s[three_tuple].checkfold << " -> ";
s[three_tuple].checkfold += VOLATILE * payoff;
fout << s[three_tuple].checkfold << '\n';
//s[three_tuple].betlow -= VOLATILE * payoff / 2;
//s[three_tuple].bethigh -= VOLATILE * payoff / 2;
}
else if(action == 1) {
fout << three_tuple << ", 1: " << s[three_tuple].betlow << " -> ";
//s[three_tuple].checkfold -= VOLATILE * payoff / 2;
s[three_tuple].betlow += VOLATILE * payoff;
fout << s[three_tuple].betlow << '\n';
//s[three_tuple].bethigh -= VOLATILE * payoff / 2;
}
else if(action == 2) {
fout << three_tuple << ", 2: " << s[three_tuple].bethigh << " -> ";
//s[three_tuple].checkfold -= VOLATILE * payoff / 2;
//s[three_tuple].betlow -= VOLATILE * payoff / 2;
s[three_tuple].bethigh += VOLATILE * payoff;
fout << s[three_tuple].bethigh << '\n';
}
}
/*for(int i = 0; i < NUM_STATES; ++i) {
s[i].checkfold = max(0.1, min(0.9, s[i].checkfold));
s[i].betlow = max(0.1, min(0.9, s[i].betlow));
s[i].bethigh = max(0.1, min(0.9, s[i].bethigh));
}*/
b.actions.clear();
}