-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.cpp
More file actions
365 lines (318 loc) · 7.11 KB
/
Copy pathexplore.cpp
File metadata and controls
365 lines (318 loc) · 7.11 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "util.h"
#include<cmath>
/*#include<iostream>
#include<vector>
#include<map>
#include<cassert>
#include<fstream>*/
using namespace std;
/*This is for exploring some of the data that we've scouted so far and seeing if we get interesting correlations
For example, does the defense flag mean anything to the opposing score (or that alliance's score)
And it would be interesting to see just what the correlation coefficients are between each of the columns
That might be a little bit wierd given that not all of the columns are necessarily numeric, but we'll see what happens
Also would like to have OPR for each of the teams as one of the things to correlate against
would also be nice to have match scores to comare against
*/
//start generic code
template<typename K,typename V>
map<K,V> to_map(vector<pair<K,V>> in){
map<K,V> r;
for(auto [k,v]:in){
r[k]=v;
}
return r;
}
template<typename T>
T car(vector<T> v){
assert(v.size());
return v[0];
}
template<typename K,typename V>
set<K> keys(map<K,V> const& a){
set<K> r;
for(auto elem:a){
r|=elem.first;
}
return r;
}
template<typename T>
set<pair<T,T>> unique_pairs(set<T> in){
set<pair<T,T>> r;
for(auto at=begin(in);at!=in.end();++at){
auto at2=at;
at2++;
for(;at2!=in.end();++at2){
r|=make_pair(*at,*at2);
}
}
return r;
}
template<typename T>
vector<pair<T,T>> cross(vector<T> a){
vector<pair<T,T>> r;
for(auto elem:a){
for(auto b:a){
r|=make_pair(elem,b);
}
}
return r;
}
double std_dev(vector<double> const& in){
assert(in.size());
auto u=mean(in);
return sqrt(mean(mapf(
[u](auto x){ return pow(u-x,2); },
in
)));
}
double covariance(vector<double> const& a,vector<double> const& b){
auto ea=mean(a);
auto eb=mean(b);
return mean(mapf(
[=](auto p){
auto [a1,b1]=p;
return (a1-ea)*(b1-eb);
},
zip(a,b)
));
}
double correlation_coefficient(vector<double> const& a,vector<double> const& b){
assert(a.size());
assert(b.size());
//could check that the standard deviations are nonzero
auto da=std_dev(a);
auto db=std_dev(b);
if(da==0 || db==0) return 0;
/*PRINT(da);
PRINT(db);*/
assert(da>0);
assert(db>0);
return covariance(a,b)/(da*db);
}
template<typename T>
auto take(size_t n,set<T> s){
return take(n,to_vec(s));
}
template<typename T>
T min(set<T> s){
assert(s.size());
return *min_element(begin(s),end(s));
}
template<typename T>
T max(set<T> s){
return max(to_vec(s));
}
//start program-specific code
string unquote(string s){
if(s.size()>1 && s[0]=='"' && s[s.size()-1]=='"'){
return s.substr(1,s.size()-2);
}
return s;
}
vector<map<string,string>> load_csv(string filename){
vector<map<string,string>> r;
ifstream f(filename);
assert(f.good());
string s;
getline(f,s);
auto labels=split(s,',');
labels=mapf(unquote,labels);
while(f.good()){
getline(f,s);
if(s.size()){
r|=to_map(zip(labels,split(s,',')));
}
}
return r;
}
void autocorrelate_file(string filename){
auto d=load_csv(filename);
PRINT(d.size());
//PRINT(d);
PRINT(car(d).size());
//auto k=keys(car(d));
//PRINT(k);
auto get_col=[=](auto key){
return mapf([=](auto row){
try{
return stod(row[key]);
}catch(...){
//PRINT(row[key]);
//nyi
throw;
}
},d);
};
map<string,vector<double>> as_data;
for(auto k:keys(car(d))){
try{
as_data[k]=get_col(k);
}catch(...){
cout<<"Non-numerical row:"<<k<<"\n";
}
}
for(auto [k,data]:as_data){
PRINT(k);
auto s=to_set(data);
//PRINT(min(s));
//PRINT(max(s));
PRINT(s);
}
//other interesting columns to add:
//totals for the two alliances
//match scores
//averages for robots vs OPR for that robot
//OPR-based expected score for each alliance
/* group(
[](auto x){
return make_pair(x["Match"],x["Alliance"]);
},
d
);*/
vector<pair<double,pair<string,string>>> found;
for(auto x:unique_pairs(keys(as_data))){
//PRINT(x);
auto const& a=as_data[x.first];//get_col(x.first);
auto const& b=as_data[x.second]; //get_col(x.second);
//PRINT(take(5,to_set(a)));
//PRINT(take(5,to_set(b)));
auto f=correlation_coefficient(a,b);
found|=make_pair(f,x);
}
found=sorted(found);
//print_lines(take(10,found));
//print_lines(take(10,reversed(found)));
print_lines(found);
}
using Team=int;
using Column_name=string;
template<typename A,typename B>
A first(pair<A,B>){
nyi
}
Team get_team(map<string,string> m){
auto s=[&](){
auto f=m.find("Team");
if(f!=m.end()){
return f->second;
}
f=m.find("team");
if(f!=m.end()){
return f->second;
}
nyi
}();
return stoi(s);
}
double format_col(string col_name,string data){
if(col_name=="climb"){
if(data=="NULL") return 0;
if(data=="P3") return 3;
if(data=="P6") return 6;
if(data=="P12") return 12;
/*if(data=="NULL") return 0;
if(data=="P3") return 1;
if(data=="P6") return 2;
if(data=="P12") return 3;*/
nyi
}
return stod(data);
}
map<Column_name,map<Team,double>> averages(string filename){
auto c=load_csv(filename);
assert(c.size());
auto columns=keys(c[0]);
map<Column_name,map<Team,double>> r;
for(auto col:columns){
try{
r[col]=to_map(mapf(
[](auto p)->pair<Team,double>{
auto values=seconds(p.second);
return make_pair(p.first,mean(values));
},
group(
[](auto x){ return x.first; },
mapf(
[&](auto x){
try{
return make_pair(
get_team(x),//stoi(x["Team"]),
format_col(col,x[col])//stod(x[col])
);
}catch(...){
cout<<"Bad:"<<x["Team"]<<": \""<<x[col]<<"\"\n";
throw;
}
},
c
)
)
));
}catch(...){
cout<<"Column failed:"<<col<<"\n";
PRINT(mapf([=](auto x){ return x[col]; },c));
}
}
return r;
}
template<typename T>
set<T> operator&(set<T> a,set<T> b){
set<T> r;
for(auto elem:a){
if(b.count(elem)){
r|=elem;
}
}
return r;
}
template<typename A,typename B>
vector<A> firsts(vector<pair<A,B>> const& v){
return mapf([](auto x){ return x.first; },v);
}
template<typename Func,typename T>
vector<T> sorted(Func f,vector<T> v){
sort(begin(v),end(v),[=](auto a,auto b){ return f(a)<f(b); });
return v;
}
void correlate_files(string file1,string file2){
auto x=averages(file1);
auto y=averages(file2);
vector<pair<double,pair<string,string>>> out;
for(auto [x_colname,x_data]:x){
for(auto [y_colname,y_data]:y){
vector<pair<double,double>> p;
for(auto k:keys(x_data)&keys(y_data)){
p|=make_pair(x_data[k],y_data[k]);
}
out|=make_pair(
correlation_coefficient(firsts(p),seconds(p)),
make_pair(x_colname,y_colname)
);
}
}
print_lines(reversed(sorted([](auto x){ return fabs(x.first); },out)));
}
int main(){
//autocorrelate_file("data/pit_data_export_orore_2019.csv");
/*correlate_files(
"data/pit_data_export_orore_2019.csv",
"data/match_data_export_orore_2019.csv"
);*/
/*correlate_files(
"data/pit_data_export_orwil_2019.csv",
"data/pit_data_export_orore_2019.csv"
);*/
/*correlate_files(
"data/match_data_export_orwil_2019.csv",
"data/match_data_export_orore_2019.csv"
);*/
/*correlate_files(
"data/pit_data_export_orore_2019.csv",
"data/2019OrOre1029.csv"
);*/
correlate_files(
"data/pit_data_export_orwil_2019.csv",
"data/2019OrWil1127.csv"
);
return 0;
}