forked from errorcodexero/standing_predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobability.cpp
More file actions
177 lines (159 loc) · 3.5 KB
/
Copy pathprobability.cpp
File metadata and controls
177 lines (159 loc) · 3.5 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
#include "probability.h"
std::map<Point,Pr> operator+(std::map<Point,Pr> a,int i){
std::map<Point,Pr> r;
for(auto [k,v]:a){
r[k+i]=v;
}
return r;
}
flat_map<Point,Pr> operator+(flat_map<Point,Pr> const& a,int i){
flat_map<Point,Pr> r;
for(auto [k,v]:a){
r[k+i]=v;
}
return r;
}
flat_map2<Point,Pr> operator+(flat_map2<Point,Pr> const& a,int i){
flat_map2<Point,Pr> r;
//this is not an efficient way to do this with this data structure
//should make a copy and then modify each of the keys
for(auto [k,v]:a){
r[k+i]=v;
}
return r;
}
double entropy(Pr p){
//units are bits.
if(p<0) p=0;
if(p>1) p=1;
if(p==0 || p==1) return 0;
assert(p>0 && p<1);
return -(log2(p)*p+log2(1-p)*(1-p))/log2(2);
}
double entropy(Team_dist const& a){
return sum(mapf(
[](auto x){ return -log2(x); },
values(a)
));
}
std::map<Point,Pr> simplify(std::map<std::pair<Point,Pr>,Pr> const& m){
std::map<Point,Pr> r;
for(auto [k,v]:m){
r[k.first]+=v;
}
return r;
}
std::map<Point,Pr> simplify(flat_map2<std::pair<Point,Pr>,Pr> const& m){
std::map<Point,Pr> r;
for(auto [k,v]:m){
r[k.first]+=v;
}
return r;
}
flat_map<Point,Pr> convolve(std::map<Point,Pr> const& a,std::map<Point,Pr> const& b){
flat_map<Point,Pr> r;
for(auto [a1,ap]:a){
for(auto [b1,bp]:b){
auto result=a1+b1;
auto pr=ap*bp;
auto f=r.find(result);
if(f==r.end()){
r[result]=pr;
}else{
f->second+=pr;
}
}
}
return r;
}
//flat_map<Point,Pr> convolve(flat_map2<Point,Pr> const& a,std::map<Point,flat_map2<Point,double>> const& b){
map_fixed<Int_limited<0,600>,Pr> convolve(flat_map2<Point,Pr> const& a,std::map<Point,flat_map2<Point,double>> const& b){
//convolution may not be the correct name for this operation.
//for each item in the first distribution, take a corresponding distribution out of 'b'
//and and add the value from there
//flat_map<Point,Pr> r;
//curiosly, it does need to be this big. I thought it might fit in 512, but it does not.
map_fixed<Int_limited<0,600>,Pr> r;
for(auto [k,v]:a){
auto find_dist=[&](){
auto f=b.find(k);
if(f!=b.end()){
return f->second;
}
auto m=max(keys(b));
auto f2=b.find(m);
assert(f2!=b.end());
return f2->second;
};
for(auto [k2,v2]:find_dist()){
auto k3=k+k2;
auto v3=v*v2;
r[k3]+=v3;
}
}
return r;
}
flat_map<Point,Pr> convolve(flat_map<Point,Pr> const& a,std::map<Point,Pr> const& b){
flat_map<Point,Pr> r;
for(auto [a1,ap]:a){
for(auto [b1,bp]:b){
auto result=a1+b1;
auto pr=ap*bp;
auto f=r.find(result);
if(f==r.end()){
r[result]=pr;
}else{
f->second+=pr;
}
}
}
return r;
}
flat_map2<Point,Pr> convolve(flat_map2<Point,Pr> const& a,std::map<Point,Pr> const& b){
flat_map2<Point,Pr> r;
for(auto [a1,ap]:a){
for(auto [b1,bp]:b){
auto result=a1+b1;
auto pr=ap*bp;
auto f=r.find(result);
if(f==r.end()){
r[result]=pr;
}else{
f->second+=pr;
}
}
}
return r;
}
flat_map2<Point,Pr> convolve(flat_map2<Point,Pr> const& a,flat_map<Point,Pr> const& b){
flat_map2<Point,Pr> r;
for(auto [a1,ap]:a){
for(auto [b1,bp]:b){
auto result=a1+b1;
auto pr=ap*bp;
auto f=r.find(result);
if(f==r.end()){
r[result]=pr;
}else{
f->second+=pr;
}
}
}
return r;
}
flat_map2<Point,Pr> convolve(flat_map2<Point,Pr> const& a,flat_map2<Point,Pr> const& b){
flat_map2<Point,Pr> r;
for(auto [a1,ap]:a){
for(auto [b1,bp]:b){
auto result=a1+b1;
auto pr=ap*bp;
auto f=r.find(result);
if(f==r.end()){
r[result]=pr;
}else{
f->second+=pr;
}
}
}
return r;
}