]> git.sesse.net Git - wloh/commitdiff
Store all player IDs as ints instead of strings. Less flexibility, but chops off...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 29 Aug 2012 21:35:22 +0000 (23:35 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 29 Aug 2012 21:35:22 +0000 (23:35 +0200)
bayeswf.cpp

index 7ae502d2492a801c8847e0b8eb07234d9f5e9bdd..bf92cc802a56290d980d399ba843e5abc54d1a18 100644 (file)
@@ -33,11 +33,11 @@ float prior_sigma;
 // Data waiting for insertion into the database.
 
 struct RatingDBTuple {
-       string player;
+       int player;
        float mu, mu_stddev;
 };
 struct CovarianceDBTuple {
-       string player1, player2;
+       int player1, player2;
        float covariance;
 };
 vector<RatingDBTuple> rating_db_tuples; 
@@ -65,7 +65,7 @@ struct match {
 map<int, vector<match> > matches_for_player;
 vector<match> all_matches;
 
-void dump_scores(const vector<string> &players, const float *mu, const float *mu_stddev, int num_players)
+void dump_scores(const vector<int> &players, const float *mu, const float *mu_stddev, int num_players)
 {
 #if USE_DB
        for (int i = 0; i < num_players; ++i) {
@@ -74,7 +74,7 @@ void dump_scores(const vector<string> &players, const float *mu, const float *mu
        }
 #else
        for (int i = 0; i < num_players; ++i) {
-               printf("%f %f %s\n", mu[i], mu_stddev[i], players[i].c_str());
+               printf("%f %f %d\n", mu[i], mu_stddev[i], players[i]);
        }
 #endif
 }
@@ -243,7 +243,7 @@ void construct_hessian(const float *mu, int num_players)
 
 // Compute uncertainty (stddev) of mu estimates, which is sqrt((H^-1)_ii),
 // where H is the Hessian (see construct_hessian()).
-void compute_mu_uncertainty(const float *mu, const vector<string> &players)
+void compute_mu_uncertainty(const float *mu, const vector<int> &players)
 {
        // FIXME: Use pseudoinverse if applicable.
        Matrix<float, Dynamic, Dynamic> ih = hessian.inverse();
@@ -305,8 +305,8 @@ void process_file(const char *filename)
                exit(1);
        }
 
-       vector<string> players;
-       map<string, int> player_map;
+       vector<int> players;
+       map<int, int> player_map;
 
        for (int i = 0; i < num_players; ++i) {
                char buf[256];
@@ -315,17 +315,17 @@ void process_file(const char *filename)
                        exit(1);
                }
 
-               players.push_back(buf);
-               player_map[buf] = i;
+               players.push_back(atoi(buf));
+               player_map[atoi(buf)] = i;
        }
 
        int num_matches = 0;
        for ( ;; ) {
-               char pl1[256], pl2[256];
+               int pl1, pl2;
                int score1, score2;
                float weight;
 
-               if (fscanf(fp, "%s %s %d %d %f", pl1, pl2, &score1, &score2, &weight) != 5) {
+               if (fscanf(fp, "%d %d %d %d %f", &pl1, &pl2, &score1, &score2, &weight) != 5) {
                        //fprintf(stderr, "Read %d matches.\n", num_matches);
                        break;
                }
@@ -333,11 +333,11 @@ void process_file(const char *filename)
                ++num_matches;
 
                if (player_map.count(pl1) == 0) {
-                       fprintf(stderr, "Unknown player '%s'\n", pl1);
+                       fprintf(stderr, "Unknown player '%d'\n", pl1);
                        exit(1);
                }
                if (player_map.count(pl2) == 0) {
-                       fprintf(stderr, "Unknown player '%s'\n", pl2);
+                       fprintf(stderr, "Unknown player '%d'\n", pl2);
                        exit(1);
                }
 
@@ -429,12 +429,13 @@ int main(int argc, char **argv)
                txn.exec("TRUNCATE ratings");
                pqxx::tablewriter writer(txn, "ratings");
                for (unsigned i = 0; i < rating_db_tuples.size(); ++i) {
-                       char mu_str[128], mu_stddev_str[128];
+                       char player_str[128], mu_str[128], mu_stddev_str[128];
+                       snprintf(player_str, 128, "%d", rating_db_tuples[i].player);
                        snprintf(mu_str, 128, "%f", rating_db_tuples[i].mu);
                        snprintf(mu_stddev_str, 128, "%f", rating_db_tuples[i].mu_stddev);
 
                        vector<string> tuple;
-                       tuple.push_back(rating_db_tuples[i].player);
+                       tuple.push_back(player_str);
                        tuple.push_back(mu_str);
                        tuple.push_back(mu_stddev_str);
                        writer.push_back(tuple);
@@ -447,12 +448,14 @@ int main(int argc, char **argv)
                txn.exec("CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )");
                pqxx::tablewriter writer(txn, "new_covariance");
                for (unsigned i = 0; i < covariance_db_tuples.size(); ++i) {
-                       char cov_str[128];
+                       char player1_str[128], player2_str[128], cov_str[128];
+                       snprintf(player1_str, 128, "%d", covariance_db_tuples[i].player1);
+                       snprintf(player2_str, 128, "%d", covariance_db_tuples[i].player2);
                        snprintf(cov_str, 128, "%f", covariance_db_tuples[i].covariance);
 
                        vector<string> tuple;
-                       tuple.push_back(covariance_db_tuples[i].player1);
-                       tuple.push_back(covariance_db_tuples[i].player2);
+                       tuple.push_back(player1_str);
+                       tuple.push_back(player2_str);
                        tuple.push_back(cov_str);
                        writer.push_back(tuple);
                }