-
Notifications
You must be signed in to change notification settings - Fork 0
/
utc.cpp
277 lines (243 loc) · 8.23 KB
/
utc.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
#include <random>
#include "float.h"
#include "utc.h"
#include "parameters.h"
using namespace std;
/**
- n: periods to go
- A: set of available actions
- B: initial Belief
- ims: improvement matrices for
- s: true optimum drawn from in each iteration
*/
/* The Generator returns the improvement achieved in this period. */
static inline int Generator(int state, int action, mat *ims) {
double *improvement_dist = ims[action].colptr(state);
return random_draw(improvement_dist, observation_count);
}
int onode_count(onode *node) {
int count = 1 + node->actions.size(); // itself
auto aend = node->actions.end();
for(auto ait = node->actions.begin(); ait!=aend;ait++) {
anode *va = &ait->second;
auto oend = va->observations.end();
for(auto oit = va->observations.begin(); oit!=oend; oit++)
count += onode_count(&oit->second);
}
return count;
}
void onode_show_N(onode *node) {
auto aend = node->actions.end();
for(auto ait = node->actions.begin(); ait!=aend;ait++)
cout << "Action" << ait->first << ": " << ait->second.N << endl;
}
vec update_history_belief(onode *h, const vec *initial_belief, const mat *ims) {
int history_length = 0;
onode *on = h;
do {
anode *an = on->father;
if(an == NULL) break;
on = an->father;
history_length++;
} while(1);
if(history_length == 0)
return *initial_belief;
int *history; // array with aoao...
history = (int*) malloc(sizeof(int)*2*history_length);
on = h;
for(int h=history_length-1;h>=0; h--) {
history[h*2+1] = on->observation_index;
anode *an = on->father;
history[h*2] = an->action_index;
}
vec current_belief;
vec *current_belief_ptr = (vec *) initial_belief;
// compute current belief based on the action (ims) and the observation (directly in the history)
for(int i=0;i<history_length;i++) {
current_belief = belief_update(current_belief_ptr, &ims[history[2*i]], history[2*i+1]);
current_belief_ptr = ¤t_belief;
}
free(history);
return current_belief;
}
/*
Version 1: Choose actions randomly with uniform distribution
Version 2: Apply the optimization for a static server count and roll out.
*/
float Rollout(int state, onode *h, const vec *initial_belief, int n, mat *ims) {
if(n == 0) return 0.0;
float value = 0.0;
#if 1
/* Version 1 */
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, action_count-1);
for(; n>0;n--) {
int action = dis(gen);
int improvement = Generator(state, action, ims);
value += improvement - action * server_cost;
state -= improvement;
}
#endif
#if 0
/* Version 2 */
// compute current belief based on the observed history.
vec current_belief = update_history_belief(h, initial_belief, ims);
for(; n>0;n--) {
int action = best_action(¤t_belief, ims, n, NULL);
int improvement = Generator(state, action, ims);
value += improvement - action * server_cost;
state -= improvement;
current_belief = belief_update(¤t_belief, &ims[action], improvement);
}
#endif
return value;
}
float Simulate(int state, onode *h, const vec *initial_belief, int n, mat *ims) {
if(n == 0) return 0.0;
// if no children exist
if(h->actions.size() == 0) {
// compute static optimum at this point..
vec current_belief = update_history_belief(h, initial_belief, ims);
double best_vstatic = -10000.0;
for(int a=0; a<action_count;a++){
double vstatic = V_static(¤t_belief, &ims[a], n);
h->actions.insert(pair<int, anode>(a, {a, 100, (float)vstatic, map<int, onode>(), h})); // initialize anode
if(vstatic > best_vstatic)
best_vstatic = vstatic;
}
return best_vstatic; //Rollout(state, h, initial_belief, n, ims);
}
// look for best action
anode *best_action_node = NULL;
int best_action = -1;
float best_action_value = -FLT_MAX;
float c = 25.0;
auto end = h->actions.end();
for (auto it = h->actions.begin(); it!=end; it++) {
anode *hb = &it->second;
float vb = hb->V + c * sqrt(log(h->N+1)/(hb->N+1));
if(vb > best_action_value) {
best_action_node = hb;
best_action = hb->action_index;
best_action_value = vb;
}
}
if(best_action_node == NULL) exit(1);
// apply action and observe
int improvement = Generator(state, best_action, ims);
int new_state = state-improvement;
float immediate_value = (float)improvement - ((float)best_action*server_cost);
auto hao_iter = best_action_node->observations.find(improvement);
if(hao_iter == best_action_node->observations.end()) // not found
hao_iter = best_action_node->observations.insert(pair<int, onode>(improvement, {improvement, 0, map<int, anode>(), best_action_node})).first; // improvement_index, N, actions, father
onode *hao = &hao_iter->second;
float R = immediate_value + Simulate(new_state, hao, initial_belief, n-1, ims);
h->N += 1;
best_action_node->N += 1;
best_action_node->V += (R - best_action_node->V) / (float)best_action_node->N;
return R;
}
utc_result Search(int periods, const vec *initial_belief, mat *ims) {
onode *h_root = new (onode){0, 0, map<int, anode>(), NULL}; // observation_index, N, actions, father
int N = 200000;
vec convergence(N);
int best_action = -1;
float best_value = -100000000.0;
for(int i=0;i<N;i++) {
if(i%1000 == 0)
cout << i << endl;
int state = random_draw(initial_belief->memptr(), initial_belief->n_elem);
Simulate(state, h_root, initial_belief, periods, ims);
best_action = -1;
best_value = -100000000.0;
for (int l=0; l<action_count; l++) {
anode *n = &h_root->actions[l];
float value = n->V;
if(value > best_value) {
best_action = l;
best_value = value;
}
}
convergence.at(i) = best_value;
}
best_action = -1;
best_value = -100000000.0;
for (int i=0; i<action_count; i++) {
anode *n = &h_root->actions[i];
float value = n->V;
if(value > best_value) {
best_action = i;
best_value = value;
}
}
utc_result res;
res.root = h_root;
res.best_action = best_action;
res.best_value = best_value;
res.convergence = convergence;
return res;
}
vec MC_utc(onode *h_root, vec *initial_belief, mat *ims, int periods) {
srand (time(NULL));
int N = 500;
vec results = vec(N);
float value = 0.0;
h_root->father = NULL;
for(int k=0;k<N;k++) {
cout << k << endl;
value = 0.0;
int o_pos = random_draw(initial_belief->memptr(),observation_count);
onode *current = h_root;
for(int n=periods;n>0;n--) {
anode *best_action_node = NULL;
int best_action = -1;
float best_action_value = -FLT_MAX;
auto end = current->actions.end();
for (auto it = current->actions.begin(); it!=end; it++) {
anode *hb = &it->second;
float vb = hb->V;
if(vb > best_action_value) {
best_action_node = hb;
best_action = hb->action_index;
best_action_value = vb;
}
}
/* If the current node does not have a high enough visitation rate. Then do some more tree searching. */
while(best_action_node->N < 100) {
int N2 = 100;
vec current_belief = update_history_belief(best_action_node->father, initial_belief, ims);
for(int i=0;i<N2;i++) {
int state = random_draw(current_belief.memptr(), initial_belief->n_elem);
Simulate(state, best_action_node->father, ¤t_belief, n, ims);
}
}
int improvement = random_draw(ims[best_action].colptr(o_pos), ims[best_action].n_rows);
o_pos = o_pos - improvement;
value += (float)improvement - (best_action * server_cost);
int counter = 1;
if(n>1) {
while(1) {
auto next = best_action_node->observations.find(improvement);
if(next == best_action_node->observations.end()) {
int N2 = 1000;
vec current_belief = update_history_belief(best_action_node->father, initial_belief, ims);
for(int i=0;i<N2;i++) {
int state = random_draw(current_belief.memptr(), initial_belief->n_elem);
Simulate(state, best_action_node->father, ¤t_belief, n, ims);
}
counter++;
if(counter % 100 == 0) {
improvement = random_draw(ims[best_action].colptr(o_pos), ims[best_action].n_rows);
}
} else {
current = &next->second;
break;
}
}
}
}
results.at(k) = value;
}
return results;
}