]> git.sesse.net Git - wloh/blobdiff - mcwordfeud.cpp
Store covariance matrix when training model.
[wloh] / mcwordfeud.cpp
index 7470af3feafcc760928b06cd724b5d03e4572299..ce9dc5fd482058895b2577817d1282d51e955e6f 100644 (file)
@@ -14,6 +14,8 @@ using namespace std;
 
 #define MAX_PLAYERS 16
 
+float match_stddev = 70.0f;
+
 struct player {
        int player_index;
        int points, margin;
@@ -50,9 +52,9 @@ float draw_gaussian(float stddev)
 float draw_gaussian(float mu, float stddev)
 {
        static bool inited = false;
-       static long unsigned seed = 123456789;
-       int kn[128];
-       float fn[128], wn[128];
+       static long unsigned seed = time(NULL);
+       static int kn[128];
+       static float fn[128], wn[128];
        if (!inited) {
                r4_nor_setup(kn, fn, wn);
                inited = true;
@@ -66,6 +68,11 @@ int main(int argc, char **argv)
 {
        int trials = atoi(argv[1]);
 
+       if (scanf("%f", &match_stddev) != 1) {
+               fprintf(stderr, "Could't read match stddev\n");
+               exit(1);
+       }
+
        int num_players;
        if (scanf("%d", &num_players) != 1) {
                fprintf(stderr, "Could't read number of players\n");
@@ -73,13 +80,14 @@ int main(int argc, char **argv)
        }
 
        if (num_players > MAX_PLAYERS) {
-               fprintf(stderr, "Max %d players supported\n");
+               fprintf(stderr, "Max %d players supported\n", MAX_PLAYERS);
                exit(1);
        }
 
        vector<string> players;
        map<string, int> player_map;
        float ratings[MAX_PLAYERS];
+       float ratings_stddev[MAX_PLAYERS];
        bool has_scores[MAX_PLAYERS][MAX_PLAYERS];
        for (int pl1 = 0; pl1 < num_players; ++pl1) {
                for (int pl2 = 0; pl2 < num_players; ++pl2) {
@@ -90,19 +98,23 @@ int main(int argc, char **argv)
        int scores[MAX_PLAYERS][MAX_PLAYERS];
        for (int i = 0; i < num_players; ++i) {
                char buf[256];
-               float rating;
-               int ret = scanf("%s %f", buf, &rating);
+               float rating, rating_stddev;
+               int ret = scanf("%s %f %f", buf, &rating, &rating_stddev);
                if (ret < 1) {
                        fprintf(stderr, "Couldn't read player %d\n", i);
                        exit(1);
                }
-               if (ret != 2) {
+               if (ret < 2) {
                        rating = 1500.0f;
                }
+               if (ret < 3) {
+                       rating_stddev = 0.0f;
+               }
 
                players.push_back(buf);
                player_map[buf] = i;
                ratings[i] = rating;
+               ratings_stddev[i] = rating_stddev;
        }
 
        for ( ;; ) {
@@ -136,6 +148,12 @@ int main(int argc, char **argv)
        }
 
        for (int i = 0; i < trials; ++i) {
+               // draw true strength for all players
+               float drawn_ratings[MAX_PLAYERS];
+               for (int p = 0; p < num_players; ++p) {
+                       drawn_ratings[p] = draw_gaussian(ratings[p], ratings_stddev[p]);
+               }
+
                // draw the missing matches
                for (int pl1 = 0; pl1 < num_players; ++pl1) {
                        for (int pl2 = pl1 + 1; pl2 < num_players; ++pl2) {
@@ -143,9 +161,9 @@ int main(int argc, char **argv)
                                        continue;
                                }
 
-                               float mu = ratings[pl1] - ratings[pl2];
+                               float mu = drawn_ratings[pl1] - drawn_ratings[pl2];
                                
-                               int score = lrintf(draw_gaussian(mu, 82.9f));
+                               int score = lrintf(draw_gaussian(mu, match_stddev));
                                scores[pl1][pl2] = score;
                                scores[pl2][pl1] = -score;
                        }