-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (290 loc) · 9.78 KB
/
main.cpp
File metadata and controls
313 lines (290 loc) · 9.78 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
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
#define LEAGUE 6 // 1 = Wood 3, 2 = Wood 2, 3 = Wood 1, 4 = Bronze, 5 = Silver, 6 = Gold, 7 = Legend
#ifdef __INTELLISENSE__
// Because of the horribly obscure way we're using the compiler, we need to define these
// to avoid false intellisense errors.
#pragma diag_suppress 312
#pragma diag_suppress 167
#endif
#include "sources/debug.h"
#include "sources/utilities.h"
#include "sources/state.h"
#include "sources/genetics.h"
#include "sources/genetics.cpp"
#include "sources/bot.h"
#include "sources/bot.cpp"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
#include <utility>
#if LEAGUE >= 6
#define MODE "GENETIC"
#else
#define MODE "HEURISTICS"
#endif
class Runtime {
private:
#if LEAGUE >= 6
std::vector<Checkpoint> checkpoints;
std::shared_ptr<Bot> racer;
std::shared_ptr<Bot> hunter;
std::shared_ptr<Bot> enemy_1;
std::shared_ptr<Bot> enemy_2;
#else
std::shared_ptr<Bot> bot;
#endif
public:
#if LEAGUE >= 6
Runtime(
std::shared_ptr<Bot> bot1,
std::shared_ptr<Bot> bot2,
std::shared_ptr<Bot> bot3,
std::shared_ptr<Bot> bot4
) : racer(std::move(bot1)),
hunter(std::move(bot2)),
enemy_1(std::move(bot3)),
enemy_2(std::move(bot4))
{
std::shared_ptr<State> hunter_state = std::make_shared<State>();
std::shared_ptr<State> enemy_1_state = std::make_shared<State>();
std::shared_ptr<State> enemy_2_state = std::make_shared<State>();
hunter->state = hunter_state;
enemy_1->state = enemy_1_state;
enemy_2->state = enemy_2_state;
DEBUG("Initializing Runtime. League: " << LEAGUE)
}
#else
Runtime() {
DEBUG("Initializing Runtime. League: " << LEAGUE)
}
#endif
void start();
void get_input();
#if LEAGUE < 6
int x, y;
int next_checkpoint_x, next_checkpoint_y;
int next_checkpoint_dist, next_checkpoint_angle;
int opponent_x, opponent_y;
#endif
};
void Runtime::get_input() {
#if LEAGUE < 6
// Before Gold League
std::cin >> this->x >> this->y >> this->next_checkpoint_x >> this->next_checkpoint_y >> this->next_checkpoint_dist >> this->next_checkpoint_angle;
std::cin.ignore();
std::cin >> this->opponent_x >> this->opponent_y;
std::cin.ignore();
#else
// After Gold League
for (int i = 0; i < 2; i++) {
int x; // x position
int y; // y position
int vx; // x speed
int vy; // y speed
int angle; // angle
int next_check_point_id; // next check point id
std::cin >> x >> y >> vx >> vy >> angle >> next_check_point_id; std::cin.ignore();
#if PRINT_INIT
DEBUG(x << " " << y << " " << vx << " " << vy << " " << angle << " " << next_check_point_id)
#endif
if (i == 0) {
racer->state->x = x;
racer->state->y = y;
racer->state->vx = vx;
racer->state->vy = vy;
racer->state->angle = angle;
racer->state->next_checkpoint_id = next_check_point_id;
} else {
hunter->state->x = x;
hunter->state->y = y;
hunter->state->vx = vx;
hunter->state->vy = vy;
hunter->state->angle = angle;
hunter->state->next_checkpoint_id = next_check_point_id;
}
}
for (int i = 0; i < 2; i++) {
int x_2; // x position (opponent)
int y_2; // y position (opponent)
int vx_2; // x speed (opponent)
int vy_2; // y speed (opponent)
int angle_2; // angle (opponent)
int next_check_point_id_2; // next check point id (opponent)
std::cin >> x_2 >> y_2 >> vx_2 >> vy_2 >> angle_2 >> next_check_point_id_2; std::cin.ignore();
#if PRINT_INIT
DEBUG(x_2 << " " << y_2 << " " << vx_2 << " " << vy_2 << " " << angle_2 << " " << next_check_point_id_2)
#endif
if (i == 0) {
enemy_1->state->x = x_2;
enemy_1->state->y = y_2;
enemy_1->state->vx = vx_2;
enemy_1->state->vy = vy_2;
enemy_1->state->angle = angle_2;
enemy_1->state->next_checkpoint_id = next_check_point_id_2;
} else {
enemy_2->state->x = x_2;
enemy_2->state->y = y_2;
enemy_2->state->vx = vx_2;
enemy_2->state->vy = vy_2;
enemy_2->state->angle = angle_2;
enemy_2->state->next_checkpoint_id = next_check_point_id_2;
}
}
#endif
}
void Runtime::start() {
int current_turn = 0;
int timeout = 100;
#if LEAGUE >= 6
// After Gold League
int laps; std::cin >> laps; std::cin.ignore();
int checkpoint_count; std::cin >> checkpoint_count; std::cin.ignore();
#if PRINT_INIT
DEBUG("Printing init")
DEBUG(laps)
DEBUG(checkpoint_count)
#endif
for (int i = 0; i < checkpoint_count; i++) {
int checkpoint_x, checkpoint_y;
std::cin >> checkpoint_x >> checkpoint_y;
std::cin.ignore();
#if PRINT_INIT
DEBUG(checkpoint_x << " " << checkpoint_y)
#endif
checkpoints.push_back(
Checkpoint(checkpoint_x, checkpoint_y)
);
}
#else
std::shared_ptr<State> state(new State());
bot = std::make_shared<Bot>(state);
#endif
while (
this->get_input(), true
) {
current_turn++;
DEBUG("Turn: " << current_turn);
#if LEAGUE < 6
std::unique_ptr<State> updated_state(new State());
updated_state->x = this->x;
updated_state->y = this->y;
updated_state->next_checkpoint_x = this->next_checkpoint_x;
updated_state->next_checkpoint_y = this->next_checkpoint_y;
updated_state->next_checkpoint_dist = this->next_checkpoint_dist;
updated_state->next_checkpoint_angle = this->next_checkpoint_angle;
updated_state->opponent_x = this->opponent_x;
updated_state->opponent_y = this->opponent_y;
bot->update_state(updated_state);
// Update state
// Update bot
bot->update(current_turn);
// Output
bot->output();
#else
racer->setTarget(
checkpoints[
static_cast<std::vector<int>::size_type>(
racer->state->next_checkpoint_id
)
].x,
checkpoints[
static_cast<std::vector<int>::size_type>(
racer->state->next_checkpoint_id
)
].y
);
hunter->setTarget(
checkpoints[
static_cast<std::vector<int>::size_type>(
hunter->state->next_checkpoint_id
)
].x,
checkpoints[
static_cast<std::vector<int>::size_type>(
hunter->state->next_checkpoint_id
)
].y
);
racer->setAcceleration(100);
hunter->setAcceleration(20);
// Start simulation
Genetics::Simulation simulation;
// Under this context, I want to avoid STL containers
State* states[4];
states[0] = racer->state.get();
states[1] = hunter->state.get();
states[2] = enemy_1->state.get();
states[3] = enemy_2->state.get();
DEBUG("Simulating " << MAX_SOLUTIONS << " solutions...");
Genetics::Solution elite = simulation.run(
states,
checkpoints,
timeout
);
// Apply the solution to the bots during MAX_MOVES amount of turns
for (int i = 0; i < MAX_MOVES; i++) {
DEBUG("Applying move: " << i)
DEBUG("Expected X Y in Turn " << current_turn + i + 1 << " for racer:")
DEBUG(" " << elite.moves_racer[i].x << " " << elite.moves_racer[i].y);
DEBUG("Expected X Y in Turn " << current_turn + i + 1 << " for hunter:")
DEBUG(" " << elite.moves_hunter[i].x << " " << elite.moves_hunter[i].y);
DEBUG("Current X Y in Turn " << current_turn << " for racer:")
DEBUG(" " << racer->state->x << " " << racer->state->y);
DEBUG("Current X Y in Turn " << current_turn << " for hunter:");
DEBUG(" " << hunter->state->x << " " << hunter->state->y);
DEBUG("Distance from current racer position to expected")
float dist = Genetics::Simulation::distance(
Point {
racer->state->x,
racer->state->y
},
Point {
elite.moves_racer[i].x,
elite.moves_racer[i].y
}
);
DEBUG(" " << dist);
DEBUG("Distance from current hunter position to expected")
dist = Genetics::Simulation::distance(
Point {
hunter->state->x,
hunter->state->y
},
Point {
elite.moves_hunter[i].x,
elite.moves_hunter[i].y
}
);
DEBUG(" " << dist);
racer->update(
elite.moves_racer[i]
);
hunter->update(
elite.moves_hunter[i]
);
racer->output();
hunter->output();
}
#endif
}
}
int main() {
#if LEAGUE >= 6
// We will use this base to copy the bot into different
// shared pointers.
std::shared_ptr<State> bot_state = std::make_shared<State>();
std::shared_ptr<Bot> bot = std::make_shared<Bot>(
bot_state
);
Runtime runtime = Runtime(
bot, // racer
std::make_shared<Bot>(*bot), // hunter
std::make_shared<Bot>(*bot), // enemy_1
std::make_shared<Bot>(*bot) // enemy_2
);
#else
Runtime runtime = Runtime();
#endif
runtime.start();
}