-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
203 lines (182 loc) · 4.58 KB
/
Copy pathtable.cpp
File metadata and controls
203 lines (182 loc) · 4.58 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
#include<functional>
#include<sys/stat.h>
#include "util.h"
using namespace std;
/*
This makes charts of how many points you getting scoring hatches vs scoring balls
*/
static const int SPACES=8+6*2;
static const int NULL_HATCHES=6;
static const int TELEOP_LENGTH=135;
int teleop_length(){
return TELEOP_LENGTH;
}
int points(unsigned balls,unsigned hatches){
int hatch_points=2*min(hatches,SPACES);
int ball_spaces_available=min(SPACES,hatches+NULL_HATCHES);
int scored_balls=min(ball_spaces_available,balls);
int ball_points=scored_balls*3;
return hatch_points+ball_points;
}
int marginal_ball(int balls,int hatches){
return points(balls+1,hatches)-points(balls,hatches);
}
int marginal_hatch(int balls,int hatches){
return points(balls,hatches+1)-points(balls,hatches);
}
template<typename Func,typename Colorizer>
void run(string filename,string title,Func f,Colorizer color){
stringstream ss;
ss<<tr(
tag("td colspan=2 rowspan=2","")+
tag("th colspan="+as_string(SPACES+1),"Hatches")
);
ss<<tr(
[](){
stringstream ss;
for(auto balls:range_st<SPACES+1>()){
ss<<th(balls);
}
return ss.str();
}()
);
for(auto balls:range_st<SPACES+1>()){
stringstream s2;
s2<<th(balls);
for(auto hatches:range_st<SPACES+1>()){
s2<<tag("td "+color(balls,hatches),f(balls,hatches));
}
ss<<tr(
[&]()->string{
if(balls==0){
return tag("th rowspan="+as_string(SPACES+1),"Balls");
}
return "";
}()+
s2.str()
);
}
int r=mkdir("output",0755);
if(r!=0){
assert(r==-1);
//This happens when the directory already exists.
if(errno!=EEXIST){
perror("Failed to create output directory");
exit(1);
}
}
write_file("output/"+filename,
html(
head(
tag("title",title)
)+
body(
h1(title)+
tag("table border",ss.str())
)
)
);
}
int standard(){
auto color=[](int balls,int hatches){
auto p=points(balls,hatches);
if(p<50){
auto s=as_hex(rerange(0,50,0,255,points(balls,hatches)));
return "bgcolor='#ff"+s+s+"'";
}
auto s=as_hex(rerange(50,100,255,0,points(balls,hatches)));
return "bgcolor='#"+s+"ff"+s+"'";
};
auto color2=[](int balls,int hatches){
auto s=as_hex(rerange(0,5,0,255,marginal_hatch(balls,hatches)));
return "bgcolor='#ff"+s+s+"'";
};
auto color3=[](int balls,int hatches){
auto s=as_hex(rerange(0,5,0,255,marginal_ball(balls,hatches)));
return "bgcolor='#ff"+s+s+"'";
};
run(
"basic.html",
"Points earned",points,color
);
run(
"hatch.html","Marginal value of a hatch",marginal_hatch,color2
);
run(
"ball.html","Marginal value of a ball",marginal_ball,color3
);
return 0;
}
int points1(pair<int,int> a){
return points(a.first,a.second);
}
void with_limit(string name,std::function<int(int,int)> time_used){
set<pair<int,int>> bests;
for(auto x:group(
[=](auto p){ return time_used(p.first,p.second); },
cross(range(SPACES+1),range(SPACES+1))
)){
auto best_score=max(MAP(points1,x.second));
bests|=filter([best_score](auto y){ return points1(y)==best_score; },x.second);
}
for(int limit:range(1+TELEOP_LENGTH)){
auto color=[&limit,bests,time_used](int balls,int hatches)->string{
if(time_used(balls,hatches)>limit){
return "bgcolor=black";
}
if(make_pair(balls,hatches)&bests){
return "bgcolor=gold";
}
auto p=points(balls,hatches);
if(p<50){
auto s=as_hex(rerange(0,50,0,255,points(balls,hatches)));
return "bgcolor='#ff"+s+s+"'";
}
auto s=as_hex(rerange(50,100,255,0,points(balls,hatches)));
return "bgcolor='#"+s+"ff"+s+"'";
};
run(
name+"_"+as_string(limit)+".html",
"Points earned, limit "+as_string(limit)+" seconds",points,color
);
}
}
int by_pieces(){
with_limit("1s1s",[](int a,int b){ return a+b; });
with_limit("1s2s",[](int a,int b){ return a+2*b; });
with_limit("2s1s",[](int a,int b){ return 2*a+b; });
with_limit("3s2s",[](int a,int b){ return 3*a+2*b; });
with_limit("2s3s",[](int a,int b){ return 2*a+3*b; });
with_limit("5s4s",[](int a,int b){ return 5*a+4*b; });
with_limit("7s4s",[](int a,int b){ return 7*a+4*b; });
return 0;
}
#ifdef TABLE_MAIN
int main(int argc,char **argv){
vector<pair<const char *,std::function<int()>>> options{
make_pair("--standard",standard),
make_pair("--by_pieces",by_pieces)
};
auto help=[&](){
cout<<"Available arguments:\n";
for(auto [arg,func]:options){
(void)func;
cout<<"\t"<<arg<<"\n";
}
return 0;
};
options|=make_pair("--help",help);
if(argc!=2){
cout<<"Argument required.\n\n";
help();
return 1;
}
for(auto [arg,func]:options){
if(argv[1]==string(arg)){
return func();
}
}
cerr<<"Unrecognized argument: \""<<argv[1]<<"\"\n";
return 1;
}
#endif