-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscountedCfrTrainable.cpp
193 lines (165 loc) · 7.48 KB
/
DiscountedCfrTrainable.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
//
// Created by Xuefeng Huang on 2020/1/31.
//
#include "include/trainable/DiscountedCfrTrainable.h"
//#define DEBUG;
DiscountedCfrTrainable::DiscountedCfrTrainable(vector<PrivateCards> *privateCards,
ActionNode &actionNode) : action_node(actionNode) {
this->privateCards = privateCards;
this->action_number = action_node.getChildrens().size();
this->card_number = privateCards->size();
this->evs = vector<float>(this->action_number * this->card_number,0.0);
this->r_plus = vector<float>(this->action_number * this->card_number,0.0);
this->r_plus_sum = vector<float>(this->card_number,0.0);
this->cum_r_plus = vector<float>(this->action_number * this->card_number,0.0);
//this->cum_r_plus_sum = vector<float>(this->card_number);
}
bool DiscountedCfrTrainable::isAllZeros(const vector<float>& input_array) {
for(float i:input_array){
if (i != 0)return false;
}
return true;
}
const vector<float> DiscountedCfrTrainable::getAverageStrategy() {
vector<float> average_strategy;
average_strategy = vector<float>(this->action_number * this->card_number);
for (int private_id = 0; private_id < this->card_number; private_id++) {
float r_plus_sum = 0;
for (int action_id = 0; action_id < action_number; action_id++) {
int index = action_id * this->card_number + private_id;
r_plus_sum += this->cum_r_plus[index];
}
for (int action_id = 0; action_id < action_number; action_id++) {
int index = action_id * this->card_number + private_id;
if(r_plus_sum) {
average_strategy[index] = this->cum_r_plus[index] / r_plus_sum;
}else{
average_strategy[index] = 1.0 / this->action_number;
}
}
}
return average_strategy;
}
const vector<float> DiscountedCfrTrainable::getcurrentStrategy() {
return this->getcurrentStrategyNoCache();
}
void DiscountedCfrTrainable::copyStrategy(shared_ptr<Trainable> other_trainable){
shared_ptr<DiscountedCfrTrainable> trainable = dynamic_pointer_cast<DiscountedCfrTrainable>(other_trainable);
this->r_plus.assign(trainable->r_plus.begin(),trainable->r_plus.end());
this->cum_r_plus.assign(trainable->cum_r_plus.begin(),trainable->cum_r_plus.end());
}
const vector<float> DiscountedCfrTrainable::getcurrentStrategyNoCache() {
vector<float> current_strategy;
current_strategy = vector<float>(this->action_number * this->card_number);
if(this->r_plus_sum.empty()){
fill(current_strategy.begin(),current_strategy.end(),1.0 / this->action_number);
}else {
for (int action_id = 0; action_id < action_number; action_id++) {
for (int private_id = 0; private_id < this->card_number; private_id++) {
int index = action_id * this->card_number + private_id;
if(this->r_plus_sum[private_id] != 0) {
current_strategy[index] = max(float(0.0),this->r_plus[index]) / this->r_plus_sum[private_id];
}else{
current_strategy[index] = 1.0 / (this->action_number);
}
#ifdef DEBUG
if(this->r_plus[index] != this->r_plus[index]) throw runtime_error("nan found");
#endif
}
}
}
return current_strategy;
}
void DiscountedCfrTrainable::setEv(const vector<float>& evs){
if(evs.size() != this->evs.size()) throw runtime_error("size mismatch in discountcfrtrainable setEV");
for(int i = 0;i < evs.size();i ++) if(evs[i] == evs[i])this->evs[i] = evs[i];
}
void DiscountedCfrTrainable::updateRegrets(const vector<float>& regrets, int iteration_number, const vector<float>& reach_probs) {
#ifdef DEBUG
if(regrets.size() != this->action_number * this->card_number) throw runtime_error("length not match");
#endif
auto alpha_coef = pow(iteration_number, this->alpha);
alpha_coef = alpha_coef / (1 + alpha_coef);
//Arrays.fill(this.r_plus_sum,0);
fill(r_plus_sum.begin(),r_plus_sum.end(),0);
//fill(cum_r_plus_sum.begin(),cum_r_plus_sum.end(),0);
for (int action_id = 0;action_id < action_number;action_id ++) {
for(int private_id = 0;private_id < this->card_number;private_id ++){
int index = action_id * this->card_number + private_id;
float one_reg = regrets[index];
// 更新 R+
this->r_plus[index] = one_reg + this->r_plus[index];
if(this->r_plus[index] > 0){
this->r_plus[index] *= alpha_coef;
}else{
this->r_plus[index] *= beta;
}
this->r_plus_sum[private_id] += max(float(0.0),this->r_plus[index]);
// 更新累计策略
// this.cum_r_plus[index] += this.r_plus[index] * iteration_number;
// this.cum_r_plus_sum[private_id] += this.cum_r_plus[index];
}
}
vector<float> current_strategy = this->getcurrentStrategyNoCache();
float strategy_coef = pow(((float)iteration_number / (iteration_number + 1)),gamma);
for (int action_id = 0;action_id < action_number;action_id ++) {
for(int private_id = 0;private_id < this->card_number;private_id ++) {
int index = action_id * this->card_number + private_id;
this->cum_r_plus[index] *= this->theta;
this->cum_r_plus[index] += current_strategy[index] * strategy_coef;// * reach_probs[private_id];
//this->cum_r_plus_sum[private_id] += this->cum_r_plus[index] ;
}
}
}
json DiscountedCfrTrainable::dump_strategy(bool with_state) {
if(with_state) throw runtime_error("state storage not implemented");
json strategy;
const vector<float>& average_strategy = this->getAverageStrategy();
vector<GameActions>& game_actions = action_node.getActions();
vector<string> actions_str;
for(GameActions& one_action:game_actions) {
actions_str.push_back(
one_action.toString()
);
}
for(int i = 0;i < this->privateCards->size();i ++){
PrivateCards& one_private_card = (*this->privateCards)[i];
vector<float> one_strategy(this->action_number);
for(int j = 0;j < this->action_number;j ++){
int strategy_index = j * this->privateCards->size() + i;
one_strategy[j] = average_strategy[strategy_index];
}
strategy[tfm::format("%s",one_private_card.toString())] = one_strategy;
}
json retjson;
retjson["actions"] = std::move(actions_str);
retjson["strategy"] = std::move(strategy);
return std::move(retjson);
}
json DiscountedCfrTrainable::dump_evs() {
json evs;
const vector<float>& average_evs = this->evs;
vector<GameActions>& game_actions = action_node.getActions();
vector<string> actions_str;
for(GameActions& one_action:game_actions) {
actions_str.push_back(
one_action.toString()
);
}
for(int i = 0;i < this->privateCards->size();i ++){
PrivateCards& one_private_card = (*this->privateCards)[i];
vector<float> one_evs(this->action_number);
for(int j = 0;j < this->action_number;j ++){
int evs_index = j * this->privateCards->size() + i;
one_evs[j] = average_evs[evs_index];
}
evs[tfm::format("%s",one_private_card.toString())] = one_evs;
}
json retjson;
retjson["actions"] = std::move(actions_str);
retjson["evs"] = std::move(evs);
return std::move(retjson);
}
Trainable::TrainableType DiscountedCfrTrainable::get_type() {
return DISCOUNTED_CFR_TRAINABLE;
}