aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/modules/ranking.rs
blob: b0ebad88a0163b25f47dbe701455d4e887599f31 (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
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
use crate::db::bots::BotVersion;
use crate::db::maps::Map;
use crate::{db::bots::Bot, DbPool, GlobalConfig};

use crate::db;
use crate::modules::matches::{MatchPlayer, RunMatch};
use diesel::{PgConnection, QueryResult};
use rand::seq::SliceRandom;
use std::collections::HashMap;
use std::mem;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio;

// TODO: put these in a config
const RANKER_INTERVAL: u64 = 60;
const RANKER_NUM_MATCHES: i64 = 10_000;

pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) {
    // TODO: make this configurable
    // play at most one match every n seconds
    let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL));
    let mut db_conn = db_pool
        .get()
        .await
        .expect("could not get database connection");
    loop {
        interval.tick().await;
        let bots =
            db::bots::all_active_bots_with_version(&mut db_conn).expect("could not load bots");
        if bots.len() < 2 {
            // not enough bots to play a match
            continue;
        }

        let selected_bots: Vec<(Bot, BotVersion)> = bots
            .choose_multiple(&mut rand::thread_rng(), 2)
            .cloned()
            .collect();

        let maps = db::maps::get_ranked_maps(&mut db_conn).expect("could not load map");
        let map = match maps.choose(&mut rand::thread_rng()).cloned() {
            None => continue, // no maps available
            Some(map) => map,
        };

        play_ranked_match(config.clone(), map, selected_bots, db_pool.clone()).await;
        recalculate_ratings(&mut db_conn).expect("could not recalculate ratings");
    }
}

pub async fn play_ranked_match(
    config: Arc<GlobalConfig>,
    map: Map,
    selected_bots: Vec<(Bot, BotVersion)>,
    db_pool: DbPool,
) {
    let mut players = Vec::new();
    for (bot, bot_version) in selected_bots {
        let player = MatchPlayer::BotVersion {
            bot: Some(bot),
            version: bot_version,
        };
        players.push(player);
    }

    let (_, handle) = RunMatch::new(config, true, map, players)
        .run(db_pool.clone())
        .await
        .expect("failed to run match");
    // wait for match to complete, so that only one ranking match can be running
    let _outcome = handle.await;
}

fn recalculate_ratings(db_conn: &mut PgConnection) -> QueryResult<()> {
    let start = Instant::now();
    let match_stats = fetch_match_stats(db_conn)?;
    let ratings = estimate_ratings_from_stats(match_stats);

    for (bot_id, rating) in ratings {
        db::ratings::set_rating(bot_id, rating, db_conn).expect("could not update bot rating");
    }
    let elapsed = Instant::now() - start;
    // TODO: set up proper logging infrastructure
    println!("computed ratings in {} ms", elapsed.subsec_millis());
    Ok(())
}

#[derive(Default)]
struct MatchStats {
    total_score: f64,
    num_matches: usize,
}

fn fetch_match_stats(db_conn: &mut PgConnection) -> QueryResult<HashMap<(i32, i32), MatchStats>> {
    let matches = db::matches::fetch_ranked_maps(RANKER_NUM_MATCHES, db_conn)?;

    let mut match_stats = HashMap::<(i32, i32), MatchStats>::new();
    for m in matches {
        if m.match_players.len() != 2 {
            continue;
        }
        let (mut a_id, mut b_id) = match (&m.match_players[0].bot, &m.match_players[1].bot) {
            (Some(ref a), Some(ref b)) => (a.id, b.id),
            _ => continue,
        };
        // score of player a
        let mut score = match m.base.winner {
            None => 0.5,
            Some(0) => 1.0,
            Some(1) => 0.0,
            _ => panic!("invalid winner"),
        };

        // put players in canonical order: smallest id first
        if b_id < a_id {
            mem::swap(&mut a_id, &mut b_id);
            score = 1.0 - score;
        }

        let entry = match_stats.entry((a_id, b_id)).or_default();
        entry.num_matches += 1;
        entry.total_score += score;
    }
    Ok(match_stats)
}

/// Tokenizes player ids to a set of consecutive numbers
struct PlayerTokenizer {
    id_to_ix: HashMap<i32, usize>,
    ids: Vec<i32>,
}

impl PlayerTokenizer {
    fn new() -> Self {
        PlayerTokenizer {
            id_to_ix: HashMap::new(),
            ids: Vec::new(),
        }
    }

    fn tokenize(&mut self, id: i32) -> usize {
        match self.id_to_ix.get(&id) {
            Some(&ix) => ix,
            None => {
                let ix = self.ids.len();
                self.ids.push(id);
                self.id_to_ix.insert(id, ix);
                ix
            }
        }
    }

    fn detokenize(&self, ix: usize) -> i32 {
        self.ids[ix]
    }

    fn player_count(&self) -> usize {
        self.ids.len()
    }
}

