-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh.cc
393 lines (297 loc) · 11.1 KB
/
mh.cc
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
#include <algorithm>
#include <cassert>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <random>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// Global variables
const vector<string> positions = {"por", "def", "mig", "dav"}; // all the possible positions
map<string, int> max_points_pos = {{"por", 0}, {"def", 0}, {"mig", 0}, {"dav", 0}}; // max points of all players in each position
string output_file;
chrono::time_point <chrono::high_resolution_clock> start_time;
// Parameters of the metaheuristic algorithm
const uint population_size = 2000; // number of solutions selected in each iteration
const uint num_combined = 500; // number of solutions combined and mutated in each iteration
const double mutation_rate = 0.15; // probability of mutation of each player in a mutated solution
const uint max_no_improvement = 2000; // maximum number of iterations without improvement allowed
// Random number generator
random_device rd;
mt19937 gen(rd());
// Returns a random integer between 0 and n-1
uint rand_uint(uint n) {
uniform_int_distribution<uint> distribution(0, n-1);
return distribution(gen);
}
// Returns a random float bewteen 0 and 1
double rand_0to1() {
uniform_real_distribution<double> distribution(0, 1);
return distribution(gen);
}
class Player
{
public:
static inline double alpha;
string name, pos;
int price, points;
Player(const string& name, const string& pos, int price, int points)
: name(name), pos(pos), price(price), points(points) {}
bool operator== (const Player& other) const {
return name == other.name and pos == other.pos and price == other.price and points == other.points;
}
bool operator< (const Player& other) const {
return get_value() < other.get_value();
}
bool operator> (const Player& other) const {
return get_value() > other.get_value();
}
private:
double get_value() const {
if (price == 0) return 0;
return pow(points, alpha + 1) / price;
}
};
using PlayerList = vector<Player>; // vector of players
using PlayerMap = map<string, PlayerList>; // map of players by position
PlayerMap players_map; // Global variable to store all the players
struct Query
{
uint N1, N2, N3;
int max_cost, max_price_per_player;
map<string, uint> max_num_players = {{"por", 1}, {"def", N1}, {"mig", N2}, {"dav", N3}};
};
Query query; // global variable to store the query given
class Solution {
private:
map<string, PlayerList> players;
int cost, points;
bool valid, valid_needs_update;
public:
Solution() : cost(0), points(0), valid_needs_update(true) {
for (auto pos : positions) {
players[pos] = PlayerList();
}
}
int get_points() const { return points; }
void add_player(const Player& player) {
players[player.pos].push_back(player);
cost += player.price;
points += player.points;
valid_needs_update = true;
}
void remove_player(const Player& p) {
cost -= p.price;
points -= p.points;
auto it = find(players[p.pos].begin(), players[p.pos].end(), p);
players[p.pos].erase(it);
valid_needs_update = true;
}
bool is_valid() {
if (valid_needs_update) {
update_valid();
valid_needs_update = false;
}
return valid;
}
const PlayerList& at(string pos) const {
return players.at(pos);
}
bool operator> (Solution& other) {
return fitness() > other.fitness();
}
// Writes the solution in the output file
void write() const {
ofstream output(output_file);
auto end_time = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
output << fixed;
output.precision(1);
output << duration/1000.0 << endl;
const map<string, string> pos_to_UPPER = {{"por","POR"}, {"def","DEF"}, {"mig","MIG"}, {"dav","DAV"}};
for (auto pos : positions) {
output << pos_to_UPPER.at(pos) << ": ";
write_players(pos, output);
}
output << "Punts: " << points << endl;
output << "Preu: " << cost << endl;
output.close();
}
private:
// Updates the valid attribute of the solution
void update_valid() {
if (cost > query.max_cost) {
valid = false;
return;
}
for (auto pos : positions) {
// check that there are no repeated players in the same position
for (uint i = 0; i < players.at(pos).size(); i++) {
if (find(players.at(pos).begin() + i + 1, players.at(pos).end(), players.at(pos)[i]) != players.at(pos).end()) {
valid = false;
return;
}
}
}
valid = true;
}
// Returns the fitness of a solution
int fitness() {
if (not is_valid()) return 0;
return points;
}
// Writes the players of a given position in the output files
// @param pos: the position of the players to be written
void write_players(string pos, ofstream& output) const {
bool first = true;
for (Player p : players.at(pos)) {
if (first) {
first = false;
output << p.name;
} else {
output << ";" << p.name;
}
}
output << endl;
}
};
Solution best_solution; // global variable to store the best solution found so far
using Population = vector<Solution>;
Query read_query(const string& input_query) {
ifstream file(input_query);
uint N1, N2, N3;
int max_cost, max_price_per_player;
file >> N1 >> N2 >> N3 >> max_cost >> max_price_per_player;
return {N1, N2, N3, max_cost, max_price_per_player};
}
/*
* Reads the players database in data_base.txt and returns a map of all the players separated by position
* and sorted by a heuristic determining the best players to be considered first.
*/
void read_players_map()
{
string databaseFile = "data_base.txt";
ifstream in(databaseFile);
while (not in.eof()) {
string name, position, club;
int points, price;
getline(in, name, ';');
if (name == "") break;
getline(in, position, ';');
in >> price;
char aux; in >> aux;
getline(in, club, ';');
in >> points;
string aux2;
getline(in,aux2);
if (price > query.max_price_per_player) continue; // filter out the players with higher price than the maximum
if (points == 0) continue;
Player player = {name, position, price, points};
players_map[player.pos].push_back(player);
max_points_pos[position] = max(max_points_pos[position], points);
}
in.close();
// remove players that are worse in points and price than other players in the same position given the maximum number of players in each position
for (auto pos : positions) {
for (uint i = 0; i < players_map[pos].size(); i++) {
Player player = players_map[pos][i];
uint count = count_if(players_map[pos].begin(), players_map[pos].end(), [player](const Player& other) {
return other.price <= player.price and other.points >= player.points;
});
if (count > query.max_num_players[pos]) {
players_map[pos].erase(players_map[pos].begin() + i);
i--;
}
}
}
// add fake players to each position given the maximum number of players in each position
for (auto pos : positions) {
for (uint i = 1; i <= query.max_num_players[pos]; i++) {
Player fake_player = {"Fake_" + pos + to_string(i), pos, 0, 0};
players_map[pos].push_back(fake_player);
}
}
}
// Selects two parents from the population uniformly at random
pair<Solution, Solution> select_parents(const Population& population) {
vector<Solution> parents (2);
sample(population.begin(), population.end(), parents.begin(), 2, gen);
return {parents[0], parents[1]};
}
// Mutates a solution by removing and adding players randomly
void mutate(Solution& solution) {
for (auto pos : positions) {
for (Player p : solution.at(pos)) {
if (rand_0to1() < mutation_rate) {
solution.remove_player(p);
solution.add_player(players_map[pos][rand_uint(players_map[pos].size())]);
}
}
}
return;
}
// Recombines two solutions by removing and adding players from each position, and mutates the resulting solutions
void recombine_and_mutate(const Solution& parent1, const Solution& parent2, Population& population) {
for (uint i = 0; i < num_combined; ++i) {
Solution new_solution = parent1;
for (auto pos : positions){
for (unsigned int j = 0; j < new_solution.at(pos).size(); ++j) {
if (rand_uint(2) == 0) {
new_solution.remove_player(new_solution.at(pos)[j]);
new_solution.add_player(parent2.at(pos)[j]);
}
}
}
mutate(new_solution);
population.push_back(new_solution);
}
}
// Selects the best individuals of the population
void select_individuals(Population& population) {
sort(population.begin(), population.end(), [](Solution& s1, Solution& s2) {
return s1 > s2;
});
population = Population(population.begin(), population.begin() + min(population_size, (uint)population.size()));
}
// Generates an initial population of random solutions
Population generate_initial_population() {
Population initial_population;
for (uint i = 0; i < population_size; ++i) {
Solution new_solution;
for (auto pos : positions) {
for (uint j = 0; j < query.max_num_players[pos]; j++) {
new_solution.add_player(players_map[pos][rand_uint(players_map[pos].size())]);
}
}
initial_population.push_back(new_solution);
}
return initial_population;
}
void metaheuristica(int population_size) {
Population population = generate_initial_population();
uint no_improvement_count = 0;
while (no_improvement_count++ < max_no_improvement) {
auto [parent1, parent2] = select_parents(population);
recombine_and_mutate(parent1, parent2, population);
select_individuals(population);
Solution candidate = population[0];
if (candidate.get_points() > best_solution.get_points() and candidate.is_valid()) {
best_solution = candidate;
best_solution.write();
no_improvement_count = 0;
}
}
}
int main(int argc, char *argv[]) {
start_time = chrono::high_resolution_clock::now(); // start the timer
string input_database = argv[1];
string input_query = argv[2];
output_file = argv[3];
query = read_query(input_query);
Player::alpha = pow(query.max_cost / 1e7, 0.3);
read_players_map();
metaheuristica(population_size);
}