-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLUT_lawbot.java
632 lines (512 loc) · 17.7 KB
/
LUT_lawbot.java
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package Spurs;
import robocode.*;
import java.awt.*;
import java.io.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import Spurs.neuralNet_law;
//This robot gonna use lookuptable as consultant to fight!
public class LUT_lawbot extends AdvancedRobot {
/*--------variables-------*/
/*--------hyper-parameter part-------*/
private final double alpha = 0.1;//learning rate
private final double gamma = 0.8;//discount
private final double exploratory_rate = 0.2;//exploratory_rate
/*--------temp-parameter String-------*/
private String state_lut;
private String[] state_action_pre = new String[2];
private String[] state_action_for = new String[2];
private double[] energy_Tmp = new double[2];
String tmp_v ="";//store temporary score
/*--------temp-parameter Double-------*/
double reward=0;
double energy_Dif =0;
/*--------temp-parameter static-------*/
static int[] winsRate =new int[1001];
/*--------temp-parameter robot-------*/
double bearing;
double distance = 0;
/*--------temp-parameter strategy-------*/
int next_Action = 0;
int turnDirection;
/*--------temp-parameter circle-------*/
double absoluteBearing;
double bearingFromGun;
double bearingFromRadar;
boolean movingForward;
boolean inWall;
/*--------ON_OFF_POLICY-------*/
boolean ON_OFF_POLICY=true; //true means on policy
/*--------LOOKUPTABLE-------*//*--------LOOKUPTABLE-------*/
public static Object LUT[][] = new Object[9*4*3*6*4][2];
double[][] in = new double[LUT.length][5];
double[][] out = new double[LUT.length][1];
/*--------FILE-------*//*--------FILE-------*/
excel_test n = new excel_test(); public LUT_lawbot() throws IOException {}
//File LUT_law =new File("/Users/Lawrence Li/robocode/robots/Spurs/LUT_robot.data/LUT_law.txt");
//File LUT_law =new File("/Users/Lawrence Li/robocode/robots/LUT_law.txt");
File LUT_law =new File("/Users/Lawrence Li/robocode/robots/Spurs/LUT_robot.data/LUT_law22.txt");
File Win_rate = new File("/Users/Lawrence Li/robocode/robots/Spurs/LUT_robot.data/Win_rate.txt");
File dd = new File("/Users/Lawrence Li/Documents/data_file/dd.txt"); // distance diff
File winsplot = new File("/Users/Lawrence Li/robocode/robots/Spurs/LUT_robot.data/Win.xlsx");
/*--------variables-------*/
/*--------Methods-------*//*--------Methods-------*//*--------Methods-------*/
/*--------Methods-------*//*--------Methods-------*//*--------Methods-------*/
/*--------save and load-------*/
//save the value of (State, Action) in to a file
public void save() {
PrintStream w = null;
try {
w = new PrintStream(new FileOutputStream(LUT_law));
for (int i=0;i<LUT.length;i++) {
w.println(LUT[i][0]+","+LUT[i][1]);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
w.flush();
w.close();
}
}//save
public void load() throws IOException {
BufferedReader reader = new BufferedReader(new
FileReader(LUT_law));
String line = reader.readLine();
try {
int st=0;
while (line != null) {
String splitLine[] = line.split(",");
LUT[st][0] = splitLine[0];
LUT[st][1] = splitLine[1];
if(st<LUT.length-1){st++;}
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
reader.close();
}
}//load
public void load_nn() throws IOException {
BufferedReader reader = new BufferedReader(new
FileReader(LUT_law));
String line = reader.readLine();
try {
int st=0;
while (line != null) {
String splitLine[] = line.split(",");
LUT[st][0] = splitLine[0];
for (int i = 0; i < splitLine[0].length(); i++) {
in[st][i] = Character.getNumericValue(splitLine[0].charAt(i));
}
LUT[st][1] = splitLine[1];
out[st][0] = Double.parseDouble(splitLine[1]);
if(st<LUT.length-1){st++;}
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
reader.close();
}
}//load
public void saveWinRate(){
PrintStream w = null;
try
{
w = new PrintStream(new FileOutputStream(Win_rate));
for(int i=0; i<winsRate.length; i++)
w.println(winsRate[i]);
}
catch (IOException e) {
e.printStackTrace();
}finally {
w.flush();
w.close();
}
}
public void savedd(){// save distance difference
PrintStream w = null;
try
{
w = new PrintStream(new FileOutputStream(Win_rate));
for(int i=0; i<winsRate.length; i++)
w.println(winsRate[i]);
}
catch (IOException e) {
e.printStackTrace();
}finally {
w.flush();
w.close();
}
}
/*--------OnRobots Methods-------*/
public void onBattleEnded(BattleEndedEvent e) {
n.write_excel(winsRate);
saveWinRate();
}
public void onHitRobot(HitRobotEvent e){
reward -= 2;
if (e.isMyFault()) {
reverseDirection();
}
} //our robot hit by enemy robot
public void onBulletHit(BulletHitEvent event){reward += 3;} //one of our bullet hits enemy robot
public void onHitByBullet(HitByBulletEvent event){reward -=2;} //when our robot is hit by a bullet
public void onHitWall(HitWallEvent e){
reward -= 3;
reverseDirection();
}
public void onRoundEnded(RoundEndedEvent e){ save();saveWinRate(); }
public void onWin(WinEvent event){
reward += 10;
winsRate[getRoundNum()] = 1;
}
public void onDeath(DeathEvent event){
reward = -10;
winsRate[getRoundNum()] = 0;
} //on series
public void onScannedRobot(ScannedRobotEvent e) {
bearing = e.getBearing();
distance = e.getDistance();
energy_Dif = getEnergy()-e.getEnergy();
absoluteBearing = getHeading() + e.getBearing();
bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
bearingFromRadar = normalRelativeAngleDegrees(absoluteBearing - getRadarHeading());
//smartFire(distance);
state_lut = quantize_state(getX(),getY(),bearing,distance,getEnergy(),e.getEnergy());
if (Math.abs(bearingFromGun) <= 4) {
setTurnGunRight(bearingFromGun);
if (getGunHeat() == 0 && getEnergy() > .2) {
fire(Math.min(4.5 - Math.abs(bearingFromGun) / 2 - distance / 250, getEnergy() - .1));
}
}
else {
setTurnGunRight(bearingFromGun);
}
if (bearingFromGun == 0) {
scan();
}
}
/*--------Strategy Methods-------*/
public void action4(int c){
switch (c){
case 1:{
retrieve();
//afraid();
//retrieve;
break;}
case 2:{
Yajigigi();
//brave();
//toward enemy
break;}
case 3:{
circle();
//rainbow();
break;
}
case 4:{
//circle()
// fire(2);
dragon();
break;
}
}
}
public int greedy_action() {
double value_tmp = -500;
int action = 1;
int[] f = {1,2,3,4};
for (int j = 0; j < LUT.length; j++) {
for (int i : f) {
String state_action = state_lut + Integer.toString(i);
if (LUT[j][0].equals(state_action)) {
if (value_tmp < Double.valueOf((String) LUT[j][1])) {
value_tmp = Double.valueOf((String) LUT[j][1]);
action = i;
break;
}
}
}
}
return action;
}
public int exploratory_action() {
int action=0;
action = 1+(int)(Math.random()*(4));
return action;
}
public void smartFire(double robotDistance) {
if (robotDistance <= 200.0D && this.getEnergy() >= 15.0D) {
if (robotDistance > 50.0D) {
this.fire(2.0D);
} else {
this.fire(3.0D);
}
} else {
this.fire(1.0D);
}
}
/*--------Data Process Methods-------*/
public String action_value_LUT(String st){
String value="";
for (int j = 0; j < LUT.length; j++) {
if (st.equals(LUT[j][0])) {
value = (String)LUT[j][1];
break;
}
}//for j
return value;
}
public String learn_from_LUT(String g,String punishment){
double pain = Double.parseDouble(punishment);
double gain = Double.parseDouble(g);
double new_gain = gain+alpha*(reward+gamma*pain - gain);
String gg = Double.toString(new_gain);
return gg;
}
private void update(String [] sa){
for (int j = 0; j < LUT.length; j++) {
if (sa[0].equals(LUT[j][0])) {
LUT[j][1] = sa[1];
break;
}
}//for j
}
/*--------Quantization Methods-------*/
public int quantize_bearing(double bearing){
int k = 1;
if(0 <= bearing && bearing < 90 ){
k = 1;
}else if(90 <= bearing && bearing < 180 ){
k = 2;
}else if(-180 <= bearing && bearing < -90 ){
k = 3;
}else if(-90 <= bearing && bearing < 0 ){
k = 4;
}
return k;
}
public int quantize_distance_lut(double distance){
int dis = (int)(distance/96);//real distance
if(dis<3){return 1;}
else if(dis>2 && dis<6){return 2;}
else{return 3;}
}
public int quantize_energy(double energy) {
int ene = (int) (energy / 10)+1;
return ene;
}
public int quantize_energy_pro(double e1,double e2) {
int e=1+(int)Math.random()*6;
if(e1<e2 && e1<4){e=1;}
if(e1<e2 && e1>3 && e1<7){e=2;}
if(e1<e2 && e1>6){e=3;}
if(e1>e2 && e1<4){e=4;}
if(e1>e2 && e1>4 && e1<7){e=5;}
if(e1>e2 && e1>6){e=6;}
return e;
}
public int quantize_location(double a, double b){
int a1 = (int)(a/270)+1;
int b1 = (int)(b/200)+1;
int l =1;
String ab = Integer.toString(a1)+Integer.toString(b1);
switch (ab){
case "11":{l=1;break;}
case "12":{l=2;break;}
case "13":{l=3;break;}
case "21":{l=4;break;}
case "22":{l=5;break;}
case "23":{l=6;break;}
case "31":{l=7;break;}
case "32":{l=8;break;}
case "33":{l=9;break;}
}
return l;
}
public String quantize_state(double x,double y, double bearing, double distance, double energy_me,double energy_enemy){
int loc = quantize_location(x,y);//location area
int bea = quantize_bearing(bearing);//bearing
int dis = quantize_distance_lut(distance); //distance
int e1 = quantize_energy(energy_me);
int e2 = quantize_energy(energy_enemy);
int ene = quantize_energy_pro(e1,e2); //quantized energy
String state = Integer.toString(loc)
+Integer.toString(bea)
+Integer.toString(dis)
+Integer.toString(ene);
return state;
}
/*--------Actions Methods-------*/
private void retrieve(){
setTurnLeft(bearing);
setBack(distance+5);
if (getX() > 100 &&
getY() > 100 &&
getBattleFieldWidth() - getX() > 100 &&
getBattleFieldHeight() - getY() > 100 &&
inWall == true) {
inWall = false;
}
if (getX() <= 100 ||
getY() <= 100 ||
getBattleFieldWidth() - getX() <= 100 ||
getBattleFieldHeight() - getY() <= 100) {
if ( inWall == false){
reverseDirection();
inWall = true;
}
}
execute();
}
private void dragon(){
setTurnRight(90);
setAhead(distance);
if (getX() > 100 &&
getY() > 100 &&
getBattleFieldWidth() - getX() > 100 &&
getBattleFieldHeight() - getY() > 100 &&
inWall == true) {
inWall = false;
}
if (getX() <= 100 ||
getY() <= 100 ||
getBattleFieldWidth() - getX() <= 100 ||
getBattleFieldHeight() - getY() <= 100) {
if ( inWall == false){
reverseDirection();
inWall = true;
}
}
execute();
}
private void Yajigigi(){
setTurnLeft(bearing);
setAhead(distance+5);
if (getX() > 100 &&
getY() > 100 &&
getBattleFieldWidth() - getX() > 100 &&
getBattleFieldHeight() - getY() > 100 &&
inWall == true) {
inWall = false;
}
if (getX() <= 100 ||
getY() <= 100 ||
getBattleFieldWidth() - getX() <= 100 ||
getBattleFieldHeight() - getY() <= 100) {
if ( inWall == false){
reverseDirection();
inWall = true;
}
}
execute();
}
public void reverseDirection() {
if (movingForward) {
setBack(40000);
movingForward = false;
} else {
setAhead(40000);
movingForward = true;
}
}
public void circle(){
if (movingForward){
setTurnRight(normalRelativeAngleDegrees(bearing + 80));
} else {
setTurnRight(normalRelativeAngleDegrees(bearing + 100));
}
if (getX() > 100 &&
getY() > 100 &&
getBattleFieldWidth() - getX() > 100 &&
getBattleFieldHeight() - getY() > 100 &&
inWall == true) {
inWall = false;
}
if (getX() <= 100 ||
getY() <= 100 ||
getBattleFieldWidth() - getX() <= 100 ||
getBattleFieldHeight() - getY() <= 100) {
if ( inWall == false){
reverseDirection();
inWall = true;
}
}
execute();
}
/*--------Main Part RUN-------*/
public void run(){
// Set colors
setBodyColor(new Color(221, 175, 19));
setGunColor(new Color(11,77,113));
setRadarColor(new Color(99,228,199));
setBulletColor(new Color(255,238,0));
setScanColor(new Color(255,241,46));
// Every part of the robot moves freely from the others.
setAdjustRadarForRobotTurn(true);
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("in.length = "+in.length);
System.out.println("out.length = "+out.length);
//neuralNet_law nn_Agent = new neuralNet_law(in,out);
save();
turnRadarLeft(360);
while (true) {
//turnRadarLeft(360);
try {
load();
}
catch (IOException e) {
e.printStackTrace();
}
//explore or greedy!
if(next_Action == 0) {
if (Math.random() < exploratory_rate) {
next_Action = exploratory_action();
} else {
next_Action = greedy_action();
}
}
//this *_pre is the action and Q_value at present
state_action_pre[0] = state_lut + Integer.toString(next_Action);
state_action_pre[1] = action_value_LUT(state_action_pre[0]);
//Before action there is no reward!
reward=0;
//enery_tmp store the energy difference before and after action
energy_Tmp[0] = energy_Dif;
//Take action in the pre-set method
action4(next_Action);
//Check where I am
turnRadarLeft(360);
energy_Tmp[1] = energy_Dif;
//If the energy difference is growing, it's great
if(energy_Tmp[0]-energy_Tmp[1]>0){reward +=1;}
else if(energy_Tmp[0]-energy_Tmp[1]==0){reward +=0;}
else{reward -= 1;}
//The next move is exploratory or greedy?
//No matter what happen we use off-policy to learn
if(ON_OFF_POLICY) {
if (Math.random() < exploratory_rate) {
next_Action = exploratory_action();
} else {
next_Action = greedy_action();
}
}else{
next_Action = greedy_action();
}
state_action_for[0] = state_lut + Integer.toString(next_Action);
state_action_for[1] = action_value_LUT(state_action_for[0]);
tmp_v = learn_from_LUT(state_action_pre[1],state_action_for[1]);
state_action_pre[1] = tmp_v;
update(state_action_pre);
//store what i got.
save();
}
}
}