-
Notifications
You must be signed in to change notification settings - Fork 7
/
Analyze.cpp
333 lines (290 loc) · 9.53 KB
/
Analyze.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Copyright (c) 2014 Maarten Baert <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
*/
#include "Analyze.h"
#include "ExpectiMax.h"
#include <iostream>
template<typename A, typename B>
inline bool CompareFirst(const std::pair<A, B>& a, const std::pair<A, B>& b) { return (a.first < b.first); }
void Analyze_Test1(BoardDB* boarddb) {
unsigned int binsize = 10;
std::vector<std::pair<unsigned int, unsigned int> > table;
for(BoardScore &result : boarddb->m_results) {
unsigned int value = 0;
for(unsigned int location = 0; location < 16; ++location) {
unsigned int cell = GetCell(result.m_board, location);
if(cell != 0)
value += 1 << cell;
}
table.push_back(std::make_pair(value, result.m_score));
}
std::sort(table.begin(), table.end(), CompareFirst<unsigned int, unsigned int>);
std::vector<std::pair<unsigned int, unsigned int> > table_avg;
unsigned int cur_value = 0;
uint64_t cur_score = 0;
unsigned int cur_duplicates = 0;
for(auto &p : table) {
if(cur_value != p.first / binsize) {
if(cur_duplicates != 0) {
unsigned int value = cur_value * binsize + binsize / 2;
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
table_avg.push_back(std::make_pair(value, score));
}
cur_value = p.first / binsize;
cur_score = 0;
cur_duplicates = 0;
}
cur_score += p.second;
++cur_duplicates;
}
if(cur_duplicates != 0) {
unsigned int value = cur_value * binsize + binsize / 2;
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
table_avg.push_back(std::make_pair(value, score));
}
std::cout << "analyze_value = array([\n\t";
for(unsigned int i = 0; i < table_avg.size(); ++i) {
std::cout << table_avg[i].first;
if(i != table.size() - 1) {
if(i % 20 == 19)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
std::cout << "analyze_score = array([\n\t";
for(unsigned int i = 0; i < table_avg.size(); ++i) {
std::cout << table_avg[i].second;
if(i != table.size() - 1) {
if(i % 20 == 19)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
}
void Analyze_Test2(BoardDB* boarddb) {
constexpr unsigned int pbins = 4;
unsigned int binsize = 20;
unsigned int pairbins[16] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3};
std::vector<std::pair<unsigned int, unsigned int> > table[pbins];
for(BoardScore &result : boarddb->m_results) {
unsigned int histogram[16] = {0};
unsigned int value = 0;
for(unsigned int location = 0; location < 16; ++location) {
unsigned int cell = GetCell(result.m_board, location);
if(cell != 0)
value += 1 << cell;
++histogram[cell];
}
unsigned int maxpbin = 0;
for(unsigned int i = 0; i < 16; ++i) {
if(histogram[i] > 1)
maxpbin = pairbins[i];
}
for(unsigned int pbin = 0; pbin <= maxpbin; ++pbin) {
table[pbin].push_back(std::make_pair(value, result.m_score));
}
}
std::vector<unsigned int> table_score[pbins];
unsigned int max_size = 0;
for(unsigned int pbin = 0; pbin < pbins; ++pbin) {
std::sort(table[pbin].begin(), table[pbin].end(), CompareFirst<unsigned int, unsigned int>);
unsigned int cur_value = 0;
uint64_t cur_score = 0;
unsigned int cur_duplicates = 0;
for(auto &t : table[pbin]) {
if(cur_value != t.first / binsize) {
if(cur_duplicates != 0) {
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
if(table_score[pbin].size() <= cur_value)
table_score[pbin].resize(cur_value + 1, 0);
table_score[pbin][cur_value] = score;
}
cur_value = t.first / binsize;
cur_score = 0;
cur_duplicates = 0;
}
cur_score += t.second;
++cur_duplicates;
}
if(cur_duplicates != 0) {
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
if(table_score[pbin].size() <= cur_value)
table_score[pbin].resize(cur_value + 1);
table_score[pbin][cur_value] = score;
}
if(table_score[pbin].size() > max_size)
max_size = table_score[pbin].size();
}
std::cout << "analyze_value = array([\n\t";
for(unsigned int i = 0; i < max_size; ++i) {
unsigned int value = i * binsize + binsize / 2;
std::cout << value;
if(i != max_size - 1) {
if(i % 20 == 19)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
std::cout << "analyze_score = array([\n\t";
for(unsigned int i = 0; i < max_size; ++i) {
std::cout << "[";
for(unsigned int p = 0; p < pbins; ++p) {
std::cout << ((i < table_score[p].size())? table_score[p][i] : 0);
if(p != pbins - 1) {
std::cout << ", ";
}
}
std::cout << "]";
if(i != max_size - 1) {
if(i % (20 / pbins) == (20 / pbins) - 1)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
}
void Analyze_Test3(BoardDB* boarddb) {
constexpr unsigned int pbins = 4;
unsigned int binsize = 200;
unsigned int pbinoverhead[pbins] = {0, 50, 100, 200};
std::vector<std::pair<unsigned int, unsigned int> > table[pbins];
for(BoardScore &result : boarddb->m_results) {
unsigned int histogram[16] = {0};
for(unsigned int location = 0; location < 16; ++location) {
unsigned int cell = GetCell(result.m_board, location);
++histogram[cell];
}
unsigned int value = 0, overhead = 0;
for(unsigned int i = 1; i < 16; ++i) {
value += histogram[i] << i;
if(value > (2u << i)) {
unsigned int extra = value - (2u << i);
//if(extra > overhead)
// overhead = extra;
overhead += extra;
}
}
for(unsigned int pbin = 0; pbin < pbins; ++pbin) {
if(overhead >= pbinoverhead[pbin])
table[pbin].push_back(std::make_pair(value, result.m_score));
}
}
std::vector<unsigned int> table_score[pbins];
unsigned int max_size = 0;
for(unsigned int pbin = 0; pbin < pbins; ++pbin) {
std::sort(table[pbin].begin(), table[pbin].end(), CompareFirst<unsigned int, unsigned int>);
unsigned int cur_value = 0;
uint64_t cur_score = 0;
unsigned int cur_duplicates = 0;
for(auto &t : table[pbin]) {
if(cur_value != t.first / binsize) {
if(cur_duplicates != 0) {
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
if(table_score[pbin].size() <= cur_value)
table_score[pbin].resize(cur_value + 1, 0);
table_score[pbin][cur_value] = score;
}
cur_value = t.first / binsize;
cur_score = 0;
cur_duplicates = 0;
}
cur_score += t.second;
++cur_duplicates;
}
if(cur_duplicates != 0) {
unsigned int score = (cur_score + cur_duplicates / 2) / cur_duplicates;
if(table_score[pbin].size() <= cur_value)
table_score[pbin].resize(cur_value + 1);
table_score[pbin][cur_value] = score;
}
if(table_score[pbin].size() > max_size)
max_size = table_score[pbin].size();
}
std::cout << "analyze_value = array([\n\t";
for(unsigned int i = 0; i < max_size; ++i) {
unsigned int value = i * binsize + binsize / 2;
std::cout << value;
if(i != max_size - 1) {
if(i % 20 == 19)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
std::cout << "analyze_score = array([\n\t";
for(unsigned int i = 0; i < max_size; ++i) {
std::cout << "[";
for(unsigned int p = 0; p < pbins; ++p) {
std::cout << ((i < table_score[p].size())? table_score[p][i] : 0);
if(p != pbins - 1) {
std::cout << ", ";
}
}
std::cout << "]";
if(i != max_size - 1) {
if(i % (20 / pbins) == (20 / pbins) - 1)
std::cout << ",\n\t";
else
std::cout << ", ";
}
}
std::cout << "])" << std::endl;
}
/*void Analyze_Test4(BoardDB* boarddb) {
constexpr unsigned int bins = 200;
double table_mu[bins] = {0.0}, table_sigma[bins] = {0.0};
unsigned int table_number[bins] = {0};
HeuristicParameters params;
unsigned int weight_table[16];
GetDefaultHeuristicParameters(¶ms);
for(unsigned int i = 0; i < 16; ++i) {
unsigned int weight3 = i * params.m_values[PARAM_CENTEROFMASS4];
unsigned int weight2 = i * (params.m_values[PARAM_CENTEROFMASS3] + weight3);
unsigned int weight1 = i * (params.m_values[PARAM_CENTEROFMASS2] + weight2);
unsigned int weight0 = i * (params.m_values[PARAM_CENTEROFMASS1] + weight1);
weight_table[i] = weight0;
}
for(BoardScore &result : boarddb->m_results) {
unsigned int score = params.m_values[PARAM_STILLALIVE];
unsigned int freecell = params.m_values[PARAM_FREECELL];
int wx = 0, wy = 0;
for(unsigned int j = 0; j < 4; ++j) {
for(unsigned int i = 0; i < 4; ++i) {
unsigned int value = GetCell(result.m_board, i, j);
if(value == 0) {
score += freecell;
freecell >>= 1;
} else {
unsigned int weight = weight_table[value];
wx += ((int) i * 2 - 3) * (int) weight;
wy += ((int) j * 2 - 3) * (int) weight;
}
}
}
score += (abs(wx) + abs(wy)) >> 10;
unsigned int bin = std::min(bins - 1, result.m_score / 1000);
table_mu[bin] += (double) score;
++table_number[bin];
}
for(unsigned int i = 0; i < bins; ++i) {
table_mu[i] /= (double) table_number[bins];
}
}*/