summaryrefslogtreecommitdiff
path: root/scheduling/scheduling.cc
blob: 6b03e316dede2812623d386331e862809fce7486 (plain)
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
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

using std::string;
using std::vector;
using std::map;

int kIterations = 1000000;

vector<string> Split(const string& message, char deliminator) {
  std::istringstream stream(message);
  vector<string> result;
  string tmp;
  while (std::getline(stream, tmp, deliminator)) {
    result.emplace_back(tmp);
  }
  return result;
}

struct Player {
  Player(const string id, const int rank, const int desired_games)
      : id_(id), rank_(rank), desired_games_(desired_games) {}
  Player() = default;
  Player(const Player& other) = default;
  Player& operator=(const Player& other) = default;

  string id_;
  int rank_;
  int desired_games_;

  string DebugString() const {
    std::ostringstream output;
    output << "Player(id=" << id_ << ", rank=" << rank_
           << ", desired_games=" << desired_games_ << ")";
    return output.str();
  }

  string ToString() const {
    std::ostringstream output;
    output << id_ << " (" << std::abs(rank_) << (rank_ > 0 ? 'd' : 'k')
           << ")";
    return output.str();
  }
};

std::unique_ptr<Player> ParsePlayer(const vector<string>& data, int round) {
  const int kPlayerTokens = 7;
  if (data.size() < kPlayerTokens) {
    return nullptr;
  }
  string id = data[0];
  std::istringstream rank_stream(data[2]);
  int rank;
  rank_stream >> rank;
  char sign;
  rank_stream >> sign;
  if (sign != 'k' && sign != 'd') {
    return nullptr;
  }
  if (sign == 'k') {
    rank = -rank;
  }
  std::istringstream desired_games_stream(data[3 + round]);
  int desired_games;
  desired_games_stream >> desired_games;
  return std::unique_ptr<Player>(new Player(id, rank, desired_games));
}

int RankDifference(const Player& p1, const Player& p2) {
  int r1 = p1.rank_; if (r1 < 0) { r1++; }
  int r2 = p2.rank_; if (r2 < 0) { r2++; }
  return std::abs(r1 - r2);
}

bool LegalMatch(const Player& p1, const Player& p2) {
  return RankDifference(p1, p2) < 10;
}

template <typename GeneratorT>
vector<std::tuple<string, string>> GenerateMatches(
    GeneratorT generator,
    vector<Player> players) {
  std::vector<std::tuple<int, int>> possible_matches;
  for (int i = 0; i < players.size(); i++) {
    for (int j = 0; j < players.size(); j++) {
      // X vs. y and y vs. x is the same match. Also excludes x vs. x.
      Player* p1 = &players[i];
      Player* p2 = &players[j];
      if (i < j && LegalMatch(*p1, *p2)) {
        possible_matches.emplace_back(std::tuple<int, int>(i, j));
      }
    }
  }
  std::shuffle(possible_matches.begin(), possible_matches.end(),
               generator);

  vector<std::tuple<string, string>> result;
  for (const auto&m : possible_matches) {
    Player* p1 = &players[std::get<0>(m)];
    Player* p2 = &players[std::get<1>(m)];
    if (p1->desired_games_ && p2->desired_games_) {
      result.push_back(std::tuple<string, string>(p1->id_, p2->id_));
      p1->desired_games_--;
      p2->desired_games_--;
    }
  }
  return result;
}

double Score(const map<string, Player>& players,
             const vector<std::tuple<string, string>> matches) {
  double score = 0.0;
  for (const auto& match : matches) {
    const Player& p1 = players.at(std::get<0>(match));
    const Player& p2 = players.at(std::get<1>(match));
    double rankScore = 500 - 10 * pow(RankDifference(p1, p2), 1.5);
    score += rankScore;
  }
  return score;
}

struct Match {
  Player white_;
  Player black_;
  double komi_;
  int handicap_;

  string ToString() const {
    std::ostringstream stream;
    stream << white_.ToString() << " vs. " << black_.ToString();
    stream << " (komi: " << komi_;
    if (handicap_) {
      stream << "; H" << handicap_;
    }
    stream << ")";
    return stream.str();
  }

  string TabSeparated() const {
    std::ostringstream stream;
    stream << white_.id_ << "\t";
    stream << black_.id_ << "\t";
    if (handicap_) {
      stream << "H" << handicap_ << "\t";
    } else {
      stream << "None\t";
    }
    stream << komi_;
    return stream.str();
  }
};

template <typename GeneratorT>
Match CreateMatch(GeneratorT generator, const Player& p1, const Player& p2) {
  Player white;
  Player black;
  double komi = 0.0;
  int handicap = 0;

  if (p1.rank_ == p2.rank_) {
    std::bernoulli_distribution distribution(0.5);
    komi = 7.5;
    if (distribution(generator)) {
      white = p1;
      black = p2;
    } else {
      white = p2;
      black = p1;
    }
  } else {
    komi = 0.5;
    if (p1.rank_ > p2.rank_) {
      white = p1;
      black = p2;
    } else {
      white = p2;
      black = p1;
    }
    if (RankDifference(p1, p2) > 1) {
      handicap = std::min(9, RankDifference(p1, p2));
    }
  }
  return {white, black, komi, handicap};
}

int main() {
  const int kRound = 1;
  vector<Player> players;

  string line;
  while (std::getline(std::cin, line)) {
    std::unique_ptr<Player> player = ParsePlayer(Split(line, '\t'), kRound);
    printf("%s\n", player->DebugString().c_str());
    if (player) {
      players.emplace_back(*player);
    } else {
      printf("Failed to parse player: %s\n", line.c_str());
    }
  }

  // Needed by Score().
  map<string, Player> playerMap;
  for (const Player& player : players) {
    playerMap[player.id_] = player;
  }

  // Generate randomized pairings and pick the best one.
  vector<std::tuple<string, string>> best_matches;
  double best_score = std::numeric_limits<double>::lowest();
  for (int i = 0; i < kIterations; i++) {
    if (i && i % (kIterations / 10) == 0) {
      printf("%.2f%% done\n", 100.0 * i / kIterations);
    }
    std::random_device rd;
    std::mt19937 generator(rd());
    auto matches = GenerateMatches(generator, players);
    double score = Score(playerMap, matches);
    if (score > best_score) {
      best_score = score;
      printf("Iteration(%d): New best score(%.3f)\n", i, score);
      best_matches = matches;
    }
  }

  // Designate colors, handicap, komi, and print a sorted list of matches.
  vector<Match> finalized_matches;
  for (auto pair : best_matches) {
    std::random_device rd;
    std::mt19937 generator(rd());
    const Player& p1 = playerMap[std::get<0>(pair)];
    const Player& p2 = playerMap[std::get<1>(pair)];
    finalized_matches.emplace_back(CreateMatch(generator, p1, p2));
  }
  auto match_comparator = [](const Match& m, const Match& n) -> bool {
    return (m.white_.rank_ + m.black_.rank_) >
        (n.white_.rank_ + n.black_.rank_);
  };
  std::sort(finalized_matches.begin(), finalized_matches.end(),
            match_comparator);

  std::ostringstream export_filename;
  export_filename << "matches_r" << kRound << ".tsv";
  std::ofstream export_file;
  export_file.open(export_filename.str());
  for (const Match& match : finalized_matches) {
    printf("%s\n", match.ToString().c_str());
    export_file << match.TabSeparated() << "\n";
  }
}