-
Notifications
You must be signed in to change notification settings - Fork 3
/
Inoryy's CSB.cpp
721 lines (578 loc) · 16.5 KB
/
Inoryy's CSB.cpp
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/* -------------------------------------Soure: https://github.com/Inoryy/csb-ai-starter-----------------------------------------------*/
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("unroll-loops")
#include "stdio.h"
#include "math.h"
#include <iostream>
#include <algorithm>
#include <memory>
#include <chrono>
#include <vector>
using namespace std;
using namespace std::chrono;
high_resolution_clock::time_point now = high_resolution_clock::now();
#define TIME duration_cast<duration<double>>(high_resolution_clock::now() - now).count()
class Point;
class Unit;
class Pod;
class Collision;
class Checkpoint;
class Solution;
class Bot;
void load();
void play();
void print_move(int, float, Pod*);
constexpr int CP = 0;
constexpr int POD = 1;
constexpr int DEPTH = 6;
constexpr float SHIELD_PROB = 10;
constexpr int MAX_THRUST = 100;
constexpr float E = 0.00001;
int r = -1;
int turn = 0;
int sols_ct = 0;
bool is_p2 = false;
int cp_ct, laps;
Pod* pods[4];
Checkpoint* cps[10];
inline int fastrand() {
static unsigned int g_seed = 42;
g_seed = (214013*g_seed+2531011);
return (g_seed>>16)&0x7FFF;
}
inline int rnd(int b) {
return fastrand() % b;
}
inline int rnd(int a, int b) {
return a + rnd(b - a + 1);
}
class Collision {
public:
Unit* a;
Unit* b;
float t;
Collision() {}
Collision(Unit* a, Unit* b, float t) {
this->a = a;
this->b = b;
this->t = t;
}
};
class Point {
public:
float x, y;
Point() {};
Point(float x, float y) {
this->x = x;
this->y = y;
}
inline virtual float dist(Point p) {
return sqrt(pow((x - p.x), 2) + pow((y - p.y), 2));
}
inline virtual float dist(Point* p) {
return sqrt(dist2(p));
}
inline virtual float dist2(Point* p) {
return pow((x - p->x), 2) + pow((y - p->y), 2);
}
Point closest(Point* a, Point* b) {
float da = b->y - a->y;
float db = a->x - b->x;
float c1 = da*a->x + db*a->y;
float c2 = -db*x + da*y;
float det = da*da + db*db;
float cx, cy;
if (det != 0) {
cx = (da*c1 - db*c2) / det;
cy = (da*c2 + db*c1) / det;
} else {
cx = x, cy = y;
}
return Point(cx, cy);
}
};
class Unit: public Point {
private:
float cache[5];
public:
int id, type;
float r, vx, vy;
virtual void bounce(Unit* u) {};
inline float collision_time(Unit* u) {
if (vx == u->vx && vy == u->vy) {
return -1;
}
float sr2 = u->type == CP ? 357604 : 640000;
float dx = x - u->x;
float dy = y - u->y;
float dvx = vx - u->vx;
float dvy = vy - u->vy;
float a = dvx*dvx + dvy*dvy;
if (a < E) return -1;
float b = -2.0*(dx*dvx + dy*dvy);
float delta = b*b - 4.0*a*(dx*dx + dy*dy - sr2);
if (delta < 0.0) return -1;
float t = (b - sqrt(delta))*(1.0/(2.0*a));
if (t <= 0.0 || t > 1.0) return -1;
return t;
}
void save() {
cache[0] = x;
cache[1] = y;
cache[2] = vx;
cache[3] = vy;
}
void load() {
x = cache[0];
y = cache[1];
vx = cache[2];
vy = cache[3];
}
};
class Checkpoint: public Unit {
public:
Checkpoint(int id, float x, float y) {
this->id = id;
this->x = x;
this->y = y;
this->vx = this->vy = 0;
this->type = CP;
this->r = 600;
}
void bounce(Unit*) {}
};
class Pod: public Unit {
public:
float angle = -1;
float next_angle = -1;
bool has_boost;
int ncpid, checked, timeout, shield;
Pod* partner;
// TODO maybe replace cache array with primitives?
float cache[10];
Pod(int id) {
this->id = id;
this->r = 400;
this->type = POD;
this->ncpid = 1;
// TODO move timeout to global/team var
this->timeout = 100;
this->has_boost = true;
this->checked = this->shield = 0;
}
float score() {
return checked*50000 - this->dist(cps[this->ncpid]);
}
void apply(int thrust, float angle) {
angle = max((float)-18., min((float)18., angle));
this->angle += angle;
if (this->angle >= 360.) {
this->angle = this->angle - 360.;
} else if (this->angle < 0.0) {
this->angle += 360.;
}
if (thrust == -1) {
this->shield = 4;
} else {
boost(thrust);
}
}
void rotate(Point* p) {
float a = diff_angle(p);
a = max((float)-18., min((float)18., a));
angle += a;
if (angle >= 360.) {
angle = angle - 360.;
} else if (angle < 0.0) {
angle += 360.;
}
}
void boost(int thrust) {
if (shield > 0) return;
float ra = angle * M_PI / 180.0;
vx += cos(ra) * thrust;
vy += sin(ra) * thrust;
}
void move(float t) {
x += vx * t;
y += vy * t;
}
void end() {
x = round(x);
y = round(y);
vx = trunc(vx * 0.85);
vy = trunc(vy * 0.85);
if (checked >= cp_ct * laps) {
ncpid = 0;
checked = cp_ct * laps;
}
timeout--;
if (shield > 0) shield--;
}
void bounce(Unit* u) {
if (u->type == CP) {
checked += 1;
timeout = partner->timeout = 100;
ncpid = (ncpid + 1) % cp_ct;
return;
}
bounce_w_pod(static_cast<Pod*>(u));
}
void bounce_w_pod(Pod* u) {
float m1 = shield == 4 ? 10. : 1.;
float m2 = u->shield == 4 ? 10. : 1.;
float mcoeff = (m1 + m2) / (m1 * m2);
float nx = x - u->x;
float ny = y - u->y;
float dst2 = nx*nx + ny*ny;
float dvx = vx - u->vx;
float dvy = vy - u->vy;
float prod = (nx*dvx + ny*dvy) / (dst2 * mcoeff);
float fx = nx * prod;
float fy = ny * prod;
float m1_inv = 1.0 / m1;
float m2_inv = 1.0 / m2;
vx -= fx * m1_inv;
vy -= fy * m1_inv;
u->vx += fx * m2_inv;
u->vy += fy * m2_inv;
float impulse = sqrt(fx*fx + fy*fy);
if (impulse < 120.) {
float df = 120.0 / impulse;
fx *= df;
fy *= df;
}
vx -= fx * m1_inv;
vy -= fy * m1_inv;
u->vx += fx * m2_inv;
u->vy += fy * m2_inv;
}
inline float diff_angle(Point* p) {
float a = get_angle(p);
float right = angle <= a ? a - angle : 360. - angle + a;
float left = angle >= a ? angle - a : angle + 360. - a;
if (right < left) {
return right;
}
return -left;
}
inline float get_angle(Point* p) {
float d = this->dist(p);
float dx = (p->x - x) / d;
float dy = (p->y - y) / d;
float a = acos(dx) * 180 / M_PI;
if (dy < 0) {
a = 360 - a;
}
return a;
}
void update(int x, int y, int vx, int vy, float angle, int ncpid) {
if (shield > 0) shield--;
if (ncpid != this->ncpid) {
timeout = partner->timeout = 100;
checked++;
} else {
timeout--;
}
this->x = x;
this->y = y;
this->vx = vx;
this->vy = vy;
this->ncpid = ncpid;
if (is_p2 && id > 1) swap(angle, this->next_angle);
this->angle = angle;
if (::r == 0) this->angle = 1 + diff_angle(cps[1]);
save();
}
void update(int shield, bool has_boost) {
this->shield = shield;
this->has_boost = has_boost;
}
void save() {
Unit::save();
cache[0] = ncpid;
cache[1] = checked;
cache[2] = timeout;
cache[3] = shield;
cache[4] = angle;
cache[5] = has_boost;
}
void load() {
Unit::load();
ncpid = cache[0];
checked = cache[1];
timeout = cache[2];
shield = cache[3];
angle = cache[4];
has_boost = cache[5];
}
};
class Solution {
public:
float score = -1;
int thrusts[DEPTH*2];
float angles[DEPTH*2];
Solution(bool with_rnd = false) {
if (with_rnd) randomize();
}
void shift() {
for (int i = 1; i < DEPTH; i++) {
angles[i-1] = angles[i];
thrusts[i-1] = thrusts[i];
angles[i-1+DEPTH] = angles[i+DEPTH];
thrusts[i-1+DEPTH] = thrusts[i+DEPTH];
}
randomize(DEPTH-1, true);
randomize(2*DEPTH-1, true);
score = -1;
}
void mutate() {
randomize(rnd(2*DEPTH));
}
void mutate(Solution* child) {
copy(begin(angles), end(angles), begin(child->angles));
copy(begin(thrusts), end(thrusts), begin(child->thrusts));
child->mutate();
child->score = -1;
}
void randomize(int idx, bool full = false) {
int r = rnd(2);
if (full || r == 0) angles[idx] = max(-18, min(18, rnd(-40, 40)));
if (full || r == 1) {
if (rnd(100) >= SHIELD_PROB) {
thrusts[idx] = max(0, min(MAX_THRUST, rnd((int) -0.5*MAX_THRUST, 2*MAX_THRUST)));
} else {
thrusts[idx] = -1;
}
}
score = -1;
}
void randomize() {
for (int i = 0; i < 2*DEPTH; i++) randomize(i, true);
}
};
class Bot {
public:
int id = 0;
Bot() {};
Bot(int id) {
this->id = id;
}
virtual void move() = 0;
Pod* runner() {
return runner(pods[id], pods[id+1]);
}
Pod* blocker() {
return blocker(pods[id], pods[id+1]);
}
Pod* runner(Pod* pod0, Pod* pod1) {
return pod0->score() - pod1->score() >= -1000 ? pod0 : pod1;
}
Pod* blocker(Pod* pod0, Pod* pod1) {
return runner(pod0, pod1)->partner;
}
};
class ReflexBot : public Bot {
public:
ReflexBot() {}
ReflexBot(int id) {
this->id = id;
}
void move() {
move_runner();
move_blocker();
}
void move_as_main() {
move_runner(true);
move_blocker(true);
}
void move_runner(bool for_output = false) {
Pod* pod = !for_output ? runner() : pods[0];
Checkpoint* cp = cps[pod->ncpid];
Point t(cp->x - 3*pod->vx, cp->y - 3*pod->vy);
float raw_angle = pod->diff_angle(&t);
int thrust = abs(raw_angle) < 90 ? MAX_THRUST : 0;
float angle = max((float) -18, min((float) 18, raw_angle));
if (!for_output) pod->apply(thrust, angle);
else print_move(thrust, angle, pod);
}
void move_blocker(bool for_output = false) {
Pod* pod = !for_output ? blocker() : pods[1];
Checkpoint* cp = cps[pod->ncpid];
Point t(cp->x - 3*pod->vx, cp->y - 3*pod->vy);
float raw_angle = pod->diff_angle(&t);
int thrust = abs(raw_angle) < 90 ? MAX_THRUST : 0;
float angle = max((float) -18, min((float) 18, raw_angle));
if (!for_output) pod->apply(thrust, angle);
else print_move(thrust, angle, pod);
}
};
class SearchBot : public Bot {
public:
Solution sol;
vector<Bot*> oppBots;
SearchBot() {}
SearchBot(int id) {
this->id = id;
}
void move(Solution* sol) {
pods[id]->apply(sol->thrusts[turn], sol->angles[turn]);
pods[id+1]->apply(sol->thrusts[turn+DEPTH], sol->angles[turn+DEPTH]);
}
void move() {
move(&sol);
}
void solve(float time, bool with_seed = false) {
Solution best;
if (with_seed) {
best = sol;
best.shift();
} else {
best.randomize();
if (r == 0 && pods[id]->dist(cps[1]) > 4000) best.thrusts[0] = 650;
}
get_score(&best);
Solution child;
while (TIME < time) {
best.mutate(&child);
if (get_score(&child) > get_score(&best)) best = child;
}
sol = best;
}
float get_score(Solution* sol) {
if (sol->score == -1) {
vector<float> scores;
for (Bot* oppBot : oppBots) {
scores.push_back(get_bot_score(sol, oppBot));
}
sol->score = *min_element(scores.begin(), scores.end());
}
return sol->score;
}
float get_bot_score(Solution* sol, Bot* opp) {
float score = 0;
while (turn < DEPTH) {
move(sol);
opp->move();
play();
if (turn == 0) score += 0.1*evaluate();
turn++;
}
score += 0.9*evaluate();
load();
if (r > 0) sols_ct++;
return score;
}
float evaluate() {
Pod* my_runner = runner(pods[id], pods[id+1]);
Pod* my_blocker = blocker(pods[id], pods[id+1]);
Pod* opp_runner = runner(pods[(id+2) % 4], pods[(id+3) % 4]);
Pod* opp_blocker = blocker(pods[(id+2) % 4], pods[(id+3) % 4]);
float score = my_runner->score() - opp_runner->score();
// TODO maybe not a great idea? :)
score -= my_blocker->dist(my_runner);
return score;
}
};
void load() {
for (int i = 0; i < 4; i++) pods[i]->load();
turn = 0;
}
void play() {
float t = 0.0;
while (t < 1.0) {
Collision first_col = {NULL, NULL, -1};
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
float col_time = pods[i]->collision_time(pods[j]);
if (col_time > -1 && col_time + t < 1.0 && (first_col.t == -1 || col_time < first_col.t)) {
first_col.a = pods[i];
first_col.b = pods[j];
first_col.t = col_time;
}
}
// TODO this is wasteful, get rid of it
float col_time = pods[i]->collision_time(cps[pods[i]->ncpid]);
if (col_time > -1 && col_time + t < 1.0 && (first_col.t == -1 || col_time < first_col.t)) {
first_col.a = pods[i];
first_col.b = cps[pods[i]->ncpid];
first_col.t = col_time;
}
}
if (first_col.t == -1) {
for (int i = 0; i < 4; i++) {
pods[i]->move(1.0 - t);
}
t = 1.0;
} else {
for (int i = 0; i < 4; i++) {
pods[i]->move(first_col.t);
}
first_col.a->bounce(first_col.b);
t += first_col.t;
}
}
for (int i = 0; i < 4; i++) {
pods[i]->end();
}
}
void print_move(int thrust, float angle, Pod* pod) {
float a = pod->angle + angle;
if (a >= 360.0) {
a = a - 360.0;
} else if (a < 0.0) {
a += 360.0;
}
a = a * M_PI / 180.0;
float px = pod->x + cos(a) * 10000.0;
float py = pod->y + sin(a) * 10000.0;
char copyright[] = "github.com/inoryy/csb-ai-starter"; // do not remove
if (thrust == -1) {
printf("%d %d SHIELD %s\n", (int) round(px), (int) round(py), copyright);
pod->shield = 4;
} else if (thrust == 650) {
pod->has_boost = false;
printf("%d %d BOOST %s\n", (int) round(px), (int) round(py), copyright);
} else {
printf("%d %d %d %s\n", (int) round(px), (int) round(py), thrust, copyright);
}
}
int main() {
cin >> laps >> cp_ct;
for (int i = 0; i < cp_ct; i++) {
int cx, cy;
cin >> cx >> cy;
cps[i] = new Checkpoint(i, cx, cy);
}
for (int i = 0; i < 4; i++) pods[i] = new Pod(i);
pods[0]->partner = pods[1];
pods[1]->partner = pods[0];
pods[2]->partner = pods[3];
pods[3]->partner = pods[2];
ReflexBot me_reflex;
SearchBot opp(2);
opp.oppBots.push_back(&me_reflex);
SearchBot me;
me.oppBots.push_back(&opp);
while (1) {
r++;
for (int i = 0; i < 4; i++) {
int x, y, vx, vy, angle, ncpid;
cin >> x >> y >> vx >> vy >> angle >> ncpid;
if (r == 0 && i > 1 && angle > -1) is_p2 = true;
pods[i]->update(x, y, vx, vy, angle, ncpid);
}
now = high_resolution_clock::now();
float time_limit = r ? 0.142 : 0.98;
time_limit *= 0.3;
// use this to test reflex bot behavior
// me_reflex.move_as_main();
opp.solve(time_limit*0.15);
me.solve(time_limit, r > 0);
if (r > 0) cerr << "Avg iters: " << sols_ct / r << "; Avg sims: " << sols_ct*DEPTH / r << endl;
print_move(me.sol.thrusts[0], me.sol.angles[0], pods[0]);
print_move(me.sol.thrusts[DEPTH], me.sol.angles[DEPTH], pods[1]);
}
}