-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.html
378 lines (329 loc) · 10.6 KB
/
analysis.html
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
366
367
368
369
370
371
372
373
374
375
376
377
378
<html>
<head>
<title>Code Red Scouting</title>
<link rel= "stylesheet" href="../css/default.css" type="text/css">
<script type="text/javascript">
var tickTime=5;
var MATCH_LENGTH=140;
var numDisplay=10;
var robots=[];
//INPUT FUNCTIONS \/
function enter(e){//allows for the user to press the enter key instead of pressing "analyze"
tickTime=document.getElementById("a").value;
numDisplay=document.getElementById("b").value;
}
function gatherData(){//gathers information from the server
var teams=[];
var xmlhttp;
MATCH_LENGTH=document.getElementById("a").value;
numDisplay=document.getElementById("b").value;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
result=xmlhttp.responseText;
document.getElementById("output").innerHTML=result;
console.log("working...");
calculate(result);
}
}
//document.write("/php/graph.php?team="+document.getElementById("t").value);
xmlhttp.open("GET","/php/analysis.php",true);
xmlhttp.send();
}
//ANALYSIS CLASSES \/
//Robot is a class for storing robot info
function Robot(team,defense,scoreProb,scoreVal,autonVal,toss,capture,drive,passSpeed){
this.team=team;
this.defense=defense;
this.scoreProb=scoreProb;
this.scoreVal=scoreVal;
this.autonVal=autonVal;
this.toss=toss;
this.capture=capture;
this.drive=drive;
this.passSpeed=passSpeed;
//this.hasBall=true;
this.taskTime=-1;
this.task="";
this.setTask=function(task){
this.task=task;
if(task.localeCompare("pass")==0 || task.localeCompare("shoot")==0)
this.taskTime=passSpeed;
else
this.taskTime=-1;
};
this.pass=function(defense){
if(Math.random()<=scoreProb-defense/10){ //TODO?: use a better random number generator?
if(this.hasBall){//TODO?:change above, keep better track?
this.hasBall = false;
return true;
}
}
return false;
};
this.tick=function(defended){
if(this.taskTime!=-1)
this.taskTime-=tickTime;
if(this.taskTime>0 || this.taskTime==-1){//if the robot still has to "wind up"
return false;
}else{
if(this.task.localeCompare("pass")==0){
//return this.pass(defended);
return true;
}else if(this.task.localeCompare("shoot")==0){
//return this.pass(defended);
return true;
}else if(this.task.localeCompare("defend")==0){
//return (Math.random()<.75);
return true;
}
}
};
}
//Alliance is a class for storing 3 robots and processing them
function Alliance(a,b,c){
this.a=a;
this.b=b;
this.c=c;
this.bots=new Array();
this.bots[0]=a;this.bots[1]=b;this.bots[2]=c;
this.ballHeat=10;
this.score=0;
this.passes=0;
var t=Math.floor(Math.random()*this.bots.length);
this.bots[t].setTask("pass");
this.bots[(t+1)%this.bots.length].setTask("pass");
this.bots[(t+2)%this.bots.length].setTask("defend");
this.wins=0;
this.avgScore=0;
this.matches=0;
this.playAuton=function(){
this.score+=this.a.autonVal+this.b.autonVal+this.c.autonVal;
};
this.pass=function(robot){
target=(robot+Math.floor(Math.random()*2+1))%3;
if(Math.random()<.3334 && robot.toss && bots[target].capture){
this.ballHeat+=20;
} else {
this.ballHeat+=10;
}
this.passes++;
//console.log("passes:"+this.passes);
if(this.passes>2){
this.bots[target].setTask('shoot');
}else{
this.bots[target].setTask('pass');
}
};
this.tick=function(defended){
if(a.tick(defended)){
if(a.task.localeCompare("pass")==0){
this.pass(1);
this.a.setTask("tea");
} else if(a.task.localeCompare("shoot")==0){
this.score+=this.ballHeat;
console.log("Scored for "+this.ballHeat+" points");
this.bots[Math.floor(Math.random()*this.bots.length)].setTask("pass");//reset everything for another cycle
this.ballHeat=10;
this.passes=0;
}
}
if(b.tick(defended)){
if(b.task.localeCompare("pass")==0){
this.pass(2);
this.b.setTask("tea");//once we are done this is aboot all we can do
} else if(b.task.localeCompare("shoot")){
this.score+=this.ballHeat;
console.log("Scored for "+this.ballHeat+" points");
this.bots[Math.floor(Math.random()*this.bots.length)].setTask("pass");//reset everything for another cycle
this.ballHeat=10;
this.passes=0;
}
}
if(c.tick(defended)){
if(c.task.localeCompare("pass")==0){
this.pass(3);
this.c.setTask("tea");//once we are done this is aboot all we can do
} else if(c.task.localeCompare("shoot")){
this.score+=this.ballHeat;
console.log("Scored for "+this.ballHeat+" points");
this.bots[Math.floor(Math.random()*this.bots.length)].setTask("pass");//reset everything for another cycle
this.ballHeat=10;
this.passes=0;
}
}
};
this.getDefense=function(){
ret=0;
if(a.task.localeCompare("defend")==0)
ret+=this.a.defense/(140/tickTime);//TODO: add random element
if(b.task.localeCompare("defend")==0)
ret+=this.b.defense/(140/tickTime);//TODO: add random element
if(c.task.localeCompare("defend")==0)
ret+=this.c.defense/(140/tickTime);//TODO: add random element
return ret;
};
this.win=function(won){
this.avgScore+=this.score;
this.matches++;
if(won){
this.wins++;
}
this.ballHeat=10;
this.score=0;
this.passes=0;
var t=Math.floor(Math.random()*this.bots.length);
this.bots[t].setTask("pass");
this.bots[(t+1)%this.bots.length].setTask("pass");
this.bots[(t+2)%this.bots.length].setTask("defend");
};
this.getWinR=function(){
return this.wins/this.matches;
};
this.getInfo=function(){
return "<div class='floater'>teams: "+this.a.team+","+this.b.team+","+this.c.team+"</br>Simulated Stats:</br>win ratio: "+this.wins+"</br>average score: "+this.avgScore/this.matches+"</div>";//TODO: add any relevant information if any
};
}
//ANALYSIS FUNCTIONS \/
function playMatch(a,b){//returns true if a wins false if b wins
a.playAuton();
b.playAuton();
timeLeft=140;
while(timeLeft>0){
a.tick(b.getDefense());
b.tick(a.getDefense());
timeLeft-=tickTime;
}
if(a.score>b.score){
console.log(a.score);
return true;
}else{// shakhzadyan smaek daedi
console.log(b.score);
return false;
}
}
function calculate(str){//actually finds the best alliance
var robots=createBots(str,0,-1);//TODO:make this section gather user input
var alls=createAlliances(robots);
console.log("simulating matches");
for(var i=0;i<alls.length;i++){
for(var ii=i;ii<alls.length;ii++){
if(playMatch(alls[i],alls[ii])){
alls[i].win(true);
alls[ii].win(false);
}else{
alls[i].win(false);
alls[ii].win(true);
}
}
}
console.log("sorting robots...");
sortByWinR(alls,0,alls.length-1);
document.getElementById("output").innerHTML="";
for(i=0;i<alls.length && i<numDisplay;i++){
document.getElementById("output").innerHTML=document.getElementById("output").innerHTML+"</br>"+alls[i].getInfo();
}
}
function sortByWinR(alls,left,right){//Merge sort to sort by best win/loss ratio
function partition(arr,left,right,piv){
var val=arr[piv].getWinR();
var temp=arr[piv];
arr[piv]=arr[right];
arr[right]=temp;
var storeInd=left;
for(var i=left;i<right;i++){
if(arr[i].getWinR()>val){
temp=arr[i];
arr[i]=arr[storeInd];
arr[storeInd]=temp;
storeInd++;
}
}
temp=arr[storeInd];
arr[storeInd]=arr[right];
arr[right]=temp;
return storeInd;
}
if(left<right){
var pivInd=left+1;
var newInd=partition(alls,left,right,pivInd);
sortByWinR(alls,left,newInd-1);
sortByWinR(alls,newInd+1,right);
}
}
function createAlliances(bots){//Creates all of the possible permutations of alliances and returns the result
console.log("creaing alliances...");
var ret= [];
for(var i=0;i<bots.length;i++){//TODO: check to see if right
for(var ii=i;ii<bots.length;ii++){
for(var iii=ii;iii<bots.length;iii++){//theoretically this goes through only alliances that don't exist
if(i!=ii && ii!=iii && i!=iii)
ret.push(new Alliance(bots[i],bots[ii],bots[iii]));
}
}
}
return ret;
}
//This function interprets the string from analysis.php and creates an array of robots
//margin represents the minimum score the robot can recieve from one shot (no assists)
//ignore represents a list of robots to ignore
function createBots(bots,margin,ignore){
//document.getElementById("output").innerHTML=document.getElementById("output").innerHTML+"</br>creating robots...";
console.log("creating robots...");
var ret= [];
var i=0;
var valid=true;
console.log(bots);
while(!(bots.indexOf(";")==-1)){
//var str=bots.substring(1,bots.indexOf(';'));
var str=bots;
var team=parseInt( str.substring(0,str.indexOf(':')) );
str=str.substring(str.indexOf(',')+1);
var defense=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var scoreProb=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var scoreVal=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var autonVal=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var toss=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var capture=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var drive=parseInt( str.substring(0,str.indexOf(',')) );
str=str.substring(str.indexOf(',')+1);
var passSpeed=parseInt( str.substring(0,str.indexOf(';')) );
/*valid=true;
for(i=0;i<ignore.length;i++){
if(team==ignore[i] || scoreVal<margin)
valid=false;
}*/
if(valid){
ret.push(new Robot(team,defense,scoreProb,scoreVal,autonVal,toss,capture,drive,passSpeed));
}
bots=bots.substring(1).substring(bots.substring(1).indexOf(";")+1);
}
return ret;
}
</script>
</head>
<body>
<div class= "formb1">
<a href="index.html"><img src="assets/Scouting4.png" alt="Scouting4.png" width="639" height="200"></a>
</br><ul id="header"><li><a href="match_entry.html">Match entry</a></li><li><a href="robot_attributes.html">Robot Data</a></li><li><a href="heatmap.html">Heatmap Enter</a></li></ul><ul id="header"><li><a href="match_view.html">View Matches</a></li><li><a href="php/robot_view.php">View Robots</a></li><li><a href="graph.html">Graphs</a></li><li><a href="analysis.html">Analysis</a></li><li><a href="heatmap_view.html">Heatmap View</a></li></ul>
<h1>Analysis</h1>
<!--<form>-->
Time for ticks (lower values will be more accurate but slower):<input type="number" name="team" id="a" value="5" onkeypress="enter(event)"> </br>
Maximum number of alliances displayed:<input type="number" name="team" id="b" value="10" onkeypress="enter(event)"> </br>
<button type="button" onClick="gatherData()">analyze</button>
<!--</form>-->
<div id="output"></div>
</body>
</div>
</html>