fn sigmoid(logit: f64) -> f64 {
    1.0 / (1.0 + (-logit).exp())
}

fn estimate_ratings_from_stats(match_stats: HashMap<(i32, i32), MatchStats>) -> Vec<(i32, f64)> {
    // map player ids to player indexes in the ratings array
    let mut input_records = Vec::<RatingInputRecord>::with_capacity(match_stats.len());
    let mut player_tokenizer = PlayerTokenizer::new();

    for ((a_id, b_id), stats) in match_stats.into_iter() {
        input_records.push(RatingInputRecord {
            p1_ix: player_tokenizer.tokenize(a_id),
            p2_ix: player_tokenizer.tokenize(b_id),
            score: stats.total_score / stats.num_matches as f64,
            weight: stats.num_matches as f64,
        })
    }

    let mut ratings = vec![0f64; player_tokenizer.player_count()];
    // TODO: fetch these from config
    let params = OptimizeRatingsParams::default();
    optimize_ratings(&mut ratings, &input_records, &params);

    ratings
        .into_iter()
        .enumerate()
        .map(|(ix, rating)| {
            (
                player_tokenizer.detokenize(ix),
                rating * 100f64 / 10f64.ln(),
            )
        })
        .collect()
}

struct RatingInputRecord {
    /// index of first player
    p1_ix: usize,
    /// index of secord player
    p2_ix: usize,
    /// score of player 1 (= 1 - score of player 2)
    score: f64,
    /// weight of this record
    weight: f64,
}

struct OptimizeRatingsParams {
    tolerance: f64,
    learning_rate: f64,
    max_iterations: usize,
    regularization_weight: f64,
}

impl Default for OptimizeRatingsParams {
    fn default() -> Self {
        OptimizeRatingsParams {
            tolerance: 10f64.powi(-8),
            learning_rate: 0.1,
            max_iterations: 10_000,
            regularization_weight: 10.0,
        }
    }
}

fn optimize_ratings(
    ratings: &mut [f64],
    input_records: &[RatingInputRecord],
    params: &OptimizeRatingsParams,
) {
    let total_weight =
        params.regularization_weight + input_records.iter().map(|r| r.weight).sum::<f64>();

    for _iteration in 0..params.max_iterations {
        let mut gradients = vec![0f64; ratings.len()];

        // calculate gradients
        for record in input_records.iter() {
            let predicted = sigmoid(ratings[record.p1_ix] - ratings[record.p2_ix]);
            let gradient = record.weight * (predicted - record.score);
            gradients[record.p1_ix] += gradient;
            gradients[record.p2_ix] -= gradient;
        }

        // apply update step
        let mut converged = true;
        for (rating, gradient) in ratings.iter_mut().zip(&gradients) {
            let update = params.learning_rate * (gradient + params.regularization_weight * *rating)
                / total_weight;
            if update > params.tolerance {
                converged = false;
            }
            *rating -= update;
        }

        if converged {
            break;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn is_close(a: f64, b: f64) -> bool {
        (a - b).abs() < 10f64.powi(-6)
    }

    #[test]
    fn test_optimize_ratings() {
        let input_records = vec![RatingInputRecord {
            p1_ix: 0,
            p2_ix: 1,
            score: 0.8,
            weight: 1.0,
        }];

        let mut ratings = vec![0.0; 2];
        optimize_ratings(
            &mut ratings,
            &input_records,
            &OptimizeRatingsParams {
                regularization_weight: 0.0,
                ..Default::default()
            },
        );
        assert!(is_close(sigmoid(ratings[0] - ratings[1]), 0.8));
    }

    #[test]
    fn test_optimize_ratings_weight() {
        let input_records = vec![
            RatingInputRecord {
                p1_ix: 0,
                p2_ix: 1,
                score: 1.0,
                weight: 1.0,
            },
            RatingInputRecord {
                p1_ix: 1,
                p2_ix: 0,
                score: 1.0,
                weight: 3.0,
            },
        ];

        let mut ratings = vec![0.0; 2];
        optimize_ratings(
            &mut ratings,
            &input_records,
            &OptimizeRatingsParams {
                regularization_weight: 0.0,
                ..Default::default()
            },
        );
        assert!(is_close(sigmoid(ratings[0] - ratings[1]), 0.25));
    }

    #[test]
    fn test_optimize_ratings_regularization() {
        let input_records = vec![RatingInputRecord {
            p1_ix: 0,
            p2_ix: 1,
            score: 0.8,
            weight: 100.0,
        }];

        let mut ratings = vec![0.0; 2];
        optimize_ratings(
            &mut ratings,
            &input_records,
            &OptimizeRatingsParams {
                regularization_weight: 1.0,
                ..Default::default()
            },
        );
        let predicted = sigmoid(ratings[0] - ratings[1]);
        assert!(0.5 < predicted && predicted < 0.8);
    }
}